[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / CodeGenCXX / const-init-cxx11.cpp
blobf90281f2d2a799c46d47847ef8fc524a2a7018c5
1 // RUN: %clang_cc1 -w -fmerge-all-constants -triple x86_64-elf-gnu -emit-llvm -o - %s -std=c++11 | FileCheck %s
3 // FIXME: The padding in all these objects should be zero-initialized.
4 namespace StructUnion {
5 struct A {
6 int n;
7 double d;
8 union U {
9 constexpr U(int x) : x(x) {}
10 constexpr U(const char *y) : y(y) {}
11 int x;
12 const char *y;
13 } u;
15 constexpr A(int n, double d, int x) : n(n), d(d), u(x) {}
16 constexpr A(int n, double d, const char *y) : n(n), d(d), u(y) {}
19 // CHECK: @_ZN11StructUnion1aE ={{.*}} constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } }
20 extern constexpr A a(1, 2.0, 3);
22 // CHECK: @_ZN11StructUnion1bE ={{.*}} constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { i8* getelementptr inbounds ([6 x i8], [6 x i8]* @{{.*}}, i32 0, i32 0) } }
23 extern constexpr A b(4, 5, "hello");
25 struct B {
26 int n;
29 // CHECK: @_ZN11StructUnion1cE ={{.*}} global {{.*}} zeroinitializer
30 // CHECK: @_ZN11StructUnion2c2E ={{.*}} global {{.*}} zeroinitializer
31 B c;
32 B c2 = B();
34 // CHECK: @_ZN11StructUnion1dE ={{.*}} global {{.*}} zeroinitializer
35 B d[10];
37 struct C {
38 constexpr C() : c(0) {}
39 int c;
42 // CHECK: @_ZN11StructUnion1eE ={{.*}} global {{.*}} zeroinitializer
43 C e[10];
45 struct D {
46 constexpr D() : d(5) {}
47 int d;
50 // CHECK: @_ZN11StructUnion1fE ={{.*}} global {{.*}} { i32 5 }
51 D f;
53 union E {
54 int a;
55 void *b = &f;
58 // CHECK: @_ZN11StructUnion1gE ={{.*}} global {{.*}} @_ZN11StructUnion1fE
59 E g;
61 // CHECK: @_ZN11StructUnion1hE ={{.*}} global {{.*}} @_ZN11StructUnion1fE
62 E h = E();
65 namespace BaseClass {
66 template<typename T, unsigned> struct X : T {};
67 struct C { char c = 1; };
68 template<unsigned... Ns> struct Cs : X<C,Ns>... {};
69 struct N { int n = 3; };
70 struct D { double d = 4.0; };
72 template<typename ...Ts>
73 struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; };
75 using Test1 = Test<N, C, Cs<1,2>, D, X<C,1>>;
76 // CHECK: @_ZN9BaseClass2t1E ={{.*}} constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8
77 extern constexpr Test1 t1 = Test1();
79 struct DN : D, N {};
80 struct DND : DN, X<D,0> {};
81 struct DNN : DN, X<N,0> {};
82 // CHECK: @_ZN9BaseClass3dndE ={{.*}} constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 }
83 extern constexpr DND dnd = DND();
84 // Note, N subobject is laid out in DN subobject's tail padding.
85 // CHECK: @_ZN9BaseClass3dnnE ={{.*}} constant {{.*}} { double 4.000000e+00, i32 3, i32 3 }
86 extern constexpr DNN dnn = DNN();
88 struct E {};
89 struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};
90 // CHECK: @_ZN9BaseClass2t2E ={{.*}} constant {{.*}} undef
91 extern constexpr Test2 t2 = Test2();
93 struct __attribute((packed)) PackedD { double y = 2; };
94 struct Test3 : C, PackedD { constexpr Test3() {} };
95 // CHECK: @_ZN9BaseClass2t3E ={{.*}} constant <{ i8, double }> <{ i8 1, double 2.000000e+00 }>
96 extern constexpr Test3 t3 = Test3();
99 namespace Array {
100 // CHECK: @_ZN5Array3arrE ={{.*}} constant [2 x i32] [i32 4, i32 0]
101 extern constexpr int arr[2] = { 4 };
103 // CHECK: @_ZN5Array1cE ={{.*}} constant [6 x [4 x i8]] [{{.*}} c"foo\00", [4 x i8] c"a\00\00\00", [4 x i8] c"bar\00", [4 x i8] c"xyz\00", [4 x i8] c"b\00\00\00", [4 x i8] c"123\00"]
104 extern constexpr char c[6][4] = { "foo", "a", { "bar" }, { 'x', 'y', 'z' }, { "b" }, '1', '2', '3' };
106 // CHECK: @_ZN5Array2ucE ={{.*}} constant [4 x i8] c"foo\00"
107 extern constexpr unsigned char uc[] = { "foo" };
109 struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; };
110 // CHECK: @_ZN5Array5ctorsE ={{.*}} constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }]
111 extern const C ctors[3];
112 constexpr C ctors[3];
114 // CHECK: @_ZN5Array1dE ={{.*}} constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] }
115 struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 };
117 struct E {
118 char c[4];
119 char d[4];
120 constexpr E() : c("foo"), d("x") {}
122 // CHECK: @_ZN5Array1eE ={{.*}} constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }
123 extern constexpr E e = E();
125 // PR13290
126 struct F { constexpr F() : n(4) {} int n; };
127 // CHECK: @_ZN5Array2f1E ={{.*}} global {{.*}} zeroinitializer
128 F f1[1][1][0] = { };
129 // CHECK: @_ZN5Array2f2E ={{.*}} global {{.* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4}}
130 F f2[2][2][2] = { };
133 namespace MemberPtr {
134 struct B1 {
135 int a, b;
136 virtual void f();
137 void g();
139 struct B2 {
140 int c, d;
141 virtual void h();
142 void i();
144 struct C : B1 {
145 int e;
146 virtual void j();
147 void k();
149 struct D : C, B2 {
150 int z;
151 virtual void l();
152 void m();
155 // CHECK: @_ZN9MemberPtr2daE ={{.*}} constant i64 8
156 // CHECK: @_ZN9MemberPtr2dbE ={{.*}} constant i64 12
157 // CHECK: @_ZN9MemberPtr2dcE ={{.*}} constant i64 32
158 // CHECK: @_ZN9MemberPtr2ddE ={{.*}} constant i64 36
159 // CHECK: @_ZN9MemberPtr2deE ={{.*}} constant i64 16
160 // CHECK: @_ZN9MemberPtr2dzE ={{.*}} constant i64 40
161 extern constexpr int (D::*da) = &B1::a;
162 extern constexpr int (D::*db) = &C::b;
163 extern constexpr int (D::*dc) = &B2::c;
164 extern constexpr int (D::*dd) = &D::d;
165 extern constexpr int (D::*de) = &C::e;
166 extern constexpr int (D::*dz) = &D::z;
168 // CHECK: @_ZN9MemberPtr2baE ={{.*}} constant i64 8
169 // CHECK: @_ZN9MemberPtr2bbE ={{.*}} constant i64 12
170 // CHECK: @_ZN9MemberPtr2bcE ={{.*}} constant i64 8
171 // CHECK: @_ZN9MemberPtr2bdE ={{.*}} constant i64 12
172 // CHECK: @_ZN9MemberPtr2beE ={{.*}} constant i64 16
173 // CHECK: @_ZN9MemberPtr3b1zE ={{.*}} constant i64 40
174 // CHECK: @_ZN9MemberPtr3b2zE ={{.*}} constant i64 16
175 extern constexpr int (B1::*ba) = (int(B1::*))&B1::a;
176 extern constexpr int (B1::*bb) = (int(B1::*))&C::b;
177 extern constexpr int (B2::*bc) = (int(B2::*))&B2::c;
178 extern constexpr int (B2::*bd) = (int(B2::*))&D::d;
179 extern constexpr int (B1::*be) = (int(B1::*))&C::e;
180 extern constexpr int (B1::*b1z) = (int(B1::*))&D::z;
181 extern constexpr int (B2::*b2z) = (int(B2::*))&D::z;
183 // CHECK: @_ZN9MemberPtr2dfE ={{.*}} constant {{.*}} { i64 1, i64 0 }
184 // CHECK: @_ZN9MemberPtr2dgE ={{.*}} constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
185 // CHECK: @_ZN9MemberPtr2dhE ={{.*}} constant {{.*}} { i64 1, i64 24 }
186 // CHECK: @_ZN9MemberPtr2diE ={{.*}} constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 }
187 // CHECK: @_ZN9MemberPtr2djE ={{.*}} constant {{.*}} { i64 9, i64 0 }
188 // CHECK: @_ZN9MemberPtr2dkE ={{.*}} constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
189 // CHECK: @_ZN9MemberPtr2dlE ={{.*}} constant {{.*}} { i64 17, i64 0 }
190 // CHECK: @_ZN9MemberPtr2dmE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
191 extern constexpr void (D::*df)() = &C::f;
192 extern constexpr void (D::*dg)() = &B1::g;
193 extern constexpr void (D::*dh)() = &B2::h;
194 extern constexpr void (D::*di)() = &D::i;
195 extern constexpr void (D::*dj)() = &C::j;
196 extern constexpr void (D::*dk)() = &C::k;
197 extern constexpr void (D::*dl)() = &D::l;
198 extern constexpr void (D::*dm)() = &D::m;
200 // CHECK: @_ZN9MemberPtr2bfE ={{.*}} constant {{.*}} { i64 1, i64 0 }
201 // CHECK: @_ZN9MemberPtr2bgE ={{.*}} constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
202 // CHECK: @_ZN9MemberPtr2bhE ={{.*}} constant {{.*}} { i64 1, i64 0 }
203 // CHECK: @_ZN9MemberPtr2biE ={{.*}} constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 }
204 // CHECK: @_ZN9MemberPtr2bjE ={{.*}} constant {{.*}} { i64 9, i64 0 }
205 // CHECK: @_ZN9MemberPtr2bkE ={{.*}} constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
206 // CHECK: @_ZN9MemberPtr3b1lE ={{.*}} constant {{.*}} { i64 17, i64 0 }
207 // CHECK: @_ZN9MemberPtr3b1mE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
208 // CHECK: @_ZN9MemberPtr3b2lE ={{.*}} constant {{.*}} { i64 17, i64 -24 }
209 // CHECK: @_ZN9MemberPtr3b2mE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 }
210 extern constexpr void (B1::*bf)() = (void(B1::*)())&C::f;
211 extern constexpr void (B1::*bg)() = (void(B1::*)())&B1::g;
212 extern constexpr void (B2::*bh)() = (void(B2::*)())&B2::h;
213 extern constexpr void (B2::*bi)() = (void(B2::*)())&D::i;
214 extern constexpr void (B1::*bj)() = (void(B1::*)())&C::j;
215 extern constexpr void (B1::*bk)() = (void(B1::*)())&C::k;
216 extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l;
217 extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m;
218 extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l;
219 extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m;
222 namespace LiteralReference {
223 struct Lit {
224 constexpr Lit() : n(5) {}
225 int n;
228 // This creates a non-const temporary and binds a reference to it.
229 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 5 }, align 4
230 // CHECK: @_ZN16LiteralReference3litE ={{.*}} constant {{.*}} @[[TEMP]], align 8
231 const Lit &lit = Lit();
233 // This creates a const temporary as part of the reference initialization.
234 // CHECK: @[[TEMP:.*]] = internal constant {{.*}} { i32 5 }, align 4
235 // CHECK: @_ZN16LiteralReference4lit2E ={{.*}} constant {{.*}} @[[TEMP]], align 8
236 const Lit &lit2 = {};
238 struct A { int &&r1; const int &&r2; };
239 struct B { A &&a1; const A &&a2; };
240 B b = { { 0, 1 }, { 2, 3 } };
241 // CHECK: @[[TEMP0:.*]] = internal global i32 0, align 4
242 // CHECK: @[[TEMP1:.*]] = internal constant i32 1, align 4
243 // CHECK: @[[TEMPA1:.*]] = internal global {{.*}} { i32* @[[TEMP0]], i32* @[[TEMP1]] }, align 8
244 // CHECK: @[[TEMP2:.*]] = internal global i32 2, align 4
245 // CHECK: @[[TEMP3:.*]] = internal constant i32 3, align 4
246 // CHECK: @[[TEMPA2:.*]] = internal constant {{.*}} { i32* @[[TEMP2]], i32* @[[TEMP3]] }, align 8
247 // CHECK: @_ZN16LiteralReference1bE ={{.*}} global {{.*}} { {{.*}}* @[[TEMPA1]], {{.*}}* @[[TEMPA2]] }, align 8
249 struct Subobj {
250 int a, b, c;
252 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 1, i32 2, i32 3 }, align 4
253 // CHECK: @_ZN16LiteralReference2soE ={{.*}} constant {{.*}} (i8* getelementptr {{.*}} @[[TEMP]]{{.*}}, i64 4)
254 constexpr int &&so = Subobj{ 1, 2, 3 }.b;
256 struct Dummy { int padding; };
257 struct Derived : Dummy, Subobj {
258 constexpr Derived() : Dummy{200}, Subobj{4, 5, 6} {}
260 using ConstDerived = const Derived;
261 // CHECK: @[[TEMPCOMMA:.*]] = internal constant {{.*}} { i32 200, i32 4, i32 5, i32 6 }
262 // CHECK: @_ZN16LiteralReference5commaE ={{.*}} constant {{.*}} getelementptr {{.*}} @[[TEMPCOMMA]]{{.*}}, i64 8)
263 constexpr const int &comma = (1, (2, ConstDerived{}).b);
265 // CHECK: @[[TEMPDERIVED:.*]] = internal global {{.*}} { i32 200, i32 4, i32 5, i32 6 }
266 // CHECK: @_ZN16LiteralReference4baseE ={{.*}} constant {{.*}} getelementptr {{.*}} @[[TEMPDERIVED]]{{.*}}, i64 4)
267 constexpr Subobj &&base = Derived{};
269 // CHECK: @_ZN16LiteralReference7derivedE ={{.*}} constant {{.*}} @[[TEMPDERIVED]]
270 constexpr Derived &derived = static_cast<Derived&>(base);
273 namespace NonLiteralConstexpr {
274 constexpr int factorial(int n) {
275 return n ? factorial(n-1) * n : 1;
277 extern void f(int *p);
279 struct NonTrivialDtor {
280 constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}
281 ~NonTrivialDtor() {
282 f(p);
285 int n;
286 int *p;
288 static_assert(!__is_literal(NonTrivialDtor), "");
289 // CHECK: @_ZN19NonLiteralConstexpr3ntdE ={{.*}} global {{.*}} { i32 120, i32* getelementptr
290 NonTrivialDtor ntd;
292 struct VolatileMember {
293 constexpr VolatileMember() : n(5) {}
294 volatile int n;
296 static_assert(!__is_literal(VolatileMember), "");
297 // CHECK: @_ZN19NonLiteralConstexpr2vmE ={{.*}} global {{.*}} { i32 5 }
298 VolatileMember vm;
300 struct Both {
301 constexpr Both() : n(10) {}
302 ~Both();
303 volatile int n;
305 // CHECK: @_ZN19NonLiteralConstexpr1bE ={{.*}} global {{.*}} { i32 10 }
306 Both b;
308 void StaticVars() {
309 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, i32* getelementptr {{.*}}
310 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd =
311 static NonTrivialDtor ntd;
312 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 }
313 // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm =
314 static VolatileMember vm;
315 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 }
316 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b =
317 static Both b;
321 // PR12067
322 namespace VirtualMembers {
323 struct A {
324 constexpr A(double d) : d(d) {}
325 virtual void f();
326 double d;
328 struct B : A {
329 constexpr B() : A(2.0), c{'h', 'e', 'l', 'l', 'o'} {}
330 constexpr B(int n) : A(n), c{'w', 'o', 'r', 'l', 'd'} {}
331 virtual void g();
332 char c[5];
334 struct C {
335 constexpr C() : n(64) {}
336 int n;
338 struct D : C, A, B {
339 constexpr D() : A(1.0), B(), s(5) {}
340 short s;
342 struct E : D, B {
343 constexpr E() : B(3), c{'b','y','e'} {}
344 char c[3];
346 // CHECK: @_ZN14VirtualMembers1eE ={{.*}} global { i8**, double, i32, i8**, double, [5 x i8], i16, i8**, double, [5 x i8], [3 x i8] } { i8** getelementptr inbounds ({ [3 x i8*], [4 x i8*], [4 x i8*] }, { [3 x i8*], [4 x i8*], [4 x i8*] }* @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 0, i32 2), double 1.000000e+00, i32 64, i8** getelementptr inbounds ({ [3 x i8*], [4 x i8*], [4 x i8*] }, { [3 x i8*], [4 x i8*], [4 x i8*] }* @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 1, i32 2), double 2.000000e+00, [5 x i8] c"hello", i16 5, i8** getelementptr inbounds ({ [3 x i8*], [4 x i8*], [4 x i8*] }, { [3 x i8*], [4 x i8*], [4 x i8*] }* @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 2, i32 2), double 3.000000e+00, [5 x i8] c"world", [3 x i8] c"bye" }
347 E e;
349 struct nsMemoryImpl {
350 virtual void f();
352 // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global { i8** } { i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTVN14VirtualMembers12nsMemoryImplE, i32 0, inrange i32 0, i32 2) }
353 __attribute__((used))
354 static nsMemoryImpl sGlobalMemory;
356 template<class T>
357 struct TemplateClass {
358 constexpr TemplateClass() : t{42} {}
359 virtual void templateMethod() {}
361 T t;
363 // CHECK: @_ZN14VirtualMembers1tE ={{.*}} global { i8**, i32 } { i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTVN14VirtualMembers13TemplateClassIiEE, i32 0, inrange i32 0, i32 2), i32 42 }
364 TemplateClass<int> t;
367 namespace PR13273 {
368 struct U {
369 int t;
370 U() = default;
373 struct S : U {
374 S() = default;
377 // CHECK: @_ZN7PR132731sE = {{.*}} zeroinitializer
378 extern const S s {};
381 namespace ArrayTemporary {
382 struct A { const int (&x)[3]; };
383 struct B { const A (&x)[2]; };
384 // CHECK: @[[A1:_ZGRN14ArrayTemporary1bE.*]] = internal constant [3 x i32] [i32 1, i32 2, i32 3]
385 // CHECK: @[[A2:_ZGRN14ArrayTemporary1bE.*]] = internal constant [3 x i32] [i32 4, i32 5, i32 6]
386 // CHECK: @[[ARR:_ZGRN14ArrayTemporary1bE.*]] = internal constant [2 x {{.*}}] [{{.*}} { [3 x i32]* @[[A1]] }, {{.*}} { [3 x i32]* @[[A2]] }]
387 // CHECK: @[[B:_ZGRN14ArrayTemporary1bE.*]] = internal global {{.*}} { [2 x {{.*}}]* @[[ARR]] }
388 // CHECK: @_ZN14ArrayTemporary1bE ={{.*}} constant {{.*}}* @[[B]]
389 B &&b = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
392 namespace UnemittedTemporaryDecl {
393 constexpr int &&ref = 0;
394 extern constexpr int &ref2 = ref;
395 // CHECK: @_ZGRN22UnemittedTemporaryDecl3refE_ = internal global i32 0
397 // FIXME: This declaration should not be emitted -- it isn't odr-used.
398 // CHECK: @_ZN22UnemittedTemporaryDecl3refE
400 // CHECK: @_ZN22UnemittedTemporaryDecl4ref2E ={{.*}} constant i32* @_ZGRN22UnemittedTemporaryDecl3refE_
403 namespace DR2126 {
404 struct A { int &&b; };
405 constexpr const A &a = {42};
406 // CHECK: @_ZGRN6DR21261aE0_ = internal global i32 42
407 // FIXME: This is unused and need not be emitted.
408 // CHECK: @_ZGRN6DR21261aE_ = internal constant {{.*}} { i32* @_ZGRN6DR21261aE0_ }
409 // CHECK: @_ZN6DR21261rE ={{.*}} constant i32* @_ZGRN6DR21261aE0_
410 int &r = a.b;
412 // Dynamically initialized: the temporary object bound to 'b' could be
413 // modified (eg, by placement 'new') before the initializer of 's' runs.
414 constexpr A &&b = {42};
415 // CHECK: @_ZN6DR21261sE ={{.*}} global i32* null
416 int &s = b.b;
419 // CHECK: @_ZZN12LocalVarInit3aggEvE1a = internal constant {{.*}} i32 101
420 // CHECK: @_ZZN12LocalVarInit4ctorEvE1a = internal constant {{.*}} i32 102
421 // CHECK: @__const._ZN12LocalVarInit8mutable_Ev.a = private unnamed_addr constant {{.*}} i32 103
422 // CHECK: @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_ = linkonce_odr constant i32 5, comdat
423 // CHECK: @_ZN33ClassTemplateWithStaticDataMember3useE ={{.*}} constant i32* @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_
424 // CHECK: @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_ = linkonce_odr hidden constant i32 5, comdat
425 // CHECK: @_ZN39ClassTemplateWithHiddenStaticDataMember3useE ={{.*}} constant i32* @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_
426 // CHECK: @_ZGRZN20InlineStaticConstRef3funEvE1i_ = linkonce_odr constant i32 10, comdat
428 // Constant initialization tests go before this point,
429 // dynamic initialization tests go after.
431 // We must emit a constant initializer for NonLiteralConstexpr::ntd, but also
432 // emit an initializer to register its destructor.
433 // CHECK: define {{.*}}cxx_global_var_init{{.*}}
434 // CHECK-NOT: NonLiteralConstexpr
435 // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev {{.*}} @_ZN19NonLiteralConstexpr3ntdE
436 // CHECK-NEXT: ret void
438 // We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.
439 // CHECK-NOT: NonLiteralConstexpr2vm
441 // We must emit a constant initializer for NonLiteralConstexpr::b, but also
442 // emit an initializer to register its destructor.
443 // CHECK: define {{.*}}cxx_global_var_init{{.*}}
444 // CHECK-NOT: NonLiteralConstexpr
445 // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr4BothD1Ev {{.*}} @_ZN19NonLiteralConstexpr1bE
446 // CHECK-NEXT: ret void
448 // CHECK: define {{.*}}NonLiteralConstexpr10StaticVars
449 // CHECK-NOT: }
450 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev
451 // CHECK-NOT: }
452 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev
454 // PR12848: Don't emit dynamic initializers for local constexpr variables.
455 namespace LocalVarInit {
456 constexpr int f(int n) { return n; }
457 struct Agg { int k; };
458 struct Ctor { constexpr Ctor(int n) : k(n) {} int k; };
459 struct Mutable { constexpr Mutable(int n) : k(n) {} mutable int k; };
461 // CHECK: define {{.*}} @_ZN12LocalVarInit6scalarEv
462 // CHECK-NOT: call
463 // CHECK: store i32 100,
464 // CHECK-NOT: call
465 // CHECK: ret i32 100
466 int scalar() { constexpr int a = { f(100) }; return a; }
468 // CHECK: define {{.*}} @_ZN12LocalVarInit3aggEv
469 // CHECK-NOT: call
470 // CHECK: ret i32 101
471 int agg() { constexpr Agg a = { f(101) }; return a.k; }
473 // CHECK: define {{.*}} @_ZN12LocalVarInit4ctorEv
474 // CHECK-NOT: call
475 // CHECK: ret i32 102
476 int ctor() { constexpr Ctor a = { f(102) }; return a.k; }
478 // CHECK: define {{.*}} @_ZN12LocalVarInit8mutable_Ev
479 // CHECK-NOT: call
480 // CHECK: call {{.*}}memcpy{{.*}} @__const._ZN12LocalVarInit8mutable_Ev.a
481 // CHECK-NOT: call
482 // Can't fold return value due to 'mutable'.
483 // CHECK-NOT: ret i32 103
484 // CHECK: }
485 int mutable_() { constexpr Mutable a = { f(103) }; return a.k; }
488 namespace CrossFuncLabelDiff {
489 // Make sure we refuse to constant-fold the variable b.
490 constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); }
491 void test() { static long b = (long)&&lbl - a(false); lbl: return; }
492 // CHECK: sub nsw i64 ptrtoint (i8* blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64),
493 // CHECK: store i64 {{.*}}, i64* @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8
496 // PR12012
497 namespace VirtualBase {
498 struct B {};
499 struct D : virtual B {};
500 D d;
501 // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev
503 template<typename T> struct X : T {
504 constexpr X() : T() {}
506 X<D> x;
507 // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev
510 // PR12145
511 namespace Unreferenced {
512 int n;
513 constexpr int *p = &n;
514 // We must not emit a load of 'p' here, since it's not odr-used.
515 int q = *p;
516 // CHECK-NOT: _ZN12Unreferenced1pE
517 // CHECK: = load i32, i32* @_ZN12Unreferenced1nE
518 // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN12Unreferenced1qE
519 // CHECK-NOT: _ZN12Unreferenced1pE
521 // Technically, we are not required to substitute variables of reference types
522 // initialized by constant expressions, because the special case for odr-use
523 // of variables in [basic.def.odr]p2 only applies to objects. But we do so
524 // anyway.
526 constexpr int &r = n;
527 // CHECK-NOT: _ZN12Unreferenced1rE
528 int s = r;
530 const int t = 1;
531 const int &rt = t;
532 int f(int);
533 int u = f(rt);
534 // CHECK: call noundef i32 @_ZN12Unreferenced1fEi(i32 noundef 1)
537 namespace InitFromConst {
538 template<typename T> void consume(T);
540 const bool b = true;
541 const int n = 5;
542 constexpr double d = 4.3;
544 struct S { int n = 7; S *p = 0; };
545 constexpr S s = S();
546 const S &r = s;
547 constexpr const S *p = &r;
548 constexpr int S::*mp = &S::n;
549 constexpr int a[3] = { 1, 4, 9 };
551 void test() {
552 // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 noundef zeroext true)
553 consume(b);
555 // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 noundef 5)
556 consume(n);
558 // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double noundef 4.300000e+00)
559 consume(d);
561 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)
562 consume<const S&>(s);
564 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)
565 consume<const S&>(r);
567 // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(%"struct.InitFromConst::S"* noundef @_ZN13InitFromConstL1sE)
568 consume(p);
570 // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)
571 consume(mp);
573 // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(i32* noundef getelementptr inbounds ([3 x i32], [3 x i32]* @_ZN13InitFromConstL1aE, i64 0, i64 0))
574 consume(a);
578 namespace Null {
579 decltype(nullptr) null();
580 // CHECK: call {{.*}} @_ZN4Null4nullEv(
581 int *p = null();
582 struct S {};
583 // CHECK: call {{.*}} @_ZN4Null4nullEv(
584 int S::*q = null();
587 namespace InlineStaticConstRef {
588 inline const int &fun() {
589 static const int &i = 10;
590 return i;
591 // CHECK: ret i32* @_ZGRZN20InlineStaticConstRef3funEvE1i_
593 const int &use = fun();
596 namespace ClassTemplateWithStaticDataMember {
597 template <typename T>
598 struct S {
599 static const int &a;
601 template <typename T>
602 const int &S<T>::a = 5;
603 const int &use = S<void>::a;
606 namespace ClassTemplateWithHiddenStaticDataMember {
607 template <typename T>
608 struct S {
609 __attribute__((visibility("hidden"))) static const int &a;
611 template <typename T>
612 const int &S<T>::a = 5;
613 const int &use = S<void>::a;
616 namespace ClassWithStaticConstexprDataMember {
617 struct X {
618 static constexpr const char &p = 'c';
621 // CHECK: @_ZGRN34ClassWithStaticConstexprDataMember1X1pE_
622 const char *f() { return &X::p; }
625 // VirtualMembers::TemplateClass::templateMethod() must be defined in this TU,
626 // not just declared.
627 // CHECK: define linkonce_odr void @_ZN14VirtualMembers13TemplateClassIiE14templateMethodEv(%"struct.VirtualMembers::TemplateClass"* {{[^,]*}} %this)