blob: 0029659ed3353cb1eff1eb84e84b72d521945092 [file] [log] [blame]
Anders Carlssonfe404562010-02-02 03:23:38 +00001// RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-darwin10 | FileCheck %s
Anders Carlsson1884eb02010-05-22 17:35:42 +00002// RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-darwin10 -O3 | FileCheck --check-prefix=CHECK-O3 %s
Anders Carlsson45147d02010-02-02 03:37:46 +00003struct A { int a; int b; };
Eli Friedman9b372742009-11-27 04:56:40 +00004struct B { int b; };
5struct C : B, A { };
6
Anders Carlsson63b071f2010-02-02 03:32:35 +00007// Zero init.
8namespace ZeroInit {
9 // CHECK: @_ZN8ZeroInit1aE = global i64 -1
10 int A::* a;
11
12 // CHECK: @_ZN8ZeroInit2aaE = global [2 x i64] [i64 -1, i64 -1]
13 int A::* aa[2];
14
15 // CHECK: @_ZN8ZeroInit3aaaE = global [2 x [2 x i64]] {{\[}}[2 x i64] [i64 -1, i64 -1], [2 x i64] [i64 -1, i64 -1]]
16 int A::* aaa[2][2];
17
18 // CHECK: @_ZN8ZeroInit1bE = global i64 -1,
19 int A::* b = 0;
Anders Carlsson2c12d032010-02-02 05:17:25 +000020
John McCall1fb0caa2010-10-22 21:05:15 +000021 // CHECK: @_ZN8ZeroInit2saE = internal global %struct.anon { i64 -1 }
Anders Carlsson2c12d032010-02-02 05:17:25 +000022 struct {
23 int A::*a;
24 } sa;
John McCall1fb0caa2010-10-22 21:05:15 +000025 void test_sa() { (void) sa; } // force emission
Anders Carlsson2c12d032010-02-02 05:17:25 +000026
John McCall1fb0caa2010-10-22 21:05:15 +000027 // CHECK: @_ZN8ZeroInit3ssaE = internal
Anders Carlsson7e673c42010-02-05 06:58:06 +000028 // CHECK: [2 x i64] [i64 -1, i64 -1]
Anders Carlsson2c12d032010-02-02 05:17:25 +000029 struct {
30 int A::*aa[2];
31 } ssa[2];
John McCall1fb0caa2010-10-22 21:05:15 +000032 void test_ssa() { (void) ssa; }
Anders Carlsson2c12d032010-02-02 05:17:25 +000033
John McCall1fb0caa2010-10-22 21:05:15 +000034 // CHECK: @_ZN8ZeroInit2ssE = internal global %1 { %struct.anon { i64 -1 } }
Anders Carlsson2c12d032010-02-02 05:17:25 +000035 struct {
36 struct {
37 int A::*pa;
38 } s;
39 } ss;
John McCall1fb0caa2010-10-22 21:05:15 +000040 void test_ss() { (void) ss; }
Anders Carlssona83fb4b2010-05-18 16:51:41 +000041
42 struct A {
43 int A::*a;
44 int b;
45 };
46
47 struct B {
48 A a[10];
49 char c;
50 int B::*b;
51 };
52
53 struct C : A, B { int j; };
54 // CHECK: @_ZN8ZeroInit1cE = global %"struct.ZeroInit::C" { [16 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00", [176 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF", i32 0, [4 x i8] zeroinitializer }
55 C c;
Anders Carlsson45147d02010-02-02 03:37:46 +000056}
Anders Carlsson63b071f2010-02-02 03:32:35 +000057
Anders Carlsson45147d02010-02-02 03:37:46 +000058// PR5674
59namespace PR5674 {
60 // CHECK: @_ZN6PR56742pbE = global i64 4
61 int A::*pb = &A::b;
Anders Carlsson63b071f2010-02-02 03:32:35 +000062}
63
Anders Carlssonfe404562010-02-02 03:23:38 +000064// Casts.
65namespace Casts {
66
Eli Friedman9b372742009-11-27 04:56:40 +000067int A::*pa;
68int C::*pc;
69
70void f() {
John McCall0bab0cd2010-08-23 01:21:21 +000071 // CHECK: store i64 -1, i64* @_ZN5Casts2paE
Eli Friedman9b372742009-11-27 04:56:40 +000072 pa = 0;
73
John McCall0bab0cd2010-08-23 01:21:21 +000074 // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2paE, align 8
75 // CHECK-NEXT: [[ADJ:%.*]] = add nsw i64 [[TMP]], 4
76 // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
77 // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
78 // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2pcE
Eli Friedman9b372742009-11-27 04:56:40 +000079 pc = pa;
80
John McCall0bab0cd2010-08-23 01:21:21 +000081 // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2pcE, align 8
82 // CHECK-NEXT: [[ADJ:%.*]] = sub nsw i64 [[TMP]], 4
83 // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
84 // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
85 // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2paE
Eli Friedman9b372742009-11-27 04:56:40 +000086 pa = static_cast<int A::*>(pc);
87}
Anders Carlssonfe404562010-02-02 03:23:38 +000088
89}
Anders Carlsson45147d02010-02-02 03:37:46 +000090
91// Comparisons
92namespace Comparisons {
93 void f() {
94 int A::*a;
95
96 // CHECK: icmp ne i64 {{.*}}, -1
97 if (a) { }
98
99 // CHECK: icmp ne i64 {{.*}}, -1
100 if (a != 0) { }
101
102 // CHECK: icmp ne i64 -1, {{.*}}
103 if (0 != a) { }
104
105 // CHECK: icmp eq i64 {{.*}}, -1
106 if (a == 0) { }
107
108 // CHECK: icmp eq i64 -1, {{.*}}
109 if (0 == a) { }
110 }
111}
Anders Carlsson3cb18bc2010-05-14 15:05:19 +0000112
113namespace ValueInit {
114
115struct A {
116 int A::*a;
117
118 char c;
119
120 A();
121};
122
Rafael Espindolac5f657f2011-01-11 00:26:26 +0000123// CHECK: define unnamed_addr void @_ZN9ValueInit1AC2Ev
Anders Carlsson3cb18bc2010-05-14 15:05:19 +0000124// CHECK: store i64 -1, i64*
125// CHECK: ret void
126A::A() : a() {}
127
128}
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000129
Anders Carlsson1884eb02010-05-22 17:35:42 +0000130namespace PR7139 {
131
132struct pair {
133 int first;
134 int second;
135};
136
137typedef int pair::*ptr_to_member_type;
138
139struct ptr_to_member_struct {
140 ptr_to_member_type data;
141 int i;
142};
143
144struct A {
145 ptr_to_member_struct a;
146
147 A() : a() {}
148};
149
150// CHECK-O3: define zeroext i1 @_ZN6PR71395checkEv() nounwind readnone
151bool check() {
152 // CHECK-O3: ret i1 true
153 return A().a.data == 0;
154}
155
Anders Carlssona40a9f32010-05-22 17:45:10 +0000156// CHECK-O3: define zeroext i1 @_ZN6PR71396check2Ev() nounwind readnone
157bool check2() {
158 // CHECK-O3: ret i1 true
159 return ptr_to_member_type() == 0;
160}
161
Anders Carlsson1884eb02010-05-22 17:35:42 +0000162}
Anders Carlsson0a87b372010-05-27 18:51:01 +0000163
164namespace VirtualBases {
165
166struct A {
167 char c;
168 int A::*i;
169};
170
171// FIXME: A::i should be initialized to -1 here.
172struct B : virtual A { };
173B b;
174
175// FIXME: A::i should be initialized to -1 here.
176struct C : virtual A { int A::*i; };
177C c;
178
179// FIXME: C::A::i should be initialized to -1 here.
180struct D : C { int A::*i; };
181D d;
182
183}
Anders Carlssone9a05b72010-06-03 15:36:07 +0000184
185namespace Test1 {
186
187// Don't crash when A contains a bit-field.
188struct A {
189 int A::* a;
190 int b : 10;
191};
192A a;
193
194}
Douglas Gregorb61e2a32010-09-02 15:00:29 +0000195
196namespace BoolPtrToMember {
197 struct X {
198 bool member;
199 };
200
201 // CHECK: define i8* @_ZN15BoolPtrToMember1fERNS_1XEMS0_b
202 bool &f(X &x, bool X::*member) {
203 // CHECK: {{bitcast.* to i8\*}}
204 // CHECK-NEXT: getelementptr inbounds i8*
205 // CHECK-NEXT: ret i8*
206 return x.*member;
207 }
208}
Anders Carlsson96e93662010-10-31 01:21:47 +0000209
210namespace PR8507 {
211
212struct S;
213void f(S* p, double S::*pm) {
214 if (0 < p->*pm) {
215 }
216}
217
218}
219