1 // RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s 2 3 namespace test0 { 4 // CHECK-LABEL: define void @_ZN5test04testEi( 5 // CHECK: define internal void @___ZN5test04testEi_block_invoke{{.*}}( 6 // CHECK: define internal void @___ZN5test04testEi_block_invoke_2{{.*}}( 7 void test(int x) { 8 ^{ ^{ (void) x; }; }; 9 } 10 } 11 12 extern void (^out)(); 13 14 namespace test1 { 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 18 void test1() { 19 const int NumHorsemen = 4; 20 out = ^{ (void) NumHorsemen; }; 21 } 22 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; }; 27 void test2() { 28 const loc target = { 5, 6 }; 29 out = ^{ (void) target; }; 30 } 31 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; }; 38 void test3() { 39 const mut obj = { 5 }; 40 out = ^{ (void) obj; }; 41 } 42 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(); }; 50 void test4() { 51 const scope obj = { 5 }; 52 out = ^{ (void) obj; }; 53 } 54 55 // ...or non-trivial copy constructors, but it's not clear how to do 56 // that and still have a constant initializer in '03. 57 } 58 59 namespace test2 { 60 struct A { 61 A(); 62 A(const A &); 63 ~A(); 64 }; 65 66 struct B { 67 B(); 68 B(const B &); 69 ~B(); 70 }; 71 72 // CHECK-LABEL: define void @_ZN5test24testEv() 73 void test() { 74 __block A a; 75 __block B b; 76 } 77 78 // CHECK-LABEL: define internal void @__Block_byref_object_copy 79 // CHECK: call void @_ZN5test21AC1ERKS0_( 80 81 // CHECK-LABEL: define internal void @__Block_byref_object_dispose 82 // CHECK: call void @_ZN5test21AD1Ev( 83 84 // CHECK-LABEL: define internal void @__Block_byref_object_copy 85 // CHECK: call void @_ZN5test21BC1ERKS0_( 86 87 // CHECK-LABEL: define internal void @__Block_byref_object_dispose 88 // CHECK: call void @_ZN5test21BD1Ev( 89 } 90 91 // rdar://problem/9334739 92 // Make sure we mark destructors for parameters captured in blocks. 93 namespace test3 { 94 struct A { 95 A(const A&); 96 ~A(); 97 }; 98 99 struct B : A { 100 }; 101 102 void test(B b) { 103 extern void consume(void(^)()); 104 consume(^{ (void) b; }); 105 } 106 } 107 108 // rdar://problem/9971485 109 namespace test4 { 110 struct A { 111 A(); 112 ~A(); 113 }; 114 115 void foo(A a); 116 117 void test() { 118 extern void consume(void(^)()); 119 consume(^{ return foo(A()); }); 120 } 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: bitcast i8* [[BLOCKDESC]] to <{ i8*, i32, i32, i8*, %struct.__block_descriptor* }>* 126 // CHECK: call void @_ZN5test41AC1Ev([[A]]* [[TMP]]) 127 // CHECK-NEXT: call void @_ZN5test43fooENS_1AE([[A]]* [[TMP]]) 128 // CHECK-NEXT: call void @_ZN5test41AD1Ev([[A]]* [[TMP]]) 129 // CHECK-NEXT: ret void 130 } 131 132 namespace test5 { 133 struct A { 134 unsigned afield; 135 A(); 136 A(const A&); 137 ~A(); 138 void foo() const; 139 }; 140 141 void doWithBlock(void(^)()); 142 143 void test(bool cond) { 144 A x; 145 void (^b)() = (cond ? ^{ x.foo(); } : (void(^)()) 0); 146 doWithBlock(b); 147 } 148 149 // CHECK-LABEL: define void @_ZN5test54testEb( 150 // CHECK: [[COND:%.*]] = alloca i8 151 // CHECK-NEXT: [[X:%.*]] = alloca [[A:%.*]], align 4 152 // CHECK-NEXT: [[B:%.*]] = alloca void ()*, align 8 153 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:.*]], align 8 154 // CHECK-NEXT: [[CLEANUP_ACTIVE:%.*]] = alloca i1 155 // CHECK-NEXT: [[T0:%.*]] = zext i1 156 // CHECK-NEXT: store i8 [[T0]], i8* [[COND]], align 1 157 // CHECK-NEXT: call void @_ZN5test51AC1Ev([[A]]* [[X]]) 158 // CHECK-NEXT: [[CLEANUP_ADDR:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 159 // CHECK-NEXT: [[T0:%.*]] = load i8, i8* [[COND]], align 1 160 // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1 161 // CHECK-NEXT: store i1 false, i1* [[CLEANUP_ACTIVE]] 162 // CHECK-NEXT: br i1 [[T1]], 163 164 // CHECK-NOT: br 165 // CHECK: [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 166 // CHECK-NEXT: call void @_ZN5test51AC1ERKS0_([[A]]* [[CAPTURE]], [[A]]* dereferenceable({{[0-9]+}}) [[X]]) 167 // CHECK-NEXT: store i1 true, i1* [[CLEANUP_ACTIVE]] 168 // CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to void ()* 169 // CHECK-NEXT: br label 170 // CHECK: br label 171 // CHECK: phi 172 // CHECK-NEXT: store 173 // CHECK-NEXT: load 174 // CHECK-NEXT: call void @_ZN5test511doWithBlockEU13block_pointerFvvE( 175 // CHECK-NEXT: [[T0:%.*]] = load i1, i1* [[CLEANUP_ACTIVE]] 176 // CHECK-NEXT: br i1 [[T0]] 177 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[CLEANUP_ADDR]]) 178 // CHECK-NEXT: br label 179 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[X]]) 180 // CHECK-NEXT: ret void 181 } 182 183 namespace test6 { 184 struct A { 185 A(); 186 ~A(); 187 }; 188 189 void foo(const A &, void (^)()); 190 void bar(); 191 192 void test() { 193 // Make sure that the temporary cleanup isn't somehow captured 194 // within the block. 195 foo(A(), ^{ bar(); }); 196 bar(); 197 } 198 199 // CHECK-LABEL: define void @_ZN5test64testEv() 200 // CHECK: [[TEMP:%.*]] = alloca [[A:%.*]], align 1 201 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[TEMP]]) 202 // CHECK-NEXT: call void @_ZN5test63fooERKNS_1AEU13block_pointerFvvE( 203 // CHECK-NEXT: call void @_ZN5test61AD1Ev([[A]]* [[TEMP]]) 204 // CHECK-NEXT: call void @_ZN5test63barEv() 205 // CHECK-NEXT: ret void 206 } 207 208 namespace test7 { 209 int f() { 210 static int n; 211 int *const p = &n; 212 return ^{ return *p; }(); 213 } 214 } 215 216 namespace test8 { 217 // <rdar://problem/10832617>: failure to capture this after skipping rebuild 218 // of the 'this' pointer. 219 struct X { 220 int x; 221 222 template<typename T> 223 int foo() { 224 return ^ { return x; }(); 225 } 226 }; 227 228 template int X::foo<int>(); 229 } 230 231 // rdar://13459289 232 namespace test9 { 233 struct B { 234 void *p; 235 B(); 236 B(const B&); 237 ~B(); 238 }; 239 240 void use_block(void (^)()); 241 void use_block_2(void (^)(), const B &a); 242 243 // Ensuring that creating a non-trivial capture copy expression 244 // doesn't end up stealing the block registration for the block we 245 // just parsed. That block must have captures or else it won't 246 // force registration. Must occur within a block for some reason. 247 void test() { 248 B x; 249 use_block(^{ 250 int y; 251 use_block_2(^{ (void)y; }, x); 252 }); 253 } 254 } 255