Improve validation of C++ exception handling: diagnose throwing incomplete types and jumps into protected try-catch scopes.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70242 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/JumpDiagnostics.cpp b/lib/Sema/JumpDiagnostics.cpp
index 20473e4..ae863f2 100644
--- a/lib/Sema/JumpDiagnostics.cpp
+++ b/lib/Sema/JumpDiagnostics.cpp
@@ -15,6 +15,7 @@
#include "Sema.h"
#include "clang/AST/Expr.h"
#include "clang/AST/StmtObjC.h"
+#include "clang/AST/StmtCXX.h"
using namespace clang;
namespace {
@@ -115,7 +116,6 @@
// FIXME: diagnose jumps past initialization: required in C++, warning in C.
// goto L; int X = 4; L: ;
- // FIXME: what about jumps into C++ catch blocks, what are the rules?
// If this is a declstmt with a VLA definition, it defines a scope from here
// to the end of the containing context.
@@ -184,7 +184,27 @@
BuildScopeInformation(AS->getSynchBody(), Scopes.size()-1);
continue;
}
-
+
+ // Disallow jumps into any part of a C++ try statement. This is pretty
+ // much the same as for Obj-C.
+ if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
+ Scopes.push_back(GotoScope(ParentScope, diag::note_protected_by_cxx_try,
+ TS->getSourceRange().getBegin()));
+ if (Stmt *TryBlock = TS->getTryBlock())
+ BuildScopeInformation(TryBlock, Scopes.size()-1);
+
+ // Jump from the catch into the try is not allowed either.
+ for(unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
+ CXXCatchStmt *CS = TS->getHandler(I);
+ Scopes.push_back(GotoScope(ParentScope,
+ diag::note_protected_by_cxx_catch,
+ CS->getSourceRange().getBegin()));
+ BuildScopeInformation(CS->getHandlerBlock(), Scopes.size()-1);
+ }
+
+ continue;
+ }
+
// Recursively walk the AST.
BuildScopeInformation(SubStmt, ParentScope);
}
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index e30f128..637804c 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -1494,6 +1494,7 @@
//// ActOnCXXThrow - Parse throw expressions.
virtual OwningExprResult ActOnCXXThrow(SourceLocation OpLoc,
ExprArg expr);
+ bool CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E);
/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
/// Can be interpreted either as function-style casting ("int(x)")
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index afd67fa..4c3c85b 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -88,8 +88,37 @@
/// 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) {
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 93e30ec..cf24029 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -1156,6 +1156,7 @@
// Neither of these are explicitly forbidden, but every compiler detects them
// and warns.
+ CurFunctionNeedsScopeChecking = true;
RawHandlers.release();
return Owned(new (Context) CXXTryStmt(TryLoc,
static_cast<Stmt*>(TryBlock.release()),