offsetof.c revision d82e5d30930f80a92c1270e270fdb475e3718d25
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
4
5typedef struct P { int i; float f; } PT;
6struct external_sun3_core
7{
8 unsigned c_regs;
9
10  PT  X[100];
11
12};
13
14void swap()
15{
16  int x;
17  x = offsetof(struct external_sun3_core, c_regs);
18  x = __builtin_offsetof(struct external_sun3_core, X[42].f);
19
20  x = __builtin_offsetof(struct external_sun3_core, X[42].f2);  // expected-error {{no member named 'f2'}}
21  x = __builtin_offsetof(int, X[42].f2);  // expected-error {{offsetof requires struct}}
22
23  int a[__builtin_offsetof(struct external_sun3_core, X) == 4 ? 1 : -1];
24  int b[__builtin_offsetof(struct external_sun3_core, X[42]) == 340 ? 1 : -1];
25  int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1];  // expected-error {{no member named 'f2'}}
26}
27
28extern int f();
29
30struct s1 { int a; };
31int v1 = offsetof (struct s1, a) == 0 ? 0 : f();
32
33struct s2 { int a; };
34int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f();
35
36struct s3 { int a; };
37int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f();
38
39// PR3396
40struct sockaddr_un {
41 unsigned char sun_len;
42 char sun_path[104];
43};
44int a(int len) {
45int a[__builtin_offsetof(struct sockaddr_un, sun_path[len+1])];
46}
47
48// PR4079
49union x {struct {int x;};};
50int x[__builtin_offsetof(union x, x)];
51
52// rdar://problem/7222956
53struct incomplete; // expected-note 2 {{forward declaration of 'struct incomplete'}}
54int test1[__builtin_offsetof(struct incomplete, foo)]; // expected-error {{offsetof of incomplete type 'struct incomplete'}}
55
56int test2[__builtin_offsetof(struct incomplete[10], [4].foo)]; // expected-error {{array has incomplete element type 'struct incomplete'}}
57
58// Bitfields
59struct has_bitfields {
60  int i : 7;
61  int j : 12; // expected-note{{bit-field is declared here}}
62};
63
64int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
65
66typedef struct Array { int array[1]; } Array;
67int test4 = __builtin_offsetof(Array, array);
68
69int test5() {
70  return __builtin_offsetof(Array, array[*(int*)0]); // expected-warning{{indirection of non-volatile null pointer}} expected-note{{__builtin_trap}}
71}
72