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