blob: 64c29935f5f45b737706247db2e4b272e3e21627 [file] [log] [blame]
Chris Lattnerae0ee032008-12-04 23:20:07 +00001// RUN: clang -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;
Steve Naroffba80c9a2008-09-24 23:31:10 +00007 short (^add1)(void) = ^{ return y+1; }; // expected-warning {{incompatible block pointer types initializing 'int (^)(void)', expected 'short (^)(void)'}}
Steve Naroffc50a4a52008-09-16 22:25:10 +00008
Mike Stump98eb8a72009-02-04 22:31:32 +00009 CL X = ^{
10 if (2)
11 return;
Steve Naroffc50a4a52008-09-16 22:25:10 +000012 return 1; // expected-error {{void block should not return a value}}
13 };
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
19 return; // expected-error {{non-void block should return a value}}
20 };
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
Steve Naroffba80c9a2008-09-24 23:31:10 +000029 double (^A)(void) = ^ { // expected-warning {{incompatible block pointer types initializing 'float (^)(void)', expected 'double (^)(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)
Mike Stump98eb8a72009-02-04 22:31:32 +000034 return (double)2.0;
35 return 1;
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
Mike Stump98eb8a72009-02-04 22:31:32 +000041 return 2; // expected-warning {{incompatible integer to pointer conversion returning 'int', expected 'char *'}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000042 };
Mike Stump98eb8a72009-02-04 22:31:32 +000043
Steve Naroffba80c9a2008-09-24 23:31:10 +000044 return ^{ return 1; }; // expected-warning {{incompatible block pointer types returning 'int (^)(void)', expected 'CL'}} expected-error {{returning block that lives on the local stack}}
Steve Naroffc50a4a52008-09-16 22:25:10 +000045}
46
47typedef int (^CL2)(void);
48
49CL2 foo2() {
50 return ^{ return 1; }; // expected-error {{returning block that lives on the local stack}}
51}
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) {
69 return (Boolean)(uintptr_t)INVOKE_CALLBACK2(value_equal, (uintptr_t)stack_value_or_key1, (uintptr_t)stack_value_or_key2);
70 };
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}
79void foo4() {
80 int (^xx)(const char *s) = ^(char *s) { return 1; }; // expected-warning {{incompatible block pointer types initializing 'int (^)(char *)', expected 'int (^)(char const *)'}}
81 int (*yy)(const char *s) = funk; // expected-warning {{incompatible pointer types initializing 'int (char *)', expected 'int (*)(char const *)'}}
Steve Naroff538afe32008-09-28 00:13:36 +000082
83 int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; };
Steve Naroffba80c9a2008-09-24 23:31:10 +000084}