1 // RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s 2 3 // CHECK: %[[STRUCT_BLOCK_DESCRIPTOR:.*]] = type { i64, i64 } 4 // CHECK: @[[BLOCK_DESCRIPTOR22:.*]] = internal constant { i64, i64, i8*, i8*, i8*, i8* } { i64 0, i64 36, i8* bitcast (void (i8*, i8*)* @__copy_helper_block_8_32c22_ZTSN12_GLOBAL__N_11BE to i8*), i8* bitcast (void (i8*)* @__destroy_helper_block_8_32c22_ZTSN12_GLOBAL__N_11BE to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @{{.*}}, i32 0, i32 0), i8* null }, align 8 5 6 namespace test0 { 7 // CHECK-LABEL: define void @_ZN5test04testEi( 8 // CHECK: define internal void @___ZN5test04testEi_block_invoke{{.*}}( 9 // CHECK: define internal void @___ZN5test04testEi_block_invoke_2{{.*}}( 10 void test(int x) { 11 ^{ ^{ (void) x; }; }; 12 } 13 } 14 15 extern void (^out)(); 16 17 namespace test1 { 18 // Capturing const objects doesn't require a local block. 19 // CHECK-LABEL: define void @_ZN5test15test1Ev() 20 // CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out 21 void test1() { 22 const int NumHorsemen = 4; 23 out = ^{ (void) NumHorsemen; }; 24 } 25 26 // That applies to structs too... 27 // CHECK-LABEL: define void @_ZN5test15test2Ev() 28 // CHECK: store void ()* bitcast ({{.*}} @__block_literal_global{{.*}} to void ()*), void ()** @out 29 struct loc { double x, y; }; 30 void test2() { 31 const loc target = { 5, 6 }; 32 out = ^{ (void) target; }; 33 } 34 35 // ...unless they have mutable fields... 36 // CHECK-LABEL: define void @_ZN5test15test3Ev() 37 // CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], 38 // CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()* 39 // CHECK: store void ()* [[T0]], void ()** @out 40 struct mut { mutable int x; }; 41 void test3() { 42 const mut obj = { 5 }; 43 out = ^{ (void) obj; }; 44 } 45 46 // ...or non-trivial destructors... 47 // CHECK-LABEL: define void @_ZN5test15test4Ev() 48 // CHECK: [[OBJ:%.*]] = alloca 49 // CHECK: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], 50 // CHECK: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()* 51 // CHECK: store void ()* [[T0]], void ()** @out 52 struct scope { int x; ~scope(); }; 53 void test4() { 54 const scope obj = { 5 }; 55 out = ^{ (void) obj; }; 56 } 57 58 // ...or non-trivial copy constructors, but it's not clear how to do 59 // that and still have a constant initializer in '03. 60 } 61 62 namespace test2 { 63 struct A { 64 A(); 65 A(const A &); 66 ~A(); 67 }; 68 69 struct B { 70 B(); 71 B(const B &); 72 ~B(); 73 }; 74 75 // CHECK-LABEL: define void @_ZN5test24testEv() 76 void test() { 77 __block A a; 78 __block B b; 79 } 80 81 // CHECK-LABEL: define internal void @__Block_byref_object_copy 82 // CHECK: call void @_ZN5test21AC1ERKS0_( 83 84 // CHECK-LABEL: define internal void @__Block_byref_object_dispose 85 // CHECK: call void @_ZN5test21AD1Ev( 86 87 // CHECK-LABEL: define internal void @__Block_byref_object_copy 88 // CHECK: call void @_ZN5test21BC1ERKS0_( 89 90 // CHECK-LABEL: define internal void @__Block_byref_object_dispose 91 // CHECK: call void @_ZN5test21BD1Ev( 92 } 93 94 // rdar://problem/9334739 95 // Make sure we mark destructors for parameters captured in blocks. 96 namespace test3 { 97 struct A { 98 A(const A&); 99 ~A(); 100 }; 101 102 struct B : A { 103 }; 104 105 void test(B b) { 106 extern void consume(void(^)()); 107 consume(^{ (void) b; }); 108 } 109 } 110 111 // rdar://problem/9971485 112 namespace test4 { 113 struct A { 114 A(); 115 ~A(); 116 }; 117 118 void foo(A a); 119 120 void test() { 121 extern void consume(void(^)()); 122 consume(^{ return foo(A()); }); 123 } 124 // CHECK-LABEL: define void @_ZN5test44testEv() 125 // CHECK-LABEL: define internal void @___ZN5test44testEv_block_invoke 126 // CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align 1 127 // CHECK-NEXT: store i8* [[BLOCKDESC:%.*]], i8** {{.*}}, align 8 128 // CHECK-NEXT: bitcast i8* [[BLOCKDESC]] to <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]* }>* 129 // CHECK: call void @_ZN5test41AC1Ev([[A]]* [[TMP]]) 130 // CHECK-NEXT: call void @_ZN5test43fooENS_1AE([[A]]* [[TMP]]) 131 // CHECK-NEXT: call void @_ZN5test41AD1Ev([[A]]* [[TMP]]) 132 // CHECK-NEXT: ret void 133 } 134 135 namespace test5 { 136 struct A { 137 unsigned afield; 138 A(); 139 A(const A&); 140 ~A(); 141 void foo() const; 142 }; 143 144 void doWithBlock(void(^)()); 145 146 void test(bool cond) { 147 A x; 148 void (^b)() = (cond ? ^{ x.foo(); } : (void(^)()) 0); 149 doWithBlock(b); 150 } 151 152 // CHECK-LABEL: define void @_ZN5test54testEb( 153 // CHECK: [[COND:%.*]] = alloca i8 154 // CHECK-NEXT: [[X:%.*]] = alloca [[A:%.*]], align 4 155 // CHECK-NEXT: [[B:%.*]] = alloca void ()*, align 8 156 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:.*]], align 8 157 // CHECK-NEXT: [[CLEANUP_ACTIVE:%.*]] = alloca i1 158 // CHECK-NEXT: [[T0:%.*]] = zext i1 159 // CHECK-NEXT: store i8 [[T0]], i8* [[COND]], align 1 160 // CHECK-NEXT: call void @_ZN5test51AC1Ev([[A]]* [[X]]) 161 // CHECK-NEXT: [[CLEANUP_ADDR:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 162 // CHECK-NEXT: [[T0:%.*]] = load i8, i8* [[COND]], align 1 163 // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1 164 // CHECK-NEXT: store i1 false, i1* [[CLEANUP_ACTIVE]] 165 // CHECK-NEXT: br i1 [[T1]], 166 167 // CHECK-NOT: br 168 // CHECK: [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 169 // CHECK-NEXT: call void @_ZN5test51AC1ERKS0_([[A]]* [[CAPTURE]], [[A]]* dereferenceable({{[0-9]+}}) [[X]]) 170 // CHECK-NEXT: store i1 true, i1* [[CLEANUP_ACTIVE]] 171 // CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to void ()* 172 // CHECK-NEXT: br label 173 // CHECK: br label 174 // CHECK: phi 175 // CHECK-NEXT: store 176 // CHECK-NEXT: load 177 // CHECK-NEXT: call void @_ZN5test511doWithBlockEU13block_pointerFvvE( 178 // CHECK-NEXT: [[T0:%.*]] = load i1, i1* [[CLEANUP_ACTIVE]] 179 // CHECK-NEXT: br i1 [[T0]] 180 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[CLEANUP_ADDR]]) 181 // CHECK-NEXT: br label 182 // CHECK: call void @_ZN5test51AD1Ev([[A]]* [[X]]) 183 // CHECK-NEXT: ret void 184 } 185 186 namespace test6 { 187 struct A { 188 A(); 189 ~A(); 190 }; 191 192 void foo(const A &, void (^)()); 193 void bar(); 194 195 void test() { 196 // Make sure that the temporary cleanup isn't somehow captured 197 // within the block. 198 foo(A(), ^{ bar(); }); 199 bar(); 200 } 201 202 // CHECK-LABEL: define void @_ZN5test64testEv() 203 // CHECK: [[TEMP:%.*]] = alloca [[A:%.*]], align 1 204 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[TEMP]]) 205 // CHECK-NEXT: call void @_ZN5test63fooERKNS_1AEU13block_pointerFvvE( 206 // CHECK-NEXT: call void @_ZN5test61AD1Ev([[A]]* [[TEMP]]) 207 // CHECK-NEXT: call void @_ZN5test63barEv() 208 // CHECK-NEXT: ret void 209 } 210 211 namespace test7 { 212 int f() { 213 static int n; 214 int *const p = &n; 215 return ^{ return *p; }(); 216 } 217 } 218 219 namespace test8 { 220 // <rdar://problem/10832617>: failure to capture this after skipping rebuild 221 // of the 'this' pointer. 222 struct X { 223 int x; 224 225 template<typename T> 226 int foo() { 227 return ^ { return x; }(); 228 } 229 }; 230 231 template int X::foo<int>(); 232 } 233 234 // rdar://13459289 235 namespace test9 { 236 struct B { 237 void *p; 238 B(); 239 B(const B&); 240 ~B(); 241 }; 242 243 void use_block(void (^)()); 244 void use_block_2(void (^)(), const B &a); 245 246 // Ensuring that creating a non-trivial capture copy expression 247 // doesn't end up stealing the block registration for the block we 248 // just parsed. That block must have captures or else it won't 249 // force registration. Must occur within a block for some reason. 250 void test() { 251 B x; 252 use_block(^{ 253 int y; 254 use_block_2(^{ (void)y; }, x); 255 }); 256 } 257 } 258 259 namespace test10 { 260 // Check that 'v' is included in the copy helper function name to indicate 261 // the constructor taking a volatile parameter is called to copy the captured 262 // object. 263 264 // CHECK-LABEL: define linkonce_odr hidden void @__copy_helper_block_8_32c16_ZTSVN6test101BE( 265 // CHECK: call void @_ZN6test101BC1ERVKS0_( 266 // CHECK-LABEL: define linkonce_odr hidden void @__destroy_helper_block_8_32c16_ZTSVN6test101BE( 267 // CHECK: call void @_ZN6test101BD1Ev( 268 269 struct B { 270 int a; 271 B(); 272 B(const B &); 273 B(const volatile B &); 274 ~B(); 275 }; 276 277 void test() { 278 volatile B x; 279 ^{ (void)x; }; 280 } 281 } 282 283 // Copy/dispose helper functions and block descriptors of blocks that capture 284 // objects that are non-external and non-trivial have internal linkage. 285 286 // CHECK-LABEL: define internal void @_ZN12_GLOBAL__N_14testEv( 287 // CHECK: store %[[STRUCT_BLOCK_DESCRIPTOR]]* bitcast ({ i64, i64, i8*, i8*, i8*, i8* }* @[[BLOCK_DESCRIPTOR22]] to %[[STRUCT_BLOCK_DESCRIPTOR]]*), %[[STRUCT_BLOCK_DESCRIPTOR]]** %{{.*}}, align 8 288 289 // CHECK-LABEL: define internal void @__copy_helper_block_8_32c22_ZTSN12_GLOBAL__N_11BE( 290 // CHECK-LABEL: define internal void @__destroy_helper_block_8_32c22_ZTSN12_GLOBAL__N_11BE( 291 292 namespace { 293 struct B { 294 int a; 295 B(); 296 B(const B &); 297 ~B(); 298 }; 299 300 void test() { 301 B x; 302 ^{ (void)x; }; 303 } 304 } 305 306 void callTest() { 307 test(); 308 } 309