diff options
author | Sebastian Redl <sebastian.redl@getdesigned.at> | 2009-04-27 20:27:31 +0000 |
---|---|---|
committer | Sebastian Redl <sebastian.redl@getdesigned.at> | 2009-04-27 20:27:31 +0000 |
commit | 4de47b445b9643563f24729cfcd6124614a05392 (patch) | |
tree | 8fe82e2062ea7907fad02f412b7cd494fed7fb52 /clang/lib/Sema/SemaExprCXX.cpp | |
parent | 76466373794f34303146931969c1f1905ad526fe (diff) | |
download | bcm5719-llvm-4de47b445b9643563f24729cfcd6124614a05392.tar.gz bcm5719-llvm-4de47b445b9643563f24729cfcd6124614a05392.zip |
Improve validation of C++ exception handling: diagnose throwing incomplete types and jumps into protected try-catch scopes.
llvm-svn: 70242
Diffstat (limited to 'clang/lib/Sema/SemaExprCXX.cpp')
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index afd67fac559..4c3c85bbf55 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -88,8 +88,37 @@ Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { /// ActOnCXXThrow - Parse throw expressions. Action::OwningExprResult Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { - return Owned(new (Context) CXXThrowExpr((Expr*)E.release(), Context.VoidTy, - OpLoc)); + Expr *Ex = E.takeAs<Expr>(); + if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) + return ExprError(); + return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); +} + +/// CheckCXXThrowOperand - Validate the operand of a throw. +bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { + // C++ [except.throw]p3: + // [...] adjusting the type from "array of T" or "function returning T" + // to "pointer to T" or "pointer to function returning T", [...] + DefaultFunctionArrayConversion(E); + + // If the type of the exception would be an incomplete type or a pointer + // to an incomplete type other than (cv) void the program is ill-formed. + QualType Ty = E->getType(); + int isPointer = 0; + if (const PointerType* Ptr = Ty->getAsPointerType()) { + Ty = Ptr->getPointeeType(); + isPointer = 1; + } + if (!isPointer || !Ty->isVoidType()) { + if (RequireCompleteType(ThrowLoc, Ty, + isPointer ? diag::err_throw_incomplete_ptr + : diag::err_throw_incomplete, + E->getSourceRange(), SourceRange(), QualType())) + return true; + } + + // FIXME: Construct a temporary here. + return false; } Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |