From cfd366ba74c566038c6f417da9c9becc321fd737 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Tue, 14 Jan 2020 16:30:05 +0000 Subject: Fix "pointer is null" static analyzer warnings. NFCI. Use castAs<> instead of getAs<> since the pointer is dereferenced immediately in all cases and castAs will perform the null assertion for us. --- clang/lib/Sema/SemaDeclCXX.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'clang/lib') diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 497e45631be..9916d3be77e 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -1501,13 +1501,13 @@ void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { // as pointers to member functions. if (const ReferenceType *R = NewType->getAs()) { NewType = R->getPointeeType(); - OldType = OldType->getAs()->getPointeeType(); + OldType = OldType->castAs()->getPointeeType(); } else if (const PointerType *P = NewType->getAs()) { NewType = P->getPointeeType(); - OldType = OldType->getAs()->getPointeeType(); + OldType = OldType->castAs()->getPointeeType(); } else if (const MemberPointerType *M = NewType->getAs()) { NewType = M->getPointeeType(); - OldType = OldType->getAs()->getPointeeType(); + OldType = OldType->castAs()->getPointeeType(); } if (!NewType->isFunctionProtoType()) @@ -1633,7 +1633,7 @@ static bool CheckConstexprParameterTypes(Sema &SemaRef, const FunctionDecl *FD, Sema::CheckConstexprKind Kind) { unsigned ArgIndex = 0; - const FunctionProtoType *FT = FD->getType()->getAs(); + const auto *FT = FD->getType()->castAs(); for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), e = FT->param_type_end(); i != e; ++i, ++ArgIndex) { @@ -9829,7 +9829,7 @@ QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, // Rebuild the function type "R" without any type qualifiers (in // case any of the errors above fired) and with "void" as the // return type, since constructors don't have return types. - const FunctionProtoType *Proto = R->getAs(); + const FunctionProtoType *Proto = R->castAs(); if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) return R; @@ -10027,7 +10027,7 @@ QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, if (!D.isInvalidType()) return R; - const FunctionProtoType *Proto = R->getAs(); + const FunctionProtoType *Proto = R->castAs(); FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); EPI.Variadic = false; EPI.TypeQuals = Qualifiers(); @@ -10101,7 +10101,7 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, D.setInvalidType(); } - const FunctionProtoType *Proto = R->getAs(); + const auto *Proto = R->castAs(); // Make sure we don't have any parameters. if (Proto->getNumParams() > 0) { @@ -13015,8 +13015,7 @@ void Sema::AdjustDestructorExceptionSpec(CXXDestructorDecl *Destructor) { // A declaration of a destructor that does not have an exception- // specification is implicitly considered to have the same exception- // specification as an implicit declaration. - const FunctionProtoType *DtorType = Destructor->getType()-> - getAs(); + const auto *DtorType = Destructor->getType()->castAs(); if (DtorType->hasExceptionSpec()) return; @@ -13996,8 +13995,8 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, // The parameter for the "other" object, which we are move from. ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); - QualType OtherRefType = Other->getType()-> - getAs()->getPointeeType(); + QualType OtherRefType = + Other->getType()->castAs()->getPointeeType(); // Our location for everything implicitly-generated. SourceLocation Loc = MoveAssignOperator->getEndLoc().isValid() @@ -14791,9 +14790,7 @@ Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, unsigned NumArgs = ArgsPtr.size(); Expr **Args = ArgsPtr.data(); - const FunctionProtoType *Proto - = Constructor->getType()->getAs(); - assert(Proto && "Constructor without a prototype?"); + const auto *Proto = Constructor->getType()->castAs(); unsigned NumParams = Proto->getNumParams(); // If too few arguments are available, we'll fill in the rest with defaults. @@ -14856,7 +14853,7 @@ CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, unsigned DependentParamTypeDiag, unsigned InvalidParamTypeDiag) { QualType ResultType = - FnDecl->getType()->getAs()->getReturnType(); + FnDecl->getType()->castAs()->getReturnType(); // Check that the result type is not dependent. if (ResultType->isDependentType()) @@ -15085,7 +15082,7 @@ bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { // Overloaded operators other than operator() cannot be variadic. if (Op != OO_Call && - FnDecl->getType()->getAs()->isVariadic()) { + FnDecl->getType()->castAs()->isVariadic()) { return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) << FnDecl->getDeclName(); } @@ -16458,8 +16455,8 @@ void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, const CXXMethodDecl *Old) { - const auto *NewFT = New->getType()->getAs(); - const auto *OldFT = Old->getType()->getAs(); + const auto *NewFT = New->getType()->castAs(); + const auto *OldFT = Old->getType()->castAs(); if (OldFT->hasExtParameterInfos()) { for (unsigned I = 0, E = OldFT->getNumParams(); I != E; ++I) @@ -16506,8 +16503,8 @@ bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, const CXXMethodDecl *Old) { - QualType NewTy = New->getType()->getAs()->getReturnType(); - QualType OldTy = Old->getType()->getAs()->getReturnType(); + QualType NewTy = New->getType()->castAs()->getReturnType(); + QualType OldTy = Old->getType()->castAs()->getReturnType(); if (Context.hasSameType(NewTy, OldTy) || NewTy->isDependentType() || OldTy->isDependentType()) -- cgit v1.2.3