xref: /llvm-project/clang/test/SemaCXX/offsetof.cpp (revision 10982ea3f9e11da2311b6b47d6bad2dd63bdff9d)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -Winvalid-offsetof
2 
3 struct NonPOD {
4   virtual void f();
5   int m;
6 };
7 
8 struct P {
9   NonPOD fieldThatPointsToANonPODType;
10 };
11 
12 void f() {
13   int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}}
14 }
15 
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}}
19 
20 const int o2 = sizeof(__builtin_offsetof(Derived, x));
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])];
29   int array1[__builtin_offsetof(HasArray, array[i])]; // expected-error{{variable length arrays are not permitted in C++}}
30 }
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'}}
39