[cfe-commits] r97853 - in /cfe/trunk: lib/Sema/SemaDeclCXX.cpp test/CXX/except/except.handle/p16.cpp
Douglas Gregor
dgregor at apple.com
Fri Mar 5 15:38:39 PST 2010
Author: dgregor
Date: Fri Mar 5 17:38:39 2010
New Revision: 97853
URL: http://llvm.org/viewvc/llvm-project?rev=97853&view=rev
Log:
When declaring a catch block in C++, make sure that the type being
caught can be copy-initialized and destructed. Fixes PR6518.
Added:
cfe/trunk/test/CXX/except/except.handle/p16.cpp
Modified:
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=97853&r1=97852&r2=97853&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Fri Mar 5 17:38:39 2010
@@ -5212,14 +5212,35 @@
AbstractVariableType))
Invalid = true;
- // FIXME: Need to test for ability to copy-construct and destroy the
- // exception variable.
-
- // FIXME: Need to check for abstract classes.
-
VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc,
Name, ExDeclType, TInfo, VarDecl::None);
+ if (!Invalid) {
+ if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) {
+ // C++ [except.handle]p16:
+ // The object declared in an exception-declaration or, if the
+ // exception-declaration does not specify a name, a temporary (12.2) is
+ // copy-initialized (8.5) from the exception object. [...]
+ // The object is destroyed when the handler exits, after the destruction
+ // of any automatic objects initialized within the handler.
+ //
+ // We just pretend to initialize the object with itself, then make sure
+ // it can be destroyed later.
+ InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl);
+ Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl,
+ Loc, ExDeclType, 0);
+ InitializationKind Kind = InitializationKind::CreateCopy(Loc,
+ SourceLocation());
+ InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1);
+ OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind,
+ MultiExprArg(*this, (void**)&ExDeclRef, 1));
+ if (Result.isInvalid())
+ Invalid = true;
+ else
+ FinalizeVarWithDestructor(ExDecl, RecordTy);
+ }
+ }
+
if (Invalid)
ExDecl->setInvalidDecl();
Added: cfe/trunk/test/CXX/except/except.handle/p16.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/except/except.handle/p16.cpp?rev=97853&view=auto
==============================================================================
--- cfe/trunk/test/CXX/except/except.handle/p16.cpp (added)
+++ cfe/trunk/test/CXX/except/except.handle/p16.cpp Fri Mar 5 17:38:39 2010
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// The object declared in an exception-declaration or, if the
+// exception-declaration does not specify a name, a temporary (12.2)
+// is copy-initialized (8.5) from the exception object.
+//
+template<typename T>
+class X {
+ T* ptr;
+
+public:
+ X(const X<T> &) {
+ int *ip = 0;
+ ptr = ip; // expected-error{{incompatible type assigning 'int *', expected 'float *'}}
+ }
+
+ ~X() {
+ float *fp = 0;
+ ptr = fp; // expected-error{{incompatible type assigning 'float *', expected 'int *'}}
+ }
+};
+
+void f() {
+ try {
+ } catch (X<float>) { // expected-note{{instantiation}}
+ // copy constructor
+ } catch (X<int> xi) { // expected-note{{instantiation}}
+ // destructor
+ }
+}
+
+struct Abstract {
+ virtual void f() = 0; // expected-note{{pure virtual}}
+};
+
+void g() {
+ try {
+ } catch (Abstract) { // expected-error{{variable type 'Abstract' is an abstract class}}
+ }
+}
More information about the cfe-commits
mailing list