blob: 498611ee8599398dfd43560948181486eacaa54a [file] [log] [blame]
Douglas Gregor2eef8292010-03-24 07:14:45 +00001// RUN: %clang_cc1 -fsyntax-only -verify -fexceptions -fms-extensions %s
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002
3// Straight from the standard:
4// Plain function with spec
5void f() throw(int);
6// Pointer to function with spec
7void (*fp)() throw (int);
8// Function taking reference to function with spec
9void g(void pfa() throw(int));
10// Typedef for pointer to function with spec
Sebastian Redl3cc97262009-05-31 11:47:27 +000011typedef int (*pf)() throw(int); // expected-error {{specifications are not allowed in typedefs}}
Sebastian Redl6a7330c2009-05-29 15:01:05 +000012
13// Some more:
14// Function returning function with spec
15void (*h())() throw(int);
16// Ultimate parser thrill: function with spec returning function with spec and
17// taking pointer to function with spec.
18// The actual function throws int, the return type double, the argument float.
19void (*i() throw(int))(void (*)() throw(float)) throw(double);
20// Pointer to pointer to function taking function with spec
21void (**k)(void pfa() throw(int)); // no-error
22// Pointer to pointer to function with spec
23void (**j)() throw(int); // expected-error {{not allowed beyond a single}}
24// Pointer to function returning pointer to pointer to function with spec
25void (**(*h())())() throw(int); // expected-error {{not allowed beyond a single}}
Sebastian Redlef65f062009-05-29 18:02:33 +000026
Sebastian Redl491b84c2009-10-14 14:59:48 +000027struct Incomplete; // expected-note 3 {{forward declaration}}
Sebastian Redlef65f062009-05-29 18:02:33 +000028
29// Exception spec must not have incomplete types, or pointers to them, except
30// void.
31void ic1() throw(void); // expected-error {{incomplete type 'void' is not allowed in exception specification}}
John McCall7c2342d2010-03-10 11:27:22 +000032void ic2() throw(Incomplete); // expected-error {{incomplete type 'Incomplete' is not allowed in exception specification}}
Sebastian Redlef65f062009-05-29 18:02:33 +000033void ic3() throw(void*);
John McCall7c2342d2010-03-10 11:27:22 +000034void ic4() throw(Incomplete*); // expected-error {{pointer to incomplete type 'Incomplete' is not allowed in exception specification}}
35void ic5() throw(Incomplete&); // expected-error {{reference to incomplete type 'Incomplete' is not allowed in exception specification}}
Sebastian Redl4994d2d2009-07-04 11:39:00 +000036
37// Redeclarations
38typedef int INT;
39void r1() throw(int);
40void r1() throw(int);
41
42void r2() throw(int);
43void r2() throw(INT);
44
45// throw-any spec and no spec at all are semantically equivalent
46void r3();
47void r3() throw(...);
48
49void r4() throw(int, float);
50void r4() throw(float, int);
51
52void r5() throw(int); // expected-note {{previous declaration}}
Douglas Gregor2eef8292010-03-24 07:14:45 +000053void r5(); // expected-warning {{missing exception specification}}
Sebastian Redl4994d2d2009-07-04 11:39:00 +000054
55void r6() throw(...); // expected-note {{previous declaration}}
56void r6() throw(int); // expected-error {{exception specification in declaration does not match}}
57
58void r7() throw(int); // expected-note {{previous declaration}}
59void r7() throw(float); // expected-error {{exception specification in declaration does not match}}
60
61// Top-level const doesn't matter.
62void r8() throw(int);
63void r8() throw(const int);
Sebastian Redl23c7d062009-07-07 20:29:57 +000064
Sebastian Redl5db4d902009-10-11 09:11:23 +000065// Multiple appearances don't matter.
66void r9() throw(int, int);
67void r9() throw(int, int);
68
Sebastian Redl23c7d062009-07-07 20:29:57 +000069struct A
70{
71};
72
73struct B1 : A
74{
75};
76
77struct B2 : A
78{
79};
80
81struct D : B1, B2
82{
83};
84
Sebastian Redl726212f2009-07-18 14:32:15 +000085struct P : private A
86{
87};
88
Sebastian Redl23c7d062009-07-07 20:29:57 +000089struct Base
90{
91 virtual void f1() throw();
92 virtual void f2();
93 virtual void f3() throw(...);
94 virtual void f4() throw(int, float);
95
96 virtual void f5() throw(int, float);
97 virtual void f6() throw(A);
98 virtual void f7() throw(A, int, float);
99 virtual void f8();
100
101 virtual void g1() throw(); // expected-note {{overridden virtual function is here}}
102 virtual void g2() throw(int); // expected-note {{overridden virtual function is here}}
103 virtual void g3() throw(A); // expected-note {{overridden virtual function is here}}
104 virtual void g4() throw(B1); // expected-note {{overridden virtual function is here}}
Sebastian Redl726212f2009-07-18 14:32:15 +0000105 virtual void g5() throw(A); // expected-note {{overridden virtual function is here}}
Sebastian Redl23c7d062009-07-07 20:29:57 +0000106};
107struct Derived : Base
108{
109 virtual void f1() throw();
110 virtual void f2() throw(...);
111 virtual void f3();
112 virtual void f4() throw(float, int);
113
114 virtual void f5() throw(float);
115 virtual void f6() throw(B1);
116 virtual void f7() throw(B1, B2, int);
117 virtual void f8() throw(B2, B2, int, float, char, double, bool);
118
119 virtual void g1() throw(int); // expected-error {{exception specification of overriding function is more lax}}
120 virtual void g2(); // expected-error {{exception specification of overriding function is more lax}}
121 virtual void g3() throw(D); // expected-error {{exception specification of overriding function is more lax}}
122 virtual void g4() throw(A); // expected-error {{exception specification of overriding function is more lax}}
Sebastian Redl726212f2009-07-18 14:32:15 +0000123 virtual void g5() throw(P); // expected-error {{exception specification of overriding function is more lax}}
Sebastian Redl23c7d062009-07-07 20:29:57 +0000124};
Sebastian Redle74b32a2009-08-27 19:07:16 +0000125
126// Some functions to play with below.
127void s1() throw();
128void s2() throw(int);
129void s3() throw(A);
130void s4() throw(B1);
131void s5() throw(D);
132void s6();
133void s7() throw(int, float);
134void (*s8())() throw(B1); // s8 returns a pointer to function with spec
135void s9(void (*)() throw(B1)); // s9 takes pointer to function with spec
136
137void fnptrs()
138{
139 // Assignment and initialization of function pointers.
140 void (*t1)() throw() = &s1; // valid
Sebastian Redl2c7588f2009-10-10 12:04:10 +0000141 t1 = &s2; // expected-error {{not superset}} expected-error {{incompatible type}}
142 t1 = &s3; // expected-error {{not superset}} expected-error {{incompatible type}}
143 void (&t2)() throw() = s2; // expected-error {{not superset}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000144 void (*t3)() throw(int) = &s2; // valid
145 void (*t4)() throw(A) = &s1; // valid
146 t4 = &s3; // valid
147 t4 = &s4; // valid
Sebastian Redl2c7588f2009-10-10 12:04:10 +0000148 t4 = &s5; // expected-error {{not superset}} expected-error {{incompatible type}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000149 void (*t5)() = &s1; // valid
150 t5 = &s2; // valid
151 t5 = &s6; // valid
152 t5 = &s7; // valid
Sebastian Redl2c7588f2009-10-10 12:04:10 +0000153 t1 = t3; // expected-error {{not superset}} expected-error {{incompatible type}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000154 t3 = t1; // valid
155 void (*t6)() throw(B1);
Sebastian Redl2c7588f2009-10-10 12:04:10 +0000156 t6 = t4; // expected-error {{not superset}} expected-error {{incompatible type}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000157 t4 = t6; // valid
158 t5 = t1; // valid
Sebastian Redl2c7588f2009-10-10 12:04:10 +0000159 t1 = t5; // expected-error {{not superset}} expected-error {{incompatible type}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000160
161 // return types and arguments must match exactly, no inheritance allowed
162 void (*(*t7)())() throw(B1) = &s8; // valid
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000163 void (*(*t8)())() throw(A) = &s8; // expected-error {{return types differ}}
164 void (*(*t9)())() throw(D) = &s8; // expected-error {{return types differ}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000165 void (*t10)(void (*)() throw(B1)) = &s9; // valid expected-warning{{disambiguated}}
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000166 void (*t11)(void (*)() throw(A)) = &s9; // expected-error {{argument types differ}} expected-warning{{disambiguated}}
167 void (*t12)(void (*)() throw(D)) = &s9; // expected-error {{argument types differ}} expected-warning{{disambiguated}}
Sebastian Redle74b32a2009-08-27 19:07:16 +0000168}
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000169
170// Member function stuff
171
172struct Str1 { void f() throw(int); }; // expected-note {{previous declaration}}
Douglas Gregor2eef8292010-03-24 07:14:45 +0000173void Str1::f() // expected-warning {{missing exception specification}}
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000174{
175}
176
177void mfnptr()
178{
179 void (Str1::*pfn1)() throw(int) = &Str1::f; // valid
180 void (Str1::*pfn2)() = &Str1::f; // valid
Eli Friedmancfdc81a2009-12-19 08:11:05 +0000181 void (Str1::*pfn3)() throw() = &Str1::f; // expected-error {{not superset}}
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000182}
Sebastian Redl491b84c2009-10-14 14:59:48 +0000183
184// Don't suppress errors in template instantiation.
185template <typename T> struct TEx; // expected-note {{template is declared here}}
186
187void tf() throw(TEx<int>); // expected-error {{implicit instantiation of undefined template}}
Sebastian Redle38050d2009-10-17 18:31:05 +0000188
Douglas Gregor0966f352009-12-10 18:13:52 +0000189// DR 437, class throws itself.
190struct DR437 {
191 void f() throw(DR437);
192 void g() throw(DR437*);
193 void h() throw(DR437&);
194};
195
196// DR 437 within a nested class
197struct DR437_out {
198 struct DR437_in {
199 void f() throw(DR437_out);
200 void g() throw(DR437_out*);
201 void h() throw(DR437_out&);
202 };
203};