Clang Project

clang_source_code/test/CodeGenCXX/mangle-lambdas.cpp
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
4inline void inline_func(int n) {
5  // CHECK: call i32 @_ZZ11inline_funciENKUlvE_clEv
6  int i = []{ return 1; }();
7
8  // CHECK: call i32 @_ZZ11inline_funciENKUlvE0_clEv
9  int j = [=] { return n + i; }();
10
11  // CHECK: call double @_ZZ11inline_funciENKUlvE1_clEv
12  int k = [=] () -> double { return n + i; }();
13
14  // CHECK: call 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 i32 @_ZZ11inline_funciENKUlvE2_clEv
19  // CHECK-NEXT: call i32 @_Z5inneri
20  inner();
21
22  // CHECK-NEXT: ret void
23}
24
25void call_inline_func() {
26  inline_func(17);
27}
28
29// CHECK-LABEL: define linkonce_odr i32* @_ZNK10inline_varMUlvE_clEv(
30// CHECK: @_ZZNK10inline_varMUlvE_clEvE1n
31inline auto inline_var = [] {
32  static int n = 5;
33  return &n;
34};
35
36int *use_inline_var = inline_var();
37
38// CHECK-LABEL: define linkonce_odr i32* @_ZNK12var_templateIiEMUlvE_clEv(
39// CHECK: @_ZZNK12var_templateIiEMUlvE_clEvE1n
40template<typename T> auto var_template = [] {
41  static int n = 9;
42  return &n;
43};
44
45int *use_var_template = var_template<int>();
46
47struct S {
48  void f(int = []{return 1;}()
49             + []{return 2;}(),
50         int = []{return 3;}());
51  void g(int, int);
52};
53
54void S::g(int i = []{return 1;}(),
55          int j = []{return 2; }()) {}
56
57// CHECK-LABEL: define void @_Z6test_S1S
58void test_S(S s) {
59  // CHECK: call i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
60  // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
61  // CHECK-NEXT: add nsw i32
62  // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
63  // CHECK-NEXT: call void @_ZN1S1fEii
64  s.f();
65
66  // NOTE: These manglings don't actually matter that much, because
67  // the lambdas in the default arguments of g() won't be seen by
68  // multiple translation units. We check them mainly to ensure that they don't 
69  // get the special mangling for lambdas in in-class default arguments.
70  // CHECK: call i32 @"_ZNK1S3$_0clEv"
71  // CHECK-NEXT: call i32 @"_ZNK1S3$_1clEv"
72  // CHECK-NEXT: call void @_ZN1S1gEi
73  s.g();
74
75  // CHECK-NEXT: ret void
76}
77
78// Check the linkage of the lambda call operators used in test_S.
79// CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
80// CHECK: ret i32 1
81// CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
82// CHECK: ret i32 2
83// CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
84// CHECK: ret i32 3
85// CHECK-LABEL: define internal i32 @"_ZNK1S3$_0clEv"
86// CHECK: ret i32 1
87// CHECK-LABEL: define internal i32 @"_ZNK1S3$_1clEv"
88// CHECK: ret i32 2
89
90template<typename T>
91struct ST {
92  void f(T = []{return T() + 1;}()
93           + []{return T() + 2;}(),
94         T = []{return T(3);}());
95};
96
97// CHECK-LABEL: define void @_Z7test_ST2STIdE
98void test_ST(ST<double> st) {
99  // CHECK: call double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
100  // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
101  // CHECK-NEXT: fadd double
102  // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
103  // CHECK-NEXT: call void @_ZN2STIdE1fEdd
104  st.f();
105
106  // CHECK-NEXT: ret void
107}
108
109// Check the linkage of the lambda call operators used in test_ST.
110// CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
111// CHECK: ret double 1
112// CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
113// CHECK: ret double 2
114// CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
115// CHECK: ret double 3
116
117template<typename T> 
118struct StaticMembers {
119  static T x;
120  static T y;
121  static T z;
122  static int (*f)();
123};
124
125template<typename T> int accept_lambda(T);
126
127template<typename T>
128T StaticMembers<T>::x = []{return 1;}() + []{return 2;}();
129
130template<typename T>
131T StaticMembers<T>::y = []{return 3;}();
132
133template<typename T>
134T StaticMembers<T>::z = accept_lambda([]{return 4;});
135
136template<typename T>
137int (*StaticMembers<T>::f)() = []{return 5;};
138
139// CHECK-LABEL: define internal void @__cxx_global_var_init
140// CHECK: call i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
141// CHECK-NEXT: call i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
142// CHECK-NEXT: add nsw
143// CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
144// CHECK: ret i32 1
145// CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
146// CHECK: ret i32 2
147template float StaticMembers<float>::x;
148
149// CHECK-LABEL: define internal void @__cxx_global_var_init
150// CHECK: call i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
151// CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
152// CHECK: ret i32 3
153template float StaticMembers<float>::y;
154
155// CHECK-LABEL: define internal void @__cxx_global_var_init
156// CHECK: call i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_
157// CHECK: declare i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_()
158template float StaticMembers<float>::z;
159
160// CHECK-LABEL: define internal void @__cxx_global_var_init
161// CHECK: call {{.*}} @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
162// CHECK-LABEL: define linkonce_odr i32 ()* @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
163template int (*StaticMembers<float>::f)();
164
165// CHECK-LABEL: define internal void @__cxx_global_var_init
166// CHECK: call i32 @"_ZNK13StaticMembersIdE3$_2clEv"
167// CHECK-LABEL: define internal i32 @"_ZNK13StaticMembersIdE3$_2clEv"
168// CHECK: ret i32 42
169template<> double StaticMembers<double>::z = []{return 42; }();
170
171template<typename T>
172void func_template(T = []{ return T(); }());
173
174// CHECK-LABEL: define void @_Z17use_func_templatev()
175void use_func_template() {
176  // CHECK: call i32 @"_ZZ13func_templateIiEvT_ENK3$_3clEv"
177  func_template<int>();
178}
179
180namespace std {
181  struct type_info;
182}
183namespace PR12123 {
184  struct A { virtual ~A(); } g;
185  struct B {
186    void f(const std::type_info& x = typeid([]()->A& { return g; }()));
187    void h();
188  };
189  void B::h() { f(); }
190}
191
192// CHECK-LABEL: define linkonce_odr dereferenceable({{[0-9]+}}) %"struct.PR12123::A"* @_ZZN7PR121231B1fERKSt9type_infoEd_NKUlvE_clEv
193
194// CHECK-LABEL: define {{.*}} @_Z{{[0-9]*}}testVarargsLambdaNumberingv(
195inline int testVarargsLambdaNumbering() {
196  // CHECK: testVarargsLambdaNumberingvE{{.*}}UlzE_
197  auto a = [](...) { static int n; return ++n; };
198  // CHECK: testVarargsLambdaNumberingvE{{.*}}UlvE_
199  auto b = []() { static int n; return ++n; };
200  return a() + b();
201}
202int k = testVarargsLambdaNumbering();
203
204// Check linkage of the various lambdas.
205// CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE_clEv
206// CHECK: ret i32 1
207// CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE0_clEv
208// CHECK: ret i32
209// CHECK-LABEL: define linkonce_odr double @_ZZ11inline_funciENKUlvE1_clEv
210// CHECK: ret double
211// CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUliE_clEi
212// CHECK: ret i32
213// CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE2_clEv
214// CHECK: ret i32 17
215
216// CHECK-LABEL: define linkonce_odr void @_ZN7MembersC2Ev
217// CHECK: call i32 @_ZNK7Members1xMUlvE_clEv
218// CHECK-NEXT: call i32 @_ZNK7Members1xMUlvE0_clE
219// CHECK-NEXT: add nsw i32
220// CHECK: call i32 @_ZNK7Members1yMUlvE_clEv
221// CHECK: ret void
222
223
224// Check the linkage of the lambdas used in test_Members.
225// CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE_clEv
226// CHECK: ret i32 1
227// CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE0_clEv
228// CHECK: ret i32 2
229// CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1yMUlvE_clEv
230// CHECK: ret i32 3
231
232// CHECK-LABEL: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
233
234
235namespace PR12808 {
236  template <typename> struct B {
237    int a;
238    template <typename L> constexpr B(L&& x) : a(x()) { }
239  };
240  template <typename> void b(int) {
241    [&]{ (void)B<int>([&]{ return 1; }); }();
242  }
243  void f() {
244    b<int>(1);
245  }
246  // CHECK-LABEL: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
247  // CHECK-LABEL: define linkonce_odr i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
248}
249
250
251struct Members {
252  int x = [] { return 1; }() + [] { return 2; }();
253  int y = [] { return 3; }();
254};
255
256void test_Members() {
257  Members members;
258}
259
260template<typename P> void f(P) { }
261
262struct TestNestedInstantiation {
263   void operator()() const {
264     []() -> void {
265       return f([]{});
266     }();
267   }
268};
269
270void test_NestedInstantiation() {
271  TestNestedInstantiation()();
272}
273