Skip to content

Commit 6891c72

Browse files
committed
SemaExpr - silence static analyzer getAs<> null dereference warnings. NFCI.
The static analyzer is warning about potential null dereferences, but in these cases we should be able to use castAs<> directly and if not assert will fire for us. llvm-svn: 373150
1 parent 520876d commit 6891c72

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6714,8 +6714,8 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
67146714
assert(Ty->isVectorType() && "Expected vector type");
67156715

67166716
SmallVector<Expr *, 8> initExprs;
6717-
const VectorType *VTy = Ty->getAs<VectorType>();
6718-
unsigned numElems = Ty->getAs<VectorType>()->getNumElements();
6717+
const VectorType *VTy = Ty->castAs<VectorType>();
6718+
unsigned numElems = VTy->getNumElements();
67196719

67206720
// '(...)' form of vector initialization in AltiVec: the number of
67216721
// initializers must be one or must match the size of the vector.
@@ -6726,7 +6726,7 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
67266726
// vector. If a single value is specified in the initializer then it will
67276727
// be replicated to all the components of the vector
67286728
if (numExprs == 1) {
6729-
QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
6729+
QualType ElemTy = VTy->getElementType();
67306730
ExprResult Literal = DefaultLvalueConversion(exprs[0]);
67316731
if (Literal.isInvalid())
67326732
return ExprError();
@@ -6748,7 +6748,7 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
67486748
if (getLangOpts().OpenCL &&
67496749
VTy->getVectorKind() == VectorType::GenericVector &&
67506750
numExprs == 1) {
6751-
QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
6751+
QualType ElemTy = VTy->getElementType();
67526752
ExprResult Literal = DefaultLvalueConversion(exprs[0]);
67536753
if (Literal.isInvalid())
67546754
return ExprError();
@@ -7047,8 +7047,8 @@ checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS,
70477047
QualType RHSTy = RHS.get()->getType();
70487048

70497049
// get the "pointed to" types
7050-
QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType();
7051-
QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType();
7050+
QualType lhptee = LHSTy->castAs<PointerType>()->getPointeeType();
7051+
QualType rhptee = RHSTy->castAs<PointerType>()->getPointeeType();
70527052

70537053
// ignore qualifiers on void (C99 6.5.15p3, clause 6)
70547054
if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) {
@@ -7533,8 +7533,8 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS,
75337533
LHS = RHS = true;
75347534
return QualType();
75357535
}
7536-
QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType();
7537-
QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType();
7536+
QualType lhptee = LHSTy->castAs<PointerType>()->getPointeeType();
7537+
QualType rhptee = RHSTy->castAs<ObjCObjectPointerType>()->getPointeeType();
75387538
QualType destPointee
75397539
= Context.getQualifiedType(lhptee, rhptee.getQualifiers());
75407540
QualType destType = Context.getPointerType(destPointee);
@@ -7553,8 +7553,8 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS,
75537553
LHS = RHS = true;
75547554
return QualType();
75557555
}
7556-
QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType();
7557-
QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType();
7556+
QualType lhptee = LHSTy->castAs<ObjCObjectPointerType>()->getPointeeType();
7557+
QualType rhptee = RHSTy->castAs<PointerType>()->getPointeeType();
75587558
QualType destPointee
75597559
= Context.getQualifiedType(rhptee, lhptee.getQualifiers());
75607560
QualType destType = Context.getPointerType(destPointee);
@@ -8059,8 +8059,8 @@ checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType,
80598059
return Sema::IncompatiblePointer;
80608060
return Sema::Compatible;
80618061
}
8062-
QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType();
8063-
QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType();
8062+
QualType lhptee = LHSType->castAs<ObjCObjectPointerType>()->getPointeeType();
8063+
QualType rhptee = RHSType->castAs<ObjCObjectPointerType>()->getPointeeType();
80648064

