Debian Codegraph @

clang/test/Sema/block-misc.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -Wno-strict-prototypes -verify %s -fblocks
2 void donotwarn(void);
3
4 int (^IFP) ();
5 int (^II) (int);
6 int test1(void) {
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
31 int 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
44 int^ x; // expected-error {{block pointer to non-function type is invalid}}
45 int^^ x1; // expected-error {{block pointer to non-function type is invalid}} expected-error {{block pointer to non-function type is invalid}}
46
47 void test3(void) {
48 char *^ y; // expected-error {{block pointer to non-function type is invalid}}
49 }
50
51
52
53 enum {NSBIRLazilyAllocated = 0};
54
55 int test4(int argc) {
56 ^{
57 switch (argc) {
58 case NSBIRLazilyAllocated: // is an integer constant expression.
59 default:
60 break;
61 }
62 }();
63 return 0;
64 }
65
66
67 void bar(void*);
68 static int test5g;
69 void test5() {
70 bar(^{ test5g = 1; });
71 }
72
73 const char*test6(void) {
74 return ^{
75 return __func__;
76 } ();
77 }
78
79 void (^test7a)();
80 int test7(void (^p)()) {
81 return test7a == p;
82 }
83
84
85 void test8(void) {
86 somelabel:
87 ^{ goto somelabel; }(); // expected-error {{use of undeclared label 'somelabel'}}
88 }
89
90 void test9(void) {
91 goto somelabel; // expected-error {{use of undeclared label 'somelabel'}}
92 ^{ somelabel: ; }();
93 }
94
95 void test10(int i) {
96 switch (i) {
97 case 41: ;
98 ^{ case 42: ; }(); // expected-error {{'case' statement not in switch statement}}
99 }
100 }
101
102 void test11(int i) {
103 switch (i) {
104 case 41: ;
105 ^{ break; }(); // expected-error {{'break' statement not in loop or switch statement}}
106 }
107
108 for (; i < 100; ++i)
109 ^{ break; }(); // expected-error {{'break' statement not in loop or switch statement}}
110 }
111
112 void (^test12f)(void);
113 void test12() {
114 test12f = ^test12f; // expected-error {{type name requires a specifier or qualifier}} expected-error {{expected expression}}
115 }
116
117 void *test13 = ^{
118 int X = 32;
119
120 void *P = ^{
121 return X+4; // References outer block's "X", so outer block is constant.
122 };
123 };
124
125 void test14(void) {
126 int X = 32;
127 static void *P = ^{ // expected-error {{initializer element is not a compile-time constant}}
128
129 void *Q = ^{
130 // References test14's "X": outer block is non-constant.
131 return X+4;
132 };
133 };
134 }
135
136 enum { LESS };
137
138 void foo(long (^comp)()) { // expected-note{{passing argument to parameter 'comp' here}}
139 }
140
141 void (^test15f)(void);
142 void test15(void) {
143 foo(^{ return LESS; }); // expected-error {{incompatible block pointer types passing 'int (^)(void)' to parameter of type 'long (^)()'}}
144 }
145
146 __block int test16i; // expected-error {{__block attribute not allowed, only allowed on local variables}}
147
148 void test16(__block int i) { // expected-error {{__block attribute not allowed, only allowed on local variables}}
149 int size = 5;
150 extern __block double extern_var; // expected-error {{__block attribute not allowed, only allowed on local variables}}
151 static __block char * pch; // expected-error {{__block attribute not allowed, only allowed on local variables}}
152 __block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
153 __block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
154 }
155
156 void f();
157
158 void test17(void) {
159 void (^bp)(int);
160 void (*rp)(int);
161 void (^bp1)();
162 void *vp = bp;
163
164 f(1 ? bp : vp);
165 f(1 ? vp : bp);
166 f(1 ? bp : bp1);
167 (void)(bp > rp); // expected-error {{invalid operands}}
168 (void)(bp > 0); // expected-error {{invalid operands}}
169 (void)(bp > bp); // expected-error {{invalid operands}}
170 (void)(bp > vp); // expected-error {{invalid operands}}
171 f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}}
172 (void)(bp == 1); // expected-error {{invalid operands to binary expression}}
173 (void)(bp == 0);
174 (void)(1 == bp); // expected-error {{invalid operands to binary expression}}
175 (void)(0 == bp);
176 (void)(bp < 1); // expected-error {{invalid operands to binary expression}}
177 (void)(bp < 0); // expected-error {{invalid operands to binary expression}}
178 (void)(1 < bp); // expected-error {{invalid operands to binary expression}}
179 (void)(0 < bp); // expected-error {{invalid operands to binary expression}}
180 }
181
182 void test18(void) {
183 void (^const blockA)(void) = ^{ }; // expected-note {{variable 'blockA' declared const here}}
184 blockA = ^{ }; // expected-error {{cannot assign to variable 'blockA' with const-qualified type 'void (^const)(void)}}
185 }
186
187 int test19(void) {
188 goto L0; // expected-error {{cannot jump}}
189
190 __block int x; // expected-note {{jump bypasses setup of __block variable}}
191 L0:
192 x = 0;
193 ^(){ ++x; }();
194 return x;
195 }
196
197 void test20(void) {
198 int n = 7;
199 int vla[n]; // expected-note {{declared here}}
200 int (*vm)[n] = 0; // expected-note {{declared here}}
201 vla[1] = 4341;
202 ^{
203 (void)vla[1]; // expected-error {{cannot refer to declaration with a variably modified type inside block}}
204 (void)(vm+1); // expected-error {{cannot refer to declaration with a variably modified type inside block}}
205 }();
206 }
207
208 void test21(void) {
209 int a[7]; // expected-note {{declared here}}
210 __block int b[10]; // expected-note {{declared here}}
211 a[1] = 1;
212 ^{
213 (void)a[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
214 (void)b[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
215 }();
216 }
217
218 const char * (^func)(void) = ^{ return __func__; };
219 const char * (^function)(void) = ^{ return __FUNCTION__; };
220 const char * (^pretty)(void) = ^{ return __PRETTY_FUNCTION__; };