• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
3 // clang emits the following warning by default.
4 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the
5 // following warning.
t14()6 int t14() {
7   return; // expected-warning {{non-void function 't14' should return a value}}
8 }
9 
t15()10 void t15() {
11   return 1; // expected-warning {{void function 't15' should not return a value}}
12 }
13 
14 int unknown();
15 
test0()16 void test0() {
17 }
18 
test1()19 int test1() {
20 } // expected-warning {{control reaches end of non-void function}}
21 
test2()22 int test2() {
23   a: goto a;
24 }
25 
test3()26 int test3() {
27   goto a;
28   a: ;
29 } // expected-warning {{control reaches end of non-void function}}
30 
31 
halt()32 void halt() {
33   a: goto a;
34 }
35 
36 void halt2() __attribute__((noreturn));
37 
test4()38 int test4() {
39   halt2();
40 }
41 
test5()42 int test5() {
43   halt2(), (void)1;
44 }
45 
test6()46 int test6() {
47   1, halt2();
48 }
49 
50 int j;
unknown_nohalt()51 int unknown_nohalt() {
52   return j;
53 }
54 
test7()55 int test7() {
56   unknown();
57 } // expected-warning {{control reaches end of non-void function}}
58 
test8()59 int test8() {
60   (void)(1 + unknown());
61 } // expected-warning {{control reaches end of non-void function}}
62 
63 int halt3() __attribute__((noreturn));
64 
test9()65 int test9() {
66   (void)(halt3() + unknown());
67 }
68 
test10()69 int test10() {
70   (void)(unknown() || halt3());
71 } // expected-warning {{control may reach end of non-void function}}
72 
test11()73 int test11() {
74   (void)(unknown() && halt3());
75 } // expected-warning {{control may reach end of non-void function}}
76 
test12()77 int test12() {
78   (void)(halt3() || unknown());
79 }
80 
test13()81 int test13() {
82   (void)(halt3() && unknown());
83 }
84 
test14()85 int test14() {
86   (void)(1 || unknown());
87 } // expected-warning {{control reaches end of non-void function}}
88 
test15()89 int test15() {
90   (void)(0 || unknown());
91 } // expected-warning {{control reaches end of non-void function}}
92 
test16()93 int test16() {
94   (void)(0 && unknown());
95 } // expected-warning {{control reaches end of non-void function}}
96 
test17()97 int test17() {
98   (void)(1 && unknown());
99 } // expected-warning {{control reaches end of non-void function}}
100 
test18()101 int test18() {
102   (void)(unknown_nohalt() && halt3());
103 } // expected-warning {{control may reach end of non-void function}}
104 
test19()105 int test19() {
106   (void)(unknown_nohalt() && unknown());
107 } // expected-warning {{control reaches end of non-void function}}
108 
test20()109 int test20() {
110   int i;
111   if (i)
112     return 0;
113   else if (0)
114     return 2;
115 } // expected-warning {{control may reach end of non-void function}}
116 
test21()117 int test21() {
118   int i;
119   if (i)
120     return 0;
121   else if (1)
122     return 2;
123 }
124 
test22()125 int test22() {
126   int i;
127   switch (i) default: ;
128 } // expected-warning {{control reaches end of non-void function}}
129 
test23()130 int test23() {
131   int i;
132   switch (i) {
133   case 0:
134     return 0;
135   case 2:
136     return 2;
137   }
138 } // expected-warning {{control may reach end of non-void function}}
139 
test24()140 int test24() {
141   int i;
142   switch (i) {
143     case 0:
144     return 0;
145   case 2:
146     return 2;
147   default:
148     return -1;
149   }
150 }
151 
test25()152 int test25() {
153   1 ? halt3() : unknown();
154 }
155 
test26()156 int test26() {
157   0 ? halt3() : unknown();
158 } // expected-warning {{control reaches end of non-void function}}
159 
160 int j;
161 void (*fptr)() __attribute__((noreturn));
test27()162 int test27() {
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;
174     }
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;
187   }
188   done: ;
189 }
190 
191 // PR4624
192 void test28() __attribute__((noreturn));
test28(x)193 void test28(x) { while (1) { } }
194 
195 void exit(int);
test29()196 int test29() {
197   exit(1);
198 }
199 
200 // Include these declarations here explicitly so we don't depend on system headers.
201 typedef struct __jmp_buf_tag{} jmp_buf[1];
202 
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));
205 
206 jmp_buf test30_j;
207 
test30()208 int test30() {
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
217 }
218 
219 typedef void test31_t(int status);
220 void test31(test31_t *callback __attribute__((noreturn)));
221 
test32()222 void test32() {
223   ^ (void) { while (1) { } }();
224   ^ (void) { if (j) while (1) { } }();
225   while (1) { }
226 }
227 
test33()228 void test33() {
229   if (j) while (1) { }
230 }
231 
232 // Test that 'static inline' functions are only analyzed for CFG-based warnings
233 // when they are used.
si_has_missing_return()234 static inline int si_has_missing_return() {} // expected-warning{{control reaches end of non-void function}}
si_has_missing_return_2()235 static inline int si_has_missing_return_2() {}; // expected-warning{{control reaches end of non-void function}}
236 static inline int si_forward();
si_has_missing_return_3(int x)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{{control may reach end of non-void function}}
241 
test_static_inline(int x)242 int test_static_inline(int x) {
243   si_forward();
244   return x ? si_has_missing_return_2() : si_has_missing_return_3(x);
245 }
si_forward()246 static inline int si_forward() {} // expected-warning{{control reaches end of non-void function}}
247 
248 // Test warnings on ignored qualifiers on return types.
249 const int ignored_c_quals(); // expected-warning{{'const' type qualifier on return type has no effect}}
250 const volatile int ignored_cv_quals(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
251 char* const volatile restrict ignored_cvr_quals(); // expected-warning{{'const volatile restrict' type qualifiers on return type have no effect}}
252 
253 typedef const int CI;
254 CI ignored_quals_typedef();
255 
256 const CI ignored_quals_typedef_2(); // expected-warning{{'const' type qualifier}}
257 
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 };
test_enum_cases(enum Cases C)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;
267   }
268 } // no-warning
269 
270 // PR12318 - Don't give a may reach end of non-void function warning.
test34(int x)271 int test34(int x) {
272   if (x == 1) {
273     return 3;
274   } else if ( x == 2 || 1) {
275     return 5;
276   }
277 }
278 
279 // PR18999
test35()280 int test35() {
281 lbl:
282   if (1)
283     goto lbl;
284 }
285 
286 // PR19074.
287 void abort(void) __attribute__((noreturn));
288 #define av_assert0(cond) do {\
289     if (!(cond)) {\
290       abort();\
291     }\
292   } while (0)
293 
PR19074(int x)294 int PR19074(int x) {
295   switch(x) {
296   case 0:
297     return 0;
298   default:
299     av_assert0(0);
300   } // no-warning
301 }
302 
PR19074_positive(int x)303 int PR19074_positive(int x) {
304   switch(x) {
305   case 0:
306     return 0;
307   default:
308     break;
309   }
310 } // expected-warning {{control may reach end of non-void function}}
311 
312 // sizeof(long) test.
sizeof_long()313 int sizeof_long() {
314   if (sizeof(long) == 4)
315     return 1;
316   if (sizeof(long) == 8)
317     return 2;
318 } // no-warning
319