]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/SemaCXX/warn-unreachable.cpp
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / SemaCXX / warn-unreachable.cpp
1 // RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -Wunreachable-code -Wno-unused-value
2
3 int &halt() __attribute__((noreturn));
4 int &live();
5 int dead();
6 int liveti() throw(int);
7 int (*livetip)() throw(int);
8
9 int test1() {
10   try {
11     live();
12   } catch (int i) {
13     live();
14   }
15   return 1;
16 }
17
18 void 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 }
37
38
39 void test3() {
40   halt()
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}}
45     ? 
46     dead() : dead();
47   live(),
48     float       
49       (halt()); // expected-warning {{will never be executed}}
50 }
51
52 void test4() {
53   struct S {
54     int mem;
55   } s;
56   S &foor();
57   halt(), foor()// expected-warning {{will never be executed}}
58     .mem;       
59 }
60
61 void test5() {
62   struct S {
63     int mem;
64   } s;
65   S &foonr() __attribute__((noreturn));
66   foonr()
67     .mem;       // expected-warning {{will never be executed}}
68 }
69
70 void test6() {
71   struct S {
72     ~S() { }
73     S(int i) { }
74   };
75   live(),
76     S
77       (halt());  // expected-warning {{will never be executed}}
78 }
79
80 // Don't warn about unreachable code in template instantiations, as
81 // they may only be unreachable in that specific instantiation.
82 void isUnreachable();
83
84 template <typename T> void test_unreachable_templates() {
85   T::foo();
86   isUnreachable();  // no-warning
87 }
88
89 struct TestUnreachableA {
90   static void foo() __attribute__((noreturn));
91 };
92 struct TestUnreachableB {
93   static void foo();
94 };
95
96 void test_unreachable_templates_harness() {
97   test_unreachable_templates<TestUnreachableA>();
98   test_unreachable_templates<TestUnreachableB>(); 
99 }
100
101 // Do warn about explict template specializations, as they represent
102 // actual concrete functions that somebody wrote.
103
104 template <typename T> void funcToSpecialize() {}
105 template <> void funcToSpecialize<int>() {
106   halt();
107   dead(); // expected-warning {{will never be executed}}
108 }
109