block-misc.c revision bcfce66584e47bb07f49a86b7cb39b4fdd269a5a
1// RUN: clang-cc -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) // expected-error {{comparison of distinct block types}} 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; // expected-error {{comparison of distinct block types}} 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 47int 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 67// rdar://6257721 - reference to static/global is byref by default. 68static int test5g; 69void test5() { 70 bar(^{ test5g = 1; }); 71} 72 73// rdar://6405429 - __func__ in a block refers to the containing function name. 74const char*test6() { 75 return ^{ 76 return __func__; 77 } (); 78} 79 80// radr://6732116 - block comparisons 81void (^test7a)(); 82int test7(void (^p)()) { 83 return test7a == p; 84} 85 86 87void test8() { 88somelabel: 89 // FIXME: This should say "jump out of block not legal" when gotos are allowed. 90 ^{ goto somelabel; }(); // expected-error {{goto not allowed in block literal}} 91} 92 93void test9() { 94 goto somelabel; // expected-error {{use of undeclared label 'somelabel'}} 95 ^{ somelabel: ; }(); 96} 97 98void 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 115 116