blob: f36300af4d38322f5838d7e84fe1ff63ad63ada1 [file] [log] [blame]
Anders Carlssonabea9512011-02-28 00:40:07 +00001// RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -Wunreachable-code -Wno-unused-value
Mike Stump4c45aa12010-01-21 15:20:48 +00002
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +00003int &halt() __attribute__((noreturn));
Mike Stump55f988e2010-01-21 17:21:23 +00004int &live();
Mike Stump4c45aa12010-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 Stump55f988e2010-01-21 17:21:23 +000037
38
39void test3() {
40 halt()
41 --; // expected-warning {{will never be executed}}
Ted Kremenek7dd3c732010-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 Stumpe5fba702010-01-21 19:44:04 +000046 dead() : dead();
Mike Stump2d6ceab2010-01-21 22:12:18 +000047 live(),
Ted Kremenek0c8e5a02011-07-19 14:18:48 +000048 float
49 (halt()); // expected-warning {{will never be executed}}
Mike Stump55f988e2010-01-21 17:21:23 +000050}
Mike Stumpb5c77552010-01-21 23:15:53 +000051
52void test4() {
53 struct S {
54 int mem;
55 } s;
56 S &foor();
Zhongxing Xu91071662010-02-24 02:19:28 +000057 halt(), foor()// expected-warning {{will never be executed}}
58 .mem;
Mike Stumpb5c77552010-01-21 23:15:53 +000059}
60
61void test5() {
62 struct S {
63 int mem;
64 } s;
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +000065 S &foor() __attribute__((noreturn));
Mike Stumpb5c77552010-01-21 23:15:53 +000066 foor()
67 .mem; // expected-warning {{will never be executed}}
68}
69
70void test6() {
71 struct S {
72 ~S() { }
73 S(int i) { }
74 };
75 live(),
Ted Kremenek0c8e5a02011-07-19 14:18:48 +000076 S
77 (halt()); // expected-warning {{will never be executed}}
Mike Stumpb5c77552010-01-21 23:15:53 +000078}
Ted Kremenek5dfee062011-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 Kremenek75df4ee2011-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 Blaikie25f4c192012-01-24 04:29:27 +0000109