1 // RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
4 // CHECK-LABEL: define void @_ZN5test04testEi(
5 // CHECK: define internal void @___ZN5test04testEi_block_invoke{{.*}}(
6 // CHECK: define internal void @___ZN5test04testEi_block_invoke_2{{.*}}(
15 // Capturing const objects doesn't require a local block.
16 // CHECK-LABEL: define void @_ZN5test15test1Ev()
17 // CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out
19 const int NumHorsemen = 4;
20 out = ^{ (void) NumHorsemen; };
23 // That applies to structs too...
24 // CHECK-LABEL: define void @_ZN5test15test2Ev()
25 // CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out
26 struct loc { double x, y; };
28 const loc target = { 5, 6 };
29 out = ^{ (void) target; };
32 // ...unless they have mutable fields...
33 // CHECK-LABEL: define void @_ZN5test15test3Ev()
34 // CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
35 // CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
36 // CHECK: store void ()* [[T0]], void ()** @out
37 struct mut { mutable int x; };
39 const mut obj = { 5 };
40 out = ^{ (void) obj; };
43 // ...or non-trivial destructors...
44 // CHECK-LABEL: define void @_ZN5test15test4Ev()
45 // CHECK: [[OBJ:%.*]] = alloca
46 // CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
47 // CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
48 // CHECK: store void ()* [[T0]], void ()** @out
49 struct scope { int x; ~scope(); };
51 const scope obj = { 5 };
52 out = ^{ (void) obj; };
55 // ...or non-trivial copy constructors, but it's not clear how to do
56 // that and still have a constant initializer in '03.
72 // CHECK-LABEL: define void @_ZN5test24testEv()
78 // CHECK-LABEL: define internal void @__Block_byref_object_copy
79 // CHECK: call void @_ZN5test21AC1ERKS0_(
81 // CHECK-LABEL: define internal void @__Block_byref_object_dispose
82 // CHECK: call void @_ZN5test21AD1Ev(
84 // CHECK-LABEL: define internal void @__Block_byref_object_copy
85 // CHECK: call void @_ZN5test21BC1ERKS0_(
87 // CHECK-LABEL: define internal void @__Block_byref_object_dispose
88 // CHECK: call void @_ZN5test21BD1Ev(
91 // rdar://problem/9334739
92 // Make sure we mark destructors for parameters captured in blocks.
103 extern void consume(void(^)());
104 consume(^{ (void) b; });
108 // rdar://problem/9971485
118 extern void consume(void(^)());
119 consume(^{ return foo(A()); });
121 // CHECK-LABEL: define void @_ZN5test44testEv()
122 // CHECK-LABEL: define internal void @___ZN5test44testEv_block_invoke
123 // CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align 1
124 // CHECK-NEXT: store i8* [[BLOCKDESC:%.*]], i8** {{.*}}, align 8
125 // CHECK-NEXT: load i8*
126 // CHECK-NEXT: bitcast i8* [[BLOCKDESC]] to <{ i8*, i32, i32, i8*, %struct.__block_descriptor* }>*
127 // CHECK: call void @_ZN5test41AC1Ev([[A]]* [[TMP]])
128 // CHECK-NEXT: call void @_ZN5test43fooENS_1AE([[A]]* [[TMP]])
129 // CHECK-NEXT: call void @_ZN5test41AD1Ev([[A]]* [[TMP]])
130 // CHECK-NEXT: ret void
142 void doWithBlock(void(^)());
144 void test(bool cond) {
146 void (^b)() = (cond ? ^{ x.foo(); } : (void(^)()) 0);
150 // CHECK-LABEL: define void @_ZN5test54testEb(
151 // CHECK: [[COND:%.*]] = alloca i8
152 // CHECK-NEXT: [[X:%.*]] = alloca [[A:%.*]], align 4
153 // CHECK-NEXT: [[B:%.*]] = alloca void ()*, align 8
154 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:.*]], align 8
155 // CHECK-NEXT: [[CLEANUP_ACTIVE:%.*]] = alloca i1
156 // CHECK-NEXT: [[T0:%.*]] = zext i1
157 // CHECK-NEXT: store i8 [[T0]], i8* [[COND]], align 1
158 // CHECK-NEXT: call void @_ZN5test51AC1Ev([[A]]* [[X]])
159 // CHECK-NEXT: [[CLEANUP_ADDR:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
160 // CHECK-NEXT: [[T0:%.*]] = load i8* [[COND]], align 1
161 // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1
162 // CHECK-NEXT: store i1 false, i1* [[CLEANUP_ACTIVE]]
163 // CHECK-NEXT: br i1 [[T1]],
166 // CHECK: [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
167 // CHECK-NEXT: call void @_ZN5test51AC1ERKS0_([[A]]* [[CAPTURE]], [[A]]* [[X]])
168 // CHECK-NEXT: store i1 true, i1* [[CLEANUP_ACTIVE]]
169 // CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
170 // CHECK-NEXT: br label
175 // CHECK-NEXT: call void @_ZN5test511doWithBlockEU13block_pointerFvvE(
176 // CHECK-NEXT: [[T0:%.*]] = load i1* [[CLEANUP_ACTIVE]]
177 // CHECK-NEXT: br i1 [[T0]]
178 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[CLEANUP_ADDR]])
179 // CHECK-NEXT: br label
180 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[X]])
181 // CHECK-NEXT: ret void
190 void foo(const A &, void (^)());
194 // Make sure that the temporary cleanup isn't somehow captured
196 foo(A(), ^{ bar(); });
200 // CHECK-LABEL: define void @_ZN5test64testEv()
201 // CHECK: [[TEMP:%.*]] = alloca [[A:%.*]], align 1
202 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[TEMP]])
203 // CHECK-NEXT: call void @_ZN5test63fooERKNS_1AEU13block_pointerFvvE(
204 // CHECK-NEXT: call void @_ZN5test61AD1Ev([[A]]* [[TEMP]])
205 // CHECK-NEXT: call void @_ZN5test63barEv()
206 // CHECK-NEXT: ret void
213 return ^{ return *p; }();
218 // <rdar://problem/10832617>: failure to capture this after skipping rebuild
219 // of the 'this' pointer.
225 return ^ { return x; }();
229 template int X::foo<int>();
241 void use_block(void (^)());
242 void use_block_2(void (^)(), const B &a);
244 // Ensuring that creating a non-trivial capture copy expression
245 // doesn't end up stealing the block registration for the block we
246 // just parsed. That block must have captures or else it won't
247 // force registration. Must occur within a block for some reason.
252 use_block_2(^{ (void)y; }, x);