]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/CodeGenCXX/pointers-to-data-members.cpp
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / CodeGenCXX / pointers-to-data-members.cpp
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
7 struct A { int a; int b; };
8 struct B { int b; };
9 struct C : B, A { };
10
11 // Zero init.
12 namespace 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
63 namespace PR5674 {
64   // CHECK-GLOBAL: @_ZN6PR56742pbE = global i64 4
65   int A::*pb = &A::b;
66 }
67
68 // Casts.
69 namespace Casts {
70
71 int A::*pa;
72 int C::*pc;
73
74 void 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
96 namespace 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
117 namespace ValueInit {
118
119 struct A {
120   int A::*a;
121
122   char c;
123
124   A();
125 };
126
127 // CHECK-LABEL: define void @_ZN9ValueInit1AC2Ev(%"struct.ValueInit::A"* %this) unnamed_addr
128 // CHECK: store i64 -1, i64*
129 // CHECK: ret void
130 A::A() : a() {}
131
132 }
133
134 namespace PR7139 {
135
136 struct pair {
137   int first;
138   int second;
139 };
140
141 typedef int pair::*ptr_to_member_type;
142
143 struct ptr_to_member_struct { 
144   ptr_to_member_type data;
145   int i;
146 };
147
148 struct A {
149   ptr_to_member_struct a;
150
151   A() : a() {}
152 };
153
154 // CHECK-O3: define zeroext i1 @_ZN6PR71395checkEv() [[NUW:#[0-9]+]]
155 bool check() {
156   // CHECK-O3: ret i1 true
157   return A().a.data == 0;
158 }
159
160 // CHECK-O3: define zeroext i1 @_ZN6PR71396check2Ev() [[NUW]]
161 bool check2() {
162   // CHECK-O3: ret i1 true
163   return ptr_to_member_type() == 0;
164 }
165
166 }
167
168 namespace VirtualBases {
169
170 struct 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
176 struct B : virtual A { };
177 B b;
178
179 // CHECK-GLOBAL: @_ZN12VirtualBases1cE = global %"struct.VirtualBases::C" { i32 (...)** null, i64 -1, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
180 struct C : virtual A { int A::*i; };
181 C 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
184 struct D : C { int A::*i; };
185 D d;
186
187 }
188
189 namespace Test1 {
190
191 // Don't crash when A contains a bit-field.
192 struct A {
193   int A::* a;
194   int b : 10;
195 };
196 A a;
197
198 }
199
200 namespace BoolPtrToMember {
201   struct X {
202     bool member;
203   };
204
205   // CHECK-LABEL: 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
214 namespace PR8507 {
215   
216 struct S;
217 void f(S* p, double S::*pm) {
218   if (0 < p->*pm) {
219   }
220 }
221
222 }
223
224 namespace 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
234 namespace 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
244 namespace PR13097 {
245   struct X { int x; X(const X&); };
246   struct A {
247     int qq;
248       X x;
249   };
250   A f();
251   X g() { return f().*&A::x; }
252   // CHECK-LABEL: define void @_ZN7PR130971gEv
253   // CHECK: call void @_ZN7PR130971fEv
254   // CHECK-NOT: memcpy
255   // CHECK: call void @_ZN7PR130971XC1ERKS0_
256 }
257
258 // CHECK-O3: attributes [[NUW]] = { nounwind readnone{{.*}} }