1 // RUN: %clang_cc1 -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -pedantic -fsyntax-only %s -verify -fblocks
2 
3 extern int printf(const char *, ...);
4 
5 typedef void (^CL)(void);
6 
foo(void)7 CL foo(void) {
8   short y;
9   short (^add1)(void) = ^{ return y+1; }; // expected-error {{incompatible block pointer types initializing 'short (^)(void)' with an expression of type 'int (^)(void)'}}
10 
11   CL X = ^{
12     if (2)
13       return;
14     return 1;  // expected-error {{return type 'int' must match previous return type 'void' when block literal has unspecified explicit return type}}
15   };
16 
17   int (^Y) (void)  = ^{
18     if (3)
19       return 1;
20     else
21       return; // expected-error {{return type 'void' must match previous return type 'int' when block literal has unspecified explicit return type}}
22   };
23 
24   char *(^Z)(void) = ^{
25     if (3)
26       return "";
27     else
28       return (char*)0;
29   };
30 
31   double (^A)(void) = ^ { // expected-error {{incompatible block pointer types initializing 'double (^)(void)' with an expression of type 'float (^)(void)'}}
32     if (1)
33       return (float)1.0;
34     else
35       if (2)
36         return (double)2.0; // expected-error {{return type 'double' must match previous return type 'float' when block literal has unspecified explicit return type}}
37     return 1; // expected-error {{return type 'int' must match previous return type 'float' when block literal has unspecified explicit return type}}
38   };
39   char *(^B)(void) = ^{
40     if (3)
41       return "";
42     else
43       return 2; // expected-error {{return type 'int' must match previous return type 'char *' when block literal has unspecified explicit return type}}
44   };
45 
46   return ^{ return 1; }; // expected-error {{incompatible block pointer types returning 'int (^)(void)' from a function with result type 'CL' (aka 'void (^)(void)')}}
47 }
48 
49 typedef int (^CL2)(void);
50 
foo2(void)51 CL2 foo2(void) {
52   return ^{ return 1; };
53 }
54 
55 typedef unsigned int * uintptr_t;
56 typedef char Boolean;
57 typedef int CFBasicHash;
58 
59 #define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
60 
61 typedef struct {
62     Boolean (^isEqual)(const CFBasicHash *, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key);
63 } CFBasicHashCallbacks;
64 
foo3(void)65 int foo3(void) {
66     CFBasicHashCallbacks cb;
67 
68     Boolean (*value_equal)(uintptr_t, uintptr_t) = 0;
69 
70     cb.isEqual = ^(const CFBasicHash *table, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key) {
71       return (Boolean)(uintptr_t)INVOKE_CALLBACK2(value_equal, (uintptr_t)stack_value_or_key1, (uintptr_t)stack_value_or_key2);
72     };
73 }
74 
funk(char * s)75 static int funk(char *s) {
76   if (^{} == ((void*)0))
77     return 1;
78   else
79     return 0;
80 }
81 void next(void);
foo4(void)82 void foo4(void) {
83   int (^xx)(const char *s) = ^(char *s) { return 1; }; // expected-error {{incompatible block pointer types initializing 'int (^)(const char *)' with an expression of type 'int (^)(char *)'}}
84   int (*yy)(const char *s) = funk; // expected-warning {{incompatible function pointer types initializing 'int (*)(const char *)' with an expression of type 'int (char *)'}}
85 
86   int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; };
87 }
88 
89 typedef void (^bptr)(void);
90 
foo5(int j)91 bptr foo5(int j) {
92   __block int i;
93   if (j)
94     return ^{ ^{ i=0; }(); };  // expected-error {{returning block that lives on the local stack}}
95   return ^{ i=0; };  // expected-error {{returning block that lives on the local stack}}
96   return (^{ i=0; });  // expected-error {{returning block that lives on the local stack}}
97   return (void*)(^{ i=0; });  // expected-error {{returning block that lives on the local stack}}
98 }
99 
100 int (*funcptr3[5])(long);
101 int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block cannot return array type}} expected-error {{incompatible pointer to integer conversion}}
102 int sz9 = sizeof(^int(*())()[3]{ }); // expected-error {{function cannot return array type}}
103                                      // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}}
104 
foo6(void)105 void foo6(void) {
106   int (^b)(int) __attribute__((noreturn));
107   b = ^ (int i) __attribute__((noreturn)) { return 1; };  // expected-error {{block declared 'noreturn' should not return}}
108   b(1);
109   int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}}
110 }
111 
112 
foo7(void)113 void foo7(void)
114 {
115  const int (^BB) (void) = ^{ const int i = 1; return i; }; // OK - initializing 'const int (^)(void)' with an expression of type 'int (^)(void)'
116 
117  const int (^CC) (void)  = ^const int{ const int i = 1; return i; };
118 
119 
120   int i;
121   int (^FF) (void)  = ^{ return i; }; // OK
122   int (^EE) (void)  = ^{ return i+1; }; // OK
123 
124   __block int j;
125   int (^JJ) (void)  = ^{ return j; }; // OK
126   int (^KK) (void)  = ^{ return j+1; }; // OK
127 
128   __block const int k;
129   const int cint = 100;
130 
131   int (^MM) (void)  = ^{ return k; };
132   int (^NN) (void)  = ^{ return cint; };
133 }
134 
135 // rdar://11069896
136 void (^blk)(void) = ^{
137     return (void)0; // expected-warning {{void block literal should not return void expression}}
138 };
139 
140 // rdar://13463504
141 enum Test8 { T8_a, T8_b, T8_c };
test8(void)142 void test8(void) {
143   extern void test8_helper(int (^)(int));
144   test8_helper(^(int flag) { if (flag) return T8_a; return T8_b; });
145 }
test8b(void)146 void test8b(void) {
147   extern void test8_helper2(char (^)(int)); // expected-note {{here}}
148   test8_helper2(^(int flag) { if (flag) return T8_a; return T8_b; }); // expected-error {{passing 'enum Test8 (^)(int)' to parameter of type 'char (^)(int)'}}
149 }
150