Douglas Gregor | 5b6f769 | 2010-08-30 15:04:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -fexceptions %s |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2 | |
| 3 | // Straight from the standard: |
| 4 | // Plain function with spec |
| 5 | void f() throw(int); |
| 6 | // Pointer to function with spec |
| 7 | void (*fp)() throw (int); |
| 8 | // Function taking reference to function with spec |
| 9 | void g(void pfa() throw(int)); |
| 10 | // Typedef for pointer to function with spec |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 11 | typedef int (*pf)() throw(int); // expected-error {{specifications are not allowed in typedefs}} |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 12 | |
| 13 | // Some more: |
| 14 | // Function returning function with spec |
| 15 | void (*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. |
| 19 | void (*i() throw(int))(void (*)() throw(float)) throw(double); |
| 20 | // Pointer to pointer to function taking function with spec |
| 21 | void (**k)(void pfa() throw(int)); // no-error |
| 22 | // Pointer to pointer to function with spec |
| 23 | void (**j)() throw(int); // expected-error {{not allowed beyond a single}} |
| 24 | // Pointer to function returning pointer to pointer to function with spec |
| 25 | void (**(*h())())() throw(int); // expected-error {{not allowed beyond a single}} |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 26 | |
Sebastian Redl | 491b84c | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 27 | struct Incomplete; // expected-note 3 {{forward declaration}} |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 28 | |
| 29 | // Exception spec must not have incomplete types, or pointers to them, except |
| 30 | // void. |
| 31 | void ic1() throw(void); // expected-error {{incomplete type 'void' is not allowed in exception specification}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 32 | void ic2() throw(Incomplete); // expected-error {{incomplete type 'Incomplete' is not allowed in exception specification}} |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 33 | void ic3() throw(void*); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 34 | void ic4() throw(Incomplete*); // expected-error {{pointer to incomplete type 'Incomplete' is not allowed in exception specification}} |
| 35 | void ic5() throw(Incomplete&); // expected-error {{reference to incomplete type 'Incomplete' is not allowed in exception specification}} |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 36 | |
| 37 | // Redeclarations |
| 38 | typedef int INT; |
| 39 | void r1() throw(int); |
| 40 | void r1() throw(int); |
| 41 | |
| 42 | void r2() throw(int); |
| 43 | void r2() throw(INT); |
| 44 | |
| 45 | // throw-any spec and no spec at all are semantically equivalent |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 46 | void r4() throw(int, float); |
| 47 | void r4() throw(float, int); |
| 48 | |
| 49 | void r5() throw(int); // expected-note {{previous declaration}} |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 50 | void r5(); // expected-warning {{missing exception specification}} |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 51 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 52 | void r7() throw(int); // expected-note {{previous declaration}} |
| 53 | void r7() throw(float); // expected-error {{exception specification in declaration does not match}} |
| 54 | |
| 55 | // Top-level const doesn't matter. |
| 56 | void r8() throw(int); |
| 57 | void r8() throw(const int); |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 58 | |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 59 | // Multiple appearances don't matter. |
| 60 | void r9() throw(int, int); |
| 61 | void r9() throw(int, int); |
| 62 | |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 63 | struct A |
| 64 | { |
| 65 | }; |
| 66 | |
| 67 | struct B1 : A |
| 68 | { |
| 69 | }; |
| 70 | |
| 71 | struct B2 : A |
| 72 | { |
| 73 | }; |
| 74 | |
| 75 | struct D : B1, B2 |
| 76 | { |
| 77 | }; |
| 78 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 79 | struct P : private A |
| 80 | { |
| 81 | }; |
| 82 | |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 83 | struct Base |
| 84 | { |
| 85 | virtual void f1() throw(); |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 86 | virtual void f4() throw(int, float); |
| 87 | |
| 88 | virtual void f5() throw(int, float); |
| 89 | virtual void f6() throw(A); |
| 90 | virtual void f7() throw(A, int, float); |
| 91 | virtual void f8(); |
| 92 | |
| 93 | virtual void g1() throw(); // expected-note {{overridden virtual function is here}} |
| 94 | virtual void g2() throw(int); // expected-note {{overridden virtual function is here}} |
| 95 | virtual void g3() throw(A); // expected-note {{overridden virtual function is here}} |
| 96 | virtual void g4() throw(B1); // expected-note {{overridden virtual function is here}} |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 97 | virtual void g5() throw(A); // expected-note {{overridden virtual function is here}} |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 98 | }; |
| 99 | struct Derived : Base |
| 100 | { |
| 101 | virtual void f1() throw(); |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 102 | virtual void f4() throw(float, int); |
| 103 | |
| 104 | virtual void f5() throw(float); |
| 105 | virtual void f6() throw(B1); |
| 106 | virtual void f7() throw(B1, B2, int); |
| 107 | virtual void f8() throw(B2, B2, int, float, char, double, bool); |
| 108 | |
| 109 | virtual void g1() throw(int); // expected-error {{exception specification of overriding function is more lax}} |
| 110 | virtual void g2(); // expected-error {{exception specification of overriding function is more lax}} |
| 111 | virtual void g3() throw(D); // expected-error {{exception specification of overriding function is more lax}} |
| 112 | virtual void g4() throw(A); // expected-error {{exception specification of overriding function is more lax}} |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 113 | virtual void g5() throw(P); // expected-error {{exception specification of overriding function is more lax}} |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 114 | }; |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 115 | |
| 116 | // Some functions to play with below. |
| 117 | void s1() throw(); |
| 118 | void s2() throw(int); |
| 119 | void s3() throw(A); |
| 120 | void s4() throw(B1); |
| 121 | void s5() throw(D); |
| 122 | void s6(); |
| 123 | void s7() throw(int, float); |
| 124 | void (*s8())() throw(B1); // s8 returns a pointer to function with spec |
| 125 | void s9(void (*)() throw(B1)); // s9 takes pointer to function with spec |
| 126 | |
| 127 | void fnptrs() |
| 128 | { |
| 129 | // Assignment and initialization of function pointers. |
| 130 | void (*t1)() throw() = &s1; // valid |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 131 | t1 = &s2; // expected-error {{not superset}} expected-error {{incompatible type}} |
| 132 | t1 = &s3; // expected-error {{not superset}} expected-error {{incompatible type}} |
| 133 | void (&t2)() throw() = s2; // expected-error {{not superset}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 134 | void (*t3)() throw(int) = &s2; // valid |
| 135 | void (*t4)() throw(A) = &s1; // valid |
| 136 | t4 = &s3; // valid |
| 137 | t4 = &s4; // valid |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 138 | t4 = &s5; // expected-error {{not superset}} expected-error {{incompatible type}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 139 | void (*t5)() = &s1; // valid |
| 140 | t5 = &s2; // valid |
| 141 | t5 = &s6; // valid |
| 142 | t5 = &s7; // valid |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 143 | t1 = t3; // expected-error {{not superset}} expected-error {{incompatible type}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 144 | t3 = t1; // valid |
| 145 | void (*t6)() throw(B1); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 146 | t6 = t4; // expected-error {{not superset}} expected-error {{incompatible type}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 147 | t4 = t6; // valid |
| 148 | t5 = t1; // valid |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 149 | t1 = t5; // expected-error {{not superset}} expected-error {{incompatible type}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 150 | |
| 151 | // return types and arguments must match exactly, no inheritance allowed |
| 152 | void (*(*t7)())() throw(B1) = &s8; // valid |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 153 | void (*(*t8)())() throw(A) = &s8; // expected-error {{return types differ}} |
| 154 | void (*(*t9)())() throw(D) = &s8; // expected-error {{return types differ}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 155 | void (*t10)(void (*)() throw(B1)) = &s9; // valid expected-warning{{disambiguated}} |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 156 | void (*t11)(void (*)() throw(A)) = &s9; // expected-error {{argument types differ}} expected-warning{{disambiguated}} |
| 157 | void (*t12)(void (*)() throw(D)) = &s9; // expected-error {{argument types differ}} expected-warning{{disambiguated}} |
Sebastian Redl | e74b32a | 2009-08-27 19:07:16 +0000 | [diff] [blame] | 158 | } |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 159 | |
| 160 | // Member function stuff |
| 161 | |
| 162 | struct Str1 { void f() throw(int); }; // expected-note {{previous declaration}} |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 163 | void Str1::f() // expected-warning {{missing exception specification}} |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 164 | { |
| 165 | } |
| 166 | |
| 167 | void mfnptr() |
| 168 | { |
| 169 | void (Str1::*pfn1)() throw(int) = &Str1::f; // valid |
| 170 | void (Str1::*pfn2)() = &Str1::f; // valid |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 171 | void (Str1::*pfn3)() throw() = &Str1::f; // expected-error {{not superset}} |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 172 | } |
Sebastian Redl | 491b84c | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 173 | |
| 174 | // Don't suppress errors in template instantiation. |
| 175 | template <typename T> struct TEx; // expected-note {{template is declared here}} |
| 176 | |
| 177 | void tf() throw(TEx<int>); // expected-error {{implicit instantiation of undefined template}} |
Sebastian Redl | e38050d | 2009-10-17 18:31:05 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | 0966f35 | 2009-12-10 18:13:52 +0000 | [diff] [blame] | 179 | // DR 437, class throws itself. |
| 180 | struct DR437 { |
| 181 | void f() throw(DR437); |
| 182 | void g() throw(DR437*); |
| 183 | void h() throw(DR437&); |
| 184 | }; |
| 185 | |
| 186 | // DR 437 within a nested class |
| 187 | struct DR437_out { |
| 188 | struct DR437_in { |
| 189 | void f() throw(DR437_out); |
| 190 | void g() throw(DR437_out*); |
| 191 | void h() throw(DR437_out&); |
| 192 | }; |
| 193 | }; |