block-misc.c revision e030358cc06e1cbce3c2e00ca67c946f9164b2a8
1// RUN: %clang_cc1 -fsyntax-only -verify %s -fblocks
2void donotwarn();
3
4int (^IFP) ();
5int (^II) (int);
6int test1() {
7  int (^PFR) (int) = 0; // OK
8  PFR = II;             // OK
9
10  if (PFR == II)        // OK
11    donotwarn();
12
13  if (PFR == IFP)       // OK
14    donotwarn();
15
16  if (PFR == (int (^) (int))IFP) // OK
17    donotwarn();
18
19  if (PFR == 0)         // OK
20    donotwarn();
21
22  if (PFR)              // OK
23    donotwarn();
24
25  if (!PFR)             // OK
26    donotwarn();
27
28  return PFR != IFP;    // OK
29}
30
31int test2(double (^S)()) {
32  double (^I)(int)  = (void*) S;
33  (void*)I = (void *)S; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
34
35  void *pv = I;
36
37  pv = S;
38
39  I(1);
40
41  return (void*)I == (void *)S;
42}
43
44int^ x; // expected-error {{block pointer to non-function type is invalid}}
45int^^ x1; // expected-error {{block pointer to non-function type is invalid}} expected-error {{block pointer to non-function type is invalid}}
46
47void test3() {
48  char *^ y; // expected-error {{block pointer to non-function type is invalid}}
49}
50
51
52
53enum {NSBIRLazilyAllocated = 0};
54
55int test4(int argc) {  // rdar://6251437
56  ^{
57    switch (argc) {
58      case NSBIRLazilyAllocated:  // is an integer constant expression.
59      default:
60        break;
61    }
62  }();
63  return 0;
64}
65
66
67void bar(void*);
68// rdar://6257721 - reference to static/global is byref by default.
69static int test5g;
70void test5() {
71  bar(^{ test5g = 1; });
72}
73
74// rdar://6405429 - __func__ in a block refers to the containing function name.
75const char*test6() {
76  return ^{
77    return __func__;
78  } ();
79}
80
81// radr://6732116 - block comparisons
82void (^test7a)();
83int test7(void (^p)()) {
84  return test7a == p;
85}
86
87
88void test8() {
89somelabel:
90 // FIXME: This should say "jump out of block not legal" when gotos are allowed.
91  ^{ goto somelabel; }();   // expected-error {{goto not allowed in block literal}}
92}
93
94void test9() {
95  goto somelabel;       // expected-error {{use of undeclared label 'somelabel'}}
96  ^{ somelabel: ; }();
97}
98
99void test10(int i) {
100  switch (i) {
101  case 41: ;
102  ^{ case 42: ; }();     // expected-error {{'case' statement not in switch statement}}
103  }
104}
105
106void test11(int i) {
107  switch (i) {
108  case 41: ;
109    ^{ break; }();     // expected-error {{'break' statement not in loop or switch statement}}
110  }
111
112  for (; i < 100; ++i)
113    ^{ break; }();     // expected-error {{'break' statement not in loop or switch statement}}
114}
115
116void (^test12f)(void);
117void test12() {
118  test12f = ^test12f;  // expected-error {{type name requires a specifier or qualifier}} expected-error {{expected expression}}
119}
120
121// rdar://6808730
122void *test13 = ^{
123  int X = 32;
124
125  void *P = ^{
126    return X+4;  // References outer block's "X", so outer block is constant.
127  };
128};
129
130void test14() {
131  int X = 32;
132  static void *P = ^{  // expected-error {{initializer element is not a compile-time constant}}
133
134    void *Q = ^{
135      // References test14's "X": outer block is non constant.
136      return X+4;
137    };
138  };
139}
140
141enum { LESS };
142
143void foo(long (^comp)()) {
144}
145
146void (^test15f)(void);
147void test15() {
148  foo(^{ return LESS; }); // expected-error {{incompatible block pointer types passing 'int (^)(void)', expected 'long (^)()'}}
149}
150
151__block int test16i;  // expected-error {{__block attribute not allowed, only allowed on local variables}}
152
153void test16(__block int i) { // expected-error {{__block attribute not allowed, only allowed on local variables}}
154  int size = 5;
155  extern __block double extern_var; // expected-error {{__block attribute not allowed, only allowed on local variables}}
156  static __block char * pch; // expected-error {{__block attribute not allowed, only allowed on local variables}}
157  __block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
158  __block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
159}
160
161void f();
162
163void test17() {
164  void (^bp)(int);
165  void (*rp)(int);
166  void (^bp1)();
167  void *vp = bp;
168
169  f(1 ? bp : vp);
170  f(1 ? vp : bp);
171  f(1 ? bp : bp1);
172  (void)(bp > rp); // expected-error {{invalid operands}}
173  (void)(bp > 0); // expected-error {{invalid operands}}
174  (void)(bp > bp); // expected-error {{invalid operands}}
175  (void)(bp > vp); // expected-error {{invalid operands}}
176  f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}}
177  (void)(bp == 1); // expected-error {{invalid operands to binary expression}}
178  (void)(bp == 0);
179  (void)(1 == bp); // expected-error {{invalid operands to binary expression}}
180  (void)(0 == bp);
181  (void)(bp < 1); // expected-error {{invalid operands to binary expression}}
182  (void)(bp < 0); // expected-error {{invalid operands to binary expression}}
183  (void)(1 < bp); // expected-error {{invalid operands to binary expression}}
184  (void)(0 < bp); // expected-error {{invalid operands to binary expression}}
185}
186
187void test18() {
188  void (^const  blockA)(void) = ^{ };
189  blockA = ^{ }; // expected-error {{read-only variable is not assignable}}
190}
191
192// rdar://7072507
193int test19() {
194  goto L0;       // expected-error {{illegal goto into protected scope}}
195
196  __block int x; // expected-note {{jump bypasses setup of __block variable}}
197L0:
198  x = 0;
199  ^(){ ++x; }();
200  return x;
201}
202
203// radr://7438948
204void test20() {
205  int n = 7;
206  int vla[n]; // expected-note {{declared at}}
207  int (*vm)[n] = 0; // expected-note {{declared at}}
208  vla[1] = 4341;
209  ^{
210    (void)vla[1];  // expected-error {{cannot refer to declaration with a variably modified type inside block}}
211    (void)(vm+1);  // expected-error {{cannot refer to declaration with a variably modified type inside block}}
212  }();
213}
214
215// radr://7438948
216void test21() {
217  int a[7]; // expected-note {{declared at}}
218  a[1] = 1;
219  ^{
220    (void)a[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
221  }();
222}
223