xref: /llvm-project/clang/test/CodeGenCXX/meminit-initializers-odr.cpp (revision ca619613801233ef2def8c3cc7d311d5ed0033cb)
1*ca619613SCorentin Jabot // RUN: %clang_cc1 -std=c++20 %s -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s
2*ca619613SCorentin Jabot 
3*ca619613SCorentin Jabot struct ThisShouldBeCalled {
ThisShouldBeCalledThisShouldBeCalled4*ca619613SCorentin Jabot     ThisShouldBeCalled() {}
5*ca619613SCorentin Jabot };
6*ca619613SCorentin Jabot 
7*ca619613SCorentin Jabot template <typename T>
8*ca619613SCorentin Jabot struct ThisShouldBeCalledTPL {
ThisShouldBeCalledTPLThisShouldBeCalledTPL9*ca619613SCorentin Jabot     ThisShouldBeCalledTPL() {}
10*ca619613SCorentin Jabot };
11*ca619613SCorentin Jabot 
f()12*ca619613SCorentin Jabot consteval int f () {
13*ca619613SCorentin Jabot     return 42;
14*ca619613SCorentin Jabot }
15*ca619613SCorentin Jabot 
16*ca619613SCorentin Jabot struct WithConsteval {
WithConstevalWithConsteval17*ca619613SCorentin Jabot     WithConsteval(int x = f()) {}
18*ca619613SCorentin Jabot };
19*ca619613SCorentin Jabot 
20*ca619613SCorentin Jabot template <typename T>
21*ca619613SCorentin Jabot struct WithConstevalTPL {
WithConstevalTPLWithConstevalTPL22*ca619613SCorentin Jabot     WithConstevalTPL(T x = f()) {}
23*ca619613SCorentin Jabot };
24*ca619613SCorentin Jabot 
25*ca619613SCorentin Jabot 
26*ca619613SCorentin Jabot struct Base {
27*ca619613SCorentin Jabot     ThisShouldBeCalled y = {};
28*ca619613SCorentin Jabot };
29*ca619613SCorentin Jabot 
30*ca619613SCorentin Jabot struct S : Base {
31*ca619613SCorentin Jabot     ThisShouldBeCalledTPL<int> A =  {};
32*ca619613SCorentin Jabot     WithConsteval B = {};
33*ca619613SCorentin Jabot     WithConstevalTPL<double> C = {};
34*ca619613SCorentin Jabot };
Do(S=S{})35*ca619613SCorentin Jabot void Do(S = S{}) {}
36*ca619613SCorentin Jabot 
test()37*ca619613SCorentin Jabot void test() {
38*ca619613SCorentin Jabot     Do();
39*ca619613SCorentin Jabot }
40*ca619613SCorentin Jabot 
41*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN18ThisShouldBeCalledC2Ev
42*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN21ThisShouldBeCalledTPLIiEC2Ev
43*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN13WithConstevalC2Ei
44*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN16WithConstevalTPLIdEC2Ed
45*ca619613SCorentin Jabot 
46*ca619613SCorentin Jabot namespace check_arrays {
47*ca619613SCorentin Jabot 
48*ca619613SCorentin Jabot template <typename T>
49*ca619613SCorentin Jabot struct inner {
innercheck_arrays::inner50*ca619613SCorentin Jabot     inner() {}
51*ca619613SCorentin Jabot };
52*ca619613SCorentin Jabot 
53*ca619613SCorentin Jabot struct S {
54*ca619613SCorentin Jabot    inner<int> a {};
55*ca619613SCorentin Jabot };
56*ca619613SCorentin Jabot 
57*ca619613SCorentin Jabot class C {
58*ca619613SCorentin Jabot     S s[1]{};
59*ca619613SCorentin Jabot };
60*ca619613SCorentin Jabot 
f()61*ca619613SCorentin Jabot int f() {
62*ca619613SCorentin Jabot     C c;
63*ca619613SCorentin Jabot     return 0;
64*ca619613SCorentin Jabot }
65*ca619613SCorentin Jabot 
66*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN12check_arrays5innerIiEC2Ev
67*ca619613SCorentin Jabot 
68*ca619613SCorentin Jabot }
69*ca619613SCorentin Jabot 
70*ca619613SCorentin Jabot namespace check_field_inits_in_base_constructors {
71*ca619613SCorentin Jabot 
72*ca619613SCorentin Jabot template <typename>
73*ca619613SCorentin Jabot struct ShouldBeODRUsed {
ShouldBeODRUsedcheck_field_inits_in_base_constructors::ShouldBeODRUsed74*ca619613SCorentin Jabot   ShouldBeODRUsed() {}
75*ca619613SCorentin Jabot };
76*ca619613SCorentin Jabot class k {
77*ca619613SCorentin Jabot // The private here is important,
78*ca619613SCorentin Jabot // otherwise it would be aggregate initialized.
79*ca619613SCorentin Jabot private:
80*ca619613SCorentin Jabot   ShouldBeODRUsed<k> a = {};
81*ca619613SCorentin Jabot };
82*ca619613SCorentin Jabot 
83*ca619613SCorentin Jabot struct b {
84*ca619613SCorentin Jabot   k c{};
85*ca619613SCorentin Jabot };
test()86*ca619613SCorentin Jabot void test() { b d; }
87*ca619613SCorentin Jabot 
88*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN38check_field_inits_in_base_constructors15ShouldBeODRUsedINS_1kEEC2Ev
89*ca619613SCorentin Jabot 
90*ca619613SCorentin Jabot }
91*ca619613SCorentin Jabot 
92*ca619613SCorentin Jabot namespace check_referenced_when_defined_in_default_parameter {
93*ca619613SCorentin Jabot 
94*ca619613SCorentin Jabot template <typename T>
95*ca619613SCorentin Jabot struct Test {
Testcheck_referenced_when_defined_in_default_parameter::Test96*ca619613SCorentin Jabot     Test(auto&&) {}
97*ca619613SCorentin Jabot };
98*ca619613SCorentin Jabot 
99*ca619613SCorentin Jabot struct Options {
__anon8c9660960102check_referenced_when_defined_in_default_parameter::Options100*ca619613SCorentin Jabot     Test<bool(bool x)> identity = [](bool x) -> bool { return x; };
101*ca619613SCorentin Jabot };
102*ca619613SCorentin Jabot 
103*ca619613SCorentin Jabot struct Wrapper {
104*ca619613SCorentin Jabot   Wrapper(const Options& options = Options());
105*ca619613SCorentin Jabot };
106*ca619613SCorentin Jabot 
Func()107*ca619613SCorentin Jabot void Func() { Options options; }
108*ca619613SCorentin Jabot 
109*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN50check_referenced_when_defined_in_default_parameter7OptionsC2Ev
110*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN50check_referenced_when_defined_in_default_parameter4TestIFbbEEC1INS_7Options8identityMUlbE_EEEOT_
111*ca619613SCorentin Jabot // CHECK-LABEL: @_ZN50check_referenced_when_defined_in_default_parameter4TestIFbbEEC2INS_7Options8identityMUlbE_EEEOT_
112*ca619613SCorentin Jabot 
113*ca619613SCorentin Jabot }
114*ca619613SCorentin Jabot 
115*ca619613SCorentin Jabot namespace lambda_body {
116*ca619613SCorentin Jabot template <typename a>
templated_func()117*ca619613SCorentin Jabot int templated_func() {
118*ca619613SCorentin Jabot     return 0;
119*ca619613SCorentin Jabot }
120*ca619613SCorentin Jabot struct test_body {
121*ca619613SCorentin Jabot   int mem = templated_func<int>();
122*ca619613SCorentin Jabot };
123*ca619613SCorentin Jabot struct test_capture {
124*ca619613SCorentin Jabot   int mem = templated_func<double>();
125*ca619613SCorentin Jabot };
126*ca619613SCorentin Jabot 
127*ca619613SCorentin Jabot struct S {
__anon8c9660960202lambda_body::S128*ca619613SCorentin Jabot   int a = [_ = test_capture{}] { (void)test_body{}; return 0;}();
129*ca619613SCorentin Jabot };
130*ca619613SCorentin Jabot 
test()131*ca619613SCorentin Jabot void test() {
132*ca619613SCorentin Jabot     S s;
133*ca619613SCorentin Jabot }
134*ca619613SCorentin Jabot 
135*ca619613SCorentin Jabot // CHECK-LABEL: define{{.*}} @_ZN11lambda_body14templated_funcIdEEiv
136*ca619613SCorentin Jabot // CHECK-LABEL: define{{.*}} @_ZNK11lambda_body1S1aMUlvE_clEv
137*ca619613SCorentin Jabot // CHECK-LABEL: define{{.*}} @_ZN11lambda_body14templated_funcIiEEiv
138*ca619613SCorentin Jabot 
139*ca619613SCorentin Jabot 
140*ca619613SCorentin Jabot }
141