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