[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / exceptions.cpp
blobc2e5adca3e65042592129cef1c94efd1c44e1ce0
1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %std_cxx17- %s
4 struct A; // expected-note 4 {{forward declaration of 'A'}}
6 struct Abstract { virtual void f() = 0; }; // expected-note {{unimplemented pure virtual method 'f'}}
8 void trys() {
9 int k = 42;
10 try {
11 } catch(int i) { // expected-note {{previous definition}}
12 int j = i;
13 int i; // expected-error {{redefinition of 'i'}}
14 } catch(float i) {
15 } catch(void v) { // expected-error {{cannot catch incomplete type 'void'}}
16 } catch(A a) { // expected-error {{cannot catch incomplete type 'A'}}
17 } catch(A *a) { // expected-error {{cannot catch pointer to incomplete type 'A'}}
18 } catch(A &a) { // expected-error {{cannot catch reference to incomplete type 'A'}}
19 } catch(Abstract) { // expected-error {{variable type 'Abstract' is an abstract class}}
20 } catch(...) {
21 int ref = k;
23 int ref = k;
25 int j = i; // expected-error {{use of undeclared identifier 'i'}}
28 try {
29 } catch(...) { // expected-error {{catch-all handler must come last}}
30 } catch(int) {
34 void throws() {
35 throw;
36 throw 0;
37 throw throw; // expected-error {{cannot throw object of incomplete type 'void'}}
38 throw (A*)0; // expected-error {{cannot throw pointer to object of incomplete type 'A'}}
41 void jumps() {
42 l1:
43 goto l5;
44 goto l4; // expected-error {{cannot jump}}
45 goto l3; // expected-error {{cannot jump}}
46 goto l2; // expected-error {{cannot jump}}
47 goto l1;
48 try { // expected-note 4 {{jump bypasses initialization of try block}}
49 l2:
50 goto l5;
51 goto l4; // expected-error {{cannot jump}}
52 goto l3; // expected-error {{cannot jump}}
53 goto l2;
54 goto l1;
55 } catch(int) { // expected-note 4 {{jump bypasses initialization of catch block}}
56 l3:
57 goto l5;
58 goto l4; // expected-error {{cannot jump}}
59 goto l3;
60 goto l2; // expected-error {{cannot jump}}
61 goto l1;
62 } catch(...) { // expected-note 4 {{jump bypasses initialization of catch block}}
63 l4:
64 goto l5;
65 goto l4;
66 goto l3; // expected-error {{cannot jump}}
67 goto l2; // expected-error {{cannot jump}}
68 goto l1;
70 l5:
71 goto l5;
72 goto l4; // expected-error {{cannot jump}}
73 goto l3; // expected-error {{cannot jump}}
74 goto l2; // expected-error {{cannot jump}}
75 goto l1;
78 struct BadReturn {
79 BadReturn() try {
80 } catch(...) {
81 // Try to hide
82 try {
83 } catch(...) {
85 if (0)
86 return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
90 BadReturn(int);
93 BadReturn::BadReturn(int) try {
94 } catch(...) {
95 // Try to hide
96 try {
97 } catch(int) {
98 return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
99 } catch(...) {
101 if (0)
102 return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
107 // Cannot throw an abstract type.
108 class foo {
109 public:
110 foo() {}
111 void bar () {
112 throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}}
114 virtual void test () = 0; // expected-note{{unimplemented pure virtual method 'test'}}
117 namespace PR6831 {
118 namespace NA { struct S; }
119 namespace NB { struct S; }
121 void f() {
122 using namespace NA;
123 using namespace NB;
124 try {
125 } catch (int S) {
130 #if __cplusplus < 201703L
131 namespace Decay {
132 struct A {
133 void f() throw (A[10]);
136 template<typename T> struct B {
137 void f() throw (B[10]);
139 template struct B<int>;
141 void f() throw (int[10], int(*)());
142 void f() throw (int*, int());
144 template<typename T> struct C {
145 void f() throw (T);
146 #if __cplusplus <= 199711L
147 // expected-error@-2 {{pointer to incomplete type 'Decay::E' is not allowed in exception specification}}
148 #endif
150 struct D {
151 C<D[10]> c;
153 struct E;
154 #if __cplusplus <= 199711L
155 // expected-note@-2 {{forward declaration of 'Decay::E'}}
156 #endif
158 C<E[10]> e;
159 #if __cplusplus <= 199711L
160 // expected-note@-2 {{in instantiation of template class 'Decay::C<Decay::E[10]>' requested here}}
161 #endif
164 void rval_ref() throw (int &&); // expected-error {{rvalue reference type 'int &&' is not allowed in exception specification}}
165 #if __cplusplus <= 199711L
166 // expected-warning@-2 {{rvalue references are a C++11 extension}}
167 #endif
168 #endif
170 namespace HandlerInversion {
171 struct B {};
172 struct D : B {};
173 struct D2 : D {};
175 void f1() {
176 try {
177 } catch (B &b) { // expected-note {{for type 'B &'}}
178 } catch (D &d) { // expected-warning {{exception of type 'D &' will be caught by earlier handler}}
182 void f2() {
183 try {
184 } catch (B *b) { // expected-note {{for type 'B *'}}
185 } catch (D *d) { // expected-warning {{exception of type 'D *' will be caught by earlier handler}}
189 void f3() {
190 try {
191 } catch (D &d) { // Ok
192 } catch (B &b) {
196 void f4() {
197 try {
198 } catch (B &b) { // Ok
202 void f5() {
203 try {
204 } catch (int) {
205 } catch (float) {
209 void f6() {
210 try {
211 } catch (B &b) { // expected-note {{for type 'B &'}}
212 } catch (D2 &d) { // expected-warning {{exception of type 'D2 &' will be caught by earlier handler}}
216 void f7() {
217 try {
218 } catch (B *b) { // Ok
219 } catch (D &d) { // Ok
222 try {
223 } catch (B b) { // Ok
224 } catch (D *d) { // Ok
228 void f8() {
229 try {
230 } catch (const B &b) { // expected-note {{for type 'const B &'}}
231 } catch (D2 &d) { // expected-warning {{exception of type 'D2 &' will be caught by earlier handler}}
234 try {
235 } catch (B &b) { // expected-note {{for type 'B &'}}
236 } catch (const D2 &d) { // expected-warning {{exception of type 'const D2 &' will be caught by earlier handler}}
239 try {
240 } catch (B b) { // expected-note {{for type 'B'}}
241 } catch (D &d) { // expected-warning {{exception of type 'D &' will be caught by earlier handler}}
246 namespace ConstVolatileThrow {
247 struct S {
248 S() {} // precxx17-note{{candidate constructor not viable}}
249 S(const S &s); // precxx17-note{{candidate constructor not viable}}
252 typedef const volatile S CVS;
254 void f() {
255 throw CVS(); // precxx17-error{{no matching constructor for initialization}}
259 namespace ConstVolatileCatch {
260 struct S {
261 S() {}
262 S(const volatile S &s);
264 private:
265 S(const S &s); // expected-note {{declared private here}}
268 void f();
270 void g() {
271 try {
272 f();
273 } catch (volatile S s) { // expected-error {{calling a private constructor}}
278 namespace PR28047 {
279 void test1(int i) { // expected-note {{declared here}}
280 try {
281 } catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}} \
282 expected-warning {{variable length arrays in C++ are a Clang extension}} \
283 expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}
286 void test2() {
287 int i; // expected-note {{declared here}}
288 try {
289 } catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}} \
290 expected-warning {{variable length arrays in C++ are a Clang extension}} \
291 expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}