summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/warn-unreachable.cpp
blob: 843720fa5fd34593f4be7c296a9f6429f2e3c530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -Wunreachable-code-aggressive -Wno-unused-value

int &halt() __attribute__((noreturn));
int &live();
int dead();
int liveti() throw(int);
int (*livetip)() throw(int);

int test1() {
  try {
    live();
  } catch (int i) {
    live();
  }
  return 1;
}

void test2() {
  try {
    live();
  } catch (int i) {
    live();
  }
  try {
    liveti();
  } catch (int i) {
    live();
  }
  try {
    livetip();
  } catch (int i) {
    live();
  }
  throw 1;
  dead();       // expected-warning {{will never be executed}}
}


void test3() {
  halt()
    --;         // expected-warning {{will never be executed}}
  // FIXME: The unreachable part is just the '?', but really all of this
  // code is unreachable and shouldn't be separately reported.
  halt()        // expected-warning {{will never be executed}}
    ? 
    dead() : dead();
  live(),
    float       
      (halt()); // expected-warning {{will never be executed}}
}

void test4() {
  struct S {
    int mem;
  } s;
  S &foor();
  halt(), foor()// expected-warning {{will never be executed}}
    .mem;       
}

void test5() {
  struct S {
    int mem;
  } s;
  S &foonr() __attribute__((noreturn));
  foonr()
    .mem;       // expected-warning {{will never be executed}}
}

void test6() {
  struct S {
    ~S() { }
    S(int i) { }
  };
  live(),
    S
      (halt());  // expected-warning {{will never be executed}}
}

// Don't warn about unreachable code in template instantiations, as
// they may only be unreachable in that specific instantiation.
void isUnreachable();

template <typename T> void test_unreachable_templates() {
  T::foo();
  isUnreachable();  // no-warning
}

struct TestUnreachableA {
  static void foo() __attribute__((noreturn));
};
struct TestUnreachableB {
  static void foo();
};

void test_unreachable_templates_harness() {
  test_unreachable_templates<TestUnreachableA>();
  test_unreachable_templates<TestUnreachableB>(); 
}

// Do warn about explict template specializations, as they represent
// actual concrete functions that somebody wrote.

template <typename T> void funcToSpecialize() {}
template <> void funcToSpecialize<int>() {
  halt();
  dead(); // expected-warning {{will never be executed}}
}

// Handle 'try' code dominating a dead return.
enum PR19040_test_return_t
{ PR19040_TEST_FAILURE };
namespace PR19040_libtest
{
  class A {
  public:
    ~A ();
  };
}
PR19040_test_return_t PR19040_fn1 ()
{
    try
    {
        throw PR19040_libtest::A ();
    } catch (...)
    {
        return PR19040_TEST_FAILURE;
    }
    return PR19040_TEST_FAILURE; // expected-warning {{will never be executed}}
}

__attribute__((noreturn))
void raze();

namespace std {
template<typename T> struct basic_string {
  basic_string(const T* x) {}
  ~basic_string() {};
};
typedef basic_string<char> string;
}

std::string testStr() {
  raze();
  return ""; // no-warning
}

std::string testStrWarn(const char *s) {
  raze();
  return s; // expected-warning {{will never be executed}}
}

bool testBool() {
  raze();
  return true; // no-warning
}

static const bool ConditionVar = 1;
int test_global_as_conditionVariable() {
  if (ConditionVar)
    return 1;
  return 0; // no-warning
}

// Handle unreachable temporary destructors.
class A {
public:
  A();
  ~A();
};

__attribute__((noreturn))
void raze(const A& x);

void test_with_unreachable_tmp_dtors(int x) {
  raze(x ? A() : A()); // no-warning
}

// Test sizeof - sizeof in enum declaration.
enum { BrownCow = sizeof(long) - sizeof(char) };
enum { CowBrown = 8 - 1 };


int test_enum_sizeof_arithmetic() {
  if (BrownCow)
    return 1;
  return 2;
}

int test_enum_arithmetic() {
  if (CowBrown)
    return 1;
  return 2; // expected-warning {{never be executed}}
}

int test_arithmetic() {
  if (8 -1)
    return 1;
  return 2; // expected-warning {{never be executed}}
}

OpenPOWER on IntegriCloud