return.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -verify -fblocks -Wno-unreachable-code -Wno-unused-value
  2. // clang emits the following warning by default.
  3. // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the
  4. // following warning.
  5. int t14() {
  6. return; // expected-warning {{non-void function 't14' should return a value}}
  7. }
  8. void t15() {
  9. return 1; // expected-warning {{void function 't15' should not return a value}}
  10. }
  11. int unknown();
  12. void test0() {
  13. }
  14. int test1() {
  15. } // expected-warning {{control reaches end of non-void function}}
  16. int test2() {
  17. a: goto a;
  18. }
  19. int test3() {
  20. goto a;
  21. a: ;
  22. } // expected-warning {{control reaches end of non-void function}}
  23. void halt() {
  24. a: goto a;
  25. }
  26. void halt2() __attribute__((noreturn));
  27. int test4() {
  28. halt2();
  29. }
  30. int test5() {
  31. halt2(), (void)1;
  32. }
  33. int test6() {
  34. 1, halt2();
  35. }
  36. int j;
  37. int unknown_nohalt() {
  38. return j;
  39. }
  40. int test7() {
  41. unknown();
  42. } // expected-warning {{control reaches end of non-void function}}
  43. int test8() {
  44. (void)(1 + unknown());
  45. } // expected-warning {{control reaches end of non-void function}}
  46. int halt3() __attribute__((noreturn));
  47. int test9() {
  48. (void)(halt3() + unknown());
  49. }
  50. int test10() {
  51. (void)(unknown() || halt3());
  52. } // expected-warning {{control may reach end of non-void function}}
  53. int test11() {
  54. (void)(unknown() && halt3());
  55. } // expected-warning {{control may reach end of non-void function}}
  56. int test12() {
  57. (void)(halt3() || unknown());
  58. }
  59. int test13() {
  60. (void)(halt3() && unknown());
  61. }
  62. int test14() {
  63. (void)(1 || unknown());
  64. } // expected-warning {{control reaches end of non-void function}}
  65. int test15() {
  66. (void)(0 || unknown());
  67. } // expected-warning {{control reaches end of non-void function}}
  68. int test16() {
  69. (void)(0 && unknown());
  70. } // expected-warning {{control reaches end of non-void function}}
  71. int test17() {
  72. (void)(1 && unknown());
  73. } // expected-warning {{control reaches end of non-void function}}
  74. int test18() {
  75. (void)(unknown_nohalt() && halt3());
  76. } // expected-warning {{control may reach end of non-void function}}
  77. int test19() {
  78. (void)(unknown_nohalt() && unknown());
  79. } // expected-warning {{control reaches end of non-void function}}
  80. int test20() {
  81. int i;
  82. if (i)
  83. return 0;
  84. else if (0)
  85. return 2;
  86. } // expected-warning {{control may reach end of non-void function}}
  87. int test21() {
  88. int i;
  89. if (i)
  90. return 0;
  91. else if (1)
  92. return 2;
  93. }
  94. int test22() {
  95. int i;
  96. switch (i) default: ;
  97. } // expected-warning {{control reaches end of non-void function}}
  98. int test23() {
  99. int i;
  100. switch (i) {
  101. case 0:
  102. return 0;
  103. case 2:
  104. return 2;
  105. }
  106. } // expected-warning {{control may reach end of non-void function}}
  107. int test24() {
  108. int i;
  109. switch (i) {
  110. case 0:
  111. return 0;
  112. case 2:
  113. return 2;
  114. default:
  115. return -1;
  116. }
  117. }
  118. int test25() {
  119. 1 ? halt3() : unknown();
  120. }
  121. int test26() {
  122. 0 ? halt3() : unknown();
  123. } // expected-warning {{control reaches end of non-void function}}
  124. int j;
  125. void (*fptr)() __attribute__((noreturn));
  126. int test27() {
  127. switch (j) {
  128. case 1:
  129. do { } while (1);
  130. break;
  131. case 2:
  132. for (;;) ;
  133. break;
  134. case 3:
  135. for (;1;) ;
  136. for (;0;) {
  137. goto done;
  138. }
  139. return 1;
  140. case 4:
  141. while (0) { goto done; }
  142. return 1;
  143. case 5:
  144. while (1) { return 1; }
  145. break;
  146. case 6:
  147. fptr();
  148. break;
  149. default:
  150. return 1;
  151. }
  152. done: ;
  153. }
  154. // PR4624
  155. void test28() __attribute__((noreturn));
  156. void test28(x) { while (1) { } }
  157. void exit(int);
  158. int test29() {
  159. exit(1);
  160. }
  161. // Include these declarations here explicitly so we don't depend on system headers.
  162. typedef struct __jmp_buf_tag{} jmp_buf[1];
  163. extern void longjmp (struct __jmp_buf_tag __env[1], int __val) __attribute__ ((noreturn));
  164. extern void _longjmp (struct __jmp_buf_tag __env[1], int __val) __attribute__ ((noreturn));
  165. jmp_buf test30_j;
  166. int test30() {
  167. if (j)
  168. longjmp(test30_j, 1);
  169. else
  170. #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
  171. longjmp(test30_j, 2);
  172. #else
  173. _longjmp(test30_j, 1);
  174. #endif
  175. }
  176. typedef void test31_t(int status);
  177. void test31(test31_t *callback __attribute__((noreturn)));
  178. void test32() {
  179. ^ (void) { while (1) { } }();
  180. ^ (void) { if (j) while (1) { } }();
  181. while (1) { }
  182. }
  183. void test33() {
  184. if (j) while (1) { }
  185. }
  186. // Test that 'static inline' functions are only analyzed for CFG-based warnings
  187. // when they are used.
  188. static inline int si_has_missing_return() {} // expected-warning{{control reaches end of non-void function}}
  189. static inline int si_has_missing_return_2() {}; // expected-warning{{control reaches end of non-void function}}
  190. static inline int si_forward();
  191. static inline int si_has_missing_return_3(int x) {
  192. if (x)
  193. return si_has_missing_return_3(x+1);
  194. } // expected-warning{{control may reach end of non-void function}}
  195. int test_static_inline(int x) {
  196. si_forward();
  197. return x ? si_has_missing_return_2() : si_has_missing_return_3(x);
  198. }
  199. static inline int si_forward() {} // expected-warning{{control reaches end of non-void function}}
  200. // Test warnings on ignored qualifiers on return types.
  201. const int ignored_c_quals(); // expected-warning{{'const' type qualifier on return type has no effect}}
  202. const volatile int ignored_cv_quals(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
  203. char* const volatile restrict ignored_cvr_quals(); // expected-warning{{'const volatile restrict' type qualifiers on return type have no effect}}
  204. typedef const int CI;
  205. CI ignored_quals_typedef();
  206. const CI ignored_quals_typedef_2(); // expected-warning{{'const' type qualifier}}
  207. // Test that for switch(enum) that if the switch statement covers all the cases
  208. // that we don't consider that for -Wreturn-type.
  209. enum Cases { C1, C2, C3, C4 };
  210. int test_enum_cases(enum Cases C) {
  211. switch (C) {
  212. case C1: return 1;
  213. case C2: return 2;
  214. case C4: return 3;
  215. case C3: return 4;
  216. }
  217. } // no-warning
  218. // PR12318 - Don't give a may reach end of non-void function warning.
  219. int test34(int x) {
  220. if (x == 1) {
  221. return 3;
  222. } else if ( x == 2 || 1) {
  223. return 5;
  224. }
  225. }
  226. // PR18999
  227. int test35() {
  228. lbl:
  229. if (1)
  230. goto lbl;
  231. }
  232. // PR19074.
  233. void abort(void) __attribute__((noreturn));
  234. #define av_assert0(cond) do {\
  235. if (!(cond)) {\
  236. abort();\
  237. }\
  238. } while (0)
  239. int PR19074(int x) {
  240. switch(x) {
  241. case 0:
  242. return 0;
  243. default:
  244. av_assert0(0);
  245. } // no-warning
  246. }
  247. int PR19074_positive(int x) {
  248. switch(x) {
  249. case 0:
  250. return 0;
  251. default:
  252. break;
  253. }
  254. } // expected-warning {{control may reach end of non-void function}}
  255. // sizeof(long) test.
  256. int sizeof_long() {
  257. if (sizeof(long) == 4)
  258. return 1;
  259. if (sizeof(long) == 8)
  260. return 2;
  261. } // no-warning