1 // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm -std=c++14 -fblocks -fobjc-arc -o - %s | FileCheck %s
3 // CHECK: %[[S:.*]] = type { i32 }
4 // CHECK: %[[CLASS_ANON_2:.*]] = type { ptr }
5 // CHECK: %[[CLASS_ANON_3:.*]] = type { %[[S]] }
7 // CHECK: %[[BLOCK_CAPTURED0:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %[[BLOCK:.*]], i32 0, i32 5
8 // CHECK: %[[V0:.*]] = getelementptr inbounds %[[LAMBDA_CLASS:.*]], ptr %[[THIS:.*]], i32 0, i32 0
9 // CHECK: %[[V1:.*]] = load ptr, ptr %[[V0]], align 8
10 // CHECK: store ptr %[[V1]], ptr %[[BLOCK_CAPTURED0]], align 8
11 // CHECK: %[[BLOCK_CAPTURED1:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 6
12 // CHECK: %[[V2:.*]] = getelementptr inbounds %[[LAMBDA_CLASS]], ptr %[[THIS]], i32 0, i32 1
13 // CHECK: %[[V3:.*]] = load ptr, ptr %[[V2]], align 8
14 // CHECK: store ptr %[[V3]], ptr %[[BLOCK_CAPTURED1]], align 8
16 void foo1(int &, int &);
18 void block_in_lambda(int &s1, int &s2) {
19 auto lambda = [&s1, &s2]() {
29 namespace CaptureByReference {
34 // Block copy/dispose helpers aren't needed because 'a' is captured by
37 // CHECK-LABEL: define{{.*}} void @_ZN18CaptureByReference5test0Ev(
38 // CHECK-LABEL: define internal void @"_ZZN18CaptureByReference5test0EvENK3$_0clEv"(
39 // CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4
40 // CHECK: store ptr @"__block_descriptor_40_e5_v8\01?0ls32l8", ptr %[[BLOCK_DESCRIPTOR]], align 8
44 [&]{ ^{ a = 0; }(); }();
47 // Block copy/dispose helpers shouldn't have to retain/release 'a' because it
48 // is captured by reference.
50 // CHECK-LABEL: define{{.*}} void @_ZN18CaptureByReference5test1Ev(
51 // CHECK-LABEL: define internal void @"_ZZN18CaptureByReference5test1EvENK3$_0clEv"(
52 // CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4
53 // CHECK: store ptr @"__block_descriptor_56_8_32s40s_e5_v8\01?0l", ptr %[[BLOCK_DESCRIPTOR]], align 8
56 id a = getObj(), b = getObj(), c = getObj();
57 [&a, b, c]{ ^{ a = 0; use(b); use(c); }(); }();
65 S &operator=(const S&);
72 // CHECK: define internal noundef i32 @"_ZZN18CaptureByReference5test2EvENK3$_0clIiEEDaT_"(ptr {{[^,]*}} %{{.*}}, i32 noundef %{{.*}})
73 // CHECK: %[[BLOCK:.*]] = alloca <{ ptr, i32, i32, ptr, {{.*}}, ptr }>, align 8
74 // CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, {{.*}}, ptr }>, ptr %[[BLOCK]], i32 0, i32 5
75 // CHECK: %[[V0:.*]] = getelementptr inbounds %[[CLASS_ANON_2]], ptr %{{.*}}, i32 0, i32 0
76 // CHECK: %[[V1:.*]] = load ptr, ptr %[[V0]], align 8
77 // CHECK: store ptr %[[V1]], ptr %[[BLOCK_CAPTURED]], align 8
81 auto fn = [&](const auto a){
89 // CHECK: define internal noundef i32 @"_ZZN18CaptureByReference5test3EvENK3$_0clIiEEDaT_"(ptr {{[^,]*}} %{{.*}}, i32 noundef %{{.*}})
90 // CHECK: %[[BLOCK:.*]] = alloca <{ ptr, i32, i32, ptr, ptr, %[[S]] }>, align 8
91 // CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, %[[S]] }>, ptr %[[BLOCK]], i32 0, i32 5
92 // CHECK: %[[V0:.*]] = getelementptr inbounds %[[CLASS_ANON_3]], ptr %{{.*}}, i32 0, i32 0
93 // CHECK: call void @_ZN18CaptureByReference1SC1ERKS0_(ptr {{[^,]*}} %[[BLOCK_CAPTURED]], ptr {{.*}} %[[V0]])
97 auto fn = [=](const auto a){
105 // CHECK-LABEL: define linkonce_odr hidden void @__copy_helper_block_8_32s40s(
106 // CHECK-NOT: call void @llvm.objc.storeStrong(
107 // CHECK: %[[V4:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 5
108 // CHECK: %[[V5:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 5
109 // CHECK: %[[BLOCKCOPY_SRC:.*]] = load ptr, ptr %[[V4]], align 8
110 // CHECK: store ptr null, ptr %[[V5]], align 8
111 // CHECK: call void @llvm.objc.storeStrong(ptr %[[V5]], ptr %[[BLOCKCOPY_SRC]])
112 // CHECK: %[[V6:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 6
113 // CHECK: %[[V7:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 6
114 // CHECK: %[[BLOCKCOPY_SRC2:.*]] = load ptr, ptr %[[V6]], align 8
115 // CHECK: store ptr null, ptr %[[V7]], align 8
116 // CHECK: call void @llvm.objc.storeStrong(ptr %[[V7]], ptr %[[BLOCKCOPY_SRC2]])
117 // CHECK-NOT: call void @llvm.objc.storeStrong(
120 // CHECK-LABEL: define linkonce_odr hidden void @__destroy_helper_block_8_32s40s(
121 // CHECK: %[[V2:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 5
122 // CHECK: %[[V3:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 6
123 // CHECK-NOT: call void @llvm.objc.storeStrong(
124 // CHECK: call void @llvm.objc.storeStrong(ptr %[[V3]], ptr null)
125 // CHECK: call void @llvm.objc.storeStrong(ptr %[[V2]], ptr null)
126 // CHECK-NOT: call void @llvm.objc.storeStrong(