blob: 350f7a3bafb814e8eb9aaf30c499703c28809932 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 %s -emit-llvm -o %t -fblocks
Daniel Dunbar4fcfde42009-11-08 01:45:36 +00002// RUN: grep "_Block_object_dispose" %t | count 17
John McCall6b5a61b2011-02-07 10:33:21 +00003// RUN: grep "__copy_helper_block_" %t | count 14
4// RUN: grep "__destroy_helper_block_" %t | count 14
Fariborz Jahanian830937b2010-12-02 17:02:11 +00005// RUN: grep "__Block_byref_object_copy_" %t | count 2
6// RUN: grep "__Block_byref_object_dispose_" %t | count 2
Daniel Dunbar4fcfde42009-11-08 01:45:36 +00007// RUN: grep "i32 135)" %t | count 2
Mike Stumpb7477cf2009-04-10 18:52:28 +00008// RUN: grep "_Block_object_assign" %t | count 10
Mike Stump80bd2062009-03-06 06:35:21 +00009
Daniel Dunbar23afaad2009-11-17 08:57:36 +000010int printf(const char *, ...);
Mike Stump58919e12009-03-04 13:17:22 +000011
Mike Stump58a85142009-03-04 22:48:06 +000012void test1() {
13 __block int a;
14 int b=2;
15 a=1;
16 printf("a is %d, b is %d\n", a, b);
John McCall6b5a61b2011-02-07 10:33:21 +000017 ^{ a = 10; printf("a is %d, b is %d\n", a, b); }(); // needs copy/dispose
Mike Stump58a85142009-03-04 22:48:06 +000018 printf("a is %d, b is %d\n", a, b);
19 a = 1;
20 printf("a is %d, b is %d\n", a, b);
21}
22
Mike Stump58a85142009-03-04 22:48:06 +000023void test2() {
24 __block int a;
25 a=1;
26 printf("a is %d\n", a);
John McCall6b5a61b2011-02-07 10:33:21 +000027 ^{ // needs copy/dispose
28 ^{ // needs copy/dispose
Mike Stump58a85142009-03-04 22:48:06 +000029 a = 10;
30 }();
31 }();
32 printf("a is %d\n", a);
33 a = 1;
34 printf("a is %d\n", a);
35}
36
Mike Stumpa4f668f2009-03-06 01:33:24 +000037void test3() {
Mike Stump45031c02009-03-06 02:29:21 +000038 __block int k;
Mike Stumpa4f668f2009-03-06 01:33:24 +000039 __block int (^j)(int);
John McCall6b5a61b2011-02-07 10:33:21 +000040 ^{j=0; k=0;}(); // needs copy/dispose
Mike Stumpa4f668f2009-03-06 01:33:24 +000041}
42
Mike Stump7f28a9c2009-03-13 23:34:28 +000043int test4() {
44 extern int g;
45 static int i = 1;
John McCall6b5a61b2011-02-07 10:33:21 +000046 ^(int j){ i = j; g = 0; }(0); // does not need copy/dispose
Mike Stump7f28a9c2009-03-13 23:34:28 +000047 return i + g;
48}
49
50int g;
51
Mike Stumpf8575aa2009-03-18 15:54:29 +000052void test5() {
53 __block struct { int i; } i;
John McCall6b5a61b2011-02-07 10:33:21 +000054 ^{ (void)i; }(); // needs copy/dispose
Mike Stumpf8575aa2009-03-18 15:54:29 +000055}
56
Mike Stumpa8b60c92009-03-21 21:00:35 +000057void test6() {
58 __block int i;
John McCall6b5a61b2011-02-07 10:33:21 +000059 ^{ i=1; }(); // needs copy/dispose
60 ^{}(); // does not need copy/dispose
Mike Stumpb7477cf2009-04-10 18:52:28 +000061}
62
63void test7() {
John McCall6b5a61b2011-02-07 10:33:21 +000064 ^{ // does not need copy/dispose
Mike Stumpb7477cf2009-04-10 18:52:28 +000065 __block int i;
John McCall6b5a61b2011-02-07 10:33:21 +000066 ^{ i = 1; }(); // needs copy/dispose
Mike Stumpb7477cf2009-04-10 18:52:28 +000067 }();
Mike Stumpa8b60c92009-03-21 21:00:35 +000068}
69
Mike Stump58919e12009-03-04 13:17:22 +000070int main() {
Mike Stump7f28a9c2009-03-13 23:34:28 +000071 int rv = 0;
Mike Stump58a85142009-03-04 22:48:06 +000072 test1();
73 test2();
Mike Stumpa4f668f2009-03-06 01:33:24 +000074 test3();
Mike Stump7f28a9c2009-03-13 23:34:28 +000075 rv += test4();
Mike Stumpf8575aa2009-03-18 15:54:29 +000076 test5();
Mike Stump7f28a9c2009-03-13 23:34:28 +000077 return rv;
Mike Stump58919e12009-03-04 13:17:22 +000078}