| Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s | 
| Chris Lattner | d3fb6ad | 2007-08-30 17:51:09 +0000 | [diff] [blame] | 2 |  | 
|  | 3 | #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) | 
|  | 4 |  | 
|  | 5 | typedef struct P { int i; float f; } PT; | 
|  | 6 | struct external_sun3_core | 
|  | 7 | { | 
|  | 8 | unsigned c_regs; | 
|  | 9 |  | 
|  | 10 | PT  X[100]; | 
|  | 11 |  | 
|  | 12 | }; | 
|  | 13 |  | 
|  | 14 | void 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 Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 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]; | 
| Chris Lattner | e0c5414 | 2008-01-30 06:46:30 +0000 | [diff] [blame] | 25 | int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1];  // expected-error {{no member named 'f2'}} | 
| Chris Lattner | d3fb6ad | 2007-08-30 17:51:09 +0000 | [diff] [blame] | 26 | } | 
|  | 27 |  | 
| Anders Carlsson | 601bae3 | 2008-12-06 22:27:22 +0000 | [diff] [blame] | 28 | extern int f(); | 
| Chris Lattner | e0c5414 | 2008-01-30 06:46:30 +0000 | [diff] [blame] | 29 |  | 
|  | 30 | struct s1 { int a; }; | 
| Anders Carlsson | 601bae3 | 2008-12-06 22:27:22 +0000 | [diff] [blame] | 31 | int v1 = offsetof (struct s1, a) == 0 ? 0 : f(); | 
| Chris Lattner | e0c5414 | 2008-01-30 06:46:30 +0000 | [diff] [blame] | 32 |  | 
|  | 33 | struct s2 { int a; }; | 
| Anders Carlsson | 601bae3 | 2008-12-06 22:27:22 +0000 | [diff] [blame] | 34 | int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f(); | 
| Chris Lattner | e0c5414 | 2008-01-30 06:46:30 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | struct s3 { int a; }; | 
| Anders Carlsson | 601bae3 | 2008-12-06 22:27:22 +0000 | [diff] [blame] | 37 | int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f(); | 
| Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | // PR3396 | 
|  | 40 | struct sockaddr_un { | 
|  | 41 | unsigned char sun_len; | 
|  | 42 | char sun_path[104]; | 
|  | 43 | }; | 
|  | 44 | int a(int len) { | 
|  | 45 | int a[__builtin_offsetof(struct sockaddr_un, sun_path[len+1])]; | 
|  | 46 | } | 
|  | 47 |  | 
| Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 48 | // PR4079 | 
|  | 49 | union x {struct {int x;};}; | 
|  | 50 | int x[__builtin_offsetof(union x, x)]; | 
| John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 51 |  | 
|  | 52 | // rdar://problem/7222956 | 
|  | 53 | struct incomplete; // expected-note 2 {{forward declaration of 'struct incomplete'}} | 
|  | 54 | int test1[__builtin_offsetof(struct incomplete, foo)]; // expected-error {{offsetof of incomplete type 'struct incomplete'}} | 
|  | 55 |  | 
| Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 56 | int test2[__builtin_offsetof(struct incomplete[10], [4].foo)]; // expected-error {{array has incomplete element type 'struct incomplete'}} | 
|  | 57 |  | 
|  | 58 | // Bitfields | 
|  | 59 | struct has_bitfields { | 
|  | 60 | int i : 7; | 
|  | 61 | int j : 12; // expected-note{{bit-field is declared here}} | 
|  | 62 | }; | 
|  | 63 |  | 
|  | 64 | int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}} | 
| Douglas Gregor | 72be24f | 2010-04-30 20:35:01 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | typedef struct Array { int array[1]; } Array; | 
|  | 67 | int test4 = __builtin_offsetof(Array, array); |