1// RUN: %clang_cc1 %s -emit-llvm -o %t.ll -triple=x86_64-apple-darwin10
2// RUN: FileCheck %s < %t.ll
3// RUN: FileCheck -check-prefix=CHECK-GLOBAL %s < %t.ll
4// RUN: %clang_cc1 %s -emit-llvm -o %t-opt.ll -triple=x86_64-apple-darwin10 -O3
5// RUN: FileCheck --check-prefix=CHECK-O3 %s < %t-opt.ll
6
7struct A { int a; int b; };
8struct B { int b; };
9struct C : B, A { };
10
11// Zero init.
12namespace ZeroInit {
13  // CHECK-GLOBAL: @_ZN8ZeroInit1aE = global i64 -1
14  int A::* a;
15
16  // CHECK-GLOBAL: @_ZN8ZeroInit2aaE = global [2 x i64] [i64 -1, i64 -1]
17  int A::* aa[2];
18
19  // CHECK-GLOBAL: @_ZN8ZeroInit3aaaE = global [2 x [2 x i64]] {{\[}}[2 x i64] [i64 -1, i64 -1], [2 x i64] [i64 -1, i64 -1]]
20  int A::* aaa[2][2];
21
22  // CHECK-GLOBAL: @_ZN8ZeroInit1bE = global i64 -1,
23  int A::* b = 0;
24
25  // CHECK-GLOBAL: @_ZN8ZeroInit2saE = internal global %struct.anon { i64 -1 }
26  struct {
27    int A::*a;
28  } sa;
29  void test_sa() { (void) sa; } // force emission
30
31  // CHECK-GLOBAL: @_ZN8ZeroInit3ssaE = internal
32  // CHECK-GLOBAL: [2 x i64] [i64 -1, i64 -1]
33  struct {
34    int A::*aa[2];
35  } ssa[2];
36  void test_ssa() { (void) ssa; }
37
38  // CHECK-GLOBAL: @_ZN8ZeroInit2ssE = internal global %struct.anon.1 { %struct.anon.2 { i64 -1 } }
39  struct {
40    struct {
41      int A::*pa;
42    } s;
43  } ss;
44  void test_ss() { (void) ss; }
45
46  struct A {
47    int A::*a;
48    int b;
49  };
50
51  struct B {
52    A a[10];
53    char c;
54    int B::*b;
55  };
56
57  struct C : A, B { int j; };
58  // CHECK-GLOBAL: @_ZN8ZeroInit1cE = global {{%.*}} { %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::B" { [10 x %"struct.ZeroInit::A"] [%"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }], i8 0, i64 -1 }, i32 0 }, align 8
59  C c;
60}
61
62// PR5674
63namespace PR5674 {
64  // CHECK-GLOBAL: @_ZN6PR56742pbE = global i64 4
65  int A::*pb = &A::b;
66}
67
68// Casts.
69namespace Casts {
70
71int A::*pa;
72int C::*pc;
73
74void f() {
75  // CHECK:      store i64 -1, i64* @_ZN5Casts2paE
76  pa = 0;
77
78  // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2paE, align 8
79  // CHECK-NEXT: [[ADJ:%.*]] = add nsw i64 [[TMP]], 4
80  // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
81  // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
82  // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2pcE
83  pc = pa;
84
85  // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2pcE, align 8
86  // CHECK-NEXT: [[ADJ:%.*]] = sub nsw i64 [[TMP]], 4
87  // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
88  // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
89  // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2paE
90  pa = static_cast<int A::*>(pc);
91}
92
93}
94
95// Comparisons
96namespace Comparisons {
97  void f() {
98    int A::*a;
99
100    // CHECK: icmp ne i64 {{.*}}, -1
101    if (a) { }
102
103    // CHECK: icmp ne i64 {{.*}}, -1
104    if (a != 0) { }
105
106    // CHECK: icmp ne i64 -1, {{.*}}
107    if (0 != a) { }
108
109    // CHECK: icmp eq i64 {{.*}}, -1
110    if (a == 0) { }
111
112    // CHECK: icmp eq i64 -1, {{.*}}
113    if (0 == a) { }
114  }
115}
116
117namespace ValueInit {
118
119struct A {
120  int A::*a;
121
122  char c;
123
124  A();
125};
126
127// CHECK: define void @_ZN9ValueInit1AC2Ev(%"struct.ValueInit::A"* %this) unnamed_addr
128// CHECK: store i64 -1, i64*
129// CHECK: ret void
130A::A() : a() {}
131
132}
133
134namespace PR7139 {
135
136struct pair {
137  int first;
138  int second;
139};
140
141typedef int pair::*ptr_to_member_type;
142
143struct ptr_to_member_struct {
144  ptr_to_member_type data;
145  int i;
146};
147
148struct A {
149  ptr_to_member_struct a;
150
151  A() : a() {}
152};
153
154// CHECK-O3: define zeroext i1 @_ZN6PR71395checkEv() nounwind readnone
155bool check() {
156  // CHECK-O3: ret i1 true
157  return A().a.data == 0;
158}
159
160// CHECK-O3: define zeroext i1 @_ZN6PR71396check2Ev() nounwind readnone
161bool check2() {
162  // CHECK-O3: ret i1 true
163  return ptr_to_member_type() == 0;
164}
165
166}
167
168namespace VirtualBases {
169
170struct A {
171  char c;
172  int A::*i;
173};
174
175// CHECK-GLOBAL: @_ZN12VirtualBases1bE = global %"struct.VirtualBases::B" { i32 (...)** null, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
176struct B : virtual A { };
177B b;
178
179// CHECK-GLOBAL: @_ZN12VirtualBases1cE = global %"struct.VirtualBases::C" { i32 (...)** null, i64 -1, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
180struct C : virtual A { int A::*i; };
181C c;
182
183// CHECK-GLOBAL: @_ZN12VirtualBases1dE = global %"struct.VirtualBases::D" { %"struct.VirtualBases::C.base" { i32 (...)** null, i64 -1 }, i64 -1, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
184struct D : C { int A::*i; };
185D d;
186
187}
188
189namespace Test1 {
190
191// Don't crash when A contains a bit-field.
192struct A {
193  int A::* a;
194  int b : 10;
195};
196A a;
197
198}
199
200namespace BoolPtrToMember {
201  struct X {
202    bool member;
203  };
204
205  // CHECK: define i8* @_ZN15BoolPtrToMember1fERNS_1XEMS0_b
206  bool &f(X &x, bool X::*member) {
207    // CHECK: {{bitcast.* to i8\*}}
208    // CHECK-NEXT: getelementptr inbounds i8*
209    // CHECK-NEXT: ret i8*
210    return x.*member;
211  }
212}
213
214namespace PR8507 {
215
216struct S;
217void f(S* p, double S::*pm) {
218  if (0 < p->*pm) {
219  }
220}
221
222}
223
224namespace test4 {
225  struct A             { int A_i; };
226  struct B : virtual A { int A::*B_p; };
227  struct C : virtual B { int    *C_p; };
228  struct D :         C { int    *D_p; };
229
230  // CHECK-GLOBAL: @_ZN5test41dE = global %"struct.test4::D" { %"struct.test4::C.base" zeroinitializer, i32* null, %"struct.test4::B.base" { i32 (...)** null, i64 -1 }, %"struct.test4::A" zeroinitializer }, align 8
231  D d;
232}
233
234namespace PR11487 {
235  union U
236  {
237    int U::* mptr;
238    char x[16];
239  } x;
240  // CHECK-GLOBAL: @_ZN7PR114871xE = global %"union.PR11487::U" { i64 -1, [8 x i8] zeroinitializer }, align 8
241
242}
243