Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only %s -verify -fblocks |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 2 | |
| 3 | typedef void (^CL)(void); |
| 4 | |
| 5 | CL foo() { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6 | short y; |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 7 | short (^add1)(void) = ^{ return y+1; }; // expected-error {{incompatible block pointer types initializing 'int (^)(void)', expected 'short (^)(void)'}} |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 8 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9 | CL X = ^{ |
| 10 | if (2) |
| 11 | return; |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 12 | return 1; // expected-error {{void block should not return a value}} |
| 13 | }; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 14 | |
| 15 | int (^Y) (void) = ^{ |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 16 | if (3) |
| 17 | return 1; |
| 18 | else |
| 19 | return; // expected-error {{non-void block should return a value}} |
| 20 | }; |
| 21 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 22 | char *(^Z)(void) = ^{ |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 23 | if (3) |
| 24 | return ""; |
| 25 | else |
| 26 | return (char*)0; |
| 27 | }; |
| 28 | |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 29 | double (^A)(void) = ^ { // expected-error {{incompatible block pointer types initializing 'float (^)(void)', expected 'double (^)(void)'}} |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 30 | if (1) |
| 31 | return (float)1.0; |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 32 | else |
| 33 | if (2) |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 34 | return (double)2.0; |
| 35 | return 1; |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 36 | }; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 37 | char *(^B)(void) = ^{ |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 38 | if (3) |
| 39 | return ""; |
| 40 | else |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 41 | return 2; // expected-warning {{incompatible integer to pointer conversion returning 'int', expected 'char *'}} |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 42 | }; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 43 | |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 44 | return ^{ return 1; }; // expected-error {{incompatible block pointer types returning 'int (^)(void)', expected 'CL'}} |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | typedef int (^CL2)(void); |
| 48 | |
| 49 | CL2 foo2() { |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 50 | return ^{ return 1; }; |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 51 | } |
Steve Naroff | 1656442 | 2008-09-24 22:26:48 +0000 | [diff] [blame] | 52 | |
| 53 | typedef unsigned int * uintptr_t; |
| 54 | typedef char Boolean; |
| 55 | typedef int CFBasicHash; |
| 56 | |
| 57 | #define INVOKE_CALLBACK2(P, A, B) (P)(A, B) |
| 58 | |
| 59 | typedef struct { |
| 60 | Boolean (^isEqual)(const CFBasicHash *, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key); |
| 61 | } CFBasicHashCallbacks; |
| 62 | |
| 63 | int 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 Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 72 | |
| 73 | static int funk(char *s) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 74 | if (^{} == ((void*)0)) |
| 75 | return 1; |
| 76 | else |
| 77 | return 0; |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 78 | } |
| 79 | void foo4() { |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 80 | int (^xx)(const char *s) = ^(char *s) { return 1; }; // expected-error {{incompatible block pointer types initializing 'int (^)(char *)', expected 'int (^)(char const *)'}} |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 81 | int (*yy)(const char *s) = funk; // expected-warning {{incompatible pointer types initializing 'int (char *)', expected 'int (*)(char const *)'}} |
Steve Naroff | 538afe3 | 2008-09-28 00:13:36 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 83 | int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; }; // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ |
| 84 | // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 85 | } |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 86 | |
| 87 | typedef void (^bptr)(void); |
| 88 | |
| 89 | bptr foo5(int j) { |
| 90 | __block int i; |
| 91 | if (j) |
| 92 | return ^{ ^{ i=0; }(); }; // expected-error {{returning block that lives on the local stack}} |
| 93 | return ^{ i=0; }; // expected-error {{returning block that lives on the local stack}} |
| 94 | } |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 95 | |
| 96 | int (*funcptr3[5])(long); |
| 97 | int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block declared as returning an array}} |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame^] | 98 | |
| 99 | void foo6() { |
| 100 | void (^b)(int) __attribute__((noreturn)); |
| 101 | b = ^ (int i) __attribute__((noreturn)) { return 1; }; // expected-error {{block declared 'noreturn' should not return}} |
| 102 | b(1); |
| 103 | } |