blob: 17cee62d16fc8051637ad069c8b81d6203317ab6 [file] [log] [blame]
Douglas Gregord1702062010-04-29 00:18:15 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof
Anders Carlsson2bbb86b2009-05-01 23:20:30 +00002
3struct NonPOD {
4 virtual void f();
5 int m;
6};
7
8struct P {
9 NonPOD fieldThatPointsToANonPODType;
10};
11
12void f() {
John McCall85f90552010-03-10 11:27:22 +000013 int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}}
Anders Carlsson2bbb86b2009-05-01 23:20:30 +000014}
15
Eli Friedman78cde142009-12-04 07:18:51 +000016struct Base { int x; };
17struct Derived : Base { int y; };
18int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}}
Douglas Gregor7ca84af2009-12-12 07:25:49 +000019
20const int o2 = sizeof(__builtin_offsetof(Derived, x));
Douglas Gregor882211c2010-04-28 22:16:22 +000021
22struct HasArray {
23 int array[17];
24};
25
26// Constant and non-constant offsetof expressions
27void test_ice(int i) {
28 int array0[__builtin_offsetof(HasArray, array[5])];
Douglas Gregor959d5a02010-05-22 16:17:30 +000029 int array1[__builtin_offsetof(HasArray, array[i])];
Douglas Gregor882211c2010-04-28 22:16:22 +000030}
Douglas Gregor10982ea2010-04-28 22:36:06 +000031
32// Bitfields
33struct has_bitfields {
34 int i : 7;
35 int j : 12; // expected-note{{bit-field is declared here}}
36};
37
38int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
Douglas Gregord1702062010-04-29 00:18:15 +000039
40// offsetof referring to members of a base class.
41struct Base1 {
42 int x;
43};
44
45struct Base2 {
46 int y;
47};
48
49struct Derived2 : public Base1, public Base2 {
50 int z;
51};
52
53int derived1[__builtin_offsetof(Derived2, x) == 0? 1 : -1];
54int derived2[__builtin_offsetof(Derived2, y) == 4? 1 : -1];
55int derived3[__builtin_offsetof(Derived2, z) == 8? 1 : -1];
Eli Friedman74ef7cf2010-08-05 10:11:36 +000056
57// offsetof referring to anonymous struct in base.
58// PR7769
59struct foo {
60 struct {
61 int x;
62 };
63};
64
65struct bar : public foo {
66};
67
68int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1];