[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / CodeGenCXX / value-init.cpp
blob1430076557e4f5a13072bed9c69f4d0122cd2e1d
1 // RUN: %clang_cc1 -std=c++98 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX98
2 // RUN: %clang_cc1 -std=c++17 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX17
4 struct A {
5 virtual ~A();
6 };
8 struct B : A { };
10 struct C {
11 int i;
12 B b;
15 // CHECK: _Z15test_value_initv
16 void test_value_init() {
17 // This value initialization requires zero initialization of the 'B'
18 // subobject followed by a call to its constructor.
19 // PR5800
21 // CHECK: store i32 17
22 // CHECK: call void @llvm.memset.p0.i64
23 // CHECK: call void @_ZN1BC1Ev
24 C c = { 17 } ;
25 // CHECK: call void @_ZN1CD1Ev
28 enum enum_type { negative_number = -1, magic_number = 42 };
30 class enum_holder
32 enum_type m_enum;
34 public:
35 enum_holder() : m_enum(magic_number) { }
38 struct enum_holder_and_int
40 enum_holder e;
41 int i;
44 // CHECK: _Z24test_enum_holder_and_intv()
45 void test_enum_holder_and_int() {
46 // CHECK: alloca
47 // CHECK-NEXT: call void @llvm.memset
48 // CHECK-NEXT: call void @_ZN19enum_holder_and_intC1Ev
49 enum_holder_and_int();
50 // CHECK-NEXT: ret void
53 // PR7834: don't crash.
54 namespace test1 {
55 struct A {
56 int A::*f;
57 A();
58 A(const A&);
59 A &operator=(const A &);
62 struct B {
63 A base;
66 void foo() {
67 B();
71 namespace ptrmem {
72 struct S {
73 int mem1;
74 int S::*mem2;
77 // CHECK-LABEL: define{{.*}} i32 @_ZN6ptrmem4testEPNS_1SE
78 int test(S *s) {
79 // CHECK: call void @llvm.memcpy.p0.p0.i64
80 // CHECK: getelementptr
81 // CHECK: ret
82 return s->*S().mem2;
86 namespace PR9801 {
88 struct Test {
89 Test() : i(10) {}
90 Test(int i) : i(i) {}
91 int i;
92 private:
93 int j;
96 struct Test2 {
97 Test t;
100 struct Test3 : public Test { };
102 // CHECK-LABEL: define{{.*}} void @_ZN6PR98011fEv
103 void f() {
104 // CHECK-NOT: call void @llvm.memset.p0.i64
105 // CHECK: call void @_ZN6PR98014TestC1Ei
106 // CHECK-NOT: call void @llvm.memset.p0.i64
107 // CHECK: call void @_ZN6PR98014TestC1Ev
108 Test partial[3] = { 1 };
110 // CHECK-NOT: call void @llvm.memset.p0.i64
111 // CHECK: call void @_ZN6PR98014TestC1Ev
112 // CHECK-NOT: call void @_ZN6PR98014TestC1Ev
113 Test empty[3] = {};
115 // CHECK: call void @llvm.memset.p0.i64
116 // CHECK-NOT: call void @llvm.memset.p0.i64
117 // CHECK-CXX98: call void @_ZN6PR98015Test2C1Ev
118 // CHECK-CXX17: call void @_ZN6PR98014TestC1Ev
119 // CHECK-NOT: call void @_ZN6PR98015Test2C1Ev
120 Test2 empty2[3] = {};
122 // CHECK: call void @llvm.memset.p0.i64
123 // CHECK-NOT: call void @llvm.memset.p0.i64
124 // CHECK-CXX98: call void @_ZN6PR98015Test3C1Ev
125 // CHECK-CXX17: call void @_ZN6PR98014TestC2Ev
126 // CHECK-NOT: call void @llvm.memset.p0.i64
127 // CHECK-NOT: call void @_ZN6PR98015Test3C1Ev
128 Test3 empty3[3] = {};
133 namespace zeroinit {
134 struct S { int i; };
136 // CHECK-LABEL: define{{.*}} i32 @_ZN8zeroinit4testEv()
137 int test() {
138 // CHECK: call void @llvm.memset.p0.i64
139 // CHECK: ret i32 0
140 return S().i;
143 struct X0 {
144 X0() { }
145 int x;
148 struct X1 : X0 {
149 int x1;
150 void f();
153 // CHECK-LABEL: define{{.*}} void @_ZN8zeroinit9testX0_X1Ev
154 void testX0_X1() {
155 // CHECK: call void @llvm.memset.p0.i64
156 // CHECK-NEXT: call void @_ZN8zeroinit2X1C1Ev
157 // CHECK-NEXT: call void @_ZN8zeroinit2X11fEv
158 X1().f();
161 template<typename>
162 struct X2 : X0 {
163 int x2;
164 void f();
167 template<typename>
168 struct X3 : X2<int> {
169 X3() : X2<int>() { }
170 int i;
174 // CHECK-LABEL: define{{.*}} void @_ZN8zeroinit9testX0_X3Ev
175 void testX0_X3() {
176 // CHECK-NOT: call void @llvm.memset
177 // CHECK: call void @_ZN8zeroinit2X3IiEC1Ev
178 // CHECK: call void @_ZN8zeroinit2X2IiE1fEv
179 // CHECK-NEXT: ret void
180 X3<int>().f();
183 // More checks at EOF
186 namespace PR8726 {
187 class C;
188 struct S {
189 const C &c1;
190 int i;
191 const C &c2;
193 void f(const C& c) {
194 S s = {c, 42, c};
199 // rdar://problem/9355931
200 namespace test6 {
201 struct A { A(); A(int); };
203 void test() {
204 A arr[10][20] = { 5 };
206 // CHECK-LABEL: define{{.*}} void @_ZN5test64testEv()
207 // CHECK: [[ARR:%.*]] = alloca [10 x [20 x [[A:%.*]]]],
209 // CHECK-NEXT: [[INNER:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]], ptr [[ARR]], i64 0, i64 0
210 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [20 x [[A]]], ptr [[INNER]], i64 0, i64 0
211 // CHECK-NEXT: call void @_ZN5test61AC1Ei(ptr {{[^,]*}} [[T0]], i32 noundef 5)
212 // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [[A]], ptr [[T0]], i64 1
213 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[T0]], i64 20
214 // CHECK-NEXT: br label
215 // CHECK: [[CUR:%.*]] = phi ptr [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
216 // CHECK-NEXT: call void @_ZN5test61AC1Ev(ptr {{[^,]*}} [[CUR]])
217 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
218 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[NEXT]], [[END]]
219 // CHECK-NEXT: br i1
221 // CHECK: [[BEGIN:%.*]] = getelementptr inbounds [20 x [[A]]], ptr [[INNER]], i64 1
222 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [20 x [[A]]], ptr [[INNER]], i64 10
223 // CHECK-NEXT: br label
224 // CHECK: [[CUR:%.*]] = phi ptr [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
226 // Inner loop.
227 // CHECK-NEXT: [[IBEGIN:%.*]] = getelementptr inbounds [20 x [[A]]], ptr [[CUR]], i{{32|64}} 0, i{{32|64}} 0
228 // CHECK-NEXT: [[IEND:%.*]] = getelementptr inbounds [[A]], ptr [[IBEGIN]], i64 20
229 // CHECK-NEXT: br label
230 // CHECK: [[ICUR:%.*]] = phi ptr [ [[IBEGIN]], {{%.*}} ], [ [[INEXT:%.*]], {{%.*}} ]
231 // CHECK-NEXT: call void @_ZN5test61AC1Ev(ptr {{[^,]*}} [[ICUR]])
232 // CHECK-NEXT: [[INEXT:%.*]] = getelementptr inbounds [[A]], ptr [[ICUR]], i64 1
233 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[INEXT]], [[IEND]]
234 // CHECK-NEXT: br i1 [[T0]],
236 // CHECK: [[NEXT]] = getelementptr inbounds [20 x [[A]]], ptr [[CUR]], i64 1
237 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[NEXT]], [[END]]
238 // CHECK-NEXT: br i1 [[T0]]
239 // CHECK: ret void
242 namespace PR11124 {
243 // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
244 struct A { int a; A(); A(int); };
245 struct B : virtual A { int b; };
246 struct C : B { C(); };
247 C::C() : A(3), B() {}
248 // CHECK-LABEL: define{{.*}} void @_ZN7PR111241CC1Ev
249 // CHECK: call void @llvm.memset.p0.i64(ptr align 8 {{.*}}, i8 0, i64 12, i1 false)
250 // CHECK-NEXT: call void @_ZN7PR111241BC2Ev
251 // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
253 struct B2 : virtual A { int B::*b; };
254 struct C2 : B2 { C2(); };
255 C2::C2() : A(3), B2() {}
256 // CHECK-LABEL: define{{.*}} void @_ZN7PR111242C2C1Ev
257 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %{{.*}}, ptr align 8 {{.*}}, i64 16, i1 false)
258 // CHECK-NEXT: call void @_ZN7PR111242B2C2Ev
261 // Ensure we produce an i1 here, and don't assert.
262 // CHECK-LABEL: define{{.*}} void @_Z9r170806_bv(
263 // CHECK: call void @_Z9r170806_ab(i1 noundef zeroext false)
264 void r170806_a(bool b = bool());
265 void r170806_b() { r170806_a(); }
267 namespace PR20256 {
268 struct data { int i; };
270 template<typename T = int>
271 data g() {
272 data d; // not value-init
273 return d;
275 template data g();
276 // CHECK-LABEL: define {{.*}} @_ZN7PR202561gIiEENS_4dataEv(
277 // CHECK-NOT: store
278 // CHECK-NOT: memset
279 // CHECK: }
281 template<typename ...T>
282 data h(T ...t) {
283 data d(t...); // value-init
284 return d;
286 template data h();
287 // CHECK-LABEL: define {{.*}} @_ZN7PR202561hIJEEENS_4dataEDpT_(
288 // CHECK: call void @llvm.memset
289 // CHECK: }
292 template<typename T = int>
293 data j() {
294 data d = {}; // value-init
295 return d;
297 template data j();
298 // CHECK-LABEL: define {{.*}} @_ZN7PR202561jIiEENS_4dataEv(
299 // CHECK: call void @llvm.memset
300 // CHECK: }
302 data f() {
303 data d; // not value-init
304 return d;
306 // CHECK-LABEL: define {{.*}} @_ZN7PR202561fEv(
307 // CHECK-NOT: store
308 // CHECK-NOT: memset
309 // CHECK: }
311 data i() {
312 data d = {}; // value-init
313 return d;
315 // CHECK-LABEL: define {{.*}} @_ZN7PR202561iEv(
316 // CHECK: call void @llvm.memset
317 // CHECK: }
320 // CHECK-LABEL: define {{.*}}@_Z20explicitly_defaultedv
321 int explicitly_defaulted() {
322 struct A { A() = default; int n; };
323 // CHECK: call void @llvm.memset
324 A a = A();
325 return a.n;
326 } // CHECK-LABEL: }
328 // CHECK-LABEL: define linkonce_odr void @_ZN8zeroinit2X3IiEC2Ev(ptr {{[^,]*}} %this) unnamed_addr
329 // CHECK: call void @llvm.memset.p0.i64
330 // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
331 // CHECK-NEXT: ret void
333 #if __cplusplus >= 201103L
334 namespace transparent_init_list {
335 struct optional_assign_base {};
336 struct optional_data_dtor_base { char dummy_[24]; };
337 struct optional : optional_data_dtor_base, optional_assign_base {};
338 optional f(optional a) { return {optional(a)}; }
340 #endif