Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -emit-llvm -o %t -fblocks |
Daniel Dunbar | 8b57697 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 2 | // RUN: grep "_Block_object_dispose" %t | count 17 |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3 | // RUN: grep "__copy_helper_block_" %t | count 14 |
| 4 | // RUN: grep "__destroy_helper_block_" %t | count 14 |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 5 | // RUN: grep "__Block_byref_object_copy_" %t | count 2 |
| 6 | // RUN: grep "__Block_byref_object_dispose_" %t | count 2 |
Daniel Dunbar | 8b57697 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 7 | // RUN: grep "i32 135)" %t | count 2 |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 8 | // RUN: grep "_Block_object_assign" %t | count 10 |
Mike Stump | cfc045e | 2009-03-06 06:35:21 +0000 | [diff] [blame] | 9 | |
Daniel Dunbar | feedba6 | 2009-11-17 08:57:36 +0000 | [diff] [blame] | 10 | int printf(const char *, ...); |
Mike Stump | 0874160 | 2009-03-04 13:17:22 +0000 | [diff] [blame] | 11 | |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 12 | void test1() { |
| 13 | __block int a; |
| 14 | int b=2; |
| 15 | a=1; |
| 16 | printf("a is %d, b is %d\n", a, b); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 17 | ^{ a = 10; printf("a is %d, b is %d\n", a, b); }(); // needs copy/dispose |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 18 | 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 Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 23 | void test2() { |
| 24 | __block int a; |
| 25 | a=1; |
| 26 | printf("a is %d\n", a); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 27 | ^{ // needs copy/dispose |
| 28 | ^{ // needs copy/dispose |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 29 | a = 10; |
| 30 | }(); |
| 31 | }(); |
| 32 | printf("a is %d\n", a); |
| 33 | a = 1; |
| 34 | printf("a is %d\n", a); |
| 35 | } |
| 36 | |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 37 | void test3() { |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 38 | __block int k; |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 39 | __block int (^j)(int); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 40 | ^{j=0; k=0;}(); // needs copy/dispose |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 43 | int test4() { |
| 44 | extern int g; |
| 45 | static int i = 1; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 46 | ^(int j){ i = j; g = 0; }(0); // does not need copy/dispose |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 47 | return i + g; |
| 48 | } |
| 49 | |
| 50 | int g; |
| 51 | |
Mike Stump | bc7d67c | 2009-03-18 15:54:29 +0000 | [diff] [blame] | 52 | void test5() { |
| 53 | __block struct { int i; } i; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 54 | ^{ (void)i; }(); // needs copy/dispose |
Mike Stump | bc7d67c | 2009-03-18 15:54:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Mike Stump | efd7caa8 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 57 | void test6() { |
| 58 | __block int i; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 59 | ^{ i=1; }(); // needs copy/dispose |
| 60 | ^{}(); // does not need copy/dispose |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void test7() { |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 64 | ^{ // does not need copy/dispose |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 65 | __block int i; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 66 | ^{ i = 1; }(); // needs copy/dispose |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 67 | }(); |
Mike Stump | efd7caa8 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mike Stump | 0874160 | 2009-03-04 13:17:22 +0000 | [diff] [blame] | 70 | int main() { |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 71 | int rv = 0; |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 72 | test1(); |
| 73 | test2(); |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 74 | test3(); |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 75 | rv += test4(); |
Mike Stump | bc7d67c | 2009-03-18 15:54:29 +0000 | [diff] [blame] | 76 | test5(); |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 77 | return rv; |
Mike Stump | 0874160 | 2009-03-04 13:17:22 +0000 | [diff] [blame] | 78 | } |