blob: 0c309c0a4e615844f46079129ea7b3fdc9f442f4 [file] [log] [blame]
Eli Friedmancd7a0992012-02-28 03:32:48 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks -fobjc-arc | 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
Stephen Hines176edba2014-12-01 14:53:08 -08007// MRC: @OBJC_METH_VAR_NAME{{.*}} = private global [5 x i8] c"copy\00"
8// MRC: @OBJC_METH_VAR_NAME{{.*}} = private global [12 x i8] c"autorelease\00"
Stephen Lin93ab6bf2013-08-15 06:47:53 +00009// MRC-LABEL: define i32 ()* @_Z1fv(
10// MRC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
Eli Friedmancd7a0992012-02-28 03:32:48 +000011// MRC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000012// MRC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070013// MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
14// MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
Eli Friedmancd7a0992012-02-28 03:32:48 +000015// MRC: ret i32 ()*
16
Stephen Lin93ab6bf2013-08-15 06:47:53 +000017// ARC-LABEL: define i32 ()* @_Z1fv(
18// ARC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
Eli Friedmancd7a0992012-02-28 03:32:48 +000019// ARC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000020// ARC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
Eli Friedmancd7a0992012-02-28 03:32:48 +000021// ARC: call i8* @objc_retainBlock
22// ARC: call i8* @objc_autoreleaseReturnValue
Eli Friedman23f02672012-03-01 04:01:32 +000023
24typedef int (^fp)();
25fp global;
26void f2() { global = []{ return 3; }; }
27
Bill Wendling89924572013-02-27 00:06:04 +000028// MRC: define void @_Z2f2v() [[NUW:#[0-9]+]] {
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000029// MRC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
Eli Friedman23f02672012-03-01 04:01:32 +000030// MRC-NOT: call
31// MRC: ret void
32// ("global" contains a dangling pointer after this function runs.)
33
Bill Wendling89924572013-02-27 00:06:04 +000034// ARC: define void @_Z2f2v() [[NUW:#[0-9]+]] {
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000035// ARC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
Eli Friedman23f02672012-03-01 04:01:32 +000036// ARC: call i8* @objc_retainBlock
37// ARC: call void @objc_release
Stephen Lin93ab6bf2013-08-15 06:47:53 +000038// ARC-LABEL: define internal i32 @___Z2f2v_block_invoke
Eli Friedman23f02672012-03-01 04:01:32 +000039// ARC: call i32 @"_ZZ2f2vENK3$_1clEv
Bill Wendlingf7a9da02013-02-20 07:22:19 +000040
John McCallf5ebf9b2013-05-03 07:33:41 +000041template <class T> void take_lambda(T &&lambda) { lambda(); }
42void take_block(void (^block)()) { block(); }
43
44// rdar://13800041
45@interface A
46- (void) test;
47@end
48@interface B : A @end
49@implementation B
50- (void) test {
51 take_block(^{
52 take_lambda([=]{
53 take_block(^{
54 take_lambda([=] {
55 [super test];
56 });
57 });
58 });
59 });
60}
61@end
62
Stephen Hines0e2c34f2015-03-23 12:09:02 -070063// ARC-LABEL: define linkonce_odr i32 ()* @_ZZNK13StaticMembersIfE1fMUlvE_clEvENKUlvE_cvU13block_pointerFivEEv
64
Eli Friedmanc1b8d092013-07-12 22:05:26 +000065// Check lines for BlockInLambda test below
Stephen Lin93ab6bf2013-08-15 06:47:53 +000066// ARC-LABEL: define internal i32 @___ZZN13BlockInLambda1X1fEvENKUlvE_clEv_block_invoke
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070067// ARC: [[Y:%.*]] = getelementptr inbounds %"struct.BlockInLambda::X", %"struct.BlockInLambda::X"* {{.*}}, i32 0, i32 1
68// ARC-NEXT: [[YVAL:%.*]] = load i32, i32* [[Y]], align 4
Eli Friedmanc1b8d092013-07-12 22:05:26 +000069// ARC-NEXT: ret i32 [[YVAL]]
70
Eli Friedman95099ef2013-06-13 20:56:27 +000071typedef int (^fptr)();
72template<typename T> struct StaticMembers {
73 static fptr f;
74};
75template<typename T>
76fptr StaticMembers<T>::f = [] { auto f = []{return 5;}; return fptr(f); }();
77template fptr StaticMembers<float>::f;
Bill Wendlingf7a9da02013-02-20 07:22:19 +000078
Eli Friedmanc1b8d092013-07-12 22:05:26 +000079namespace BlockInLambda {
80 struct X {
81 int x,y;
82 void f() {
83 [this]{return ^{return y;}();}();
84 };
85 };
86 void g(X& x) {
87 x.f();
88 };
89}
90
Eli Friedman95099ef2013-06-13 20:56:27 +000091
92// ARC: attributes [[NUW]] = { nounwind{{.*}} }
Bill Wendling89924572013-02-27 00:06:04 +000093// MRC: attributes [[NUW]] = { nounwind{{.*}} }