xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/offsetof.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct NonPOD {
4*f4a2713aSLionel Sambuc   virtual void f();
5*f4a2713aSLionel Sambuc   int m;
6*f4a2713aSLionel Sambuc };
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc struct P {
9*f4a2713aSLionel Sambuc   NonPOD fieldThatPointsToANonPODType;
10*f4a2713aSLionel Sambuc };
11*f4a2713aSLionel Sambuc 
f()12*f4a2713aSLionel Sambuc void f() {
13*f4a2713aSLionel Sambuc   int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}}
14*f4a2713aSLionel Sambuc }
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc struct Base { int x; };
17*f4a2713aSLionel Sambuc struct Derived : Base { int y; };
18*f4a2713aSLionel Sambuc int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc const int o2 = sizeof(__builtin_offsetof(Derived, x));
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc struct HasArray {
23*f4a2713aSLionel Sambuc   int array[17];
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc // Constant and non-constant offsetof expressions
test_ice(int i)27*f4a2713aSLionel Sambuc void test_ice(int i) {
28*f4a2713aSLionel Sambuc   int array0[__builtin_offsetof(HasArray, array[5])];
29*f4a2713aSLionel Sambuc   int array1[__builtin_offsetof(HasArray, array[i])];
30*f4a2713aSLionel Sambuc }
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc // Bitfields
33*f4a2713aSLionel Sambuc struct has_bitfields {
34*f4a2713aSLionel Sambuc   int i : 7;
35*f4a2713aSLionel Sambuc   int j : 12; // expected-note{{bit-field is declared here}}
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // offsetof referring to members of a base class.
41*f4a2713aSLionel Sambuc struct Base1 {
42*f4a2713aSLionel Sambuc   int x;
43*f4a2713aSLionel Sambuc };
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc struct Base2 {
46*f4a2713aSLionel Sambuc   int y;
47*f4a2713aSLionel Sambuc };
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc struct Derived2 : public Base1, public Base2 {
50*f4a2713aSLionel Sambuc   int z;
51*f4a2713aSLionel Sambuc };
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc int derived1[__builtin_offsetof(Derived2, x) == 0? 1 : -1];
54*f4a2713aSLionel Sambuc int derived2[__builtin_offsetof(Derived2, y)  == 4? 1 : -1];
55*f4a2713aSLionel Sambuc int derived3[__builtin_offsetof(Derived2, z)  == 8? 1 : -1];
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc // offsetof referring to anonymous struct in base.
58*f4a2713aSLionel Sambuc // PR7769
59*f4a2713aSLionel Sambuc struct foo {
60*f4a2713aSLionel Sambuc     struct {
61*f4a2713aSLionel Sambuc         int x;
62*f4a2713aSLionel Sambuc     };
63*f4a2713aSLionel Sambuc };
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc struct bar : public foo  {
66*f4a2713aSLionel Sambuc };
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1];
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc struct LtoRCheck {
71*f4a2713aSLionel Sambuc   int a[10];
72*f4a2713aSLionel Sambuc   int f();
73*f4a2713aSLionel Sambuc };
74*f4a2713aSLionel Sambuc int ltor = __builtin_offsetof(struct LtoRCheck, a[LtoRCheck().f]); // \
75*f4a2713aSLionel Sambuc   expected-error {{reference to non-static member function must be called}}
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc namespace PR17578 {
78*f4a2713aSLionel Sambuc struct Base {
79*f4a2713aSLionel Sambuc   int Field;
80*f4a2713aSLionel Sambuc };
81*f4a2713aSLionel Sambuc struct Derived : virtual Base {
FunPR17578::Derived82*f4a2713aSLionel Sambuc   void Fun() { (void)__builtin_offsetof(Derived, Field); } // expected-warning {{offset of on non-POD type}} \
83*f4a2713aSLionel Sambuc                                                               expected-error {{invalid application of 'offsetof' to a field of a virtual base}}
84*f4a2713aSLionel Sambuc };
85*f4a2713aSLionel Sambuc }
86