xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/lambda-expressions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fblocks -emit-llvm -o - %s -fexceptions -std=c++11 | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // CHECK-NOT: @unused
__anon1592abbe0102(int i) 4f4a2713aSLionel Sambuc auto unused = [](int i) { return i+1; };
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc // CHECK: @used = internal global
__anon1592abbe0202(int i) 7f4a2713aSLionel Sambuc auto used = [](int i) { return i+1; };
8f4a2713aSLionel Sambuc void *use = &used;
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc // CHECK: @cvar = global
__anon1592abbe0302null11f4a2713aSLionel Sambuc extern "C" auto cvar = []{};
12f4a2713aSLionel Sambuc 
a()13f4a2713aSLionel Sambuc int a() { return []{ return 1; }(); }
14f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1av
15f4a2713aSLionel Sambuc // CHECK: call i32 @"_ZZ1avENK3$_0clEv"
16f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1avENK3$_0clEv"
17f4a2713aSLionel Sambuc // CHECK: ret i32 1
18f4a2713aSLionel Sambuc 
b(int x)19f4a2713aSLionel Sambuc int b(int x) { return [x]{return x;}(); }
20f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1bi
21f4a2713aSLionel Sambuc // CHECK: store i32
22f4a2713aSLionel Sambuc // CHECK: load i32*
23f4a2713aSLionel Sambuc // CHECK: store i32
24f4a2713aSLionel Sambuc // CHECK: call i32 @"_ZZ1biENK3$_1clEv"
25f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1biENK3$_1clEv"
26f4a2713aSLionel Sambuc // CHECK: load i32*
27f4a2713aSLionel Sambuc // CHECK: ret i32
28f4a2713aSLionel Sambuc 
__anon1592abbe0602null29f4a2713aSLionel Sambuc int c(int x) { return [&x]{return x;}(); }
30f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1ci
31f4a2713aSLionel Sambuc // CHECK: store i32
32f4a2713aSLionel Sambuc // CHECK: store i32*
33f4a2713aSLionel Sambuc // CHECK: call i32 @"_ZZ1ciENK3$_2clEv"
34f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1ciENK3$_2clEv"
35f4a2713aSLionel Sambuc // CHECK: load i32**
36f4a2713aSLionel Sambuc // CHECK: load i32*
37f4a2713aSLionel Sambuc // CHECK: ret i32
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc struct D { D(); D(const D&); int x; };
__anon1592abbe0702null40f4a2713aSLionel Sambuc int d(int x) { D y[10]; [x,y] { return y[x].x; }(); }
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1di
43f4a2713aSLionel Sambuc // CHECK: call void @_ZN1DC1Ev
44f4a2713aSLionel Sambuc // CHECK: icmp ult i64 %{{.*}}, 10
45f4a2713aSLionel Sambuc // CHECK: call void @_ZN1DC1ERKS_
46f4a2713aSLionel Sambuc // CHECK: call i32 @"_ZZ1diENK3$_3clEv"
47f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1diENK3$_3clEv"
48f4a2713aSLionel Sambuc // CHECK: load i32*
49f4a2713aSLionel Sambuc // CHECK: load i32*
50f4a2713aSLionel Sambuc // CHECK: ret i32
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc struct E { E(); E(const E&); ~E(); int x; };
__anon1592abbe0802()53f4a2713aSLionel Sambuc int e(E a, E b, bool cond) { [a,b,cond](){ return (cond ? a : b).x; }(); }
54f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1e1ES_b
55f4a2713aSLionel Sambuc // CHECK: call void @_ZN1EC1ERKS_
56f4a2713aSLionel Sambuc // CHECK: invoke void @_ZN1EC1ERKS_
57f4a2713aSLionel Sambuc // CHECK: invoke i32 @"_ZZ1e1ES_bENK3$_4clEv"
58f4a2713aSLionel Sambuc // CHECK: call void @"_ZZ1e1ES_bEN3$_4D1Ev"
59f4a2713aSLionel Sambuc // CHECK: call void @"_ZZ1e1ES_bEN3$_4D1Ev"
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1e1ES_bENK3$_4clEv"
62f4a2713aSLionel Sambuc // CHECK: trunc i8
63f4a2713aSLionel Sambuc // CHECK: load i32*
64f4a2713aSLionel Sambuc // CHECK: ret i32
65f4a2713aSLionel Sambuc 
f()66f4a2713aSLionel Sambuc void f() {
67f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_Z1fv()
68f4a2713aSLionel Sambuc   // CHECK: @"_ZZ1fvENK3$_5cvPFiiiEEv"
69f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 (i32, i32)*
70f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
71f4a2713aSLionel Sambuc   int (*fp)(int, int) = [](int x, int y){ return x + y; };
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc static int k;
g()75f4a2713aSLionel Sambuc int g() {
76f4a2713aSLionel Sambuc   int &r = k;
77f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal i32 @"_ZZ1gvENK3$_6clEv"(
78f4a2713aSLionel Sambuc   // CHECK-NOT: }
79f4a2713aSLionel Sambuc   // CHECK: load i32* @_ZL1k,
80f4a2713aSLionel Sambuc   return [] { return r; } ();
81f4a2713aSLionel Sambuc };
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc // PR14773
84*0a6a1f1dSLionel Sambuc // CHECK: [[ARRVAL:%[0-9a-zA-Z]*]] = load i32* getelementptr inbounds ([0 x i32]* @_ZZ14staticarrayrefvE5array, i32 0, i64 0), align 4
85f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 [[ARRVAL]]
staticarrayref()86f4a2713aSLionel Sambuc void staticarrayref(){
87f4a2713aSLionel Sambuc   static int array[] = {};
88f4a2713aSLionel Sambuc   (void)[](){
89f4a2713aSLionel Sambuc     int (&xxx)[0] = array;
90f4a2713aSLionel Sambuc     int y = xxx[0];
91f4a2713aSLionel Sambuc   }();
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @"_ZZ11PR22071_funvENK3$_8clEv"
95*0a6a1f1dSLionel Sambuc // CHECK: ret i32* @PR22071_var
96*0a6a1f1dSLionel Sambuc int PR22071_var;
PR22071_fun()97*0a6a1f1dSLionel Sambuc int *PR22071_fun() {
98*0a6a1f1dSLionel Sambuc   constexpr int &y = PR22071_var;
99*0a6a1f1dSLionel Sambuc   return [&] { return &y; }();
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc // CHECK: define internal void @"_ZZ1hvEN3$_98__invokeEv"(%struct.A* noalias sret %agg.result) {{.*}} {
103f4a2713aSLionel Sambuc // CHECK-NOT: =
104*0a6a1f1dSLionel Sambuc // CHECK: call void @"_ZZ1hvENK3$_9clEv"(%struct.A* sret %agg.result,
105f4a2713aSLionel Sambuc // CHECK-NEXT: ret void
106f4a2713aSLionel Sambuc struct A { ~A(); };
h()107f4a2713aSLionel Sambuc void h() {
108f4a2713aSLionel Sambuc   A (*h)() = [] { return A(); };
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZ1fvEN3$_58__invokeEii"
112f4a2713aSLionel Sambuc // CHECK: store i32
113f4a2713aSLionel Sambuc // CHECK-NEXT: store i32
114f4a2713aSLionel Sambuc // CHECK-NEXT: load i32*
115f4a2713aSLionel Sambuc // CHECK-NEXT: load i32*
116f4a2713aSLionel Sambuc // CHECK-NEXT: call i32 @"_ZZ1fvENK3$_5clEii"
117f4a2713aSLionel Sambuc // CHECK-NEXT: ret i32
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @"_ZZ1e1ES_bEN3$_4D2Ev"
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc // <rdar://problem/12778708>
122f4a2713aSLionel Sambuc struct XXX {};
nestedCapture()123f4a2713aSLionel Sambuc void nestedCapture () {
124f4a2713aSLionel Sambuc   XXX localKey;
125f4a2713aSLionel Sambuc   ^() {
126f4a2713aSLionel Sambuc     [&]() {
127f4a2713aSLionel Sambuc       ^{ XXX k = localKey; };
128f4a2713aSLionel Sambuc     };
129f4a2713aSLionel Sambuc   };
130f4a2713aSLionel Sambuc }
131