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