Douglas Gregor | 6cda3e6 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fblocks |
Douglas Gregor | b65f242 | 2008-12-01 19:48:06 +0000 | [diff] [blame] | 2 | |
| 3 | void tovoid(void*); |
| 4 | |
| 5 | void tovoid_test(int (^f)(int, int)) { |
| 6 | tovoid(f); |
| 7 | } |
Anders Carlsson | 5e57831 | 2009-05-26 02:03:20 +0000 | [diff] [blame] | 8 | |
| 9 | void reference_lvalue_test(int& (^f)()) { |
| 10 | f() = 10; |
| 11 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 12 | |
| 13 | // PR 7165 |
| 14 | namespace test1 { |
| 15 | void g(void (^)()); |
| 16 | struct Foo { |
| 17 | void foo(); |
| 18 | void test() { |
| 19 | (void) ^{ foo(); }; |
| 20 | } |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | namespace test2 { |
| 25 | int repeat(int value, int (^block)(int), unsigned n) { |
| 26 | while (n--) value = block(value); |
| 27 | return value; |
| 28 | } |
| 29 | |
| 30 | class Power { |
| 31 | int base; |
| 32 | |
| 33 | public: |
| 34 | Power(int base) : base(base) {} |
| 35 | int calculate(unsigned n) { |
| 36 | return repeat(1, ^(int v) { return v * base; }, n); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | int test() { |
| 41 | return Power(2).calculate(10); |
| 42 | } |
| 43 | } |
Fariborz Jahanian | 7925561 | 2010-09-03 21:36:02 +0000 | [diff] [blame] | 44 | |
| 45 | // rdar: // 8382559 |
| 46 | namespace radar8382559 { |
| 47 | void func(bool& outHasProperty); |
| 48 | |
| 49 | int test3() { |
| 50 | __attribute__((__blocks__(byref))) bool hasProperty = false; |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 51 | bool has = true; |
| 52 | |
Fariborz Jahanian | 7925561 | 2010-09-03 21:36:02 +0000 | [diff] [blame] | 53 | bool (^b)() = ^ { |
| 54 | func(hasProperty); |
| 55 | if (hasProperty) |
| 56 | hasProperty = 0; |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 57 | if (has) |
| 58 | hasProperty = 1; |
Fariborz Jahanian | 7925561 | 2010-09-03 21:36:02 +0000 | [diff] [blame] | 59 | return hasProperty; |
| 60 | }; |
| 61 | func(hasProperty); |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 62 | func(has); |
Fariborz Jahanian | 7925561 | 2010-09-03 21:36:02 +0000 | [diff] [blame] | 63 | b(); |
| 64 | if (hasProperty) |
| 65 | hasProperty = 1; |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 66 | if (has) |
| 67 | has = 2; |
Fariborz Jahanian | 7925561 | 2010-09-03 21:36:02 +0000 | [diff] [blame] | 68 | return hasProperty = 1; |
| 69 | } |
| 70 | } |
Douglas Gregor | 6cda3e6 | 2013-03-07 22:38:24 +0000 | [diff] [blame] | 71 | |
| 72 | // Move __block variables to the heap when possible. |
| 73 | class MoveOnly { |
| 74 | public: |
| 75 | MoveOnly(); |
| 76 | MoveOnly(const MoveOnly&) = delete; |
| 77 | MoveOnly(MoveOnly&&); |
| 78 | }; |
| 79 | |
| 80 | void move_block() { |
| 81 | __block MoveOnly mo; |
| 82 | } |
| 83 | |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 84 | // Don't crash after failing to build a block due to a capture of an |
| 85 | // invalid declaration. |
| 86 | namespace test5 { |
| 87 | struct B { // expected-note 2 {{candidate constructor}} |
| 88 | void *p; |
| 89 | B(int); // expected-note {{candidate constructor}} |
| 90 | }; |
| 91 | |
| 92 | void use_block(void (^)()); |
| 93 | void use_block_2(void (^)(), const B &a); |
| 94 | |
| 95 | void test() { |
| 96 | B x; // expected-error {{no matching constructor for initialization}} |
| 97 | use_block(^{ |
| 98 | int y; |
| 99 | use_block_2(^{ (void) y; }, x); |
| 100 | }); |
| 101 | } |
| 102 | } |