[clang] 2901dc7 - Don't directly dereference getAs<> casts to avoid potential null dereferences. NFCI.
Simon Pilgrim via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 6 04:24:34 PDT 2021
Author: Simon Pilgrim
Date: 2021-04-06T12:24:19+01:00
New Revision: 2901dc7575873ed4bdfa1d7a0e79020e7a9ffb3d
URL: https://github.com/llvm/llvm-project/commit/2901dc7575873ed4bdfa1d7a0e79020e7a9ffb3d
DIFF: https://github.com/llvm/llvm-project/commit/2901dc7575873ed4bdfa1d7a0e79020e7a9ffb3d.diff
LOG: Don't directly dereference getAs<> casts to avoid potential null dereferences. NFCI.
Replace with castAs<> which asserts the cast is valid.
Fixes a number of static analyzer warnings.
Added:
Modified:
clang/lib/AST/ExprConstant.cpp
clang/lib/Analysis/CalledOnceCheck.cpp
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/CodeGen/CGCUDANV.cpp
clang/lib/CodeGen/CGExprScalar.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaExprMember.cpp
clang/lib/Sema/SemaOverload.cpp
clang/lib/Sema/SemaTemplateDeduction.cpp
clang/unittests/AST/ASTImporterTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 624b1bfde4e64..b42f3b695ec57 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10307,10 +10307,10 @@ bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
"Must both be vector types");
// Checking JUST the types are the same would be fine, except shifts don't
// need to have their types be the same (since you always shift by an int).
- assert(LHS->getType()->getAs<VectorType>()->getNumElements() ==
- E->getType()->getAs<VectorType>()->getNumElements() &&
- RHS->getType()->getAs<VectorType>()->getNumElements() ==
- E->getType()->getAs<VectorType>()->getNumElements() &&
+ assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==
+ E->getType()->castAs<VectorType>()->getNumElements() &&
+ RHS->getType()->castAs<VectorType>()->getNumElements() ==
+ E->getType()->castAs<VectorType>()->getNumElements() &&
"All operands must be the same size.");
APValue LHSValue;
diff --git a/clang/lib/Analysis/CalledOnceCheck.cpp b/clang/lib/Analysis/CalledOnceCheck.cpp
index 00bb51a1c0d3d..9fa8ac30404c3 100644
--- a/clang/lib/Analysis/CalledOnceCheck.cpp
+++ b/clang/lib/Analysis/CalledOnceCheck.cpp
@@ -983,9 +983,9 @@ class CalledOnceChecker : public ConstStmtVisitor<CalledOnceChecker> {
return false;
}
- QualType BlockType = Ty->getAs<BlockPointerType>()->getPointeeType();
+ QualType BlockType = Ty->castAs<BlockPointerType>()->getPointeeType();
// Completion handlers should have a block type with void return type.
- return BlockType->getAs<FunctionType>()->getReturnType()->isVoidType();
+ return BlockType->castAs<FunctionType>()->getReturnType()->isVoidType();
}
/// Return true if the only parameter of the function is conventional.
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 6b43a8de218e2..a38176af390d4 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -8760,7 +8760,7 @@ Value *CodeGenFunction::EmitSVEMaskedLoad(const CallExpr *E,
bool IsZExtReturn) {
QualType LangPTy = E->getArg(1)->getType();
llvm::Type *MemEltTy = CGM.getTypes().ConvertType(
- LangPTy->getAs<PointerType>()->getPointeeType());
+ LangPTy->castAs<PointerType>()->getPointeeType());
// The vector type that is returned may be
diff erent from the
// eventual type loaded from memory.
@@ -8785,7 +8785,7 @@ Value *CodeGenFunction::EmitSVEMaskedStore(const CallExpr *E,
unsigned BuiltinID) {
QualType LangPTy = E->getArg(1)->getType();
llvm::Type *MemEltTy = CGM.getTypes().ConvertType(
- LangPTy->getAs<PointerType>()->getPointeeType());
+ LangPTy->castAs<PointerType>()->getPointeeType());
// The vector type that is stored may be
diff erent from the
// eventual type stored to memory.
diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index d53a623b258c1..b224de7c197ae 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -1027,9 +1027,8 @@ void CGNVCUDARuntime::handleVarRegistration(const VarDecl *D,
D->getType()->isCUDADeviceBuiltinTextureType()) {
// Builtin surfaces and textures and their template arguments are
// also registered with CUDA runtime.
- const ClassTemplateSpecializationDecl *TD =
- cast<ClassTemplateSpecializationDecl>(
- D->getType()->getAs<RecordType>()->getDecl());
+ const auto *TD = cast<ClassTemplateSpecializationDecl>(
+ D->getType()->castAs<RecordType>()->getDecl());
const TemplateArgumentList &Args = TD->getTemplateArgs();
if (TD->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>()) {
assert(Args.size() == 2 &&
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index ef3e27ecec997..c4d49d3ac6958 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1729,7 +1729,7 @@ Value *ScalarExprEmitter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E) {
llvm::MatrixBuilder<CGBuilderTy> MB(Builder);
return MB.CreateExtractElement(
Matrix, RowIdx, ColumnIdx,
- E->getBase()->getType()->getAs<ConstantMatrixType>()->getNumRows());
+ E->getBase()->getType()->castAs<ConstantMatrixType>()->getNumRows());
}
static int getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index cf08ef4631a8e..dbc217640fd56 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9811,7 +9811,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
if (II && II->isStr(getCudaConfigureFuncName()) &&
!NewFD->isInvalidDecl() &&
NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
- if (!R->getAs<FunctionType>()->getReturnType()->isScalarType())
+ if (!R->castAs<FunctionType>()->getReturnType()->isScalarType())
Diag(NewFD->getLocation(), diag::err_config_scalar_return)
<< getCudaConfigureFuncName();
Context.setcudaConfigureCallDecl(NewFD);
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f54dd4cb6f43b..95eac2859b57f 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -14912,9 +14912,9 @@ void Sema::DefineImplicitLambdaToFunctionPointerConversion(
SynthesizedFunctionScope Scope(*this, Conv);
assert(!Conv->getReturnType()->isUndeducedType());
- QualType ConvRT = Conv->getType()->getAs<FunctionType>()->getReturnType();
+ QualType ConvRT = Conv->getType()->castAs<FunctionType>()->getReturnType();
CallingConv CC =
- ConvRT->getPointeeType()->getAs<FunctionType>()->getCallConv();
+ ConvRT->getPointeeType()->castAs<FunctionType>()->getCallConv();
CXXRecordDecl *Lambda = Conv->getParent();
FunctionDecl *CallOp = Lambda->getLambdaCallOperator();
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 1aff2a9edf0f9..223fcf2a1f26a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -5544,7 +5544,7 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
BaseExpr = LHSExp;
IndexExpr = RHSExp;
- ResultType = LHSTy->getAs<PointerType>()->getPointeeType();
+ ResultType = LHSTy->castAs<PointerType>()->getPointeeType();
} else if (RHSTy->isArrayType()) {
// Same as previous, except for 123[f().a] case
Diag(RHSExp->getBeginLoc(), diag::ext_subscript_non_lvalue)
@@ -5555,7 +5555,7 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
BaseExpr = RHSExp;
IndexExpr = LHSExp;
- ResultType = RHSTy->getAs<PointerType>()->getPointeeType();
+ ResultType = RHSTy->castAs<PointerType>()->getPointeeType();
} else {
return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value)
<< LHSExp->getSourceRange() << RHSExp->getSourceRange());
diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index f5afcb76fc96b..0663c0d277259 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -761,7 +761,7 @@ Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
if (!Base) {
TypoExpr *TE = nullptr;
QualType RecordTy = BaseType;
- if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
+ if (IsArrow) RecordTy = RecordTy->castAs<PointerType>()->getPointeeType();
if (LookupMemberExprInRecord(
*this, R, nullptr, RecordTy->getAs<RecordType>(), OpLoc, IsArrow,
SS, TemplateArgs != nullptr, TemplateKWLoc, TE))
@@ -1791,7 +1791,7 @@ Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow,
VK = VK_LValue;
} else {
QualType BaseType = BaseExpr->getType();
- if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
+ if (IsArrow) BaseType = BaseType->castAs<PointerType>()->getPointeeType();
Qualifiers BaseQuals = BaseType.getQualifiers();
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 67a867762d69c..feef15689fafd 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -3706,7 +3706,7 @@ compareConversionFunctions(Sema &S, FunctionDecl *Function1,
CallOp->getType()->getAs<FunctionProtoType>();
CallingConv CallOpCC =
- CallOp->getType()->getAs<FunctionType>()->getCallConv();
+ CallOp->getType()->castAs<FunctionType>()->getCallConv();
CallingConv DefaultFree = S.Context.getDefaultCallingConvention(
CallOpProto->isVariadic(), /*IsCXXMethod=*/false);
CallingConv DefaultMember = S.Context.getDefaultCallingConvention(
@@ -3927,7 +3927,7 @@ getFixedEnumPromtion(Sema &S, const StandardConversionSequence &SCS) {
if (!FromType->isEnumeralType())
return FixedEnumPromotion::None;
- EnumDecl *Enum = FromType->getAs<EnumType>()->getDecl();
+ EnumDecl *Enum = FromType->castAs<EnumType>()->getDecl();
if (!Enum->isFixed())
return FixedEnumPromotion::None;
@@ -10244,10 +10244,10 @@ static bool shouldSkipNotingLambdaConversionDecl(FunctionDecl *Fn) {
CXXMethodDecl *CallOp = RD->getLambdaCallOperator();
CallingConv CallOpCC =
- CallOp->getType()->getAs<FunctionType>()->getCallConv();
- QualType ConvRTy = ConvD->getType()->getAs<FunctionType>()->getReturnType();
+ CallOp->getType()->castAs<FunctionType>()->getCallConv();
+ QualType ConvRTy = ConvD->getType()->castAs<FunctionType>()->getReturnType();
CallingConv ConvToCC =
- ConvRTy->getPointeeType()->getAs<FunctionType>()->getCallConv();
+ ConvRTy->getPointeeType()->castAs<FunctionType>()->getCallConv();
return ConvToCC != CallOpCC;
}
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index ecf0c442ad46e..d755696c698c6 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -3920,7 +3920,7 @@ static bool AdjustFunctionParmAndArgTypesForDeduction(
if (isSimpleTemplateIdType(ParamType) ||
(isa<PointerType>(ParamType) &&
isSimpleTemplateIdType(
- ParamType->getAs<PointerType>()->getPointeeType())))
+ ParamType->castAs<PointerType>()->getPointeeType())))
TDF |= TDF_DerivedClass;
return false;
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index edbc648abbe15..b0a8c829809dd 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -6182,7 +6182,7 @@ TEST_P(CTAD, DeductionGuideShouldReferToANonLocalTypedef) {
ParmVarDecl *Param = Guide->getParamDecl(0);
// The type of the first param (which is a typedef) should match the typedef
// in the global scope.
- EXPECT_EQ(Param->getType()->getAs<TypedefType>()->getDecl(), Typedef);
+ EXPECT_EQ(Param->getType()->castAs<TypedefType>()->getDecl(), Typedef);
}
TEST_P(CTAD, DeductionGuideShouldReferToANonLocalTypedefInParamPtr) {
@@ -6223,7 +6223,7 @@ TEST_P(CTAD, DeductionGuideShouldCopyALocalTypedef) {
auto *Typedef = FirstDeclMatcher<TypedefNameDecl>().match(
TU, typedefNameDecl(hasName("U")));
ParmVarDecl *Param = Guide->getParamDecl(0);
- EXPECT_NE(Param->getType()->getAs<TypedefType>()->getDecl(), Typedef);
+ EXPECT_NE(Param->getType()->castAs<TypedefType>()->getDecl(), Typedef);
}
INSTANTIATE_TEST_CASE_P(ParameterizedTests, CTAD,
More information about the cfe-commits
mailing list