block-literal.c revision 397195bf3077fb42789b326f69f7d417227a0588
1// RUN: clang-cc -fsyntax-only %s -verify -fblocks
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
24	return ^{printf("\nClosure\n"); };
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:
42	takeclosure(^{ x = 4; });  // expected-error {{variable is not assignable (missing __block type specifier)}}
43  __block y = 7; // expected-warning {{type specifier missing, defaults to 'int'}}
44  takeclosure(^{ y = 8; });
45}
46
47
48void (^test3())(void) {
49  return ^{};
50}
51
52void test4() {
53  void (^noop)(void) = ^{};
54  void (*noop2)() = 0;
55}
56
57void myfunc(int (^block)(int)) {}
58
59void myfunc3(const int *x);
60
61void test5() {
62    int a;
63
64    myfunc(^(int abcd) {
65        myfunc3(&a);
66        return 1;
67    });
68}
69
70void *X;
71
72void test_arguments() {
73  int y;
74  int (^c)(char);
75  (1 ? c : 0)('x');
76  (1 ? 0 : c)('x');
77
78  (1 ? c : c)('x');
79}
80
81static int global_x = 10;
82void (^global_block)(void) = ^{ printf("global x is %d\n", global_x); };
83
84typedef void (^void_block_t)(void);
85
86static const void_block_t myBlock = ^{ };
87
88static const void_block_t myBlock2 = ^ void(void) { };
89
90#if 0
91// Old syntax. FIXME: convert/test.
92void test_byref() {
93  int i;
94
95  X = ^{| g |};  // error {{use of undeclared identifier 'g'}}
96
97  X = ^{| i,i,i | };
98
99  X = ^{|i| i = 0; };
100
101}
102
103// TODO: global closures someday.
104void *A = ^{};
105void *B = ^(int){ A = 0; };
106
107
108// Closures can not take return types at this point.
109void test_retvals() {
110  // Explicit return value.
111  ^int{};   // error {{closure with explicit return type requires argument list}}
112  X = ^void(){};
113
114  // Optional specification of return type.
115  X = ^char{ return 'x'; };  // error {{closure with explicit return type requires argument list}}
116
117  X = ^/*missing declspec*/ *() { return (void*)0; };
118  X = ^void*() { return (void*)0; };
119
120  //X = ^char(short c){ if (c) return c; else return (int)4; };
121
122}
123
124#endif
125