xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/unreachable-code.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -Wunreachable-code -fblocks -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc int j;
4*f4a2713aSLionel Sambuc void bar() { }
5*f4a2713aSLionel Sambuc int test1() {
6*f4a2713aSLionel Sambuc   for (int i = 0;
7*f4a2713aSLionel Sambuc        i != 10;
8*f4a2713aSLionel Sambuc        ++i) {  // expected-warning {{will never be executed}}
9*f4a2713aSLionel Sambuc     if (j == 23) // missing {}'s
10*f4a2713aSLionel Sambuc       bar();
11*f4a2713aSLionel Sambuc       return 1;
12*f4a2713aSLionel Sambuc   }
13*f4a2713aSLionel Sambuc   return 0;
14*f4a2713aSLionel Sambuc   return 1;    // expected-warning {{will never be executed}}
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc void test2(int i) {
18*f4a2713aSLionel Sambuc   switch (i) {
19*f4a2713aSLionel Sambuc   case 0:
20*f4a2713aSLionel Sambuc     break;
21*f4a2713aSLionel Sambuc     bar();     // expected-warning {{will never be executed}}
22*f4a2713aSLionel Sambuc   case 2:
23*f4a2713aSLionel Sambuc     switch (i) {
24*f4a2713aSLionel Sambuc     default:
25*f4a2713aSLionel Sambuc     a: goto a;
26*f4a2713aSLionel Sambuc     }
27*f4a2713aSLionel Sambuc     bar();     // expected-warning {{will never be executed}}
28*f4a2713aSLionel Sambuc   }
29*f4a2713aSLionel Sambuc   b: goto b;
30*f4a2713aSLionel Sambuc   bar();       // expected-warning {{will never be executed}}
31*f4a2713aSLionel Sambuc }
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc void test3() {
34*f4a2713aSLionel Sambuc   ^{ return;
35*f4a2713aSLionel Sambuc      bar();    // expected-warning {{will never be executed}}
36*f4a2713aSLionel Sambuc   }();
37*f4a2713aSLionel Sambuc   while (++j) {
38*f4a2713aSLionel Sambuc     continue;
39*f4a2713aSLionel Sambuc     bar();     // expected-warning {{will never be executed}}
40*f4a2713aSLionel Sambuc   }
41*f4a2713aSLionel Sambuc }
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc // PR 6130 - Don't warn about bogus unreachable code with throw's and
44*f4a2713aSLionel Sambuc // temporary objects.
45*f4a2713aSLionel Sambuc class PR6130 {
46*f4a2713aSLionel Sambuc public:
47*f4a2713aSLionel Sambuc   PR6130();
48*f4a2713aSLionel Sambuc   ~PR6130();
49*f4a2713aSLionel Sambuc };
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc int pr6130(unsigned i) {
52*f4a2713aSLionel Sambuc   switch(i) {
53*f4a2713aSLionel Sambuc     case 0: return 1;
54*f4a2713aSLionel Sambuc     case 1: return 2;
55*f4a2713aSLionel Sambuc     default:
56*f4a2713aSLionel Sambuc       throw PR6130(); // no-warning
57*f4a2713aSLionel Sambuc   }
58*f4a2713aSLionel Sambuc }
59