block-return.c 5.4 KB

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