blob: a635e998d92fe4ebd0cb53c26416a62bb8aef02e [file] [log] [blame]
Douglas Gregorca006452013-03-07 22:38:24 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fblocks
Andy Gibbsc6e68da2012-10-19 12:44:48 +00002// expected-no-diagnostics
Douglas Gregore9122662008-12-01 19:48:06 +00003
4void tovoid(void*);
5
6void tovoid_test(int (^f)(int, int)) {
7 tovoid(f);
8}
Anders Carlsson3b70b302009-05-26 02:03:20 +00009
10void reference_lvalue_test(int& (^f)()) {
11 f() = 10;
12}
John McCall87fe5d52010-05-20 01:18:31 +000013
14// PR 7165
15namespace test1 {
16 void g(void (^)());
17 struct Foo {
18 void foo();
19 void test() {
20 (void) ^{ foo(); };
21 }
22 };
23}
24
25namespace test2 {
26 int repeat(int value, int (^block)(int), unsigned n) {
27 while (n--) value = block(value);
28 return value;
29 }
30
31 class Power {
32 int base;
33
34 public:
35 Power(int base) : base(base) {}
36 int calculate(unsigned n) {
37 return repeat(1, ^(int v) { return v * base; }, n);
38 }
39 };
40
41 int test() {
42 return Power(2).calculate(10);
43 }
44}
Fariborz Jahanian9d798d12010-09-03 21:36:02 +000045
46// rdar: // 8382559
47namespace radar8382559 {
48 void func(bool& outHasProperty);
49
50 int test3() {
51 __attribute__((__blocks__(byref))) bool hasProperty = false;
Fariborz Jahanian087206d2010-09-03 23:07:53 +000052 bool has = true;
53
Fariborz Jahanian9d798d12010-09-03 21:36:02 +000054 bool (^b)() = ^ {
55 func(hasProperty);
56 if (hasProperty)
57 hasProperty = 0;
Fariborz Jahanian087206d2010-09-03 23:07:53 +000058 if (has)
59 hasProperty = 1;
Fariborz Jahanian9d798d12010-09-03 21:36:02 +000060 return hasProperty;
61 };
62 func(hasProperty);
Fariborz Jahanian087206d2010-09-03 23:07:53 +000063 func(has);
Fariborz Jahanian9d798d12010-09-03 21:36:02 +000064 b();
65 if (hasProperty)
66 hasProperty = 1;
Fariborz Jahanian087206d2010-09-03 23:07:53 +000067 if (has)
68 has = 2;
Fariborz Jahanian9d798d12010-09-03 21:36:02 +000069 return hasProperty = 1;
70 }
71}
Douglas Gregorca006452013-03-07 22:38:24 +000072
73// Move __block variables to the heap when possible.
74class MoveOnly {
75public:
76 MoveOnly();
77 MoveOnly(const MoveOnly&) = delete;
78 MoveOnly(MoveOnly&&);
79};
80
81void move_block() {
82 __block MoveOnly mo;
83}
84