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