Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / mangle-lambdas.cpp
blob5a7de97c91858f6f5cc6c6d81e6685819f6d6770
1 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.7.0 -emit-llvm -o - %s -w | FileCheck %s
3 void side_effect();
5 // CHECK-LABEL: define linkonce_odr void @_Z11inline_funci
6 inline void inline_func(int n) {
7 // CHECK: call noundef i32 @_ZZ11inline_funciENKUlvE_clEv
8 int i = []{ return side_effect(), 1; }();
10 // CHECK: call noundef i32 @_ZZ11inline_funciENKUlvE0_clEv
11 int j = [=] { return n + i; }();
13 // CHECK: call noundef double @_ZZ11inline_funciENKUlvE1_clEv
14 int k = [=] () -> double { return n + i; }();
16 // CHECK: call noundef i32 @_ZZ11inline_funciENKUliE_clEi
17 int l = [=] (int x) -> int { return x + i; }(n);
19 int inner(int i = []{ return side_effect(), 17; }());
20 // CHECK: call noundef i32 @_ZZ11inline_funciENKUlvE2_clEv
21 // CHECK-NEXT: call noundef i32 @_Z5inneri
22 inner();
24 // CHECK-NEXT: ret void
27 void call_inline_func() {
28 inline_func(17);
31 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZNK10inline_varMUlvE_clEv(
32 // CHECK: @_ZZNK10inline_varMUlvE_clEvE1n
33 inline auto inline_var = [] {
34 static int n = 5;
35 return &n;
38 int *use_inline_var = inline_var();
40 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZNK12var_templateIiEMUlvE_clEv(
41 // CHECK: @_ZZNK12var_templateIiEMUlvE_clEvE1n
42 template<typename T> auto var_template = [] {
43 static int n = 9;
44 return &n;
47 int *use_var_template = var_template<int>();
49 // CHECK-LABEL: define {{.*}} @_Z29use_var_template_substitutionN12var_templateIiEMUlvE_ENS_IfEMUlvE_E
50 void use_var_template_substitution(decltype(var_template<int>), decltype(var_template<float>)) {}
52 struct S {
53 void f(int = []{return side_effect(), 1;}()
54 + []{return side_effect(), 2;}(),
55 int = []{return side_effect(), 3;}());
56 void g(int, int);
59 void S::g(int i = []{return side_effect(), 1;}(),
60 int j = []{return side_effect(), 2; }()) {}
62 // CHECK-LABEL: define{{.*}} void @_Z6test_S1S
63 void test_S(S s) {
64 // CHECK: call noundef i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
65 // CHECK-NEXT: call noundef i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
66 // CHECK-NEXT: add nsw i32
67 // CHECK-NEXT: call noundef i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
68 // CHECK-NEXT: call void @_ZN1S1fEii
69 s.f();
71 // NOTE: These manglings don't actually matter that much, because
72 // the lambdas in the default arguments of g() won't be seen by
73 // multiple translation units. We check them mainly to ensure that they don't
74 // get the special mangling for lambdas in in-class default arguments.
75 // CHECK: call noundef i32 @"_ZNK1S3$_0clEv"
76 // CHECK-NEXT: call noundef i32 @"_ZNK1S3$_1clEv"
77 // CHECK-NEXT: call void @_ZN1S1gEi
78 s.g();
80 // CHECK-NEXT: ret void
83 // Check the linkage of the lambda call operators used in test_S.
84 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
85 // CHECK: ret i32 1
86 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
87 // CHECK: ret i32 2
88 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
89 // CHECK: ret i32 3
90 // CHECK-LABEL: define internal noundef i32 @"_ZNK1S3$_0clEv"
91 // CHECK: ret i32 1
92 // CHECK-LABEL: define internal noundef i32 @"_ZNK1S3$_1clEv"
93 // CHECK: ret i32 2
95 template<typename T>
96 struct ST {
97 void f(T = []{return T() + 1;}()
98 + []{return T() + 2;}(),
99 T = []{return T(3);}());
102 // CHECK-LABEL: define{{.*}} void @_Z7test_ST2STIdE
103 void test_ST(ST<double> st) {
104 // CHECK: call noundef double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
105 // CHECK-NEXT: call noundef double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
106 // CHECK-NEXT: fadd double
107 // CHECK-NEXT: call noundef double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
108 // CHECK-NEXT: call void @_ZN2STIdE1fEdd
109 st.f();
111 // CHECK-NEXT: ret void
114 // Check the linkage of the lambda call operators used in test_ST.
115 // CHECK-LABEL: define linkonce_odr noundef double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
116 // CHECK: ret double 1
117 // CHECK-LABEL: define linkonce_odr noundef double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
118 // CHECK: ret double 2
119 // CHECK-LABEL: define linkonce_odr noundef double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
120 // CHECK: ret double 3
122 template<typename T>
123 struct StaticMembers {
124 static T x;
125 static T y;
126 static T z;
127 static int (*f)();
130 template<typename T> int accept_lambda(T);
132 template<typename T>
133 T StaticMembers<T>::x = []{return side_effect(), 1;}() + []{return side_effect(), 2;}();
135 template<typename T>
136 T StaticMembers<T>::y = []{return side_effect(), 3;}();
138 template<typename T>
139 T StaticMembers<T>::z = accept_lambda([]{return side_effect(), 4;});
141 template<typename T>
142 int (*StaticMembers<T>::f)() = (side_effect(), []{return side_effect(), 5;});
144 // CHECK-LABEL: define internal void @__cxx_global_var_init
145 // CHECK: call noundef i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
146 // CHECK-NEXT: call noundef i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
147 // CHECK-NEXT: add nsw
148 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
149 // CHECK: ret i32 1
150 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
151 // CHECK: ret i32 2
152 template float StaticMembers<float>::x;
154 // CHECK-LABEL: define internal void @__cxx_global_var_init
155 // CHECK: call noundef i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
156 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
157 // CHECK: ret i32 3
158 template float StaticMembers<float>::y;
160 // CHECK-LABEL: define internal void @__cxx_global_var_init
161 // CHECK: call noundef i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_
162 // CHECK: declare noundef i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_()
163 template float StaticMembers<float>::z;
165 // CHECK-LABEL: define internal void @__cxx_global_var_init
166 // CHECK: call {{.*}} @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
167 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
168 template int (*StaticMembers<float>::f)();
170 // CHECK-LABEL: define internal void @__cxx_global_var_init
171 // CHECK: call noundef i32 @"_ZNK13StaticMembersIdE3$_2clEv"
172 // CHECK-LABEL: define internal noundef i32 @"_ZNK13StaticMembersIdE3$_2clEv"
173 // CHECK: ret i32 42
174 template<> double StaticMembers<double>::z = []{return side_effect(), 42; }();
176 template<typename T>
177 void func_template(T = []{ return T(); }());
179 // CHECK-LABEL: define{{.*}} void @_Z17use_func_templatev()
180 void use_func_template() {
181 // CHECK: call noundef i32 @"_ZZ13func_templateIiEvT_ENK3$_0clEv"
182 func_template<int>();
185 namespace std {
186 struct type_info {
187 bool before(const type_info &) const noexcept;
190 namespace PR12123 {
191 struct A { virtual ~A(); } g;
192 struct C { virtual ~C(); } k;
193 struct B {
194 void f(const std::type_info& x = typeid([]()->A& { return g; }()));
195 void h();
196 void j(bool cond = typeid([]() -> A & { return g; }()).before(typeid([]() -> C & { return k; }())));
198 void B::h() { f(); j(); }
201 // CHECK-LABEL: define linkonce_odr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZZN7PR121231B1fERKSt9type_infoEd_NKUlvE_clEv
202 // CHECK-LABEL: define linkonce_odr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZZN7PR121231B1jEbEd_NKUlvE_clEv
203 // CHECK-LABEL: define linkonce_odr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZZN7PR121231B1jEbEd_NKUlvE0_clEv
205 // CHECK-LABEL: define {{.*}} @_Z{{[0-9]*}}testVarargsLambdaNumberingv(
206 inline int testVarargsLambdaNumbering() {
207 // CHECK: testVarargsLambdaNumberingvE{{.*}}UlzE_
208 auto a = [](...) { static int n; return ++n; };
209 // CHECK: testVarargsLambdaNumberingvE{{.*}}UlvE_
210 auto b = []() { static int n; return ++n; };
211 return a() + b();
213 int k = testVarargsLambdaNumbering();
216 template<typename = int>
217 void ft1(int = [](int p = [] { return side_effect(), 42; } ()) {
218 return p;
219 } ());
220 void test_ft1() {
221 // CHECK: call noundef i32 @"_ZZZ3ft1IiEviENK3$_0clEiEd_NKUlvE_clEv"
222 // CHECK: call noundef i32 @"_ZZ3ft1IiEviENK3$_0clEi"
223 ft1();
225 // CHECK-LABEL: define internal noundef i32 @"_ZZ3ft1IiEviENK3$_0clEi"
226 // CHECK-LABEL: define internal noundef i32 @"_ZZZ3ft1IiEviENK3$_0clEiEd_NKUlvE_clEv"
228 struct c1 {
229 template<typename = int>
230 void mft1(int = [](int p = [] { return side_effect(), 42; } ()) {
231 return p;
232 } ());
234 void test_c1_mft1() {
235 // CHECK: call noundef i32 @_ZZZN2c14mft1IiEEviEd_NKUliE_clEiEd_NKUlvE_clEv
236 // CHECK: call noundef i32 @_ZZN2c14mft1IiEEviEd_NKUliE_clEi
237 c1{}.mft1();
239 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN2c14mft1IiEEviEd_NKUliE_clEi
240 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZN2c14mft1IiEEviEd_NKUliE_clEiEd_NKUlvE_clEv
242 template<typename = int>
243 struct ct1 {
244 void mf1(int = [](int p = [] { return side_effect(), 42; } ()) {
245 return p;
246 } ());
247 friend void ff(ct1, int = [](int p = [] { return side_effect(), 0; }()) { return p; }()) {}
249 void test_ct1_mft1() {
250 // CHECK: call noundef i32 @_ZZZN3ct1IiE3mf1EiEd_NKUliE_clEiEd_NKUlvE_clEv
251 // CHECK: call noundef i32 @_ZZN3ct1IiE3mf1EiEd_NKUliE_clEi
252 ct1<>{}.mf1();
253 // CHECK: call noundef i32 @_ZZZ2ff3ct1IiEiEd_NKUliE_clEiEd_NKUlvE_clEv
254 // CHECK: call noundef i32 @_ZZ2ff3ct1IiEiEd_NKUliE_clEi
255 ff(ct1<>{});
257 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN3ct1IiE3mf1EiEd_NKUliE_clEi
258 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZN3ct1IiE3mf1EiEd_NKUliE_clEiEd_NKUlvE_clEv
259 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ2ff3ct1IiEiEd_NKUliE_clEi
260 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZ2ff3ct1IiEiEd_NKUliE_clEiEd_NKUlvE_clEv
262 template<typename = int>
263 void ft2() {
264 [](int p = [] { return side_effect(), 42; } ()) { return p; } ();
266 template void ft2<>();
267 // CHECK: call noundef i32 @_ZZZ3ft2IiEvvENKUliE_clEiEd_NKUlvE_clEv
268 // CHECK: call noundef i32 @_ZZ3ft2IiEvvENKUliE_clEi
269 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ3ft2IiEvvENKUliE_clEi
270 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZ3ft2IiEvvENKUliE_clEiEd_NKUlvE_clEv
272 template<typename>
273 void ft3() {
274 void f(int = []{ return side_effect(), 0; }());
275 f();
277 template void ft3<int>();
278 // CHECK: call noundef i32 @"_ZZ1fiENK3$_0clEv"
279 // CHECK-LABEL: define internal noundef i32 @"_ZZ1fiENK3$_0clEv"
281 template<typename>
282 void ft4() {
283 struct lc {
284 void mf(int = []{ return side_effect(), 0; }()) {}
286 lc().mf();
288 template void ft4<int>();
289 // CHECK: call noundef i32 @_ZZZ3ft4IiEvvEN2lc2mfEiEd_NKUlvE_clEv
290 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZ3ft4IiEvvEN2lc2mfEiEd_NKUlvE_clEv
293 extern int ExternalVariable;
294 struct StaticInlineMember {
295 static constexpr auto x = [] { return ExternalVariable; };
298 // CHECK-LABEL: define void @_Z23test_StaticInlineMemberv
299 // CHECK: call {{.*}} @_ZNK18StaticInlineMember1xMUlvE_clEv
300 void test_StaticInlineMember() {
301 StaticInlineMember::x();
304 // Check linkage of the various lambdas.
305 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ11inline_funciENKUlvE_clEv
306 // CHECK: ret i32 1
307 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ11inline_funciENKUlvE0_clEv
308 // CHECK: ret i32
309 // CHECK-LABEL: define linkonce_odr noundef double @_ZZ11inline_funciENKUlvE1_clEv
310 // CHECK: ret double
311 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ11inline_funciENKUliE_clEi
312 // CHECK: ret i32
313 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZ11inline_funciENKUlvE2_clEv
314 // CHECK: ret i32 17
316 // CHECK-LABEL: define linkonce_odr void @_ZN7MembersC2Ev
317 // CHECK: call noundef i32 @_ZNK7Members1xMUlvE_clEv
318 // CHECK-NEXT: call noundef i32 @_ZNK7Members1xMUlvE0_clE
319 // CHECK-NEXT: add nsw i32
320 // CHECK: call noundef i32 @_ZNK7Members1yMUlvE_clEv
321 // CHECK: ret void
324 // Check the linkage of the lambdas used in test_Members.
325 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK7Members1xMUlvE_clEv
326 // CHECK: ret i32 1
327 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK7Members1xMUlvE0_clEv
328 // CHECK: ret i32 2
329 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZNK7Members1yMUlvE_clEv
330 // CHECK: ret i32 3
332 // CHECK-LABEL: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
335 namespace PR12808 {
336 template <typename> struct B {
337 int a;
338 template <typename L> constexpr B(L&& x) : a(x()) { }
340 template <typename> void b(int) {
341 [&]{ (void)B<int>([&]{ return side_effect(), 1; }); }();
343 void f() {
344 b<int>(1);
346 // CHECK-LABEL: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
347 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
351 struct Members {
352 int x = [] { return side_effect(), 1; }() + [] { return side_effect(), 2; }();
353 int y = [] { return side_effect(), 3; }();
356 void test_Members() {
357 Members members;
360 template<typename P> void f(P) { }
362 struct TestNestedInstantiation {
363 void operator()() const {
364 []() -> void {
365 return f([]{});
366 }();
370 void test_NestedInstantiation() {
371 TestNestedInstantiation()();