blob: dd071258e29d16faafe50816fbb530c076730804 [file] [log] [blame]
Anders Carlsson6774b1f2011-02-28 00:40:07 +00001// RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -Wunreachable-code -Wno-unused-value
Mike Stump04c68512010-01-21 15:20:48 +00002
Anders Carlssonaf7534f2010-09-03 00:25:02 +00003int &halt() __attribute__((noreturn));
Mike Stumpcc3a8532010-01-21 17:21:23 +00004int &live();
Mike Stump04c68512010-01-21 15:20:48 +00005int dead();
6int liveti() throw(int);
7int (*livetip)() throw(int);
8
9int test1() {
10 try {
11 live();
12 } catch (int i) {
13 live();
14 }
15 return 1;
16}
17
18void test2() {
19 try {
20 live();
21 } catch (int i) {
22 live();
23 }
24 try {
25 liveti();
26 } catch (int i) {
27 live();
28 }
29 try {
30 livetip();
31 } catch (int i) {
32 live();
33 }
34 throw 1;
35 dead(); // expected-warning {{will never be executed}}
36}
Mike Stumpcc3a8532010-01-21 17:21:23 +000037
38
39void test3() {
40 halt()
41 --; // expected-warning {{will never be executed}}
Ted Kremenekf8e4b482010-12-16 08:22:16 +000042 // 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}}
45 ?
Mike Stumpc18c4032010-01-21 19:44:04 +000046 dead() : dead();
Mike Stumpfcd6f942010-01-21 22:12:18 +000047 live(),
Ted Kremenek9e100ea2011-07-19 14:18:48 +000048 float
49 (halt()); // expected-warning {{will never be executed}}
Mike Stumpcc3a8532010-01-21 17:21:23 +000050}
Mike Stump60dbeeb2010-01-21 23:15:53 +000051
52void test4() {
53 struct S {
54 int mem;
55 } s;
56 S &foor();
Zhongxing Xua396e612010-02-24 02:19:28 +000057 halt(), foor()// expected-warning {{will never be executed}}
58 .mem;
Mike Stump60dbeeb2010-01-21 23:15:53 +000059}
60
61void test5() {
62 struct S {
63 int mem;
64 } s;
Richard Smith541b38b2013-09-20 01:15:31 +000065 S &foonr() __attribute__((noreturn));
66 foonr()
Mike Stump60dbeeb2010-01-21 23:15:53 +000067 .mem; // expected-warning {{will never be executed}}
68}
69
70void test6() {
71 struct S {
72 ~S() { }
73 S(int i) { }
74 };
75 live(),
Ted Kremenek9e100ea2011-07-19 14:18:48 +000076 S
77 (halt()); // expected-warning {{will never be executed}}
Mike Stump60dbeeb2010-01-21 23:15:53 +000078}
Ted Kremenek7f770032011-11-30 21:22:09 +000079
80// Don't warn about unreachable code in template instantiations, as
81// they may only be unreachable in that specific instantiation.
82void isUnreachable();
83
84template <typename T> void test_unreachable_templates() {
85 T::foo();
86 isUnreachable(); // no-warning
87}
88
89struct TestUnreachableA {
90 static void foo() __attribute__((noreturn));
91};
92struct TestUnreachableB {
93 static void foo();
94};
95
96void test_unreachable_templates_harness() {
97 test_unreachable_templates<TestUnreachableA>();
98 test_unreachable_templates<TestUnreachableB>();
99}
100
Ted Kremenek85825ae2011-12-01 00:59:17 +0000101// Do warn about explict template specializations, as they represent
102// actual concrete functions that somebody wrote.
103
104template <typename T> void funcToSpecialize() {}
105template <> void funcToSpecialize<int>() {
106 halt();
107 dead(); // expected-warning {{will never be executed}}
108}
David Blaikie13ab2a42012-01-24 04:29:27 +0000109