xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/exceptions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct A; // expected-note 4 {{forward declaration of 'A'}}
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc struct Abstract { virtual void f() = 0; }; // expected-note {{unimplemented pure virtual method 'f'}}
6f4a2713aSLionel Sambuc 
trys()7f4a2713aSLionel Sambuc void trys() {
8f4a2713aSLionel Sambuc   try {
9f4a2713aSLionel Sambuc   } catch(int i) { // expected-note {{previous definition}}
10f4a2713aSLionel Sambuc     int j = i;
11f4a2713aSLionel Sambuc     int i; // expected-error {{redefinition of 'i'}}
12f4a2713aSLionel Sambuc   } catch(float i) {
13f4a2713aSLionel Sambuc   } catch(void v) { // expected-error {{cannot catch incomplete type 'void'}}
14f4a2713aSLionel Sambuc   } catch(A a) { // expected-error {{cannot catch incomplete type 'A'}}
15f4a2713aSLionel Sambuc   } catch(A *a) { // expected-error {{cannot catch pointer to incomplete type 'A'}}
16f4a2713aSLionel Sambuc   } catch(A &a) { // expected-error {{cannot catch reference to incomplete type 'A'}}
17f4a2713aSLionel Sambuc   } catch(Abstract) { // expected-error {{variable type 'Abstract' is an abstract class}}
18f4a2713aSLionel Sambuc   } catch(...) {
19f4a2713aSLionel Sambuc     int j = i; // expected-error {{use of undeclared identifier 'i'}}
20f4a2713aSLionel Sambuc   }
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc   try {
23f4a2713aSLionel Sambuc   } catch(...) { // expected-error {{catch-all handler must come last}}
24f4a2713aSLionel Sambuc   } catch(int) {
25f4a2713aSLionel Sambuc   }
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
throws()28f4a2713aSLionel Sambuc void throws() {
29f4a2713aSLionel Sambuc   throw;
30f4a2713aSLionel Sambuc   throw 0;
31f4a2713aSLionel Sambuc   throw throw; // expected-error {{cannot throw object of incomplete type 'void'}}
32f4a2713aSLionel Sambuc   throw (A*)0; // expected-error {{cannot throw pointer to object of incomplete type 'A'}}
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
jumps()35f4a2713aSLionel Sambuc void jumps() {
36f4a2713aSLionel Sambuc l1:
37f4a2713aSLionel Sambuc   goto l5;
38*0a6a1f1dSLionel Sambuc   goto l4; // expected-error {{cannot jump}}
39*0a6a1f1dSLionel Sambuc   goto l3; // expected-error {{cannot jump}}
40*0a6a1f1dSLionel Sambuc   goto l2; // expected-error {{cannot jump}}
41f4a2713aSLionel Sambuc   goto l1;
42f4a2713aSLionel Sambuc   try { // expected-note 4 {{jump bypasses initialization of try block}}
43f4a2713aSLionel Sambuc   l2:
44f4a2713aSLionel Sambuc     goto l5;
45*0a6a1f1dSLionel Sambuc     goto l4; // expected-error {{cannot jump}}
46*0a6a1f1dSLionel Sambuc     goto l3; // expected-error {{cannot jump}}
47f4a2713aSLionel Sambuc     goto l2;
48f4a2713aSLionel Sambuc     goto l1;
49f4a2713aSLionel Sambuc   } catch(int) { // expected-note 4 {{jump bypasses initialization of catch block}}
50f4a2713aSLionel Sambuc   l3:
51f4a2713aSLionel Sambuc     goto l5;
52*0a6a1f1dSLionel Sambuc     goto l4; // expected-error {{cannot jump}}
53f4a2713aSLionel Sambuc     goto l3;
54*0a6a1f1dSLionel Sambuc     goto l2; // expected-error {{cannot jump}}
55f4a2713aSLionel Sambuc     goto l1;
56f4a2713aSLionel Sambuc   } catch(...) { // expected-note 4 {{jump bypasses initialization of catch block}}
57f4a2713aSLionel Sambuc   l4:
58f4a2713aSLionel Sambuc     goto l5;
59f4a2713aSLionel Sambuc     goto l4;
60*0a6a1f1dSLionel Sambuc     goto l3; // expected-error {{cannot jump}}
61*0a6a1f1dSLionel Sambuc     goto l2; // expected-error {{cannot jump}}
62f4a2713aSLionel Sambuc     goto l1;
63f4a2713aSLionel Sambuc   }
64f4a2713aSLionel Sambuc l5:
65f4a2713aSLionel Sambuc   goto l5;
66*0a6a1f1dSLionel Sambuc   goto l4; // expected-error {{cannot jump}}
67*0a6a1f1dSLionel Sambuc   goto l3; // expected-error {{cannot jump}}
68*0a6a1f1dSLionel Sambuc   goto l2; // expected-error {{cannot jump}}
69f4a2713aSLionel Sambuc   goto l1;
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc struct BadReturn {
BadReturnBadReturn73f4a2713aSLionel Sambuc   BadReturn() try {
74f4a2713aSLionel Sambuc   } catch(...) {
75f4a2713aSLionel Sambuc     // Try to hide
76f4a2713aSLionel Sambuc     try {
77f4a2713aSLionel Sambuc     } catch(...) {
78f4a2713aSLionel Sambuc       {
79f4a2713aSLionel Sambuc         if (0)
80f4a2713aSLionel Sambuc           return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
81f4a2713aSLionel Sambuc       }
82f4a2713aSLionel Sambuc     }
83f4a2713aSLionel Sambuc   }
84f4a2713aSLionel Sambuc   BadReturn(int);
85f4a2713aSLionel Sambuc };
86f4a2713aSLionel Sambuc 
BadReturn(int)87f4a2713aSLionel Sambuc BadReturn::BadReturn(int) try {
88f4a2713aSLionel Sambuc } catch(...) {
89f4a2713aSLionel Sambuc   // Try to hide
90f4a2713aSLionel Sambuc   try {
91f4a2713aSLionel Sambuc   } catch(int) {
92f4a2713aSLionel Sambuc     return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
93f4a2713aSLionel Sambuc   } catch(...) {
94f4a2713aSLionel Sambuc     {
95f4a2713aSLionel Sambuc       if (0)
96f4a2713aSLionel Sambuc         return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
97f4a2713aSLionel Sambuc     }
98f4a2713aSLionel Sambuc   }
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc // Cannot throw an abstract type.
102f4a2713aSLionel Sambuc class foo {
103f4a2713aSLionel Sambuc public:
foo()104f4a2713aSLionel Sambuc   foo() {}
bar()105f4a2713aSLionel Sambuc   void bar () {
106f4a2713aSLionel Sambuc     throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}}
107f4a2713aSLionel Sambuc   }
108f4a2713aSLionel Sambuc   virtual void test () = 0; // expected-note{{unimplemented pure virtual method 'test'}}
109f4a2713aSLionel Sambuc };
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc namespace PR6831 {
112f4a2713aSLionel Sambuc   namespace NA { struct S; }
113f4a2713aSLionel Sambuc   namespace NB { struct S; }
114f4a2713aSLionel Sambuc 
f()115f4a2713aSLionel Sambuc   void f() {
116f4a2713aSLionel Sambuc     using namespace NA;
117f4a2713aSLionel Sambuc     using namespace NB;
118f4a2713aSLionel Sambuc     try {
119f4a2713aSLionel Sambuc     } catch (int S) {
120f4a2713aSLionel Sambuc     }
121f4a2713aSLionel Sambuc   }
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc namespace Decay {
125f4a2713aSLionel Sambuc   struct A {
126f4a2713aSLionel Sambuc     void f() throw (A[10]);
127f4a2713aSLionel Sambuc   };
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   template<typename T> struct B {
130f4a2713aSLionel Sambuc     void f() throw (B[10]);
131f4a2713aSLionel Sambuc   };
132f4a2713aSLionel Sambuc   template struct B<int>;
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   void f() throw (int[10], int(*)());
135f4a2713aSLionel Sambuc   void f() throw (int*, int());
136f4a2713aSLionel Sambuc 
137f4a2713aSLionel Sambuc   template<typename T> struct C {
138f4a2713aSLionel Sambuc     void f() throw (T); // expected-error {{pointer to incomplete type 'Decay::E' is not allowed in exception specification}}
139f4a2713aSLionel Sambuc   };
140f4a2713aSLionel Sambuc   struct D {
141f4a2713aSLionel Sambuc     C<D[10]> c;
142f4a2713aSLionel Sambuc   };
143f4a2713aSLionel Sambuc   struct E; // expected-note {{forward declaration}}
144f4a2713aSLionel Sambuc   C<E[10]> e; // expected-note {{in instantiation of}}
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc void rval_ref() throw (int &&); // expected-error {{rvalue reference type 'int &&' is not allowed in exception specification}} expected-warning {{C++11}}
148