blob: dfae99216f8a19bfa13f12050911c64354906e95 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s
Chris Lattnerd3fb6ad2007-08-30 17:51:09 +00002
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}}
Anders Carlsson5a1deb82008-01-29 15:56:48 +000022
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];
Chris Lattnere0c54142008-01-30 06:46:30 +000025 int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1]; // expected-error {{no member named 'f2'}}
Chris Lattnerd3fb6ad2007-08-30 17:51:09 +000026}
27
Anders Carlsson601bae32008-12-06 22:27:22 +000028extern int f();
Chris Lattnere0c54142008-01-30 06:46:30 +000029
30struct s1 { int a; };
Anders Carlsson601bae32008-12-06 22:27:22 +000031int v1 = offsetof (struct s1, a) == 0 ? 0 : f();
Chris Lattnere0c54142008-01-30 06:46:30 +000032
33struct s2 { int a; };
Anders Carlsson601bae32008-12-06 22:27:22 +000034int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f();
Chris Lattnere0c54142008-01-30 06:46:30 +000035
36struct s3 { int a; };
Anders Carlsson601bae32008-12-06 22:27:22 +000037int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f();
Eli Friedman35183ac2009-02-27 06:44:11 +000038
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
Eli Friedmane9356962009-04-26 20:50:44 +000048// PR4079
49union x {struct {int x;};};
50int x[__builtin_offsetof(union x, x)];
John McCalld00f2002009-11-04 03:03:43 +000051
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 test1[__builtin_offsetof(struct incomplete[10], [4].foo)]; // expected-error {{array has incomplete element type 'struct incomplete'}}