blob: c57b3e141064f467e4689cc4fee6597d211ce8b3 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s -fblocks
Steve Naroffdd972f22008-09-05 22:11:13 +00002void donotwarn();
3
4int (^IFP) ();
5int (^II) (int);
6int test1() {
Mike Stump1eb44332009-09-09 15:08:12 +00007 int (^PFR) (int) = 0; // OK
8 PFR = II; // OK
Steve Naroffdd972f22008-09-05 22:11:13 +00009
Mike Stump1eb44332009-09-09 15:08:12 +000010 if (PFR == II) // OK
Mike Stumpaab0f7a2009-04-01 01:17:39 +000011 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000012
Mike Stump1eb44332009-09-09 15:08:12 +000013 if (PFR == IFP) // OK
Mike Stumpaab0f7a2009-04-01 01:17:39 +000014 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000015
Mike Stumpaab0f7a2009-04-01 01:17:39 +000016 if (PFR == (int (^) (int))IFP) // OK
17 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000018
Mike Stump1eb44332009-09-09 15:08:12 +000019 if (PFR == 0) // OK
Mike Stumpaab0f7a2009-04-01 01:17:39 +000020 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000021
Mike Stump1eb44332009-09-09 15:08:12 +000022 if (PFR) // OK
Mike Stumpaab0f7a2009-04-01 01:17:39 +000023 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000024
Mike Stump1eb44332009-09-09 15:08:12 +000025 if (!PFR) // OK
Mike Stumpaab0f7a2009-04-01 01:17:39 +000026 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000027
Mike Stump1eb44332009-09-09 15:08:12 +000028 return PFR != IFP; // OK
Steve Naroffdd972f22008-09-05 22:11:13 +000029}
30
31int test2(double (^S)()) {
Mike Stumpaab0f7a2009-04-01 01:17:39 +000032 double (^I)(int) = (void*) S;
Mike Stump1eb44332009-09-09 15:08:12 +000033 (void*)I = (void *)S; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
Steve Naroffdd972f22008-09-05 22:11:13 +000034
Mike Stumpaab0f7a2009-04-01 01:17:39 +000035 void *pv = I;
Steve Naroffdd972f22008-09-05 22:11:13 +000036
Mike Stump1eb44332009-09-09 15:08:12 +000037 pv = S;
Steve Naroffdd972f22008-09-05 22:11:13 +000038
Mike Stumpaab0f7a2009-04-01 01:17:39 +000039 I(1);
40
41 return (void*)I == (void *)S;
Steve Naroffdd972f22008-09-05 22:11:13 +000042}
43
44int^ x; // expected-error {{block pointer to non-function type is invalid}}
Mike Stumpbfa2ac02009-02-08 07:59:54 +000045int^^ x1; // expected-error {{block pointer to non-function type is invalid}} expected-error {{block pointer to non-function type is invalid}}
Steve Naroffdd972f22008-09-05 22:11:13 +000046
Mike Stumpd1969d82009-07-22 00:43:08 +000047void test3() {
Mike Stumpaab0f7a2009-04-01 01:17:39 +000048 char *^ y; // expected-error {{block pointer to non-function type is invalid}}
Steve Naroffdd972f22008-09-05 22:11:13 +000049}
50
Chris Lattnerf7037b12008-09-28 05:30:26 +000051
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}
Chris Lattner639e2d32008-10-20 05:16:36 +000065
66
Chris Lattnere0303582010-01-09 20:43:19 +000067void bar(void*);
Chris Lattner639e2d32008-10-20 05:16:36 +000068// rdar://6257721 - reference to static/global is byref by default.
69static int test5g;
70void test5() {
71 bar(^{ test5g = 1; });
72}
73
Chris Lattner371f2582008-12-04 23:50:19 +000074// rdar://6405429 - __func__ in a block refers to the containing function name.
75const char*test6() {
Mike Stumpaab0f7a2009-04-01 01:17:39 +000076 return ^{
77 return __func__;
78 } ();
Chris Lattner371f2582008-12-04 23:50:19 +000079}
80
Mike Stumpaab0f7a2009-04-01 01:17:39 +000081// radr://6732116 - block comparisons
Chris Lattner5718a352009-04-18 19:32:54 +000082void (^test7a)();
83int test7(void (^p)()) {
84 return test7a == p;
Mike Stumpaab0f7a2009-04-01 01:17:39 +000085}
Chris Lattner5718a352009-04-18 19:32:54 +000086
87
88void test8() {
89somelabel:
Mike Stumpa3899eb2010-01-19 23:08:01 +000090 ^{ goto somelabel; }(); // expected-error {{use of undeclared label 'somelabel'}}
Chris Lattner5718a352009-04-18 19:32:54 +000091}
92
93void test9() {
94 goto somelabel; // expected-error {{use of undeclared label 'somelabel'}}
95 ^{ somelabel: ; }();
96}
97
Chris Lattnerbcfce662009-04-18 20:10:59 +000098void test10(int i) {
99 switch (i) {
100 case 41: ;
101 ^{ case 42: ; }(); // expected-error {{'case' statement not in switch statement}}
102 }
103}
104
105void test11(int i) {
106 switch (i) {
107 case 41: ;
108 ^{ break; }(); // expected-error {{'break' statement not in loop or switch statement}}
109 }
110
111 for (; i < 100; ++i)
112 ^{ break; }(); // expected-error {{'break' statement not in loop or switch statement}}
113}
114
Chris Lattner5c59e2b2009-04-21 22:38:46 +0000115void (^test12f)(void);
116void test12() {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 test12f = ^test12f; // expected-error {{type name requires a specifier or qualifier}} expected-error {{expected expression}}
Chris Lattner5c59e2b2009-04-21 22:38:46 +0000118}
119
Chris Lattner17f3a6d2009-04-21 22:26:47 +0000120// rdar://6808730
121void *test13 = ^{
122 int X = 32;
123
124 void *P = ^{
125 return X+4; // References outer block's "X", so outer block is constant.
126 };
127};
128
129void test14() {
130 int X = 32;
131 static void *P = ^{ // expected-error {{initializer element is not a compile-time constant}}
132
133 void *Q = ^{
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 // References test14's "X": outer block is non-constant.
Chris Lattner17f3a6d2009-04-21 22:26:47 +0000135 return X+4;
136 };
137 };
138}
Mike Stump25efa102009-04-21 22:51:42 +0000139
Mike Stump914d3db2009-04-21 23:03:34 +0000140enum { LESS };
141
Douglas Gregora41a8c52010-04-22 00:20:18 +0000142void foo(long (^comp)()) { // expected-note{{passing argument to parameter 'comp' here}}
Mike Stump914d3db2009-04-21 23:03:34 +0000143}
144
145void (^test15f)(void);
146void test15() {
Douglas Gregord4eea832010-04-09 00:35:39 +0000147 foo(^{ return LESS; }); // expected-error {{incompatible block pointer types passing 'int (^)(void)' to parameter of type 'long (^)()'}}
Mike Stump25efa102009-04-21 22:51:42 +0000148}
Mike Stumpea000bf2009-04-30 00:19:40 +0000149
150__block int test16i; // expected-error {{__block attribute not allowed, only allowed on local variables}}
151
152void test16(__block int i) { // expected-error {{__block attribute not allowed, only allowed on local variables}}
Mike Stumpc975bb02009-05-01 23:41:47 +0000153 int size = 5;
Mike Stumpea000bf2009-04-30 00:19:40 +0000154 extern __block double extern_var; // expected-error {{__block attribute not allowed, only allowed on local variables}}
155 static __block char * pch; // expected-error {{__block attribute not allowed, only allowed on local variables}}
Mike Stumpc975bb02009-05-01 23:41:47 +0000156 __block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
157 __block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
Mike Stumpea000bf2009-04-30 00:19:40 +0000158}
Mike Stumpdd3e1662009-05-07 03:14:14 +0000159
Chris Lattnere0303582010-01-09 20:43:19 +0000160void f();
161
Mike Stumpdd3e1662009-05-07 03:14:14 +0000162void test17() {
163 void (^bp)(int);
164 void (*rp)(int);
165 void (^bp1)();
166 void *vp = bp;
167
168 f(1 ? bp : vp);
169 f(1 ? vp : bp);
Eli Friedman687abff2009-06-08 04:24:21 +0000170 f(1 ? bp : bp1);
Mike Stumpdd3e1662009-05-07 03:14:14 +0000171 (void)(bp > rp); // expected-error {{invalid operands}}
172 (void)(bp > 0); // expected-error {{invalid operands}}
173 (void)(bp > bp); // expected-error {{invalid operands}}
174 (void)(bp > vp); // expected-error {{invalid operands}}
175 f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}}
Mike Stumpaf199f32009-05-07 18:43:07 +0000176 (void)(bp == 1); // expected-error {{invalid operands to binary expression}}
177 (void)(bp == 0);
178 (void)(1 == bp); // expected-error {{invalid operands to binary expression}}
179 (void)(0 == bp);
180 (void)(bp < 1); // expected-error {{invalid operands to binary expression}}
181 (void)(bp < 0); // expected-error {{invalid operands to binary expression}}
182 (void)(1 < bp); // expected-error {{invalid operands to binary expression}}
183 (void)(0 < bp); // expected-error {{invalid operands to binary expression}}
Mike Stumpdd3e1662009-05-07 03:14:14 +0000184}
Mike Stump7bc8d962009-05-07 21:56:17 +0000185
186void test18() {
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700187 void (^const blockA)(void) = ^{ }; // expected-note {{variable 'blockA' declared const here}}
188 blockA = ^{ }; // expected-error {{cannot assign to variable 'blockA' with const-qualified type 'void (^const)(void)}}
Mike Stump7bc8d962009-05-07 21:56:17 +0000189}
Chris Lattnerbe6d2592009-07-19 20:17:11 +0000190
191// rdar://7072507
192int test19() {
Stephen Hines176edba2014-12-01 14:53:08 -0800193 goto L0; // expected-error {{cannot jump}}
Chris Lattnerbe6d2592009-07-19 20:17:11 +0000194
195 __block int x; // expected-note {{jump bypasses setup of __block variable}}
196L0:
197 x = 0;
198 ^(){ ++x; }();
199 return x;
200}
201
Mike Stump0d6fd572010-01-05 02:56:35 +0000202// radr://7438948
203void test20() {
204 int n = 7;
Anders Carlssond1aa8002010-04-23 02:20:12 +0000205 int vla[n]; // expected-note {{declared here}}
206 int (*vm)[n] = 0; // expected-note {{declared here}}
Mike Stump0d6fd572010-01-05 02:56:35 +0000207 vla[1] = 4341;
208 ^{
209 (void)vla[1]; // expected-error {{cannot refer to declaration with a variably modified type inside block}}
210 (void)(vm+1); // expected-error {{cannot refer to declaration with a variably modified type inside block}}
211 }();
212}
Mike Stump28497342010-01-05 03:10:36 +0000213
Mike Stumpfc4e4e12010-01-05 03:16:33 +0000214// radr://7438948
Mike Stump28497342010-01-05 03:10:36 +0000215void test21() {
Anders Carlssond1aa8002010-04-23 02:20:12 +0000216 int a[7]; // expected-note {{declared here}}
217 __block int b[10]; // expected-note {{declared here}}
Mike Stump28497342010-01-05 03:10:36 +0000218 a[1] = 1;
219 ^{
220 (void)a[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
Fariborz Jahanian8596bbe2010-03-16 23:39:51 +0000221 (void)b[1]; // expected-error {{cannot refer to declaration with an array type inside block}}
Mike Stump28497342010-01-05 03:10:36 +0000222 }();
223}
Fariborz Jahanianeb024ac2010-07-23 21:53:24 +0000224
225// rdar ://8218839
226const char * (^func)(void) = ^{ return __func__; };
227const char * (^function)(void) = ^{ return __FUNCTION__; };
228const char * (^pretty)(void) = ^{ return __PRETTY_FUNCTION__; };