blob: fe15ccf8fef5d3e66ba21c44548c4a796d65c4f8 [file] [log] [blame]
Richard Smith9ca5c422011-10-13 22:29:44 +00001// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 %s -emit-llvm -o %t
Douglas Gregor8a556ce2011-09-22 15:15:51 +00002// RUN: FileCheck %s < %t
3// RUN: FileCheck -check-prefix=CHECK-PR10720 %s < %t
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +00004
5extern "C" int printf(...);
6
7struct M {
8 M() { printf("M()\n"); }
9 M(int i) { iM = i; printf("M(%d)\n", i); }
10 int iM;
11 void MPR() {printf("iM = %d\n", iM); };
12};
13
14struct P {
15 P() { printf("P()\n"); }
16 P(int i) { iP = i; printf("P(%d)\n", i); }
17 int iP;
18 void PPR() {printf("iP = %d\n", iP); };
19};
20
21struct Q {
22 Q() { printf("Q()\n"); }
23 Q(int i) { iQ = i; printf("Q(%d)\n", i); }
24 int iQ;
25 void QPR() {printf("iQ = %d\n", iQ); };
26};
27
28struct N : M , P, Q {
29 N() : f1(1.314), P(2000), ld(00.1234+f1), M(1000), Q(3000),
30 d1(3.4567), i1(1234), m1(100) { printf("N()\n"); }
31 M m1;
32 M m2;
33 float f1;
34 int i1;
35 float d1;
Mike Stump11289f42009-09-09 15:08:12 +000036 void PR() {
37 printf("f1 = %f d1 = %f i1 = %d ld = %f \n", f1,d1,i1, ld);
38 MPR();
39 PPR();
40 QPR();
41 printf("iQ = %d\n", iQ);
42 printf("iP = %d\n", iP);
43 printf("iM = %d\n", iM);
44 // FIXME. We don't yet support this syntax.
45 // printf("iQ = %d\n", (*this).iQ);
46 printf("iQ = %d\n", this->iQ);
47 printf("iP = %d\n", this->iP);
48 printf("iM = %d\n", this->iM);
49 }
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +000050 float ld;
51 float ff;
Fariborz Jahanian7481bd82009-08-21 17:09:38 +000052 M arr_m[3];
53 P arr_p[1][3];
54 Q arr_q[2][3][4];
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +000055};
56
57int main() {
58 M m1;
59
60 N n1;
61 n1.PR();
62}
63
Douglas Gregora4b592a2009-12-19 03:01:41 +000064// PR5826
65template <class T> struct A {
66 A() {}
67 A(int) {}
68 A(const A&) {}
69 ~A() {}
70 operator int() {return 0;}
71};
72
Stephen Lin43622612013-08-15 06:47:53 +000073// CHECK-LABEL: define void @_Z1fv()
Douglas Gregora4b592a2009-12-19 03:01:41 +000074void f() {
75 // CHECK: call void @_ZN1AIsEC1Ei
76 A<short> a4 = 97;
77
78 // CHECK-NEXT: store i32 17
79 int i = 17;
80
81 // CHECK-NEXT: call void @_ZN1AIsED1Ev
82 // CHECK-NOT: call void @_ZN1AIsED1Ev
83 // CHECK: ret void
84}
Douglas Gregorc14922f2010-03-26 22:43:07 +000085
John McCall769250e2010-09-17 02:31:44 +000086// Make sure we initialize the vtable pointer if it's required by a
87// base initializer.
88namespace InitVTable {
89 struct A { A(int); };
90 struct B : A {
91 virtual int foo();
92 B();
93 B(int);
94 };
95
Stephen Lin43622612013-08-15 06:47:53 +000096 // CHECK-LABEL: define void @_ZN10InitVTable1BC2Ev(%"struct.InitVTable::B"* %this) unnamed_addr
Reid Kleckner8d585132014-12-03 21:00:21 +000097 // CHECK: [[T0:%.*]] = bitcast [[B:%.*]]* [[THIS:%.*]] to i32 (...)***
Peter Collingbourne25a2b702016-12-13 20:50:44 +000098 // CHECK-NEXT: store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTVN10InitVTable1BE, i32 0, inrange i32 0, i32 2) to i32 (...)**), i32 (...)*** [[T0]]
David Blaikiea953f282015-02-27 21:19:58 +000099 // CHECK: [[VTBL:%.*]] = load i32 ([[B]]*)**, i32 ([[B]]*)*** {{%.*}}
David Blaikie218b7832015-02-27 19:18:17 +0000100 // CHECK-NEXT: [[FNP:%.*]] = getelementptr inbounds i32 ([[B]]*)*, i32 ([[B]]*)** [[VTBL]], i64 0
David Blaikiea953f282015-02-27 21:19:58 +0000101 // CHECK-NEXT: [[FN:%.*]] = load i32 ([[B]]*)*, i32 ([[B]]*)** [[FNP]]
John McCall769250e2010-09-17 02:31:44 +0000102 // CHECK-NEXT: [[ARG:%.*]] = call i32 [[FN]]([[B]]* [[THIS]])
John McCall00f1a672010-09-17 17:03:41 +0000103 // CHECK-NEXT: call void @_ZN10InitVTable1AC2Ei({{.*}}* {{%.*}}, i32 [[ARG]])
Reid Kleckner8d585132014-12-03 21:00:21 +0000104 // CHECK-NEXT: [[T0:%.*]] = bitcast [[B]]* [[THIS]] to i32 (...)***
Peter Collingbourne25a2b702016-12-13 20:50:44 +0000105 // CHECK-NEXT: store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTVN10InitVTable1BE, i32 0, inrange i32 0, i32 2) to i32 (...)**), i32 (...)*** [[T0]]
John McCall769250e2010-09-17 02:31:44 +0000106 // CHECK-NEXT: ret void
107 B::B() : A(foo()) {}
108
Stephen Lin43622612013-08-15 06:47:53 +0000109 // CHECK-LABEL: define void @_ZN10InitVTable1BC2Ei(%"struct.InitVTable::B"* %this, i32 %x) unnamed_addr
John McCall769250e2010-09-17 02:31:44 +0000110 // CHECK: [[ARG:%.*]] = add nsw i32 {{%.*}}, 5
111 // CHECK-NEXT: call void @_ZN10InitVTable1AC2Ei({{.*}}* {{%.*}}, i32 [[ARG]])
Reid Kleckner8d585132014-12-03 21:00:21 +0000112 // CHECK-NEXT: [[T0:%.*]] = bitcast [[B]]* {{%.*}} to i32 (...)***
Peter Collingbourne25a2b702016-12-13 20:50:44 +0000113 // CHECK-NEXT: store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTVN10InitVTable1BE, i32 0, inrange i32 0, i32 2) to i32 (...)**), i32 (...)*** [[T0]]
John McCall769250e2010-09-17 02:31:44 +0000114 // CHECK-NEXT: ret void
115 B::B(int x) : A(x + 5) {}
116}
117
Douglas Gregorc063ac22011-07-01 21:19:15 +0000118namespace rdar9694300 {
119 struct X {
120 int x;
121 };
122
Stephen Lin43622612013-08-15 06:47:53 +0000123 // CHECK-LABEL: define void @_ZN11rdar96943001fEv
Douglas Gregorc063ac22011-07-01 21:19:15 +0000124 void f() {
125 // CHECK: alloca
126 X x;
127 // CHECK-NEXT: [[I:%.*]] = alloca i32
128 // CHECK-NEXT: store i32 17, i32* [[I]]
129 int i = 17;
130 // CHECK-NEXT: ret void
131 }
132}
133
Richard Smithd86812d2012-07-05 08:39:21 +0000134// Check that we emit a zero initialization step for list-value-initialization
135// which calls a trivial default constructor.
136namespace PR13273 {
137 struct U {
138 int t;
139 U() = default;
140 };
141
142 struct S : U {
143 S() = default;
144 };
145
146 // CHECK: define {{.*}}@_ZN7PR132731fEv(
147 int f() {
148 // CHECK-NOT: }
149 // CHECK: llvm.memset{{.*}}i8 0
150 return (new S{})->t;
151 }
152}
153
Douglas Gregorc14922f2010-03-26 22:43:07 +0000154template<typename T>
155struct X {
156 X(const X &);
157
158 T *start;
159 T *end;
160};
161
162template<typename T> struct X;
163
164// Make sure that the instantiated constructor initializes start and
165// end properly.
Hal Finkela2347ba2014-07-18 15:52:10 +0000166// CHECK-LABEL: define linkonce_odr void @_ZN1XIiEC2ERKS0_(%struct.X* %this, %struct.X* dereferenceable({{[0-9]+}}) %other) unnamed_addr
Douglas Gregorc14922f2010-03-26 22:43:07 +0000167// CHECK: {{store.*null}}
168// CHECK: {{store.*null}}
169// CHECK: ret
170template<typename T>
171X<T>::X(const X &other) : start(0), end(0) { }
172
173X<int> get_X(X<int> x) { return x; }
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000174
175namespace PR10720 {
176 struct X {
177 X(const X&);
178 X(X&&);
179 X& operator=(const X&);
180 X& operator=(X&&);
181 ~X();
182 };
183
184 struct pair2 {
185 X second[4];
186
187 // CHECK-PR10720: define linkonce_odr {{.*}} @_ZN7PR107205pair2aSERKS0_
188 // CHECK-PR10720: load
189 // CHECK-PR10720: icmp ne
190 // CHECK-PR10720-NEXT: br i1
191 // CHECK-PR10720: call {{.*}} @_ZN7PR107201XaSERKS0_
192 // CHECK-PR10720: ret
193 pair2 &operator=(const pair2&) = default;
194
195 // CHECK-PR10720: define linkonce_odr {{.*}} @_ZN7PR107205pair2aSEOS0_
196 // CHECK-PR10720: load
197 // CHECK-PR10720: icmp ne
198 // CHECK-PR10720-NEXT: br i1
199 // CHECK-PR10720: call {{.*}} @_ZN7PR107201XaSEOS0_
200 // CHECK-PR10720: ret
201 pair2 &operator=(pair2&&) = default;
202
Rafael Espindolae5df59f2015-01-22 00:24:57 +0000203 // CHECK-PR10720-LABEL: define linkonce_odr void @_ZN7PR107204pairC2ERKS0_
204 // CHECK-PR10720-NOT: ret
205 // CHECK-PR10720: call void @llvm.memcpy
206 // CHECK-PR10720-NEXT: ret void
207
208 // CHECK-PR10720-LABEL: define linkonce_odr void @_ZN7PR107205pair2C2ERKS0_
209 // CHECK-PR10720-NOT: ret
Rafael Espindolae5df59f2015-01-22 00:24:57 +0000210 // CHECK-PR10720: call void @_ZN7PR107201XC1ERKS0_
Richard Smith30e304e2016-12-14 00:03:17 +0000211 // CHECK-PR10720: icmp eq
212 // CHECK-PR10720-NEXT: br i1
Rafael Espindolae5df59f2015-01-22 00:24:57 +0000213 // CHECK-PR10720: ret void
214
Stephen Lin43622612013-08-15 06:47:53 +0000215 // CHECK-PR10720-LABEL: define linkonce_odr void @_ZN7PR107205pair2C2EOS0_
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000216 // CHECK-PR10720-NOT: ret
217 // CHECK-PR10720: load
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000218 // CHECK-PR10720: call void @_ZN7PR107201XC1EOS0_
Richard Smith30e304e2016-12-14 00:03:17 +0000219 // CHECK-PR10720: icmp eq
220 // CHECK-PR10720-NEXT: br i1
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000221 // CHECK-PR10720: ret void
222 pair2(pair2&&) = default;
223
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000224 pair2(const pair2&) = default;
225 };
226
Richard Smith0d1f3cb2012-02-26 00:31:33 +0000227 struct pair : X { // Make the copy constructor non-trivial, so we actually generate it.
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000228 int second[4];
Douglas Gregor8a556ce2011-09-22 15:15:51 +0000229 pair(const pair&) = default;
230 };
231
232 void foo(const pair &x, const pair2 &x2) {
233 pair y(x);
234 pair2 y2(x2);
235 pair2 y2m(static_cast<pair2&&>(y2));
236
237 y2 = x2;
238 y2m = static_cast<pair2&&>(y2);
239 }
240
241}