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
inline_func(int n)4 inline 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 
call_inline_func()25 void call_inline_func() {
26   inline_func(17);
27 }
28 
29 // CHECK-LABEL: define linkonce_odr i32* @_ZNK10inline_varMUlvE_clEv(
30 // CHECK: @_ZZNK10inline_varMUlvE_clEvE1n
__anon2bcbc0890602null31 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 i32* @_ZNK12var_templateIiEMUlvE_clEv(
39 // CHECK: @_ZZNK12var_templateIiEMUlvE_clEvE1n
__anon2bcbc0890702null40 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
use_var_template_substitution(decltype(var_template<int>),decltype(var_template<float>))48 void use_var_template_substitution(decltype(var_template<int>), decltype(var_template<float>)) {}
49 
50 struct S {
__anon2bcbc0890802S51   void f(int = []{return 1;}()
__anon2bcbc0890902S52              + []{return 2;}(),
__anon2bcbc0890a02S53          int = []{return 3;}());
54   void g(int, int);
55 };
56 
g(int i=[]{}(),int j=[]{}())57 void S::g(int i = []{return 1;}(),
__anon2bcbc0890c02null58           int j = []{return 2; }()) {}
59 
60 // CHECK-LABEL: define{{.*}} void @_Z6test_S1S
test_S(S s)61 void test_S(S s) {
62   // CHECK: call i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
63   // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
64   // CHECK-NEXT: add nsw i32
65   // CHECK-NEXT: call 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 i32 @"_ZNK1S3$_0clEv"
74   // CHECK-NEXT: call 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 i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
83 // CHECK: ret i32 1
84 // CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
85 // CHECK: ret i32 2
86 // CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
87 // CHECK: ret i32 3
88 // CHECK-LABEL: define internal i32 @"_ZNK1S3$_0clEv"
89 // CHECK: ret i32 1
90 // CHECK-LABEL: define internal i32 @"_ZNK1S3$_1clEv"
91 // CHECK: ret i32 2
92 
93 template<typename T>
94 struct ST {
__anon2bcbc0890d02ST95   void f(T = []{return T() + 1;}()
__anon2bcbc0890e02ST96            + []{return T() + 2;}(),
__anon2bcbc0890f02ST97          T = []{return T(3);}());
98 };
99 
100 // CHECK-LABEL: define{{.*}} void @_Z7test_ST2STIdE
test_ST(ST<double> st)101 void test_ST(ST<double> st) {
102   // CHECK: call double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
103   // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
104   // CHECK-NEXT: fadd double
105   // CHECK-NEXT: call 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 double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
114 // CHECK: ret double 1
115 // CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
116 // CHECK: ret double 2
117 // CHECK-LABEL: define linkonce_odr 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>
__anon2bcbc0891102null131 T StaticMembers<T>::x = []{return 1;}() + []{return 2;}();
132 
133 template<typename T>
__anon2bcbc0891202null134 T StaticMembers<T>::y = []{return 3;}();
135 
136 template<typename T>
__anon2bcbc0891302null137 T StaticMembers<T>::z = accept_lambda([]{return 4;});
138 
139 template<typename T>
__anon2bcbc0891402null140 int (*StaticMembers<T>::f)() = []{return 5;};
141 
142 // CHECK-LABEL: define internal void @__cxx_global_var_init
143 // CHECK: call i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
144 // CHECK-NEXT: call i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
145 // CHECK-NEXT: add nsw
146 // CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
147 // CHECK: ret i32 1
148 // CHECK-LABEL: define linkonce_odr 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 i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
154 // CHECK-LABEL: define linkonce_odr 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 i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_
160 // CHECK: declare 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 i32 ()* @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
166 template int (*StaticMembers<float>::f)();
167 
168 // CHECK-LABEL: define internal void @__cxx_global_var_init
169 // CHECK: call i32 @"_ZNK13StaticMembersIdE3$_2clEv"
170 // CHECK-LABEL: define internal i32 @"_ZNK13StaticMembersIdE3$_2clEv"
171 // CHECK: ret i32 42
__anon2bcbc0891502null172 template<> double StaticMembers<double>::z = []{return 42; }();
173 
174 template<typename T>
__anon2bcbc0891602null175 void func_template(T = []{ return T(); }());
176 
177 // CHECK-LABEL: define{{.*}} void @_Z17use_func_templatev()
use_func_template()178 void use_func_template() {
179   // CHECK: call 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 {
__anon2bcbc0891702PR12123::B192     void f(const std::type_info& x = typeid([]()->A& { return g; }()));
193     void h();
__anon2bcbc0891902PR12123::B194     void j(bool cond = typeid([]() -> A & { return g; }()).before(typeid([]() -> C & { return k; }())));
195   };
h()196   void B::h() { f(); j(); }
197 }
198 
199 // CHECK-LABEL: define linkonce_odr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %"struct.PR12123::A"* @_ZZN7PR121231B1fERKSt9type_infoEd_NKUlvE_clEv
200 // CHECK-LABEL: define linkonce_odr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %"struct.PR12123::A"* @_ZZN7PR121231B1jEbEd_NKUlvE_clEv
201 // CHECK-LABEL: define linkonce_odr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %"struct.PR12123::C"* @_ZZN7PR121231B1jEbEd_NKUlvE0_clEv
202 
203 // CHECK-LABEL: define {{.*}} @_Z{{[0-9]*}}testVarargsLambdaNumberingv(
testVarargsLambdaNumbering()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 // Check linkage of the various lambdas.
214 // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE_clEv
215 // CHECK: ret i32 1
216 // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE0_clEv
217 // CHECK: ret i32
218 // CHECK-LABEL: define linkonce_odr double @_ZZ11inline_funciENKUlvE1_clEv
219 // CHECK: ret double
220 // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUliE_clEi
221 // CHECK: ret i32
222 // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE2_clEv
223 // CHECK: ret i32 17
224 
225 // CHECK-LABEL: define linkonce_odr void @_ZN7MembersC2Ev
226 // CHECK: call i32 @_ZNK7Members1xMUlvE_clEv
227 // CHECK-NEXT: call i32 @_ZNK7Members1xMUlvE0_clE
228 // CHECK-NEXT: add nsw i32
229 // CHECK: call i32 @_ZNK7Members1yMUlvE_clEv
230 // CHECK: ret void
231 
232 
233 // Check the linkage of the lambdas used in test_Members.
234 // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE_clEv
235 // CHECK: ret i32 1
236 // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE0_clEv
237 // CHECK: ret i32 2
238 // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1yMUlvE_clEv
239 // CHECK: ret i32 3
240 
241 // CHECK-LABEL: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
242 
243 
244 namespace PR12808 {
245   template <typename> struct B {
246     int a;
BPR12808::B247     template <typename L> constexpr B(L&& x) : a(x()) { }
248   };
b(int)249   template <typename> void b(int) {
250     [&]{ (void)B<int>([&]{ return 1; }); }();
251   }
f()252   void f() {
253     b<int>(1);
254   }
255   // CHECK-LABEL: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
256   // CHECK-LABEL: define linkonce_odr i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
257 }
258 
259 
260 struct Members {
__anon2bcbc0891f02Members261   int x = [] { return 1; }() + [] { return 2; }();
__anon2bcbc0892002Members262   int y = [] { return 3; }();
263 };
264 
test_Members()265 void test_Members() {
266   Members members;
267 }
268 
f(P)269 template<typename P> void f(P) { }
270 
271 struct TestNestedInstantiation {
operator ()TestNestedInstantiation272    void operator()() const {
273      []() -> void {
274        return f([]{});
275      }();
276    }
277 };
278 
test_NestedInstantiation()279 void test_NestedInstantiation() {
280   TestNestedInstantiation()();
281 }
282