summaryrefslogtreecommitdiff
path: root/test/SemaCXX/exceptions.cpp
blob: 1e786adaa1b41363fda36069b00048466a4eddb5 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s

struct A; // expected-note 4 {{forward declaration of 'A'}}

struct Abstract { virtual void f() = 0; }; // expected-note {{unimplemented pure virtual method 'f'}}

void trys() {
  int k = 42;
  try {
  } catch(int i) { // expected-note {{previous definition}}
    int j = i;
    int i; // expected-error {{redefinition of 'i'}}
  } catch(float i) {
  } catch(void v) { // expected-error {{cannot catch incomplete type 'void'}}
  } catch(A a) { // expected-error {{cannot catch incomplete type 'A'}}
  } catch(A *a) { // expected-error {{cannot catch pointer to incomplete type 'A'}}
  } catch(A &a) { // expected-error {{cannot catch reference to incomplete type 'A'}}
  } catch(Abstract) { // expected-error {{variable type 'Abstract' is an abstract class}}
  } catch(...) {
    int ref = k;
    {
      int ref = k;
    }
    int j = i; // expected-error {{use of undeclared identifier 'i'}}
  }

  try {
  } catch(...) { // expected-error {{catch-all handler must come last}}
  } catch(int) {
  }
}

void throws() {
  throw;
  throw 0;
  throw throw; // expected-error {{cannot throw object of incomplete type 'void'}}
  throw (A*)0; // expected-error {{cannot throw pointer to object of incomplete type 'A'}}
}

void jumps() {
l1:
  goto l5;
  goto l4; // expected-error {{cannot jump}}
  goto l3; // expected-error {{cannot jump}}
  goto l2; // expected-error {{cannot jump}}
  goto l1;
  try { // expected-note 4 {{jump bypasses initialization of try block}}
  l2:
    goto l5;
    goto l4; // expected-error {{cannot jump}}
    goto l3; // expected-error {{cannot jump}}
    goto l2;
    goto l1;
  } catch(int) { // expected-note 4 {{jump bypasses initialization of catch block}}
  l3:
    goto l5;
    goto l4; // expected-error {{cannot jump}}
    goto l3;
    goto l2; // expected-error {{cannot jump}}
    goto l1;
  } catch(...) { // expected-note 4 {{jump bypasses initialization of catch block}}
  l4:
    goto l5;
    goto l4;
    goto l3; // expected-error {{cannot jump}}
    goto l2; // expected-error {{cannot jump}}
    goto l1;
  }
l5:
  goto l5;
  goto l4; // expected-error {{cannot jump}}
  goto l3; // expected-error {{cannot jump}}
  goto l2; // expected-error {{cannot jump}}
  goto l1;
}

struct BadReturn {
  BadReturn() try {
  } catch(...) {
    // Try to hide
    try {
    } catch(...) {
      {
        if (0)
          return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
      }
    }
  }
  BadReturn(int);
};

BadReturn::BadReturn(int) try {
} catch(...) {
  // Try to hide
  try {
  } catch(int) {
    return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
  } catch(...) {
    {
      if (0)
        return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
    }
  }
}

// Cannot throw an abstract type.
class foo {
public:
  foo() {}
  void bar () {
    throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}}
  }
  virtual void test () = 0; // expected-note{{unimplemented pure virtual method 'test'}}
};

namespace PR6831 {
  namespace NA { struct S; }
  namespace NB { struct S; }
  
  void f() {
    using namespace NA;
    using namespace NB;
    try {
    } catch (int S) { 
    }
  }
}

namespace Decay {
  struct A {
    void f() throw (A[10]);
  };

  template<typename T> struct B {
    void f() throw (B[10]);
  };
  template struct B<int>;

  void f() throw (int[10], int(*)());
  void f() throw (int*, int());

  template<typename T> struct C {
    void f() throw (T);
#if __cplusplus <= 199711L
    // expected-error@-2 {{pointer to incomplete type 'Decay::E' is not allowed in exception specification}}
#endif
  };
  struct D {
    C<D[10]> c;
  };
  struct E;
#if __cplusplus <= 199711L
  // expected-note@-2 {{forward declaration of 'Decay::E'}}
#endif

  C<E[10]> e;
#if __cplusplus <= 199711L
  // expected-note@-2 {{in instantiation of template class 'Decay::C<Decay::E [10]>' requested here}}
#endif
}

void rval_ref() throw (int &&); // expected-error {{rvalue reference type 'int &&' is not allowed in exception specification}}
#if __cplusplus <= 199711L
// expected-warning@-2 {{rvalue references are a C++11 extension}}
#endif

namespace HandlerInversion {
struct B {};
struct D : B {};
struct D2 : D {};

void f1() {
  try {
  } catch (B &b) { // expected-note {{for type 'HandlerInversion::B &'}}
  } catch (D &d) { // expected-warning {{exception of type 'HandlerInversion::D &' will be caught by earlier handler}}
  }
}

void f2() {
  try {
  } catch (B *b) { // expected-note {{for type 'HandlerInversion::B *'}}
  } catch (D *d) { // expected-warning {{exception of type 'HandlerInversion::D *' will be caught by earlier handler}}
  }
}

void f3() {
  try {
  } catch (D &d) { // Ok
  } catch (B &b) {
  }
}

void f4() {
  try {
  } catch (B &b) { // Ok
  }
}

void f5() {
  try {
  } catch (int) {
  } catch (float) {
  }
}

void f6() {
  try {
  } catch (B &b) {  // expected-note {{for type 'HandlerInversion::B &'}}
  } catch (D2 &d) {  // expected-warning {{exception of type 'HandlerInversion::D2 &' will be caught by earlier handler}}
  }
}

void f7() {
  try {
  } catch (B *b) { // Ok
  } catch (D &d) { // Ok
  }

  try {
  } catch (B b) { // Ok
  } catch (D *d) { // Ok
  }
}

void f8() {
  try {
  } catch (const B &b) {  // expected-note {{for type 'const HandlerInversion::B &'}}
  } catch (D2 &d) {  // expected-warning {{exception of type 'HandlerInversion::D2 &' will be caught by earlier handler}}
  }

  try {
  } catch (B &b) {  // expected-note {{for type 'HandlerInversion::B &'}}
  } catch (const D2 &d) {  // expected-warning {{exception of type 'const HandlerInversion::D2 &' will be caught by earlier handler}}
  }

  try {
  } catch (B b) { // expected-note {{for type 'HandlerInversion::B'}}
  } catch (D &d) { // expected-warning {{exception of type 'HandlerInversion::D &' will be caught by earlier handler}}
  }
}
}

namespace ConstVolatileThrow {
struct S {
  S() {}         // expected-note{{candidate constructor not viable}}
  S(const S &s); // expected-note{{candidate constructor not viable}}
};

typedef const volatile S CVS;

void f() {
  throw CVS(); // expected-error{{no matching constructor for initialization}}
}
}

namespace ConstVolatileCatch {
struct S {
  S() {}
  S(const volatile S &s);

private:
  S(const S &s); // expected-note {{declared private here}}
};

void f();

void g() {
  try {
    f();
  } catch (volatile S s) { // expected-error {{calling a private constructor}}
  }
}
}

namespace PR28047 {
void test1(int i) {
  try {
  } catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}}
  }
}
void test2() {
  int i;
  try {
  } catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}}
  }
}
}