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