2011-02-28 08:40:07 +08:00
|
|
|
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
|
2016-04-15 07:47:07 +08:00
|
|
|
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s
|
|
|
|
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
|
2009-04-28 04:27:31 +08:00
|
|
|
|
2010-03-10 19:27:22 +08:00
|
|
|
struct A; // expected-note 4 {{forward declaration of 'A'}}
|
2009-04-28 04:27:31 +08:00
|
|
|
|
2011-02-19 07:59:51 +08:00
|
|
|
struct Abstract { virtual void f() = 0; }; // expected-note {{unimplemented pure virtual method 'f'}}
|
2009-04-28 05:03:30 +08:00
|
|
|
|
2009-04-28 04:27:31 +08:00
|
|
|
void trys() {
|
Un-revert "[coroutines][PR40978] Emit error for co_yield within catch block"
Summary:
https://reviews.llvm.org/D59076 added a new coroutine error that
prevented users from using 'co_await' or 'co_yield' within a exception
handler. However, it was reverted in https://reviews.llvm.org/rC356774
because it caused a regression in nested scopes in C++ catch statements,
as documented by https://bugs.llvm.org/show_bug.cgi?id=41171.
The issue was due to an incorrect use of a `clang::ParseScope`. To fix:
1. Add a regression test for catch statement parsing that mimics the bug
report from https://bugs.llvm.org/show_bug.cgi?id=41171.
2. Re-apply the coroutines error patch from
https://reviews.llvm.org/D59076, but this time with the correct
ParseScope behavior.
Reviewers: GorNishanov, tks2103, rsmith, riccibruno, jbulow
Reviewed By: riccibruno
Subscribers: EricWF, jdoerfert, lewissbaker, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59752
llvm-svn: 356865
2019-03-25 08:53:10 +08:00
|
|
|
int k = 42;
|
2009-04-28 04:27:31 +08:00
|
|
|
try {
|
|
|
|
} catch(int i) { // expected-note {{previous definition}}
|
|
|
|
int j = i;
|
|
|
|
int i; // expected-error {{redefinition of 'i'}}
|
|
|
|
} catch(float i) {
|
|
|
|
} catch(void v) { // expected-error {{cannot catch incomplete type 'void'}}
|
2010-03-10 19:27:22 +08:00
|
|
|
} catch(A a) { // expected-error {{cannot catch incomplete type 'A'}}
|
2012-01-25 03:01:26 +08:00
|
|
|
} catch(A *a) { // expected-error {{cannot catch pointer to incomplete type 'A'}}
|
|
|
|
} catch(A &a) { // expected-error {{cannot catch reference to incomplete type 'A'}}
|
2009-04-28 05:03:30 +08:00
|
|
|
} catch(Abstract) { // expected-error {{variable type 'Abstract' is an abstract class}}
|
2009-04-28 04:27:31 +08:00
|
|
|
} catch(...) {
|
Un-revert "[coroutines][PR40978] Emit error for co_yield within catch block"
Summary:
https://reviews.llvm.org/D59076 added a new coroutine error that
prevented users from using 'co_await' or 'co_yield' within a exception
handler. However, it was reverted in https://reviews.llvm.org/rC356774
because it caused a regression in nested scopes in C++ catch statements,
as documented by https://bugs.llvm.org/show_bug.cgi?id=41171.
The issue was due to an incorrect use of a `clang::ParseScope`. To fix:
1. Add a regression test for catch statement parsing that mimics the bug
report from https://bugs.llvm.org/show_bug.cgi?id=41171.
2. Re-apply the coroutines error patch from
https://reviews.llvm.org/D59076, but this time with the correct
ParseScope behavior.
Reviewers: GorNishanov, tks2103, rsmith, riccibruno, jbulow
Reviewed By: riccibruno
Subscribers: EricWF, jdoerfert, lewissbaker, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59752
llvm-svn: 356865
2019-03-25 08:53:10 +08:00
|
|
|
int ref = k;
|
|
|
|
{
|
|
|
|
int ref = k;
|
|
|
|
}
|
2009-04-28 04:27:31 +08:00
|
|
|
int j = i; // expected-error {{use of undeclared identifier 'i'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
} catch(...) { // expected-error {{catch-all handler must come last}}
|
|
|
|
} catch(int) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void throws() {
|
|
|
|
throw;
|
|
|
|
throw 0;
|
|
|
|
throw throw; // expected-error {{cannot throw object of incomplete type 'void'}}
|
2010-03-10 19:27:22 +08:00
|
|
|
throw (A*)0; // expected-error {{cannot throw pointer to object of incomplete type 'A'}}
|
2009-04-28 04:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void jumps() {
|
|
|
|
l1:
|
|
|
|
goto l5;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l4; // expected-error {{cannot jump}}
|
|
|
|
goto l3; // expected-error {{cannot jump}}
|
|
|
|
goto l2; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l1;
|
|
|
|
try { // expected-note 4 {{jump bypasses initialization of try block}}
|
|
|
|
l2:
|
|
|
|
goto l5;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l4; // expected-error {{cannot jump}}
|
|
|
|
goto l3; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l2;
|
|
|
|
goto l1;
|
|
|
|
} catch(int) { // expected-note 4 {{jump bypasses initialization of catch block}}
|
|
|
|
l3:
|
|
|
|
goto l5;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l4; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l3;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l2; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l1;
|
|
|
|
} catch(...) { // expected-note 4 {{jump bypasses initialization of catch block}}
|
|
|
|
l4:
|
|
|
|
goto l5;
|
|
|
|
goto l4;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l3; // expected-error {{cannot jump}}
|
|
|
|
goto l2; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l1;
|
|
|
|
}
|
|
|
|
l5:
|
|
|
|
goto l5;
|
2014-09-06 08:24:58 +08:00
|
|
|
goto l4; // expected-error {{cannot jump}}
|
|
|
|
goto l3; // expected-error {{cannot jump}}
|
|
|
|
goto l2; // expected-error {{cannot jump}}
|
2009-04-28 04:27:31 +08:00
|
|
|
goto l1;
|
|
|
|
}
|
2009-04-28 05:33:24 +08:00
|
|
|
|
|
|
|
struct BadReturn {
|
|
|
|
BadReturn() try {
|
|
|
|
} catch(...) {
|
|
|
|
// Try to hide
|
|
|
|
try {
|
|
|
|
} catch(...) {
|
|
|
|
{
|
|
|
|
if (0)
|
|
|
|
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BadReturn(int);
|
|
|
|
};
|
|
|
|
|
|
|
|
BadReturn::BadReturn(int) try {
|
|
|
|
} catch(...) {
|
|
|
|
// Try to hide
|
|
|
|
try {
|
|
|
|
} catch(int) {
|
|
|
|
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
|
|
|
|
} catch(...) {
|
|
|
|
{
|
|
|
|
if (0)
|
|
|
|
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-16 02:05:39 +08:00
|
|
|
|
|
|
|
// Cannot throw an abstract type.
|
|
|
|
class foo {
|
|
|
|
public:
|
|
|
|
foo() {}
|
|
|
|
void bar () {
|
|
|
|
throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}}
|
|
|
|
}
|
2011-02-19 07:59:51 +08:00
|
|
|
virtual void test () = 0; // expected-note{{unimplemented pure virtual method 'test'}}
|
2010-04-16 02:05:39 +08:00
|
|
|
};
|
2010-04-16 07:40:53 +08:00
|
|
|
|
|
|
|
namespace PR6831 {
|
|
|
|
namespace NA { struct S; }
|
|
|
|
namespace NB { struct S; }
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
using namespace NA;
|
|
|
|
using namespace NB;
|
|
|
|
try {
|
|
|
|
} catch (int S) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-29 06:33:28 +08:00
|
|
|
|
|
|
|
namespace Decay {
|
|
|
|
struct A {
|
|
|
|
void f() throw (A[10]);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct B {
|
|
|
|
void f() throw (B[10]);
|
|
|
|
};
|
|
|
|
template struct B<int>;
|
|
|
|
|
|
|
|
void f() throw (int[10], int(*)());
|
|
|
|
void f() throw (int*, int());
|
|
|
|
|
|
|
|
template<typename T> struct C {
|
2016-04-15 07:47:07 +08:00
|
|
|
void f() throw (T);
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-error@-2 {{pointer to incomplete type 'Decay::E' is not allowed in exception specification}}
|
|
|
|
#endif
|
2012-11-29 06:33:28 +08:00
|
|
|
};
|
|
|
|
struct D {
|
|
|
|
C<D[10]> c;
|
|
|
|
};
|
2016-04-15 07:47:07 +08:00
|
|
|
struct E;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-note@-2 {{forward declaration of 'Decay::E'}}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
C<E[10]> e;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-note@-2 {{in instantiation of template class 'Decay::C<Decay::E [10]>' requested here}}
|
|
|
|
#endif
|
2012-11-29 06:33:28 +08:00
|
|
|
}
|
2012-11-29 06:52:42 +08:00
|
|
|
|
2016-04-15 07:47:07 +08:00
|
|
|
void rval_ref() throw (int &&); // expected-error {{rvalue reference type 'int &&' is not allowed in exception specification}}
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{rvalue references are a C++11 extension}}
|
|
|
|
#endif
|
2015-03-03 09:50:05 +08:00
|
|
|
|
2015-04-08 08:05:29 +08:00
|
|
|
namespace HandlerInversion {
|
|
|
|
struct B {};
|
|
|
|
struct D : B {};
|
|
|
|
struct D2 : D {};
|
|
|
|
|
|
|
|
void f1() {
|
|
|
|
try {
|
|
|
|
} catch (B &b) { // expected-note {{for type 'HandlerInversion::B &'}}
|
|
|
|
} catch (D &d) { // expected-warning {{exception of type 'HandlerInversion::D &' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f2() {
|
|
|
|
try {
|
|
|
|
} catch (B *b) { // expected-note {{for type 'HandlerInversion::B *'}}
|
|
|
|
} catch (D *d) { // expected-warning {{exception of type 'HandlerInversion::D *' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f3() {
|
|
|
|
try {
|
|
|
|
} catch (D &d) { // Ok
|
|
|
|
} catch (B &b) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f4() {
|
|
|
|
try {
|
|
|
|
} catch (B &b) { // Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f5() {
|
|
|
|
try {
|
|
|
|
} catch (int) {
|
|
|
|
} catch (float) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f6() {
|
|
|
|
try {
|
|
|
|
} catch (B &b) { // expected-note {{for type 'HandlerInversion::B &'}}
|
|
|
|
} catch (D2 &d) { // expected-warning {{exception of type 'HandlerInversion::D2 &' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f7() {
|
|
|
|
try {
|
|
|
|
} catch (B *b) { // Ok
|
|
|
|
} catch (D &d) { // Ok
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
} catch (B b) { // Ok
|
|
|
|
} catch (D *d) { // Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f8() {
|
|
|
|
try {
|
|
|
|
} catch (const B &b) { // expected-note {{for type 'const HandlerInversion::B &'}}
|
|
|
|
} catch (D2 &d) { // expected-warning {{exception of type 'HandlerInversion::D2 &' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
} catch (B &b) { // expected-note {{for type 'HandlerInversion::B &'}}
|
|
|
|
} catch (const D2 &d) { // expected-warning {{exception of type 'const HandlerInversion::D2 &' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
} catch (B b) { // expected-note {{for type 'HandlerInversion::B'}}
|
|
|
|
} catch (D &d) { // expected-warning {{exception of type 'HandlerInversion::D &' will be caught by earlier handler}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 12:38:34 +08:00
|
|
|
namespace ConstVolatileThrow {
|
2015-03-03 09:50:05 +08:00
|
|
|
struct S {
|
|
|
|
S() {} // expected-note{{candidate constructor not viable}}
|
|
|
|
S(const S &s); // expected-note{{candidate constructor not viable}}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef const volatile S CVS;
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
throw CVS(); // expected-error{{no matching constructor for initialization}}
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 12:38:34 +08:00
|
|
|
|
|
|
|
namespace ConstVolatileCatch {
|
|
|
|
struct S {
|
|
|
|
S() {}
|
|
|
|
S(const volatile S &s);
|
|
|
|
|
|
|
|
private:
|
|
|
|
S(const S &s); // expected-note {{declared private here}}
|
|
|
|
};
|
|
|
|
|
|
|
|
void f();
|
|
|
|
|
|
|
|
void g() {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
} catch (volatile S s) { // expected-error {{calling a private constructor}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 00:05:07 +08:00
|
|
|
|
|
|
|
namespace PR28047 {
|
|
|
|
void test1(int i) {
|
|
|
|
try {
|
|
|
|
} catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void test2() {
|
|
|
|
int i;
|
|
|
|
try {
|
|
|
|
} catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|