blob: 2ea4d813ab0161b8ff966b3789bcd90ee0169f1a [file] [log] [blame]
David Blaikie9b29f4f2012-10-16 18:53:14 +00001// RUN: %clang_cc1 -Wno-int-to-pointer-cast -pedantic -fsyntax-only %s -verify -fblocks
Steve Naroffc50a4a52008-09-16 22:25:10 +00002
3typedef void (^CL)(void);
4
5CL foo() {
Mike Stump98eb8a72009-02-04 22:31:32 +00006 short y;
Douglas Gregor08a41902010-04-09 17:53:29 +00007 short (^add1)(void) = ^{ return y+1; }; // expected-error {{incompatible block pointer types initializing 'short (^)(void)' with an expression of type 'int (^)(void)'}}
Steve Naroffc50a4a52008-09-16 22:25:10 +00008
Mike Stump98eb8a72009-02-04 22:31:32 +00009 CL X = ^{
10 if (2)
11 return;
Fariborz Jahanian649657e2011-12-03 23:53:56 +000012 return 1; // expected-error {{return type 'int' must match previous return type 'void' when block literal has unspecified explicit return type}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000013 };
Mike Stump98eb8a72009-02-04 22:31:32 +000014
15 int (^Y) (void) = ^{
Steve Naroffc50a4a52008-09-16 22:25:10 +000016 if (3)
17 return 1;
18 else
Fariborz Jahanian649657e2011-12-03 23:53:56 +000019 return; // expected-error {{return type 'void' must match previous return type 'int' when block literal has unspecified explicit return type}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000020 };
21
Mike Stump98eb8a72009-02-04 22:31:32 +000022 char *(^Z)(void) = ^{
Steve Naroffc50a4a52008-09-16 22:25:10 +000023 if (3)
24 return "";
25 else
26 return (char*)0;
27 };
28
Douglas Gregor08a41902010-04-09 17:53:29 +000029 double (^A)(void) = ^ { // expected-error {{incompatible block pointer types initializing 'double (^)(void)' with an expression of type 'float (^)(void)'}}
Mike Stump98eb8a72009-02-04 22:31:32 +000030 if (1)
31 return (float)1.0;
Steve Naroffc50a4a52008-09-16 22:25:10 +000032 else
33 if (2)
Fariborz Jahanian05865202011-12-03 17:47:53 +000034 return (double)2.0; // expected-error {{return type 'double' must match previous return type 'float' when block literal has unspecified explicit return type}}
35 return 1; // expected-error {{return type 'int' must match previous return type 'float' when block literal has unspecified explicit return type}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000036 };
Mike Stump98eb8a72009-02-04 22:31:32 +000037 char *(^B)(void) = ^{
Steve Naroffc50a4a52008-09-16 22:25:10 +000038 if (3)
39 return "";
40 else
Fariborz Jahanian05865202011-12-03 17:47:53 +000041 return 2; // expected-error {{return type 'int' must match previous return type 'char *' when block literal has unspecified explicit return type}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000042 };
Mike Stump98eb8a72009-02-04 22:31:32 +000043
Douglas Gregord4eea832010-04-09 00:35:39 +000044 return ^{ return 1; }; // expected-error {{incompatible block pointer types returning 'int (^)(void)' from a function with result type 'CL' (aka 'void (^)(void)')}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000045}
46
47typedef int (^CL2)(void);
48
49CL2 foo2() {
Mike Stump397195b2009-04-17 00:09:41 +000050 return ^{ return 1; };
Steve Naroffc50a4a52008-09-16 22:25:10 +000051}
Steve Naroff16564422008-09-24 22:26:48 +000052
53typedef unsigned int * uintptr_t;
54typedef char Boolean;
55typedef int CFBasicHash;
56
57#define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
58
59typedef struct {
60 Boolean (^isEqual)(const CFBasicHash *, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key);
61} CFBasicHashCallbacks;
62
63int foo3() {
64 CFBasicHashCallbacks cb;
65
66 Boolean (*value_equal)(uintptr_t, uintptr_t) = 0;
67
68 cb.isEqual = ^(const CFBasicHash *table, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key) {
Mike Stump1eb44332009-09-09 15:08:12 +000069 return (Boolean)(uintptr_t)INVOKE_CALLBACK2(value_equal, (uintptr_t)stack_value_or_key1, (uintptr_t)stack_value_or_key2);
Steve Naroff16564422008-09-24 22:26:48 +000070 };
71}
Steve Naroffba80c9a2008-09-24 23:31:10 +000072
73static int funk(char *s) {
Steve Naroff59f53942008-09-28 01:11:11 +000074 if (^{} == ((void*)0))
75 return 1;
76 else
77 return 0;
Steve Naroffba80c9a2008-09-24 23:31:10 +000078}
Chris Lattnere0303582010-01-09 20:43:19 +000079void next();
Steve Naroffba80c9a2008-09-24 23:31:10 +000080void foo4() {
Chris Lattner58f9e132010-09-05 00:04:01 +000081 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 *)'}}
82 int (*yy)(const char *s) = funk; // expected-warning {{incompatible pointer types initializing 'int (*)(const char *)' with an expression of type 'int (char *)'}}
Steve Naroff538afe32008-09-28 00:13:36 +000083
Jean-Daniel Dupas5faf5d32012-01-27 23:21:02 +000084 int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; }; // expected-warning{{implicitly declaring library function 'printf' with type 'int (const char *, ...)'}} \
Douglas Gregora316e7b2009-02-14 00:32:47 +000085 // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
Steve Naroffba80c9a2008-09-24 23:31:10 +000086}
Mike Stump397195b2009-04-17 00:09:41 +000087
88typedef void (^bptr)(void);
89
90bptr foo5(int j) {
91 __block int i;
92 if (j)
93 return ^{ ^{ i=0; }(); }; // expected-error {{returning block that lives on the local stack}}
94 return ^{ i=0; }; // expected-error {{returning block that lives on the local stack}}
Chris Lattner4ca606e2009-09-08 00:36:37 +000095 return (^{ i=0; }); // expected-error {{returning block that lives on the local stack}}
96 return (void*)(^{ i=0; }); // expected-error {{returning block that lives on the local stack}}
Mike Stump397195b2009-04-17 00:09:41 +000097}
Mike Stump4eeab842009-04-28 01:10:27 +000098
99int (*funcptr3[5])(long);
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +0000100int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block cannot return array type}} expected-warning {{incompatible pointer to integer conversion}}
Argyrios Kyrtzidisa4356ad2011-01-26 01:26:44 +0000101int sz9 = sizeof(^int(*())()[3]{ }); // expected-error {{function cannot return array type}}
Mike Stump19c30c02009-04-29 19:03:13 +0000102
103void foo6() {
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +0000104 int (^b)(int) __attribute__((noreturn));
Mike Stump19c30c02009-04-29 19:03:13 +0000105 b = ^ (int i) __attribute__((noreturn)) { return 1; }; // expected-error {{block declared 'noreturn' should not return}}
106 b(1);
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +0000107 int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}}
Mike Stump19c30c02009-04-29 19:03:13 +0000108}
Fariborz Jahanian7d5c74e2009-06-19 23:37:08 +0000109
110
111void foo7()
112{
Fariborz Jahaniand263fd12011-02-11 18:46:17 +0000113 const int (^BB) (void) = ^{ const int i = 1; return i; }; // OK - initializing 'const int (^)(void)' with an expression of type 'int (^)(void)'
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000114
Chandler Carruth5495f372010-07-14 06:36:18 +0000115 const int (^CC) (void) = ^const int{ const int i = 1; return i; };
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000116
Fariborz Jahanian7d5c74e2009-06-19 23:37:08 +0000117
118 int i;
119 int (^FF) (void) = ^{ return i; }; // OK
120 int (^EE) (void) = ^{ return i+1; }; // OK
121
122 __block int j;
123 int (^JJ) (void) = ^{ return j; }; // OK
124 int (^KK) (void) = ^{ return j+1; }; // OK
125
126 __block const int k;
127 const int cint = 100;
128
Douglas Gregora873dfc2010-02-03 00:27:59 +0000129 int (^MM) (void) = ^{ return k; };
130 int (^NN) (void) = ^{ return cint; };
Fariborz Jahanian7d5c74e2009-06-19 23:37:08 +0000131}
132
Fariborz Jahanian4e648e42012-03-21 16:45:13 +0000133// rdar://11069896
134void (^blk)(void) = ^{
Fariborz Jahanian9354f6a2012-03-21 20:28:39 +0000135 return (void)0; // expected-warning {{void block literal should not return void expression}}
Fariborz Jahanian4e648e42012-03-21 16:45:13 +0000136};