80658065
if (!lhptee.isAtLeastAsQualifiedAs(rhptee) &&
80668066
// make an exception for id<P>
@@ -9401,8 +9401,8 @@ static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc,
94019401

94029402
// if both are pointers check if operation is valid wrt address spaces
94039403
if (S.getLangOpts().OpenCL && isLHSPointer && isRHSPointer) {
9404-
const PointerType *lhsPtr = LHSExpr->getType()->getAs<PointerType>();
9405-
const PointerType *rhsPtr = RHSExpr->getType()->getAs<PointerType>();
9404+
const PointerType *lhsPtr = LHSExpr->getType()->castAs<PointerType>();
9405+
const PointerType *rhsPtr = RHSExpr->getType()->castAs<PointerType>();
94069406
if (!lhsPtr->isAddressSpaceOverlapping(*rhsPtr)) {
94079407
S.Diag(Loc,
94089408
diag::err_typecheck_op_on_nonoverlapping_address_space_pointers)
@@ -10495,7 +10495,7 @@ static QualType checkArithmeticOrEnumeralThreeWayCompare(Sema &S,
1049510495
return QualType();
1049610496
}
1049710497
QualType IntType =
10498-
LHSStrippedType->getAs<EnumType>()->getDecl()->getIntegerType();
10498+
LHSStrippedType->castAs<EnumType>()->getDecl()->getIntegerType();
1049910499
assert(IntType->isArithmeticType());
1050010500

1050110501
// We can't use `CK_IntegralCast` when the underlying type is 'bool', so we
@@ -10778,8 +10778,8 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
1077810778
if (LCanPointeeTy != RCanPointeeTy) {
1077910779
// Treat NULL constant as a special case in OpenCL.
1078010780
if (getLangOpts().OpenCL && !LHSIsNull && !RHSIsNull) {
10781-
const PointerType *LHSPtr = LHSType->getAs<PointerType>();
10782-
if (!LHSPtr->isAddressSpaceOverlapping(*RHSType->getAs<PointerType>())) {
10781+
const PointerType *LHSPtr = LHSType->castAs<PointerType>();
10782+
if (!LHSPtr->isAddressSpaceOverlapping(*RHSType->castAs<PointerType>())) {
1078310783
Diag(Loc,
1078410784
diag::err_typecheck_op_on_nonoverlapping_address_space_pointers)
1078510785
<< LHSType << RHSType << 0 /* comparison */
@@ -11044,7 +11044,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
1104411044
// the largest type to the smallest type to avoid cases where long long == long,
1104511045
// where long gets picked over long long.
1104611046
QualType Sema::GetSignedVectorType(QualType V) {
11047-
const VectorType *VTy = V->getAs<VectorType>();
11047+
const VectorType *VTy = V->castAs<VectorType>();
1104811048
unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
1104911049

1105011050
if (isa<ExtVectorType>(VTy)) {
@@ -11099,7 +11099,7 @@ QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS,
1109911099
// If AltiVec, the comparison results in a numeric type, i.e.
1110011100
// bool for C++, int for C
1110111101
if (getLangOpts().AltiVec &&
11102-
vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector)
11102+
vType->castAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector)
1110311103
return Context.getLogicalOperationType();
1110411104

1110511105
// For non-floating point types, check for self-comparisons of the form
@@ -12108,11 +12108,11 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op,
1210812108
} else if (S.getLangOpts().AltiVec && ResType->isVectorType()) {
1210912109
// OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 )
1211012110
} else if (S.getLangOpts().ZVector && ResType->isVectorType() &&
12111-
(ResType->getAs<VectorType>()->getVectorKind() !=
12111+
(ResType->castAs<VectorType>()->getVectorKind() !=
1211212112
VectorType::AltiVecBool)) {
1211312113
// The z vector extensions allow ++ and -- for non-bool vectors.
1211412114
} else if(S.getLangOpts().OpenCL && ResType->isVectorType() &&
12115-
ResType->getAs<VectorType>()->getElementType()->isIntegerType()) {
12115+
ResType->castAs<VectorType>()->getElementType()->isIntegerType()) {
1211612116
// OpenCL V1.2 6.3 says dec/inc ops operate on integer vector types.
1211712117
} else {
1211812118
S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement)
@@ -12698,7 +12698,7 @@ static ExprResult convertHalfVecBinOp(Sema &S, ExprResult LHS, ExprResult RHS,
1269812698
LHS = convertVector(LHS.get(), Context.FloatTy, S);
1269912699
auto *BO = new (Context) BinaryOperator(LHS.get(), RHS.get(), Opc, BinOpResTy,
1270012700
VK, OK, OpLoc, FPFeatures);
12701-
return convertVector(BO, ResultTy->getAs<VectorType>()->getElementType(), S);
12701+
return convertVector(BO, ResultTy->castAs<VectorType>()->getElementType(), S);
1270212702
}
1270312703

1270412704
static std::pair<ExprResult, ExprResult>
@@ -13454,7 +13454,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
1345413454
else if (resultType->isVectorType() &&
1345513455
// The z vector extensions don't allow + or - with bool vectors.
1345613456
(!Context.getLangOpts().ZVector ||
13457-
resultType->getAs<VectorType>()->getVectorKind() !=
13457+
resultType->castAs<VectorType>()->getVectorKind() !=
1345813458
VectorType::AltiVecBool))
1345913459
break;
1346013460
else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6
@@ -13483,7 +13483,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
1348313483
else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) {
1348413484
// OpenCL v1.1 s6.3.f: The bitwise operator not (~) does not operate
1348513485
// on vector float types.
13486-
QualType T = resultType->getAs<ExtVectorType>()->getElementType();
13486+
QualType T = resultType->castAs<ExtVectorType>()->getElementType();
1348713487
if (!T->isIntegerType())
1348813488
return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr)
1348913489
<< resultType << Input.get()->getSourceRange());
@@ -13528,7 +13528,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
1352813528
!Context.getLangOpts().OpenCLCPlusPlus) {
1352913529
// OpenCL v1.1 6.3.h: The logical operator not (!) does not
1353013530
// operate on vector float types.
13531-
QualType T = resultType->getAs<ExtVectorType>()->getElementType();
13531+
QualType T = resultType->castAs<ExtVectorType>()->getElementType();
1353213532
if (!T->isIntegerType())
1353313533
return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr)
1353413534
<< resultType << Input.get()->getSourceRange());
@@ -14193,7 +14193,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc,
1419314193

1419414194
// If the user wrote a function type in some form, try to use that.
1419514195
if (!BSI->FunctionType.isNull()) {
14196-
const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>();
14196+
const FunctionType *FTy = BSI->FunctionType->castAs<FunctionType>();
1419714197

1419814198
FunctionType::ExtInfo Ext = FTy->getExtInfo();
1419914199
if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true);
@@ -14665,24 +14665,24 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
1466514665
case IncompatibleObjCQualifiedId: {
1466614666
if (SrcType->isObjCQualifiedIdType()) {
1466714667
const ObjCObjectPointerType *srcOPT =
14668-
SrcType->getAs<ObjCObjectPointerType>();
14668+
SrcType->castAs<ObjCObjectPointerType>();
1466914669
for (auto *srcProto : srcOPT->quals()) {
1467014670
PDecl = srcProto;
1467114671
break;
1467214672
}
1467314673
if (const ObjCInterfaceType *IFaceT =
14674-
DstType->getAs<ObjCObjectPointerType>()->getInterfaceType())
14674+
DstType->castAs<ObjCObjectPointerType>()->getInterfaceType())
1467514675
IFace = IFaceT->getDecl();
1467614676
}
1467714677
else if (DstType->isObjCQualifiedIdType()) {
1467814678
const ObjCObjectPointerType *dstOPT =
14679-
DstType->getAs<ObjCObjectPointerType>();
14679+
DstType->castAs<ObjCObjectPointerType>();
1468014680
for (auto *dstProto : dstOPT->quals()) {
1468114681
PDecl = dstProto;
1468214682
break;
1468314683
}
1468414684
if (const ObjCInterfaceType *IFaceT =
14685-
SrcType->getAs<ObjCObjectPointerType>()->getInterfaceType())
14685+
SrcType->castAs<ObjCObjectPointerType>()->getInterfaceType())
1468614686
IFace = IFaceT->getDecl();
1468714687
}
1468814688
DiagKind = diag::warn_incompatible_qualified_id;

0 commit comments

Comments
 (0)