Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 2 | |
| 3 | struct NonPOD { |
| 4 | virtual void f(); |
| 5 | int m; |
| 6 | }; |
| 7 | |
| 8 | struct P { |
| 9 | NonPOD fieldThatPointsToANonPODType; |
| 10 | }; |
| 11 | |
| 12 | void f() { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 13 | int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}} |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 14 | } |
| 15 | |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 16 | struct Base { int x; }; |
| 17 | struct Derived : Base { int y; }; |
| 18 | int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}} |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 19 | |
| 20 | const int o2 = sizeof(__builtin_offsetof(Derived, x)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | |
| 22 | struct HasArray { |
| 23 | int array[17]; |
| 24 | }; |
| 25 | |
| 26 | // Constant and non-constant offsetof expressions |
| 27 | void test_ice(int i) { |
| 28 | int array0[__builtin_offsetof(HasArray, array[5])]; |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 29 | int array1[__builtin_offsetof(HasArray, array[i])]; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 30 | } |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 31 | |
| 32 | // Bitfields |
| 33 | struct has_bitfields { |
| 34 | int i : 7; |
| 35 | int j : 12; // expected-note{{bit-field is declared here}} |
| 36 | }; |
| 37 | |
| 38 | int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}} |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 39 | |
| 40 | // offsetof referring to members of a base class. |
| 41 | struct Base1 { |
| 42 | int x; |
| 43 | }; |
| 44 | |
| 45 | struct Base2 { |
| 46 | int y; |
| 47 | }; |
| 48 | |
| 49 | struct Derived2 : public Base1, public Base2 { |
| 50 | int z; |
| 51 | }; |
| 52 | |
| 53 | int derived1[__builtin_offsetof(Derived2, x) == 0? 1 : -1]; |
| 54 | int derived2[__builtin_offsetof(Derived2, y) == 4? 1 : -1]; |
| 55 | int derived3[__builtin_offsetof(Derived2, z) == 8? 1 : -1]; |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 56 | |
| 57 | // offsetof referring to anonymous struct in base. |
| 58 | // PR7769 |
| 59 | struct foo { |
| 60 | struct { |
| 61 | int x; |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | struct bar : public foo { |
| 66 | }; |
| 67 | |
| 68 | int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1]; |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 69 | |
| 70 | struct LtoRCheck { |
| 71 | int a[10]; |
| 72 | int f(); |
| 73 | }; |
| 74 | int ltor = __builtin_offsetof(struct LtoRCheck, a[LtoRCheck().f]); // \ |
| 75 | expected-error {{reference to non-static member function must be called}} |