[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / Sema / block-return.c
blob1ad76dcd672fc3972b7f84e6c958063ef508a082
1 // RUN: %clang_cc1 -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -pedantic -fsyntax-only %s -verify -fblocks
3 typedef void (^CL)(void);
5 CL foo(void) {
6 short y;
7 short (^add1)(void) = ^{ return y+1; }; // expected-error {{incompatible block pointer types initializing 'short (^)(void)' with an expression of type 'int (^)(void)'}}
9 CL X = ^{
10 if (2)
11 return;
12 return 1; // expected-error {{return type 'int' must match previous return type 'void' when block literal has unspecified explicit return type}}
15 int (^Y) (void) = ^{
16 if (3)
17 return 1;
18 else
19 return; // expected-error {{return type 'void' must match previous return type 'int' when block literal has unspecified explicit return type}}
22 char *(^Z)(void) = ^{
23 if (3)
24 return "";
25 else
26 return (char*)0;
29 double (^A)(void) = ^ { // expected-error {{incompatible block pointer types initializing 'double (^)(void)' with an expression of type 'float (^)(void)'}}
30 if (1)
31 return (float)1.0;
32 else
33 if (2)
34 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}}
37 char *(^B)(void) = ^{
38 if (3)
39 return "";
40 else
41 return 2; // expected-error {{return type 'int' must match previous return type 'char *' when block literal has unspecified explicit return type}}
44 return ^{ return 1; }; // expected-error {{incompatible block pointer types returning 'int (^)(void)' from a function with result type 'CL' (aka 'void (^)(void)')}}
47 typedef int (^CL2)(void);
49 CL2 foo2(void) {
50 return ^{ return 1; };
53 typedef unsigned int * uintptr_t;
54 typedef char Boolean;
55 typedef int CFBasicHash;
57 #define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
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;
63 int foo3(void) {
64 CFBasicHashCallbacks cb;
66 Boolean (*value_equal)(uintptr_t, uintptr_t) = 0;
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);
73 static int funk(char *s) {
74 if (^{} == ((void*)0))
75 return 1;
76 else
77 return 0;
79 void next(void);
80 void foo4(void) {
81 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 function pointer types initializing 'int (*)(const char *)' with an expression of type 'int (char *)'}}
84 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 *, ...)'}} \
85 // expected-note{{include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
88 typedef void (^bptr)(void);
90 bptr 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}}
95 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}}
99 int (*funcptr3[5])(long);
100 int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block cannot return array type}} expected-warning {{incompatible pointer to integer conversion}}
101 int sz9 = sizeof(^int(*())()[3]{ }); // expected-error {{function cannot return array type}}
103 void foo6(void) {
104 int (^b)(int) __attribute__((noreturn));
105 b = ^ (int i) __attribute__((noreturn)) { return 1; }; // expected-error {{block declared 'noreturn' should not return}}
106 b(1);
107 int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}}
111 void foo7(void)
113 const int (^BB) (void) = ^{ const int i = 1; return i; }; // OK - initializing 'const int (^)(void)' with an expression of type 'int (^)(void)'
115 const int (^CC) (void) = ^const int{ const int i = 1; return i; };
118 int i;
119 int (^FF) (void) = ^{ return i; }; // OK
120 int (^EE) (void) = ^{ return i+1; }; // OK
122 __block int j;
123 int (^JJ) (void) = ^{ return j; }; // OK
124 int (^KK) (void) = ^{ return j+1; }; // OK
126 __block const int k;
127 const int cint = 100;
129 int (^MM) (void) = ^{ return k; };
130 int (^NN) (void) = ^{ return cint; };
133 // rdar://11069896
134 void (^blk)(void) = ^{
135 return (void)0; // expected-warning {{void block literal should not return void expression}}
138 // rdar://13463504
139 enum Test8 { T8_a, T8_b, T8_c };
140 void test8(void) {
141 extern void test8_helper(int (^)(int));
142 test8_helper(^(int flag) { if (flag) return T8_a; return T8_b; });
144 void test8b(void) {
145 extern void test8_helper2(char (^)(int)); // expected-note {{here}}
146 test8_helper2(^(int flag) { if (flag) return T8_a; return T8_b; }); // expected-error {{passing 'enum Test8 (^)(int)' to parameter of type 'char (^)(int)'}}