blob: 3a091395d04a3686dbb0596978091b01ca2c7f82 [file] [log] [blame]
Steve Naroff3eac7692008-09-10 19:17:48 +00001// RUN: clang -fsyntax-only %s -verify
2
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 Naroff3eac7692008-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:
Steve Naroff076d6cb2008-09-26 14:41:28 +000042 takeclosure(^{ x = 4; }); // expected-error {{variable is not assignable (missing __block type specifier)}}
43 __block y = 7;
44 takeclosure(^{ y = 8; });
Steve Naroff3eac7692008-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 Naroff076d6cb2008-09-26 14:41:28 +000057void myfunc(int (^block)(int)) {}
58
59void myfunc3(int *x);
60
61void test5() {
62 int a;
63
64 myfunc(^(int abcd) {
65 myfunc3(&a);
66 return 1;
67 });
68}
69
Steve Naroff3eac7692008-09-10 19:17:48 +000070void *X;
71
72void test_arguments() {
Steve Naroff3eac7692008-09-10 19:17:48 +000073 int y;
Steve Naroff3eac7692008-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
81#if 0
82// Old syntax. FIXME: convert/test.
83void test_byref() {
84 int i;
85
86 X = ^{| g |}; // expected-error {{use of undeclared identifier 'g'}}
87
88 X = ^{| i,i,i | };
89
90 X = ^{|i| i = 0; };
91
92}
93
94// TODO: global closures someday.
95void *A = ^{};
96void *B = ^(int){ A = 0; };
97
98
99// Closures can not take return types at this point.
100void test_retvals() {
101 // Explicit return value.
102 ^int{}; // expected-error {{closure with explicit return type requires argument list}}
103 X = ^void(){};
104
105 // Optional specification of return type.
106 X = ^char{ return 'x'; }; // expected-error {{closure with explicit return type requires argument list}}
107
108 X = ^/*missing declspec*/ *() { return (void*)0; };
109 X = ^void*() { return (void*)0; };
110
111 //X = ^char(short c){ if (c) return c; else return (int)4; };
112
113}
114
115#endif