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

llvm-svn: 356441
This commit is contained in:
Don Hinton 2019-03-19 06:14:14 +00:00
parent c60bc94afc
commit f170dff3c1
5 changed files with 51 additions and 63 deletions

View File

@ -8460,7 +8460,7 @@ Error ASTImporter::ImportDefinition_New(Decl *From) {
if (!To)
return llvm::make_error<ImportError>();
if (auto *FromDC = cast<DeclContext>(From)) {
auto *FromDC = cast<DeclContext>(From);
ASTNodeImporter Importer(*this);
if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
@ -8497,9 +8497,6 @@ Error ASTImporter::ImportDefinition_New(Decl *From) {
return Importer.ImportDeclContext(FromDC, true);
}
return Error::success();
}
void ASTImporter::ImportDefinition(Decl *From) {
Error Err = ImportDefinition_New(From);
llvm::consumeError(std::move(Err));

View File

@ -1179,12 +1179,14 @@ DeclContext *DeclContext::getPrimaryContext() {
return this;
case Decl::ObjCInterface:
if (auto *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this))
if (auto *Def = OID->getDefinition())
return Def;
return this;
case Decl::ObjCProtocol:
if (auto *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this))
if (auto *Def = OPD->getDefinition())
return Def;
return this;

View File

@ -1698,7 +1698,7 @@ ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
auto offset = getOffset();
// If we're producing a pointer, this is easy.
if (auto destPtrTy = cast<llvm::PointerType>(destTy)) {
auto destPtrTy = cast<llvm::PointerType>(destTy);
if (Value.isNullPointer()) {
// FIXME: integer offsets from non-zero null pointers.
return CGM.getNullPointer(destPtrTy, DestType);
@ -1715,17 +1715,6 @@ ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
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);
return C;
}
ConstantLValue
ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
// Handle values.

View File

@ -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<llvm::Function>(Throw.getCallee()))
if (auto *Fn = dyn_cast<llvm::Function>(Throw.getCallee()))
Fn->setCallingConv(llvm::CallingConv::X86_StdCall);
}
return Throw;

View File

@ -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<OMPDeclareReductionDecl>(D))
else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
Lookups.back().addDecl(DRD);
PrevD = D;
}