xref: /llvm-project/clang/test/CodeGenCXX/mangle-lambdas.cpp (revision e9c9db34a9b04706937e9dd764d1d97ca84337b6)
1 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.7.0 -emit-llvm -o - %s -w | FileCheck %s
2 
3 void side_effect();
4 
5 // CHECK-LABEL: define linkonce_odr void @_Z11inline_funci
inline_func(int n)6 inline void inline_func(int n) {
7   // CHECK: call noundef i32 @_ZZ11inline_funciENKUlvE_clEv
8   int i = []{ return side_effect(), 1; }();
9 
10   // CHECK: call noundef i32 @_ZZ11inline_funciENKUlvE0_clEv
11   int j = [=] { return n + i; }();
12 
13   // CHECK: call noundef double @_ZZ11inline_funciENKUlvE1_clEv
14   int k = [=] () -> double { return n + i; }();
15 
16   // CHECK: call noundef i32 @_ZZ11inline_funciENKUliE_clEi
17   int l = [=] (int x) -> int { return x + i; }(n);
18 
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();
23 
24   // CHECK-NEXT: ret void
25 }
26 
call_inline_func()27 void call_inline_func() {
28   inline_func(17);
29 }
30 
31 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZNK10inline_varMUlvE_clEv(
32 // CHECK: @_ZZNK10inline_varMUlvE_clEvE1n
__anonad7161a20602null33 inline auto inline_var = [] {
34   static int n = 5;
35   return &n;
36 };
37 
38 int *use_inline_var = inline_var();
39 
40 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZNK12var_templateIiEMUlvE_clEv(
41 // CHECK: @_ZZNK12var_templateIiEMUlvE_clEvE1n
__anonad7161a20702null42 template<typename T> auto var_template = [] {
43   static int n = 9;
44   return &n;
45 };
46 
47 int *use_var_template = var_template<int>();
48 
49 // CHECK-LABEL: define {{.*}} @_Z29use_var_template_substitutionN12var_templateIiEMUlvE_ENS_IfEMUlvE_E
use_var_template_substitution(decltype(var_template<int>) ,decltype(var_template<float>) )50 void use_var_template_substitution(decltype(var_template<int>), decltype(var_template<float>)) {}
51 
52 struct S {
__anonad7161a20802S53   void f(int = []{return side_effect(), 1;}()
__anonad7161a20902S54              + []{return side_effect(), 2;}(),
__anonad7161a20a02S55          int = []{return side_effect(), 3;}());
56   void g(int, int);
57 };
58 
g(int i=[]{}(),int j=[]{}())59 void S::g(int i = []{return side_effect(), 1;}(),
__anonad7161a20c02null60           int j = []{return side_effect(), 2; }()) {}
61 
62 // CHECK-LABEL: define{{.*}} void @_Z6test_S1S
test_S(S s)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();
70 
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();
79 
80   // CHECK-NEXT: ret void
81 }
82 
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
94 
95 template<typename T>
96 struct ST {
__anonad7161a20d02ST97   void f(T = []{return T() + 1;}()
__anonad7161a20e02ST98            + []{return T() + 2;}(),
__anonad7161a20f02ST99          T = []{return T(3);}());
100 };
101 
102 // CHECK-LABEL: define{{.*}} void @_Z7test_ST2STIdE
test_ST(ST<double> st)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();
110 
111   // CHECK-NEXT: ret void
112 }
113 
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
121 
122 template<typename T>
123 struct StaticMembers {
124   static T x;
125   static T y;
126   static T z;
127   static int (*f)();
128 };
129 
130 template<typename T> int accept_lambda(T);
131 
132 template<typename T>
__anonad7161a21002null133 T StaticMembers<T>::x = []{return side_effect(), 1;}() + []{return side_effect(), 2;}();
134 
135 template<typename T>
__anonad7161a21202null136 T StaticMembers<T>::y = []{return side_effect(), 3;}();
137 
138 template<typename T>
__anonad7161a21302null139 T StaticMembers<T>::z = accept_lambda([]{return side_effect(), 4;});
140 
141 template<typename T>
__anonad7161a21402null142 int (*StaticMembers<T>::f)() = (side_effect(), []{return side_effect(), 5;});
143 
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;
153 
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;
159 
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;
164 
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)();
169 
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
__anonad7161a21502null174 template<> double StaticMembers<double>::z = []{return side_effect(), 42; }();
175 
176 template<typename T>
__anonad7161a21602null177 void func_template(T = []{ return T(); }());
178 
179 // CHECK-LABEL: define{{.*}} void @_Z17use_func_templatev()
use_func_template()180 void use_func_template() {
181   // CHECK: call noundef i32 @"_ZZ13func_templateIiEvT_ENK3$_0clEv"
182   func_template<int>();
183 }
184 
185 namespace std {
186   struct type_info {
187     bool before(const type_info &) const noexcept;
188   };
189 }
190 namespace PR12123 {
191   struct A { virtual ~A(); } g;
192   struct C { virtual ~C(); } k;
193   struct B {
__anonad7161a21702PR12123::B194     void f(const std::type_info& x = typeid([]()->A& { return g; }()));
195     void h();
__anonad7161a21802PR12123::B196     void j(bool cond = typeid([]() -> A & { return g; }()).before(typeid([]() -> C & { return k; }())));
197   };
h()198   void B::h() { f(); j(); }
199 }
200 
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
204 
205 // CHECK-LABEL: define {{.*}} @_Z{{[0-9]*}}testVarargsLambdaNumberingv(
testVarargsLambdaNumbering()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();
212 }
213 int k = testVarargsLambdaNumbering();
214 
215 
216 template<typename = int>
__anonad7161a21c02null217 void ft1(int = [](int p = [] { return side_effect(), 42; } ()) {
218                  return p;
219                } ());
test_ft1()220 void test_ft1() {
221   // CHECK: call noundef i32 @"_ZZZ3ft1IiEviENK3$_0clEiEd_NKUlvE_clEv"
222   // CHECK: call noundef i32 @"_ZZ3ft1IiEviENK3$_0clEi"
223   ft1();
224 }
225 // CHECK-LABEL: define internal noundef i32 @"_ZZ3ft1IiEviENK3$_0clEi"
226 // CHECK-LABEL: define internal noundef i32 @"_ZZZ3ft1IiEviENK3$_0clEiEd_NKUlvE_clEv"
227 
228 struct c1 {
229   template<typename = int>
__anonad7161a21f02c1230   void mft1(int = [](int p = [] { return side_effect(), 42; } ()) {
231                     return p;
232                   } ());
233 };
test_c1_mft1()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();
238 }
239 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN2c14mft1IiEEviEd_NKUliE_clEi
240 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZN2c14mft1IiEEviEd_NKUliE_clEiEd_NKUlvE_clEv
241 
242 template<typename = int>
243 struct ct1 {
__anonad7161a22002ct1244   void mf1(int = [](int p = [] { return side_effect(), 42; } ()) {
245                    return p;
246                  } ());
ffct1247   friend void ff(ct1, int = [](int p = [] { return side_effect(), 0; }()) { return p; }()) {}
248 };
test_ct1_mft1()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<>{});
256 }
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
261 
262 template<typename = int>
ft2()263 void ft2() {
264   [](int p = [] { return side_effect(), 42; } ()) { return p; } ();
265 }
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
271 
272 template<typename>
ft3()273 void ft3() {
274   void f(int = []{ return side_effect(), 0; }());
275   f();
276 }
277 template void ft3<int>();
278 // CHECK: call noundef i32 @"_ZZ1fiENK3$_0clEv"
279 // CHECK-LABEL: define internal noundef i32 @"_ZZ1fiENK3$_0clEv"
280 
281 template<typename>
ft4()282 void ft4() {
283   struct lc {
284     void mf(int = []{ return side_effect(), 0; }()) {}
285   };
286   lc().mf();
287 }
288 template void ft4<int>();
289 // CHECK: call noundef i32 @_ZZZ3ft4IiEvvEN2lc2mfEiEd_NKUlvE_clEv
290 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZ3ft4IiEvvEN2lc2mfEiEd_NKUlvE_clEv
291 
292 
293 extern int ExternalVariable;
294 struct StaticInlineMember {
__anonad7161a22802StaticInlineMember295   static constexpr auto x = [] { return ExternalVariable; };
296 };
297 
298 // CHECK-LABEL: define void @_Z23test_StaticInlineMemberv
299 // CHECK: call {{.*}} @_ZNK18StaticInlineMember1xMUlvE_clEv
test_StaticInlineMember()300 void test_StaticInlineMember() {
301   StaticInlineMember::x();
302 }
303 
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
315 
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
322 
323 
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
331 
332 // CHECK-LABEL: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
333 
334 
335 namespace PR12808 {
336   template <typename> struct B {
337     int a;
BPR12808::B338     template <typename L> constexpr B(L&& x) : a(x()) { }
339   };
b(int)340   template <typename> void b(int) {
341     [&]{ (void)B<int>([&]{ return side_effect(), 1; }); }();
342   }
f()343   void f() {
344     b<int>(1);
345   }
346   // CHECK-LABEL: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
347   // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
348 }
349 
350 
351 struct Members {
__anonad7161a22b02Members352   int x = [] { return side_effect(), 1; }() + [] { return side_effect(), 2; }();
__anonad7161a22d02Members353   int y = [] { return side_effect(), 3; }();
354 };
355 
test_Members()356 void test_Members() {
357   Members members;
358 }
359 
f(P)360 template<typename P> void f(P) { }
361 
362 struct TestNestedInstantiation {
operator ()TestNestedInstantiation363    void operator()() const {
364      []() -> void {
365        return f([]{});
366      }();
367    }
368 };
369 
test_NestedInstantiation()370 void test_NestedInstantiation() {
371   TestNestedInstantiation()();
372 }
373