Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / offsetof.c
blobf9ef48ea0aeb5cc962989342eb15309f868c51a9
1 // RUN: %clang_cc1 -Wno-pointer-to-int-cast -fsyntax-only -verify %s
3 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
5 typedef struct P { int i; float f; } PT;
6 struct external_sun3_core
8 unsigned c_regs;
10 PT X[100];
14 // Ensure the builtin works as a constant expression
15 int i = offsetof(PT, f);
17 void swap(void)
19 int x;
20 x = offsetof(struct external_sun3_core, c_regs);
21 x = __builtin_offsetof(struct external_sun3_core, X[42].f);
23 x = __builtin_offsetof(struct external_sun3_core, X[42].f2); // expected-error {{no member named 'f2'}}
24 x = __builtin_offsetof(int, X[42].f2); // expected-error {{offsetof requires struct}}
26 int a[__builtin_offsetof(struct external_sun3_core, X) == 4 ? 1 : -1];
27 int b[__builtin_offsetof(struct external_sun3_core, X[42]) == 340 ? 1 : -1];
28 int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1]; // expected-error {{no member named 'f2'}}
31 extern int f(void);
33 struct s1 { int a; };
34 int v1 = offsetof (struct s1, a) == 0 ? 0 : f();
36 struct s2 { int a; };
37 int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f();
39 struct s3 { int a; };
40 int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f();
42 // PR3396
43 struct sockaddr_un {
44 unsigned char sun_len;
45 char sun_path[104];
47 int a(int len) {
48 int a[__builtin_offsetof(struct sockaddr_un, sun_path[len+1])];
51 // PR4079
52 union x {struct {int x;};};
53 int x[__builtin_offsetof(union x, x)];
55 struct incomplete; // expected-note 2 {{forward declaration of 'struct incomplete'}}
56 int test1[__builtin_offsetof(struct incomplete, foo)]; // expected-error {{offsetof of incomplete type 'struct incomplete'}}
58 int test2[__builtin_offsetof(struct incomplete[10], [4].foo)]; // expected-error {{array has incomplete element type 'struct incomplete'}}
60 // Bitfields
61 struct has_bitfields {
62 int i : 7;
63 int j : 12; // expected-note{{bit-field is declared here}}
66 int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
68 typedef struct Array { int array[1]; } Array;
69 int test4 = __builtin_offsetof(Array, array);
71 int test5(void) {
72 return __builtin_offsetof(Array, array[*(int*)0]); // expected-warning{{indirection of non-volatile null pointer}} expected-note{{__builtin_trap}}