Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / return.c
blob14d5300e819492e84bd5f7ecdfb6223986088285
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
3 // clang emits the following warning by default.
4 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the
5 // following warning.
6 int t14(void) {
7 return; // expected-warning {{non-void function 't14' should return a value}}
10 void t15(void) {
11 return 1; // expected-warning {{void function 't15' should not return a value}}
14 int unknown(void);
16 void test0(void) {
19 int test1(void) {
20 } // expected-warning {{non-void function does not return a value}}
22 int test2(void) {
23 a: goto a;
26 int test3(void) {
27 goto a;
28 a: ;
29 } // expected-warning {{non-void function does not return a value}}
32 void halt(void) {
33 a: goto a;
36 void halt2(void) __attribute__((noreturn));
38 int test4(void) {
39 halt2();
42 int test5(void) {
43 halt2(), (void)1;
46 int test6(void) {
47 1, halt2();
50 int j;
51 int unknown_nohalt(void) {
52 return j;
55 int test7(void) {
56 unknown();
57 } // expected-warning {{non-void function does not return a value}}
59 int test8(void) {
60 (void)(1 + unknown());
61 } // expected-warning {{non-void function does not return a value}}
63 int halt3(void) __attribute__((noreturn));
65 int test9(void) {
66 (void)(halt3() + unknown());
69 int test10(void) {
70 (void)(unknown() || halt3());
71 } // expected-warning {{non-void function does not return a value in all control paths}}
73 int test11(void) {
74 (void)(unknown() && halt3());
75 } // expected-warning {{non-void function does not return a value in all control paths}}
77 int test12(void) {
78 (void)(halt3() || unknown());
81 int test13(void) {
82 (void)(halt3() && unknown());
85 int test14(void) {
86 (void)(1 || unknown());
87 } // expected-warning {{non-void function does not return a value}}
89 int test15(void) {
90 (void)(0 || unknown());
91 } // expected-warning {{non-void function does not return a value}}
93 int test16(void) {
94 (void)(0 && unknown());
95 } // expected-warning {{non-void function does not return a value}}
97 int test17(void) {
98 (void)(1 && unknown());
99 } // expected-warning {{non-void function does not return a value}}
101 int test18(void) {
102 (void)(unknown_nohalt() && halt3());
103 } // expected-warning {{non-void function does not return a value in all control paths}}
105 int test19(void) {
106 (void)(unknown_nohalt() && unknown());
107 } // expected-warning {{non-void function does not return a value}}
109 int test20(void) {
110 int i;
111 if (i)
112 return 0;
113 else if (0)
114 return 2;
115 } // expected-warning {{non-void function does not return a value in all control paths}}
117 int test21(void) {
118 int i;
119 if (i)
120 return 0;
121 else if (1)
122 return 2;
125 int test22(void) {
126 int i;
127 switch (i) default: ;
128 } // expected-warning {{non-void function does not return a value}}
130 int test23(void) {
131 int i;
132 switch (i) {
133 case 0:
134 return 0;
135 case 2:
136 return 2;
138 } // expected-warning {{non-void function does not return a value in all control paths}}
140 int test24(void) {
141 int i;
142 switch (i) {
143 case 0:
144 return 0;
145 case 2:
146 return 2;
147 default:
148 return -1;
152 int test25(void) {
153 1 ? halt3() : unknown();
156 int test26(void) {
157 0 ? halt3() : unknown();
158 } // expected-warning {{non-void function does not return a value}}
160 int j;
161 void (*fptr)(void) __attribute__((noreturn));
162 int test27(void) {
163 switch (j) {
164 case 1:
165 do { } while (1);
166 break;
167 case 2:
168 for (;;) ;
169 break;
170 case 3:
171 for (;1;) ;
172 for (;0;) {
173 goto done;
175 return 1;
176 case 4:
177 while (0) { goto done; }
178 return 1;
179 case 5:
180 while (1) { return 1; }
181 break;
182 case 6:
183 fptr();
184 break;
185 default:
186 return 1;
188 done: ;
191 // PR4624
192 void test28() __attribute__((noreturn));
193 void test28(x) { while (1) { } } // expected-warning {{parameter 'x' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int}}
195 void exit(int);
196 int test29(void) {
197 exit(1);
200 // Include these declarations here explicitly so we don't depend on system headers.
201 typedef struct __jmp_buf_tag{} jmp_buf[1];
203 extern void longjmp (struct __jmp_buf_tag __env[1], int __val) __attribute__ ((noreturn));
204 extern void _longjmp (struct __jmp_buf_tag __env[1], int __val) __attribute__ ((noreturn));
206 jmp_buf test30_j;
208 int test30(void) {
209 if (j)
210 longjmp(test30_j, 1);
211 else
212 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
213 longjmp(test30_j, 2);
214 #else
215 _longjmp(test30_j, 1);
216 #endif
219 typedef void test31_t(int status);
220 void test31(test31_t *callback __attribute__((noreturn)));
222 void test32(void) {
223 ^ (void) { while (1) { } }();
224 ^ (void) { if (j) while (1) { } }();
225 while (1) { }
228 void test33(void) {
229 if (j) while (1) { }
232 // Test that 'static inline' functions are only analyzed for CFG-based warnings
233 // when they are used.
234 static inline int si_has_missing_return(void) {} // expected-warning{{non-void function does not return a value}}
235 static inline int si_has_missing_return_2(void) {}; // expected-warning{{non-void function does not return a value}}
236 static inline int si_forward(void);
237 static inline int si_has_missing_return_3(int x) {
238 if (x)
239 return si_has_missing_return_3(x+1);
240 } // expected-warning{{non-void function does not return a value in all control paths}}
242 int test_static_inline(int x) {
243 si_forward();
244 return x ? si_has_missing_return_2() : si_has_missing_return_3(x);
246 static inline int si_forward(void) {} // expected-warning{{non-void function does not return a value}}
248 // Test warnings on ignored qualifiers on return types.
249 const int ignored_c_quals(void); // expected-warning{{'const' type qualifier on return type has no effect}}
250 const volatile int ignored_cv_quals(void); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
251 char* const volatile restrict ignored_cvr_quals(void); // expected-warning{{'const volatile restrict' type qualifiers on return type have no effect}}
253 typedef const int CI;
254 CI ignored_quals_typedef(void);
256 const CI ignored_quals_typedef_2(void); // expected-warning{{'const' type qualifier}}
258 // Test that for switch(enum) that if the switch statement covers all the cases
259 // that we don't consider that for -Wreturn-type.
260 enum Cases { C1, C2, C3, C4 };
261 int test_enum_cases(enum Cases C) {
262 switch (C) {
263 case C1: return 1;
264 case C2: return 2;
265 case C4: return 3;
266 case C3: return 4;
268 } // no-warning
270 // PR12318 - Don't give a may reach end of non-void function warning.
271 int test34(int x) {
272 if (x == 1) {
273 return 3;
274 } else if ( x == 2 || 1) {
275 return 5;
279 // PR18999
280 int test35(void) {
281 lbl:
282 if (1)
283 goto lbl;
286 int test36a(int b) {
287 if (b)
288 return 43;
289 __builtin_unreachable();
292 int test36b(int b) {
293 if (b)
294 return 43;
295 __builtin_assume(0);
298 // PR19074.
299 void abort(void) __attribute__((noreturn));
300 #define av_assert0(cond) do {\
301 if (!(cond)) {\
302 abort();\
304 } while (0)
306 int PR19074(int x) {
307 switch(x) {
308 case 0:
309 return 0;
310 default:
311 av_assert0(0);
312 } // no-warning
315 int PR19074_positive(int x) {
316 switch(x) {
317 case 0:
318 return 0;
319 default:
320 break;
322 } // expected-warning {{non-void function does not return a value in all control paths}}
324 // sizeof(long) test.
325 int sizeof_long(void) {
326 if (sizeof(long) == 4)
327 return 1;
328 if (sizeof(long) == 8)
329 return 2;
330 } // no-warning
332 int return_statement_expression(void) {
333 if (unknown())
334 return ({
335 while (0)
339 else
340 return 0;
341 } // no-warning (used to be "non-void function does not return a value in all control paths")