xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/value-init.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct A {
4f4a2713aSLionel Sambuc   virtual ~A();
5f4a2713aSLionel Sambuc };
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc struct B : A { };
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc struct C {
10f4a2713aSLionel Sambuc   int i;
11f4a2713aSLionel Sambuc   B b;
12f4a2713aSLionel Sambuc };
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc // CHECK: _Z15test_value_initv
test_value_init()15f4a2713aSLionel Sambuc void test_value_init() {
16f4a2713aSLionel Sambuc   // This value initialization requires zero initialization of the 'B'
17f4a2713aSLionel Sambuc   // subobject followed by a call to its constructor.
18f4a2713aSLionel Sambuc   // PR5800
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc   // CHECK: store i32 17
21f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset.p0i8.i64
22f4a2713aSLionel Sambuc   // CHECK: call void @_ZN1BC1Ev
23f4a2713aSLionel Sambuc   C c = { 17 } ;
24f4a2713aSLionel Sambuc   // CHECK: call void @_ZN1CD1Ev
25f4a2713aSLionel Sambuc }
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc enum enum_type { negative_number = -1, magic_number = 42 };
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc class enum_holder
30f4a2713aSLionel Sambuc {
31f4a2713aSLionel Sambuc   enum_type m_enum;
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc public:
enum_holder()34f4a2713aSLionel Sambuc   enum_holder() : m_enum(magic_number) { }
35f4a2713aSLionel Sambuc };
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc struct enum_holder_and_int
38f4a2713aSLionel Sambuc {
39f4a2713aSLionel Sambuc   enum_holder e;
40f4a2713aSLionel Sambuc   int i;
41f4a2713aSLionel Sambuc };
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc // CHECK: _Z24test_enum_holder_and_intv()
test_enum_holder_and_int()44f4a2713aSLionel Sambuc void test_enum_holder_and_int() {
45f4a2713aSLionel Sambuc   // CHECK: alloca
46f4a2713aSLionel Sambuc   // CHECK-NEXT: bitcast
47f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @llvm.memset
48f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN19enum_holder_and_intC1Ev
49f4a2713aSLionel Sambuc   enum_holder_and_int();
50f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc // PR7834: don't crash.
54f4a2713aSLionel Sambuc namespace test1 {
55f4a2713aSLionel Sambuc   struct A {
56f4a2713aSLionel Sambuc     int A::*f;
57f4a2713aSLionel Sambuc     A();
58f4a2713aSLionel Sambuc     A(const A&);
59f4a2713aSLionel Sambuc     A &operator=(const A &);
60f4a2713aSLionel Sambuc   };
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   struct B {
63f4a2713aSLionel Sambuc     A base;
64f4a2713aSLionel Sambuc   };
65f4a2713aSLionel Sambuc 
foo()66f4a2713aSLionel Sambuc   void foo() {
67f4a2713aSLionel Sambuc     B();
68f4a2713aSLionel Sambuc   }
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc namespace ptrmem {
72f4a2713aSLionel Sambuc   struct S {
73f4a2713aSLionel Sambuc     int mem1;
74f4a2713aSLionel Sambuc     int S::*mem2;
75f4a2713aSLionel Sambuc   };
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc   // CHECK-LABEL: define i32 @_ZN6ptrmem4testEPNS_1SE
test(S * s)78f4a2713aSLionel Sambuc   int test(S *s) {
79f4a2713aSLionel Sambuc     // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
80f4a2713aSLionel Sambuc     // CHECK: getelementptr
81f4a2713aSLionel Sambuc     // CHECK: ret
82f4a2713aSLionel Sambuc     return s->*S().mem2;
83f4a2713aSLionel Sambuc   }
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc namespace PR9801 {
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc struct Test {
TestPR9801::Test89f4a2713aSLionel Sambuc   Test() : i(10) {}
TestPR9801::Test90f4a2713aSLionel Sambuc   Test(int i) : i(i) {}
91f4a2713aSLionel Sambuc   int i;
92f4a2713aSLionel Sambuc private:
93f4a2713aSLionel Sambuc   int j;
94f4a2713aSLionel Sambuc };
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc struct Test2 {
97f4a2713aSLionel Sambuc   Test t;
98f4a2713aSLionel Sambuc };
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc struct Test3 : public Test { };
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_ZN6PR98011fEv
f()103f4a2713aSLionel Sambuc void f() {
104f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
105f4a2713aSLionel Sambuc   // CHECK: call void @_ZN6PR98014TestC1Ei
106f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
107f4a2713aSLionel Sambuc   // CHECK: call void @_ZN6PR98014TestC1Ev
108f4a2713aSLionel Sambuc   Test partial[3] = { 1 };
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
111f4a2713aSLionel Sambuc   // CHECK: call void @_ZN6PR98014TestC1Ev
112f4a2713aSLionel Sambuc   // CHECK-NOT: call void @_ZN6PR98014TestC1Ev
113f4a2713aSLionel Sambuc   Test empty[3] = {};
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset.p0i8.i64
116f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
117f4a2713aSLionel Sambuc   // CHECK: call void @_ZN6PR98015Test2C1Ev
118f4a2713aSLionel Sambuc   // CHECK-NOT: call void @_ZN6PR98015Test2C1Ev
119f4a2713aSLionel Sambuc   Test2 empty2[3] = {};
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset.p0i8.i64
122f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
123f4a2713aSLionel Sambuc   // CHECK: call void @_ZN6PR98015Test3C1Ev
124f4a2713aSLionel Sambuc   // CHECK-NOT: call void @llvm.memset.p0i8.i64
125f4a2713aSLionel Sambuc   // CHECK-NOT: call void @_ZN6PR98015Test3C1Ev
126f4a2713aSLionel Sambuc   Test3 empty3[3] = {};
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc namespace zeroinit {
132f4a2713aSLionel Sambuc   struct S { int i; };
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   // CHECK-LABEL: define i32 @_ZN8zeroinit4testEv()
test()135f4a2713aSLionel Sambuc   int test() {
136f4a2713aSLionel Sambuc     // CHECK: call void @llvm.memset.p0i8.i64
137f4a2713aSLionel Sambuc     // CHECK: ret i32 0
138f4a2713aSLionel Sambuc     return S().i;
139f4a2713aSLionel Sambuc   }
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc   struct X0 {
X0zeroinit::X0142f4a2713aSLionel Sambuc     X0() { }
143f4a2713aSLionel Sambuc     int x;
144f4a2713aSLionel Sambuc   };
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc   struct X1 : X0 {
147f4a2713aSLionel Sambuc     int x1;
148f4a2713aSLionel Sambuc     void f();
149f4a2713aSLionel Sambuc   };
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X1Ev
testX0_X1()152f4a2713aSLionel Sambuc   void testX0_X1() {
153f4a2713aSLionel Sambuc     // CHECK: call void @llvm.memset.p0i8.i64
154f4a2713aSLionel Sambuc     // CHECK-NEXT: call void @_ZN8zeroinit2X1C1Ev
155f4a2713aSLionel Sambuc     // CHECK-NEXT: call void @_ZN8zeroinit2X11fEv
156f4a2713aSLionel Sambuc     X1().f();
157f4a2713aSLionel Sambuc   }
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   template<typename>
160f4a2713aSLionel Sambuc   struct X2 : X0 {
161f4a2713aSLionel Sambuc     int x2;
162f4a2713aSLionel Sambuc     void f();
163f4a2713aSLionel Sambuc   };
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   template<typename>
166f4a2713aSLionel Sambuc   struct X3 : X2<int> {
X3zeroinit::X3167f4a2713aSLionel Sambuc     X3() : X2<int>() { }
168f4a2713aSLionel Sambuc     int i;
169f4a2713aSLionel Sambuc   };
170f4a2713aSLionel Sambuc 
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X3Ev
testX0_X3()173f4a2713aSLionel Sambuc   void testX0_X3() {
174f4a2713aSLionel Sambuc     // CHECK-NOT: call void @llvm.memset
175f4a2713aSLionel Sambuc     // CHECK: call void @_ZN8zeroinit2X3IiEC1Ev
176f4a2713aSLionel Sambuc     // CHECK: call void @_ZN8zeroinit2X2IiE1fEv
177f4a2713aSLionel Sambuc     // CHECK-NEXT: ret void
178f4a2713aSLionel Sambuc     X3<int>().f();
179f4a2713aSLionel Sambuc   }
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc   // More checks at EOF
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc namespace PR8726 {
185f4a2713aSLionel Sambuc class C;
186f4a2713aSLionel Sambuc struct S {
187f4a2713aSLionel Sambuc   const C &c1;
188f4a2713aSLionel Sambuc   int i;
189f4a2713aSLionel Sambuc   const C &c2;
190f4a2713aSLionel Sambuc };
f(const C & c)191f4a2713aSLionel Sambuc void f(const C& c) {
192f4a2713aSLionel Sambuc   S s = {c, 42, c};
193f4a2713aSLionel Sambuc }
194f4a2713aSLionel Sambuc 
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc // rdar://problem/9355931
198f4a2713aSLionel Sambuc namespace test6 {
199f4a2713aSLionel Sambuc   struct A { A(); A(int); };
200f4a2713aSLionel Sambuc 
test()201f4a2713aSLionel Sambuc   void test() {
202f4a2713aSLionel Sambuc     A arr[10][20] = { 5 };
203f4a2713aSLionel Sambuc   };
204f4a2713aSLionel Sambuc   // CHECK-LABEL:    define void @_ZN5test64testEv()
205f4a2713aSLionel Sambuc   // CHECK:      [[ARR:%.*]] = alloca [10 x [20 x [[A:%.*]]]],
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc   // CHECK-NEXT: [[INNER:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]]* [[ARR]], i64 0, i64 0
208f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 0, i64 0
209f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN5test61AC1Ei([[A]]* [[T0]], i32 5)
210f4a2713aSLionel Sambuc   // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [[A]]* [[T0]], i64 1
211f4a2713aSLionel Sambuc   // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[T0]], i64 20
212f4a2713aSLionel Sambuc   // CHECK-NEXT: br label
213f4a2713aSLionel Sambuc   // CHECK:      [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
214f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[CUR]])
215f4a2713aSLionel Sambuc   // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
216f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
217f4a2713aSLionel Sambuc   // CHECK-NEXT: br i1
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc   // CHECK:      [[BEGIN:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 1
220f4a2713aSLionel Sambuc   // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 10
221f4a2713aSLionel Sambuc   // CHECK-NEXT: br label
222f4a2713aSLionel Sambuc   // CHECK:      [[CUR:%.*]] = phi [20 x [[A]]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc   // Inner loop.
225f4a2713aSLionel Sambuc   // CHECK-NEXT: [[IBEGIN:%.*]] = getelementptr inbounds [20 x [[A]]]* [[CUR]], i32 0, i32 0
226f4a2713aSLionel Sambuc   // CHECK-NEXT: [[IEND:%.*]] = getelementptr inbounds [[A]]* [[IBEGIN]], i64 20
227f4a2713aSLionel Sambuc   // CHECK-NEXT: br label
228f4a2713aSLionel Sambuc   // CHECK:      [[ICUR:%.*]] = phi [[A]]* [ [[IBEGIN]], {{%.*}} ], [ [[INEXT:%.*]], {{%.*}} ]
229f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[ICUR]])
230f4a2713aSLionel Sambuc   // CHECK-NEXT: [[INEXT:%.*]] = getelementptr inbounds [[A]]* [[ICUR]], i64 1
231f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[INEXT]], [[IEND]]
232f4a2713aSLionel Sambuc   // CHECK-NEXT: br i1 [[T0]],
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc   // CHECK:      [[NEXT]] = getelementptr inbounds [20 x [[A]]]* [[CUR]], i64 1
235f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp eq [20 x [[A]]]* [[NEXT]], [[END]]
236f4a2713aSLionel Sambuc   // CHECK-NEXT: br i1 [[T0]]
237f4a2713aSLionel Sambuc   // CHECK:      ret void
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc namespace PR11124 {
241f4a2713aSLionel Sambuc   // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
242f4a2713aSLionel Sambuc   struct A { int a; A(); A(int); };
243f4a2713aSLionel Sambuc   struct B : virtual A { int b; };
244f4a2713aSLionel Sambuc   struct C : B { C(); };
C()245f4a2713aSLionel Sambuc   C::C() : A(3), B() {}
246f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN7PR111241CC1Ev
247f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset.p0i8.i64(i8* {{.*}}, i8 0, i64 12, i32 8, i1 false)
248f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN7PR111241BC2Ev
249f4a2713aSLionel Sambuc   // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   struct B2 : virtual A { int B::*b; };
252f4a2713aSLionel Sambuc   struct C2 : B2 { C2(); };
C2()253f4a2713aSLionel Sambuc   C2::C2() : A(3), B2() {}
254f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN7PR111242C2C1Ev
255f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %{{.*}}, i8* {{.*}}, i64 16, i32 8, i1 false)
256f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN7PR111242B2C2Ev
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc // Ensure we produce an i1 here, and don't assert.
260f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z9r170806_bv(
261f4a2713aSLionel Sambuc // CHECK: call void @_Z9r170806_ab(i1 zeroext false)
262f4a2713aSLionel Sambuc void r170806_a(bool b = bool());
r170806_b()263f4a2713aSLionel Sambuc void r170806_b() { r170806_a(); }
264f4a2713aSLionel Sambuc 
265*0a6a1f1dSLionel Sambuc namespace PR20256 {
266*0a6a1f1dSLionel Sambuc   struct data { int i; };
267*0a6a1f1dSLionel Sambuc 
268*0a6a1f1dSLionel Sambuc   template<typename T = int>
g()269*0a6a1f1dSLionel Sambuc   data g() {
270*0a6a1f1dSLionel Sambuc     data d; // not value-init
271*0a6a1f1dSLionel Sambuc     return d;
272*0a6a1f1dSLionel Sambuc   }
273*0a6a1f1dSLionel Sambuc   template data g();
274*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define {{.*}} @_ZN7PR202561gIiEENS_4dataEv(
275*0a6a1f1dSLionel Sambuc   // CHECK-NOT: store
276*0a6a1f1dSLionel Sambuc   // CHECK-NOT: memset
277*0a6a1f1dSLionel Sambuc   // CHECK: }
278*0a6a1f1dSLionel Sambuc 
279*0a6a1f1dSLionel Sambuc   template<typename ...T>
h(T...t)280*0a6a1f1dSLionel Sambuc   data h(T ...t) {
281*0a6a1f1dSLionel Sambuc     data d(t...); // value-init
282*0a6a1f1dSLionel Sambuc     return d;
283*0a6a1f1dSLionel Sambuc   }
284*0a6a1f1dSLionel Sambuc   template data h();
285*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define {{.*}} @_ZN7PR202561hIJEEENS_4dataEDpT_(
286*0a6a1f1dSLionel Sambuc   // CHECK: call void @llvm.memset
287*0a6a1f1dSLionel Sambuc   // CHECK: }
288*0a6a1f1dSLionel Sambuc 
289*0a6a1f1dSLionel Sambuc 
290*0a6a1f1dSLionel Sambuc   template<typename T = int>
j()291*0a6a1f1dSLionel Sambuc   data j() {
292*0a6a1f1dSLionel Sambuc     data d = {}; // value-init
293*0a6a1f1dSLionel Sambuc     return d;
294*0a6a1f1dSLionel Sambuc   }
295*0a6a1f1dSLionel Sambuc   template data j();
296*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define {{.*}} @_ZN7PR202561jIiEENS_4dataEv(
297*0a6a1f1dSLionel Sambuc   // CHECK: call void @llvm.memset
298*0a6a1f1dSLionel Sambuc   // CHECK: }
299*0a6a1f1dSLionel Sambuc 
f()300*0a6a1f1dSLionel Sambuc   data f() {
301*0a6a1f1dSLionel Sambuc     data d; // not value-init
302*0a6a1f1dSLionel Sambuc     return d;
303*0a6a1f1dSLionel Sambuc   }
304*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define {{.*}} @_ZN7PR202561fEv(
305*0a6a1f1dSLionel Sambuc   // CHECK-NOT: store
306*0a6a1f1dSLionel Sambuc   // CHECK-NOT: memset
307*0a6a1f1dSLionel Sambuc   // CHECK: }
308*0a6a1f1dSLionel Sambuc 
i()309*0a6a1f1dSLionel Sambuc   data i() {
310*0a6a1f1dSLionel Sambuc     data d = {}; // value-init
311*0a6a1f1dSLionel Sambuc     return d;
312*0a6a1f1dSLionel Sambuc   }
313*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define {{.*}} @_ZN7PR202561iEv(
314*0a6a1f1dSLionel Sambuc   // CHECK: call void @llvm.memset
315*0a6a1f1dSLionel Sambuc   // CHECK: }
316*0a6a1f1dSLionel Sambuc }
317*0a6a1f1dSLionel Sambuc 
318f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr void @_ZN8zeroinit2X3IiEC2Ev(%"struct.zeroinit::X3"* %this) unnamed_addr
319f4a2713aSLionel Sambuc // CHECK: call void @llvm.memset.p0i8.i64
320f4a2713aSLionel Sambuc // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
321f4a2713aSLionel Sambuc // CHECK-NEXT: ret void
322