1// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks -fobjc-arc -fobjc-runtime-has-weak -DWEAK_SUPPORTED | FileCheck -check-prefix=ARC %s 2// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks | FileCheck -check-prefix=MRC %s 3 4typedef int (^fp)(); 5fp f() { auto x = []{ return 3; }; return x; } 6 7// ARC: %[[LAMBDACLASS:.*]] = type { i32 } 8 9// MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [5 x i8] c"copy\00" 10// MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [12 x i8] c"autorelease\00" 11// MRC-LABEL: define{{.*}} ptr @_Z1fv( 12// MRC-LABEL: define internal noundef ptr @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv" 13// MRC: store ptr @_NSConcreteStackBlock 14// MRC: store ptr @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" 15// MRC: call noundef ptr @objc_msgSend 16// MRC: call noundef ptr @objc_msgSend 17// MRC: ret ptr 18 19// ARC-LABEL: define{{.*}} ptr @_Z1fv( 20// ARC-LABEL: define internal noundef ptr @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv" 21// ARC: store ptr @_NSConcreteStackBlock 22// ARC: store ptr @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" 23// ARC: call ptr @llvm.objc.retainBlock 24// ARC: call ptr @llvm.objc.autoreleaseReturnValue 25 26typedef int (^fp)(); 27fp global; 28void f2() { global = []{ return 3; }; } 29 30// MRC: define{{.*}} void @_Z2f2v() [[NUW:#[0-9]+]] { 31// MRC: store ptr @___Z2f2v_block_invoke, 32// MRC-NOT: call 33// MRC: ret void 34// ("global" contains a dangling pointer after this function runs.) 35 36// ARC: define{{.*}} void @_Z2f2v() [[NUW:#[0-9]+]] { 37// ARC: store ptr @___Z2f2v_block_invoke, 38// ARC: call ptr @llvm.objc.retainBlock 39// ARC: call void @llvm.objc.release 40// ARC-LABEL: define internal noundef i32 @___Z2f2v_block_invoke 41// ARC: call noundef i32 @"_ZZ2f2vENK3$_0clEv 42 43template <class T> void take_lambda(T &&lambda) { lambda(); } 44void take_block(void (^block)()) { block(); } 45 46@interface A 47- (void) test; 48@end 49@interface B : A @end 50@implementation B 51- (void) test { 52 take_block(^{ 53 take_lambda([=]{ 54 take_block(^{ 55 take_lambda([=] { 56 [super test]; 57 }); 58 }); 59 }); 60 }); 61} 62@end 63 64// ARC: define{{.*}} void @_ZN13LambdaCapture4foo1ERi(ptr noundef nonnull align 4 dereferenceable(4) %{{.*}}) 65// ARC: %[[CAPTURE0:.*]] = getelementptr inbounds nuw %[[LAMBDACLASS]], ptr %{{.*}}, i32 0, i32 0 66// ARC: store i32 %{{.*}}, ptr %[[CAPTURE0]] 67 68// ARC: define internal void @"_ZZN13LambdaCapture4foo1ERiENK3$_0clEv"(ptr {{[^,]*}} %{{.*}}) 69// ARC: %[[BLOCK:.*]] = alloca <{ ptr, i32, i32, ptr, ptr, i32 }> 70// ARC: %[[CAPTURE1:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, i32 }>, ptr %[[BLOCK]], i32 0, i32 5 71// ARC: store i32 %{{.*}}, ptr %[[CAPTURE1]] 72 73// ARC-LABEL: define internal void @"_ZZ10-[Foo foo]ENK3$_4clEv"( 74// ARC-NOT: @llvm.objc.storeStrong( 75// ARC: ret void 76 77// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_0clEv_block_invoke" 78// ARC: %[[CAPTURE2:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, i32 }>, ptr %{{.*}}, i32 0, i32 5 79// ARC: store i32 %{{.*}}, ptr %[[CAPTURE2]] 80 81// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_0clEv_block_invoke_2"(ptr noundef %{{.*}}) 82// ARC: %[[CAPTURE3:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, i32 }>, ptr %{{.*}}, i32 0, i32 5 83// ARC: %[[V1:.*]] = load i32, ptr %[[CAPTURE3]] 84// ARC: store i32 %[[V1]], ptr @_ZN13LambdaCapture1iE 85 86namespace LambdaCapture { 87 int i; 88 void foo1(int &a) { 89 auto lambda = [a]{ 90 auto block1 = ^{ 91 auto block2 = ^{ 92 i = a; 93 }; 94 block2(); 95 }; 96 block1(); 97 }; 98 lambda(); 99 } 100} 101 102// ARC-LABEL: define linkonce_odr noundef ptr @_ZZNK13StaticMembersIfE1fMUlvE_clEvENKUlvE_cvU13block_pointerFivEEv 103 104// Check lines for BlockInLambda test below 105// ARC-LABEL: define internal noundef i32 @___ZZN13BlockInLambda1X1fEvENKUlvE_clEv_block_invoke 106// ARC: [[Y:%.*]] = getelementptr inbounds nuw %"struct.BlockInLambda::X", ptr {{.*}}, i32 0, i32 1 107// ARC-NEXT: [[YVAL:%.*]] = load i32, ptr [[Y]], align 4 108// ARC-NEXT: ret i32 [[YVAL]] 109 110typedef int (^fptr)(); 111template<typename T> struct StaticMembers { 112 static fptr f; 113}; 114template<typename T> 115fptr StaticMembers<T>::f = [] { auto f = []{return 5;}; return fptr(f); }(); 116template fptr StaticMembers<float>::f; 117 118namespace BlockInLambda { 119 struct X { 120 int x,y; 121 void f() { 122 [this]{return ^{return y;}();}(); 123 }; 124 }; 125 void g(X& x) { 126 x.f(); 127 }; 128} 129 130@interface NSObject @end 131@interface Foo : NSObject @end 132@implementation Foo 133- (void)foo { 134 [&] { 135 ^{ (void)self; }(); 136 }(); 137} 138@end 139 140// Check that the delegating invoke function doesn't destruct the Weak object 141// that is passed. 142 143// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvEN3$_08__invokeENS_4WeakE"( 144// ARC: call void @"_ZZN14LambdaDelegate4testEvENK3$_0clENS_4WeakE"( 145// ARC-NEXT: ret void 146 147// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvENK3$_0clENS_4WeakE"( 148// ARC: call void @_ZN14LambdaDelegate4WeakD1Ev( 149 150#ifdef WEAK_SUPPORTED 151 152namespace LambdaDelegate { 153 154struct Weak { 155 __weak id x; 156}; 157 158void test() { 159 void (*p)(Weak) = [](Weak a) { }; 160} 161 162}; 163 164#endif 165 166// ARC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} } 167// MRC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} } 168