1 // RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -std=c++11 -Wunreachable-code-aggressive -Wno-unused-value -Wno-tautological-compare
3 int &halt() __attribute__((noreturn
));
6 int liveti() throw(int);
7 int (*livetip
)() throw(int);
35 dead(); // expected-warning {{will never be executed}}
41 --; // expected-warning {{will never be executed}}
42 // FIXME: The unreachable part is just the '?', but really all of this
43 // code is unreachable and shouldn't be separately reported.
44 halt() // expected-warning {{will never be executed}}
49 (halt()); // expected-warning {{will never be executed}}
58 halt(), foor()// expected-warning {{will never be executed}}
67 S
&foonr() __attribute__((noreturn
));
70 .mem
; // expected-warning {{will never be executed}}
81 (halt()); // expected-warning {{will never be executed}}
84 // Don't warn about unreachable code in template instantiations, as
85 // they may only be unreachable in that specific instantiation.
88 template <typename T
> void test_unreachable_templates() {
90 isUnreachable(); // no-warning
93 struct TestUnreachableA
{
94 static void foo() __attribute__((noreturn
));
96 struct TestUnreachableB
{
100 void test_unreachable_templates_harness() {
101 test_unreachable_templates
<TestUnreachableA
>();
102 test_unreachable_templates
<TestUnreachableB
>();
105 // Do warn about explicit template specializations, as they represent
106 // actual concrete functions that somebody wrote.
108 template <typename T
> void funcToSpecialize() {}
109 template <> void funcToSpecialize
<int>() {
111 dead(); // expected-warning {{will never be executed}}
114 // Handle 'try' code dominating a dead return.
115 enum PR19040_test_return_t
116 { PR19040_TEST_FAILURE
};
117 namespace PR19040_libtest
124 PR19040_test_return_t
PR19040_fn1 ()
128 throw PR19040_libtest::A ();
131 return PR19040_TEST_FAILURE
;
133 return PR19040_TEST_FAILURE
; // expected-warning {{will never be executed}}
136 __attribute__((noreturn
))
140 template<typename T
> struct basic_string
{
141 basic_string(const T
* x
) {}
144 typedef basic_string
<char> string
;
147 std::string
testStr() {
149 return ""; // expected-warning {{'return' will never be executed}}
152 std::string
testStrWarn(const char *s
) {
154 return s
; // expected-warning {{will never be executed}}
159 return true; // expected-warning {{'return' will never be executed}}
162 static const bool ConditionVar
= 1;
163 int test_global_as_conditionVariable() {
166 return 0; // no-warning
169 // Handle unreachable temporary destructors.
176 __attribute__((noreturn
))
177 void raze(const A
& x
);
179 void test_with_unreachable_tmp_dtors(int x
) {
180 raze(x
? A() : A()); // no-warning
183 // Test sizeof - sizeof in enum declaration.
184 enum { BrownCow
= sizeof(long) - sizeof(char) };
185 enum { CowBrown
= 8 - 1 };
188 int test_enum_sizeof_arithmetic() {
194 int test_enum_arithmetic() {
197 return 2; // expected-warning {{never be executed}}
200 int test_arithmetic() {
203 return 2; // expected-warning {{never be executed}}
206 int test_treat_const_bool_local_as_config_value() {
207 const bool controlValue
= false;
210 test_treat_const_bool_local_as_config_value(); // no-warning
214 int test_treat_non_const_bool_local_as_non_config_value() {
215 bool controlValue
= false;
218 // There is no warning here because 'controlValue' isn't really
219 // a control value at all. The CFG will not treat this
220 // branch as unreachable.
221 test_treat_non_const_bool_local_as_non_config_value(); // no-warning
225 void test_do_while(int x
) {
226 // Handle trivial expressions with
227 // implicit casts to bool.
230 } while (0); // no-warning
239 Frobozz
test_return_object(int flag
) {
240 return Frobozz(flag
);
241 return Frobozz(42); // expected-warning {{'return' will never be executed}}
244 Frobozz
test_return_object_control_flow(int flag
) {
245 return Frobozz(flag
);
246 return Frobozz(flag
? 42 : 24); // expected-warning {{code will never be executed}}
249 void somethingToCall();
251 static constexpr bool isConstExprConfigValue() { return true; }
253 int test_const_expr_config_value() {
254 if (isConstExprConfigValue()) {
258 somethingToCall(); // no-warning
261 int test_const_expr_config_value_2() {
262 if (!isConstExprConfigValue()) {
263 somethingToCall(); // no-warning
272 static const bool aHobbit
= true;
275 void test_static_class_var() {
279 somethingToCall(); // no-warning
282 void test_static_class_var(Frodo
&F
) {
286 somethingToCall(); // no-warning
289 void test_unreachable_for_null_increment() {
290 for (unsigned i
= 0; i
< 10 ; ) // no-warning
294 void test_unreachable_forrange_increment() {
296 for (auto i
: x
) { // expected-warning {{loop will run at most once (loop increment never executed)}}
303 // Test "silencing" with parentheses.
304 void test_with_paren_silencing(int x
) {
305 if (false) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
306 if ((false)) calledFun(); // no-warning
308 if (true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
311 calledFun(); // expected-warning {{will never be executed}}
316 calledFun(); // no-warning
318 if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
319 calledFun(); // expected-warning {{code will never be executed}}
324 calledFun(); // no-warning
329 calledFun(); // no-warning
334 void test_with_paren_silencing_impcast(int x
) {
335 if (0) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
336 if ((0)) calledFun(); // no-warning
338 if (1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
341 calledFun(); // expected-warning {{will never be executed}}
346 calledFun(); // no-warning
348 if (!1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
349 calledFun(); // expected-warning {{code will never be executed}}
354 calledFun(); // no-warning
359 calledFun(); // no-warning
364 void tautological_compare(bool x
, int y
) {
365 if (x
> 10) // expected-note {{silence}}
366 calledFun(); // expected-warning {{will never be executed}}
367 if (10 < x
) // expected-note {{silence}}
368 calledFun(); // expected-warning {{will never be executed}}
369 if (x
== 10) // expected-note {{silence}}
370 calledFun(); // expected-warning {{will never be executed}}
372 if (x
< 10) // expected-note {{silence}}
375 calledFun(); // expected-warning {{will never be executed}}
376 if (10 > x
) // expected-note {{silence}}
379 calledFun(); // expected-warning {{will never be executed}}
380 if (x
!= 10) // expected-note {{silence}}
383 calledFun(); // expected-warning {{will never be executed}}
385 if (y
!= 5 && y
== 5) // expected-note {{silence}}
386 calledFun(); // expected-warning {{will never be executed}}
388 if (y
> 5 && y
< 4) // expected-note {{silence}}
389 calledFun(); // expected-warning {{will never be executed}}
391 if (y
< 10 || y
> 5) // expected-note {{silence}}
394 calledFun(); // expected-warning {{will never be executed}}
396 if (y
== -1 && y
!= -1) // expected-note {{silence}}
397 calledFun(); // expected-warning {{will never be executed}}
399 // TODO: Extend warning to the following code: