xref: /llvm-project/clang/test/SemaCXX/offsetof.cpp (revision c45466cd9a51fe384d2b31e124b77d14c821eb70)
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof -std=c++98
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof -std=c++98 -fexperimental-new-constant-interpreter
3 
4 struct NonPOD {
5   virtual void f();
6   int m;
7 };
8 
9 struct P {
10   NonPOD fieldThatPointsToANonPODType;
11 };
12 
f()13 void f() {
14   int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}}
15 }
16 
17 struct Base { int x; };
18 struct Derived : Base { int y; };
19 int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}}
20 
21 const int o2 = sizeof(__builtin_offsetof(Derived, x));
22 
23 struct HasArray {
24   int array[17];
25 };
26 
27 // Constant and non-constant offsetof expressions
test_ice(int i)28 void test_ice(int i) {
29   int array0[__builtin_offsetof(HasArray, array[5])];
30   int array1[__builtin_offsetof(HasArray, array[i])]; // expected-warning {{variable length arrays in C++ are a Clang extension}}
31 }
32 
33 // Bitfields
34 struct has_bitfields {
35   int i : 7;
36   int j : 12; // expected-note{{bit-field is declared here}}
37 };
38 
39 int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
40 
41 // offsetof referring to members of a base class.
42 struct Base1 {
43   int x;
44 };
45 
46 struct Base2 {
47   int y;
48 };
49 
50 struct Derived2 : public Base1, public Base2 {
51   int z;
52 };
53 
54 int derived1[__builtin_offsetof(Derived2, x) == 0? 1 : -1]; // expected-warning{{offset of on non-POD type 'Derived2'}}
55 int derived2[__builtin_offsetof(Derived2, y)  == 4? 1 : -1]; // expected-warning{{offset of on non-POD type 'Derived2'}}
56 int derived3[__builtin_offsetof(Derived2, z)  == 8? 1 : -1]; // expected-warning{{offset of on non-POD type 'Derived2'}}
57 
58 // offsetof referring to anonymous struct in base.
59 // PR7769
60 struct foo {
61     struct {
62         int x;
63     };
64 };
65 
66 struct bar : public foo  {
67 };
68 
69 int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1]; // expected-warning{{offset of on non-POD type 'bar'}}
70 
71 
72 struct LtoRCheck {
73   int a[10];
74   int f();
75 };
76 int ltor = __builtin_offsetof(struct LtoRCheck, a[LtoRCheck().f]); // \
77   expected-error {{reference to non-static member function must be called}}
78 
79 namespace PR17578 {
80 struct Base {
81   int Field;
82 };
83 struct Derived : virtual Base {
FunPR17578::Derived84   void Fun() { (void)__builtin_offsetof(Derived, Field); } // expected-warning {{offset of on non-POD type}} \
85                                                               expected-error {{invalid application of 'offsetof' to a field of a virtual base}}
86 };
87 }
88 
test_definition(void)89 int test_definition(void) {
90   return __builtin_offsetof(struct A // expected-error {{'A' cannot be defined in a type specifier}}
91   {
92     int a;
93     struct B // FIXME: error diagnostic message for nested definitions
94              // https://reviews.llvm.org/D133574
95              // fixme-error{{'A' cannot be defined in '__builtin_offsetof'}}
96     {
97       int c;
98       int d;
99     };
100     B x;
101   }, a);
102 }
103