2018-04-27 14:57:00 +08:00
|
|
|
// 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
|
2012-02-28 11:32:48 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks | FileCheck -check-prefix=MRC %s
|
|
|
|
|
|
|
|
typedef int (^fp)();
|
|
|
|
fp f() { auto x = []{ return 3; }; return x; }
|
|
|
|
|
2016-09-16 08:02:06 +08:00
|
|
|
// ARC: %[[LAMBDACLASS:.*]] = type { i32 }
|
|
|
|
|
2016-09-19 00:12:14 +08:00
|
|
|
// MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [5 x i8] c"copy\00"
|
|
|
|
// MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [12 x i8] c"autorelease\00"
|
2020-12-31 12:45:56 +08:00
|
|
|
// MRC-LABEL: define{{.*}} i32 ()* @_Z1fv(
|
2013-08-15 14:47:53 +08:00
|
|
|
// MRC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
|
2012-02-28 11:32:48 +08:00
|
|
|
// MRC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
|
2012-06-27 00:06:38 +08:00
|
|
|
// MRC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
|
2015-04-17 07:25:00 +08:00
|
|
|
// MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
|
|
|
|
// MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
|
2012-02-28 11:32:48 +08:00
|
|
|
// MRC: ret i32 ()*
|
|
|
|
|
2020-12-31 12:45:56 +08:00
|
|
|
// ARC-LABEL: define{{.*}} i32 ()* @_Z1fv(
|
2013-08-15 14:47:53 +08:00
|
|
|
// ARC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
|
2012-02-28 11:32:48 +08:00
|
|
|
// ARC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
|
2012-06-27 00:06:38 +08:00
|
|
|
// ARC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
|
2018-12-19 04:33:00 +08:00
|
|
|
// ARC: call i8* @llvm.objc.retainBlock
|
|
|
|
// ARC: call i8* @llvm.objc.autoreleaseReturnValue
|
2012-03-01 12:01:32 +08:00
|
|
|
|
|
|
|
typedef int (^fp)();
|
|
|
|
fp global;
|
|
|
|
void f2() { global = []{ return 3; }; }
|
|
|
|
|
2020-12-31 12:45:56 +08:00
|
|
|
// MRC: define{{.*}} void @_Z2f2v() [[NUW:#[0-9]+]] {
|
2012-06-27 00:06:38 +08:00
|
|
|
// MRC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
|
2012-03-01 12:01:32 +08:00
|
|
|
// MRC-NOT: call
|
|
|
|
// MRC: ret void
|
|
|
|
// ("global" contains a dangling pointer after this function runs.)
|
|
|
|
|
2020-12-31 12:45:56 +08:00
|
|
|
// ARC: define{{.*}} void @_Z2f2v() [[NUW:#[0-9]+]] {
|
2012-06-27 00:06:38 +08:00
|
|
|
// ARC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
|
2018-12-19 04:33:00 +08:00
|
|
|
// ARC: call i8* @llvm.objc.retainBlock
|
|
|
|
// ARC: call void @llvm.objc.release
|
2013-08-15 14:47:53 +08:00
|
|
|
// ARC-LABEL: define internal i32 @___Z2f2v_block_invoke
|
2012-03-01 12:01:32 +08:00
|
|
|
// ARC: call i32 @"_ZZ2f2vENK3$_1clEv
|
2013-02-20 15:22:19 +08:00
|
|
|
|
2013-05-03 15:33:41 +08:00
|
|
|
template <class T> void take_lambda(T &&lambda) { lambda(); }
|
|
|
|
void take_block(void (^block)()) { block(); }
|
|
|
|
|
|
|
|
// rdar://13800041
|
|
|
|
@interface A
|
|
|
|
- (void) test;
|
|
|
|
@end
|
|
|
|
@interface B : A @end
|
|
|
|
@implementation B
|
|
|
|
- (void) test {
|
|
|
|
take_block(^{
|
|
|
|
take_lambda([=]{
|
|
|
|
take_block(^{
|
|
|
|
take_lambda([=] {
|
|
|
|
[super test];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2020-12-31 12:45:56 +08:00
|
|
|
// ARC: define{{.*}} void @_ZN13LambdaCapture4foo1ERi(i32* nonnull align 4 dereferenceable(4) %{{.*}})
|
2016-09-16 08:02:06 +08:00
|
|
|
// ARC: %[[CAPTURE0:.*]] = getelementptr inbounds %[[LAMBDACLASS]], %[[LAMBDACLASS]]* %{{.*}}, i32 0, i32 0
|
|
|
|
// ARC: store i32 %{{.*}}, i32* %[[CAPTURE0]]
|
|
|
|
|
2020-11-17 07:04:55 +08:00
|
|
|
// ARC: define internal void @"_ZZN13LambdaCapture4foo1ERiENK3$_3clEv"(%[[LAMBDACLASS]]* {{[^,]*}} %{{.*}})
|
2016-09-16 08:02:06 +08:00
|
|
|
// ARC: %[[BLOCK:.*]] = alloca <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>
|
|
|
|
// ARC: %[[CAPTURE1:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %[[BLOCK]], i32 0, i32 5
|
|
|
|
// ARC: store i32 %{{.*}}, i32* %[[CAPTURE1]]
|
|
|
|
|
2017-02-14 14:46:55 +08:00
|
|
|
// ARC-LABEL: define internal void @"_ZZ10-[Foo foo]ENK3$_4clEv"(
|
2018-12-19 04:33:00 +08:00
|
|
|
// ARC-NOT: @llvm.objc.storeStrong(
|
2017-02-14 14:46:55 +08:00
|
|
|
// ARC: ret void
|
|
|
|
|
2016-09-16 08:02:06 +08:00
|
|
|
// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke"
|
|
|
|
// ARC: %[[CAPTURE2:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5
|
|
|
|
// ARC: store i32 %{{.*}}, i32* %[[CAPTURE2]]
|
|
|
|
|
|
|
|
// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke_2"(i8* %{{.*}})
|
|
|
|
// ARC: %[[CAPTURE3:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5
|
|
|
|
// ARC: %[[V1:.*]] = load i32, i32* %[[CAPTURE3]]
|
|
|
|
// ARC: store i32 %[[V1]], i32* @_ZN13LambdaCapture1iE
|
|
|
|
|
|
|
|
namespace LambdaCapture {
|
|
|
|
int i;
|
|
|
|
void foo1(int &a) {
|
|
|
|
auto lambda = [a]{
|
|
|
|
auto block1 = ^{
|
|
|
|
auto block2 = ^{
|
|
|
|
i = a;
|
|
|
|
};
|
|
|
|
block2();
|
|
|
|
};
|
|
|
|
block1();
|
|
|
|
};
|
|
|
|
lambda();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 08:24:57 +08:00
|
|
|
// ARC-LABEL: define linkonce_odr i32 ()* @_ZZNK13StaticMembersIfE1fMUlvE_clEvENKUlvE_cvU13block_pointerFivEEv
|
|
|
|
|
2013-07-13 06:05:26 +08:00
|
|
|
// Check lines for BlockInLambda test below
|
2013-08-15 14:47:53 +08:00
|
|
|
// ARC-LABEL: define internal i32 @___ZZN13BlockInLambda1X1fEvENKUlvE_clEv_block_invoke
|
2015-02-28 03:18:17 +08:00
|
|
|
// ARC: [[Y:%.*]] = getelementptr inbounds %"struct.BlockInLambda::X", %"struct.BlockInLambda::X"* {{.*}}, i32 0, i32 1
|
2015-02-28 05:19:58 +08:00
|
|
|
// ARC-NEXT: [[YVAL:%.*]] = load i32, i32* [[Y]], align 4
|
2013-07-13 06:05:26 +08:00
|
|
|
// ARC-NEXT: ret i32 [[YVAL]]
|
|
|
|
|
2013-06-14 04:56:27 +08:00
|
|
|
typedef int (^fptr)();
|
|
|
|
template<typename T> struct StaticMembers {
|
|
|
|
static fptr f;
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
fptr StaticMembers<T>::f = [] { auto f = []{return 5;}; return fptr(f); }();
|
|
|
|
template fptr StaticMembers<float>::f;
|
|
|
|
|
2013-07-13 06:05:26 +08:00
|
|
|
namespace BlockInLambda {
|
|
|
|
struct X {
|
|
|
|
int x,y;
|
|
|
|
void f() {
|
|
|
|
[this]{return ^{return y;}();}();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
void g(X& x) {
|
|
|
|
x.f();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:46:55 +08:00
|
|
|
@interface NSObject @end
|
|
|
|
@interface Foo : NSObject @end
|
|
|
|
@implementation Foo
|
|
|
|
- (void)foo {
|
|
|
|
[&] {
|
|
|
|
^{ (void)self; }();
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
@end
|
2013-02-20 15:22:19 +08:00
|
|
|
|
2018-04-27 14:57:00 +08:00
|
|
|
// Check that the delegating invoke function doesn't destruct the Weak object
|
|
|
|
// that is passed.
|
|
|
|
|
|
|
|
// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvEN3$_58__invokeENS_4WeakE"(
|
|
|
|
// ARC: call void @"_ZZN14LambdaDelegate4testEvENK3$_5clENS_4WeakE"(
|
|
|
|
// ARC-NEXT: ret void
|
|
|
|
|
|
|
|
// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvENK3$_5clENS_4WeakE"(
|
|
|
|
// ARC: call void @_ZN14LambdaDelegate4WeakD1Ev(
|
|
|
|
|
|
|
|
#ifdef WEAK_SUPPORTED
|
|
|
|
|
|
|
|
namespace LambdaDelegate {
|
|
|
|
|
|
|
|
struct Weak {
|
|
|
|
__weak id x;
|
|
|
|
};
|
|
|
|
|
|
|
|
void test() {
|
|
|
|
void (*p)(Weak) = [](Weak a) { };
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-07-09 23:58:48 +08:00
|
|
|
// ARC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} }
|
|
|
|
// MRC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} }
|