From 64aa9a1dd1600e10da95a2a42d79e95ed0e53d7c Mon Sep 17 00:00:00 2001 From: Don Hinton Date: Tue, 19 Mar 2019 06:14:14 +0000 Subject: [PATCH] Refactor cast<>'s in if conditionals, which can only assert on failure. Summary: This patch refactors several instances of cast<> used in if conditionals. Since cast<> asserts on failure, the else branch can never be taken. In some cases, the fix is to replace cast<> with dyn_cast<>. While others required the removal of the conditional and some minor refactoring. A discussion can be seen here: http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20190318/265044.html Differential Revision: https://reviews.llvm.org/D59529 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@356441 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTImporter.cpp | 53 ++++++++++++++++----------------- lib/AST/DeclBase.cpp | 10 ++++--- lib/CodeGen/CGExprConstant.cpp | 39 +++++++++--------------- lib/CodeGen/MicrosoftCXXABI.cpp | 2 +- lib/Sema/SemaOpenMP.cpp | 2 +- 5 files changed, 47 insertions(+), 59 deletions(-) diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index cff5ac66ca..9158e902c9 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -8460,44 +8460,41 @@ Error ASTImporter::ImportDefinition_New(Decl *From) { if (!To) return llvm::make_error(); - if (auto *FromDC = cast(From)) { - ASTNodeImporter Importer(*this); + auto *FromDC = cast(From); + ASTNodeImporter Importer(*this); - if (auto *ToRecord = dyn_cast(To)) { - if (!ToRecord->getDefinition()) { - return Importer.ImportDefinition( - cast(FromDC), ToRecord, - ASTNodeImporter::IDK_Everything); - } + if (auto *ToRecord = dyn_cast(To)) { + if (!ToRecord->getDefinition()) { + return Importer.ImportDefinition( + cast(FromDC), ToRecord, + ASTNodeImporter::IDK_Everything); } + } - if (auto *ToEnum = dyn_cast(To)) { - if (!ToEnum->getDefinition()) { - return Importer.ImportDefinition( - cast(FromDC), ToEnum, ASTNodeImporter::IDK_Everything); - } + if (auto *ToEnum = dyn_cast(To)) { + if (!ToEnum->getDefinition()) { + return Importer.ImportDefinition( + cast(FromDC), ToEnum, ASTNodeImporter::IDK_Everything); } + } - if (auto *ToIFace = dyn_cast(To)) { - if (!ToIFace->getDefinition()) { - return Importer.ImportDefinition( - cast(FromDC), ToIFace, - ASTNodeImporter::IDK_Everything); - } + if (auto *ToIFace = dyn_cast(To)) { + if (!ToIFace->getDefinition()) { + return Importer.ImportDefinition( + cast(FromDC), ToIFace, + ASTNodeImporter::IDK_Everything); } + } - if (auto *ToProto = dyn_cast(To)) { - if (!ToProto->getDefinition()) { - return Importer.ImportDefinition( - cast(FromDC), ToProto, - ASTNodeImporter::IDK_Everything); - } + if (auto *ToProto = dyn_cast(To)) { + if (!ToProto->getDefinition()) { + return Importer.ImportDefinition( + cast(FromDC), ToProto, + ASTNodeImporter::IDK_Everything); } - - return Importer.ImportDeclContext(FromDC, true); } - return Error::success(); + return Importer.ImportDeclContext(FromDC, true); } void ASTImporter::ImportDefinition(Decl *From) { diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index e1b5161b02..2f7bb5333a 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -1179,13 +1179,15 @@ DeclContext *DeclContext::getPrimaryContext() { return this; case Decl::ObjCInterface: - if (auto *Def = cast(this)->getDefinition()) - return Def; + if (auto *OID = dyn_cast(this)) + if (auto *Def = OID->getDefinition()) + return Def; return this; case Decl::ObjCProtocol: - if (auto *Def = cast(this)->getDefinition()) - return Def; + if (auto *OPD = dyn_cast(this)) + if (auto *Def = OPD->getDefinition()) + return Def; return this; case Decl::ObjCCategory: diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 102d21e519..0772dcccc5 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -1698,31 +1698,20 @@ ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) { auto offset = getOffset(); // If we're producing a pointer, this is easy. - if (auto destPtrTy = cast(destTy)) { - if (Value.isNullPointer()) { - // FIXME: integer offsets from non-zero null pointers. - return CGM.getNullPointer(destPtrTy, DestType); - } - - // Convert the integer to a pointer-sized integer before converting it - // to a pointer. - // FIXME: signedness depends on the original integer type. - auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy); - llvm::Constant *C = offset; - C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy, - /*isSigned*/ false); - C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy); - return C; - } - - // Otherwise, we're basically returning an integer constant. - - // FIXME: this does the wrong thing with ptrtoint of a null pointer, - // but since we don't know the original pointer type, there's not much - // we can do about it. - - auto C = getOffset(); - C = llvm::ConstantExpr::getIntegerCast(C, destTy, /*isSigned*/ false); + auto destPtrTy = cast(destTy); + if (Value.isNullPointer()) { + // FIXME: integer offsets from non-zero null pointers. + return CGM.getNullPointer(destPtrTy, DestType); + } + + // Convert the integer to a pointer-sized integer before converting it + // to a pointer. + // FIXME: signedness depends on the original integer type. + auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy); + llvm::Constant *C = offset; + C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy, + /*isSigned*/ false); + C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy); return C; } diff --git a/lib/CodeGen/MicrosoftCXXABI.cpp b/lib/CodeGen/MicrosoftCXXABI.cpp index cf6d0cf448..7a5cdf6865 100644 --- a/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/lib/CodeGen/MicrosoftCXXABI.cpp @@ -735,7 +735,7 @@ public: CGM.CreateRuntimeFunction(FTy, "_CxxThrowException"); // _CxxThrowException is stdcall on 32-bit x86 platforms. if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { - if (auto *Fn = cast(Throw.getCallee())) + if (auto *Fn = dyn_cast(Throw.getCallee())) Fn->setCallingConv(llvm::CallingConv::X86_StdCall); } return Throw; diff --git a/lib/Sema/SemaOpenMP.cpp b/lib/Sema/SemaOpenMP.cpp index 69fcacf22a..f084a0345b 100644 --- a/lib/Sema/SemaOpenMP.cpp +++ b/lib/Sema/SemaOpenMP.cpp @@ -10901,7 +10901,7 @@ buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, for (NamedDecl *D : ULE->decls()) { if (D == PrevD) Lookups.push_back(UnresolvedSet<8>()); - else if (auto *DRD = cast(D)) + else if (auto *DRD = dyn_cast(D)) Lookups.back().addDecl(DRD); PrevD = D; } -- 2.40.0