blob: ea3477d2639d10cc8f94a5ec25fdb685715e259e [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s -fblocks
Steve Naroffdd972f22008-09-05 22:11:13 +00002void donotwarn();
3
4int (^IFP) ();
5int (^II) (int);
6int test1() {
Mike Stumpaab0f7a2009-04-01 01:17:39 +00007 int (^PFR) (int) = 0; // OK
8 PFR = II; // OK
Steve Naroffdd972f22008-09-05 22:11:13 +00009
Mike Stumpaab0f7a2009-04-01 01:17:39 +000010 if (PFR == II) // OK
11 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000012
Mike Stumpaab0f7a2009-04-01 01:17:39 +000013 if (PFR == IFP) // expected-error {{comparison of distinct block types}}
14 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 Stumpaab0f7a2009-04-01 01:17:39 +000019 if (PFR == 0) // OK
20 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000021
Mike Stumpaab0f7a2009-04-01 01:17:39 +000022 if (PFR) // OK
23 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000024
Mike Stumpaab0f7a2009-04-01 01:17:39 +000025 if (!PFR) // OK
26 donotwarn();
Steve Naroffdd972f22008-09-05 22:11:13 +000027
Mike Stumpaab0f7a2009-04-01 01:17:39 +000028 return PFR != IFP; // expected-error {{comparison of distinct block types}}
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;
33 (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 Stumpaab0f7a2009-04-01 01:17:39 +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
47int 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
67// rdar://6257721 - reference to static/global is byref by default.
68static int test5g;
69void test5() {
70 bar(^{ test5g = 1; });
71}
72
Chris Lattner371f2582008-12-04 23:50:19 +000073// rdar://6405429 - __func__ in a block refers to the containing function name.
74const char*test6() {
Mike Stumpaab0f7a2009-04-01 01:17:39 +000075 return ^{
76 return __func__;
77 } ();
Chris Lattner371f2582008-12-04 23:50:19 +000078}
79
Mike Stumpaab0f7a2009-04-01 01:17:39 +000080// radr://6732116 - block comparisons
81void (^g)();
82int foo(void (^p)()) {
83 return g == p;
84}