From: Douglas Gregor Date: Thu, 15 Apr 2010 18:05:39 +0000 (+0000) Subject: Diagnose attempts to throw an abstract class type. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf422f9d7e2e3454b2296b02202f4d5ae12644f6;p=clang Diagnose attempts to throw an abstract class type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101381 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index cb45f66cd8..7b1b2f496a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -402,6 +402,8 @@ def err_abstract_type_in_decl : Error< "%select{return|parameter|variable|field}0 type %1 is an abstract class">; def err_allocation_of_abstract_type : Error< "allocation of an object of abstract type %0">; +def err_throw_abstract_type : Error< + "cannot throw an object of abstract type %0">; def err_multiple_final_overriders : Error< "virtual function %q0 has more than one final overrider in %1">; diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 04b3d83f3e..be3ef83d99 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -411,6 +411,11 @@ bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { << E->getSourceRange())) return true; + if (RequireNonAbstractType(ThrowLoc, E->getType(), + PDiag(diag::err_throw_abstract_type) + << E->getSourceRange())) + return true; + // FIXME: This is just a hack to mark the copy constructor referenced. // This should go away when the next FIXME is fixed. const RecordType *RT = Ty->getAs(); diff --git a/test/SemaCXX/exceptions.cpp b/test/SemaCXX/exceptions.cpp index 2ed4bfe885..e009558ce3 100644 --- a/test/SemaCXX/exceptions.cpp +++ b/test/SemaCXX/exceptions.cpp @@ -97,3 +97,13 @@ BadReturn::BadReturn(int) try { } } } + +// Cannot throw an abstract type. +class foo { +public: + foo() {} + void bar () { + throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}} + } + virtual void test () = 0; // expected-note{{pure virtual function 'test'}} +};