blob: 2dc458124319be83088e9f94db046b4f993ea7e7 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only %s -verify -fblocks
Steve Naroff61f40a22008-09-10 19:17:48 +00002
3void I( void (^)(void));
4void (^noop)(void);
5
6void nothing();
7int printf(const char*, ...);
8
9typedef void (^T) (void);
10
11void takeclosure(T);
12int takeintint(int (^C)(int)) { return C(4); }
13
14T somefunction() {
15 if (^{ })
16 nothing();
17
18 noop = ^{};
19
20 noop = ^{printf("\nClosure\n"); };
21
22 I(^{ });
23
Steve Naroff61f40a22008-09-10 19:17:48 +000024 return ^{printf("\nClosure\n"); }; // expected-error {{returning block that lives on the local stack}}
25}
26void test2() {
27 int x = 4;
28
29 takeclosure(^{ printf("%d\n", x); });
30
31 while (1) {
32 takeclosure(^{
33 break; // expected-error {{'break' statement not in loop or switch statement}}
34 continue; // expected-error {{'continue' statement not in loop statement}}
35 while(1) break; // ok
36 goto foo; // expected-error {{goto not allowed}}
37 });
38 break;
39 }
40
41foo:
Eli Friedman04831aa2009-03-22 23:26:56 +000042 takeclosure(^{ x = 4; }); // expected-error {{variable is not assignable (missing __block type specifier)}}
Chris Lattner35d276f2009-02-27 18:53:28 +000043 __block y = 7; // expected-warning {{type specifier missing, defaults to 'int'}}
Steve Naroff4f6a7d72008-09-26 14:41:28 +000044 takeclosure(^{ y = 8; });
Steve Naroff61f40a22008-09-10 19:17:48 +000045}
46
47
48void (^test3())(void) {
49 return ^{}; // expected-error {{returning block that lives on the local stack}}
50}
51
52void test4() {
53 void (^noop)(void) = ^{};
54 void (*noop2)() = 0;
55}
56
Steve Naroff4f6a7d72008-09-26 14:41:28 +000057void myfunc(int (^block)(int)) {}
58
Eli Friedman5fdeae12009-03-22 23:00:19 +000059void myfunc3(const int *x);
Steve Naroff4f6a7d72008-09-26 14:41:28 +000060
61void test5() {
62 int a;
63
64 myfunc(^(int abcd) {
65 myfunc3(&a);
66 return 1;
67 });
68}
69
Steve Naroff61f40a22008-09-10 19:17:48 +000070void *X;
71
72void test_arguments() {
Steve Naroff61f40a22008-09-10 19:17:48 +000073 int y;
Steve Naroff61f40a22008-09-10 19:17:48 +000074 int (^c)(char);
75 (1 ? c : 0)('x');
76 (1 ? 0 : c)('x');
77
78 (1 ? c : c)('x');
79}
80
Steve Naroff8af6a452008-10-02 17:12:56 +000081static int global_x = 10;
82void (^global_block)(void) = ^{ printf("global x is %d\n", global_x); };
83
Steve Naroff61f40a22008-09-10 19:17:48 +000084#if 0
85// Old syntax. FIXME: convert/test.
86void test_byref() {
87 int i;
88
Chris Lattner966f0992008-11-21 01:05:04 +000089 X = ^{| g |}; // error {{use of undeclared identifier 'g'}}
Steve Naroff61f40a22008-09-10 19:17:48 +000090
91 X = ^{| i,i,i | };
92
93 X = ^{|i| i = 0; };
94
95}
96
97// TODO: global closures someday.
98void *A = ^{};
99void *B = ^(int){ A = 0; };
100
101
102// Closures can not take return types at this point.
103void test_retvals() {
104 // Explicit return value.
Chris Lattner966f0992008-11-21 01:05:04 +0000105 ^int{}; // error {{closure with explicit return type requires argument list}}
Steve Naroff61f40a22008-09-10 19:17:48 +0000106 X = ^void(){};
107
108 // Optional specification of return type.
Chris Lattner966f0992008-11-21 01:05:04 +0000109 X = ^char{ return 'x'; }; // error {{closure with explicit return type requires argument list}}
Steve Naroff61f40a22008-09-10 19:17:48 +0000110
111 X = ^/*missing declspec*/ *() { return (void*)0; };
112 X = ^void*() { return (void*)0; };
113
114 //X = ^char(short c){ if (c) return c; else return (int)4; };
115
116}
117
118#endif