Skip to content

Commit

Permalink
[analyzer][NFC] Prefer using isa<> instead getAs<> in conditions
Browse files Browse the repository at this point in the history
Depends on D125709

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D127742
  • Loading branch information
Balazs Benics committed Jun 15, 2022
1 parent 481f860 commit 96ccb69
Show file tree
Hide file tree
Showing 35 changed files with 93 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class TrackConstraintBRVisitor final : public BugReporterVisitor {
public:
TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
: Constraint(constraint), Assumption(assumption),
IsZeroCheck(!Assumption && Constraint.getAs<Loc>()) {}
IsZeroCheck(!Assumption && isa<Loc>(Constraint)) {}

void Profile(llvm::FoldingSetNodeID &ID) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ class ElementRegion : public TypedValueRegion {
ElementRegion(QualType elementType, NonLoc Idx, const SubRegion *sReg)
: TypedValueRegion(sReg, ElementRegionKind), ElementType(elementType),
Index(Idx) {
assert((!Idx.getAs<nonloc::ConcreteInt>() ||
assert((!isa<nonloc::ConcreteInt>(Idx) ||
Idx.castAs<nonloc::ConcreteInt>().getValue().isSigned()) &&
"The index must be signed");
assert(!elementType.isNull() && !elementType->isVoidType() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ inline ProgramStateRef ProgramState::assumeInclusiveRange(
if (Val.isUnknown())
return this;

assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
assert(isa<NonLoc>(Val) && "Only NonLocs are supported!");

return getStateManager().ConstraintMgr->assumeInclusiveRange(
this, Val.castAs<NonLoc>(), From, To, Assumption);
Expand All @@ -740,7 +740,7 @@ ProgramState::assumeInclusiveRange(DefinedOrUnknownSVal Val,
if (Val.isUnknown())
return std::make_pair(this, this);

assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
assert(isa<NonLoc>(Val) && "Only NonLocs are supported!");

return getStateManager().ConstraintMgr->assumeInclusiveRangeDual(
this, Val.castAs<NonLoc>(), From, To);
Expand Down
17 changes: 8 additions & 9 deletions clang/lib/StaticAnalyzer/Checkers/ArrayBoundCheckerV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion());

if (Optional<NonLoc> NV = extentBegin.getAs<NonLoc>()) {
if (NV->getAs<nonloc::ConcreteInt>()) {
if (auto ConcreteNV = NV->getAs<nonloc::ConcreteInt>()) {
std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
getSimplifiedOffsets(rawOffset.getByteOffset(),
NV->castAs<nonloc::ConcreteInt>(),
getSimplifiedOffsets(rawOffset.getByteOffset(), *ConcreteNV,
svalBuilder);
rawOffsetVal = simplifiedOffsets.first;
*NV = simplifiedOffsets.second;
Expand Down Expand Up @@ -180,13 +179,13 @@ void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
// we are doing a load/store after the last valid offset.
const MemRegion *MR = rawOffset.getRegion();
DefinedOrUnknownSVal Size = getDynamicExtent(state, MR, svalBuilder);
if (!Size.getAs<NonLoc>())
if (!isa<NonLoc>(Size))
break;

if (Size.getAs<nonloc::ConcreteInt>()) {
if (auto ConcreteSize = Size.getAs<nonloc::ConcreteInt>()) {
std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
getSimplifiedOffsets(rawOffset.getByteOffset(),
Size.castAs<nonloc::ConcreteInt>(), svalBuilder);
getSimplifiedOffsets(rawOffset.getByteOffset(), *ConcreteSize,
svalBuilder);
rawOffsetVal = simplifiedOffsets.first;
Size = simplifiedOffsets.second;
}
Expand Down Expand Up @@ -275,7 +274,7 @@ void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const {
// is unknown or undefined, we lazily substitute '0'. Otherwise,
// return 'val'.
static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val;
return val.isUndef() ? svalBuilder.makeZeroArrayIndex() : val;
}

// Scale a base value by a scaling factor, and return the scaled
Expand Down Expand Up @@ -324,7 +323,7 @@ RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state,
case MemRegion::ElementRegionKind: {
const ElementRegion *elemReg = cast<ElementRegion>(region);
SVal index = elemReg->getIndex();
if (!index.getAs<NonLoc>())
if (!isa<NonLoc>(index))
return RegionRawOffsetV2();
QualType elemType = elemReg->getElementType();
// If the element is an incomplete type, go no further.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ void VariadicMethodTypeChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
continue;

// Ignore pointer constants.
if (msg.getArgSVal(I).getAs<loc::ConcreteInt>())
if (isa<loc::ConcreteInt>(msg.getArgSVal(I)))
continue;

// Ignore pointer types annotated with 'NSObject' attribute.
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);

SVal maxMinusRight;
if (right.getAs<nonloc::ConcreteInt>()) {
if (isa<nonloc::ConcreteInt>(right)) {
maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
sizeTy);
} else {
Expand Down Expand Up @@ -1675,7 +1675,7 @@ void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
// amountCopied = min (size - dstLen - 1 , srcLen)
SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
*dstStrLengthNL, sizeTy);
if (!freeSpace.getAs<NonLoc>())
if (!isa<NonLoc>(freeSpace))
return;
freeSpace =
svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S,
DefinedOrUnknownSVal location = l.castAs<DefinedOrUnknownSVal>();

// Check for null dereferences.
if (!location.getAs<Loc>())
if (!isa<Loc>(location))
return;

ProgramStateRef state = C.getState();
Expand Down
17 changes: 7 additions & 10 deletions clang/lib/StaticAnalyzer/Checkers/GTestChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void GTestChecker::modelAssertionResultBoolConstructor(
SVal BooleanArgVal = Call->getArgSVal(0);
if (IsRef) {
// The argument is a reference, so load from it to get the boolean value.
if (!BooleanArgVal.getAs<Loc>())
if (!isa<Loc>(BooleanArgVal))
return;
BooleanArgVal = C.getState()->getSVal(BooleanArgVal.castAs<Loc>());
}
Expand Down Expand Up @@ -270,20 +270,17 @@ SVal GTestChecker::getAssertionResultSuccessFieldValue(
ProgramStateRef GTestChecker::assumeValuesEqual(SVal Val1, SVal Val2,
ProgramStateRef State,
CheckerContext &C) {
if (!Val1.getAs<DefinedOrUnknownSVal>() ||
!Val2.getAs<DefinedOrUnknownSVal>())
auto DVal1 = Val1.getAs<DefinedOrUnknownSVal>();
auto DVal2 = Val2.getAs<DefinedOrUnknownSVal>();
if (!DVal1.hasValue() || !DVal2.hasValue())
return State;

auto ValuesEqual =
C.getSValBuilder().evalEQ(State, Val1.castAs<DefinedOrUnknownSVal>(),
Val2.castAs<DefinedOrUnknownSVal>());

if (!ValuesEqual.getAs<DefinedSVal>())
C.getSValBuilder().evalEQ(State, *DVal1, *DVal2).getAs<DefinedSVal>();
if (!ValuesEqual.hasValue())
return State;

State = C.getConstraintManager().assume(
State, ValuesEqual.castAs<DefinedSVal>(), true);

State = C.getConstraintManager().assume(State, *ValuesEqual, true);
return State;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ void GenericTaintRule::process(const GenericTaintChecker &Checker,
return;

const auto WouldEscape = [](SVal V, QualType Ty) -> bool {
if (!V.getAs<Loc>())
if (!isa<Loc>(V))
return false;

const bool IsNonConstRef = Ty->isReferenceType() && !Ty.isConstQualified();
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/Iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
const auto comparison =
SVB.evalBinOp(State, Opc, NL1, NL2, SVB.getConditionType());

assert(comparison.getAs<DefinedSVal>() &&
"Symbol comparison must be a `DefinedSVal`");
assert(isa<DefinedSVal>(comparison) &&
"Symbol comparison must be a `DefinedSVal`");

return !State->assume(comparison.castAs<DefinedSVal>(), false);
}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ void IteratorModeling::handlePtrIncrOrDecr(CheckerContext &C,
const Expr *Iterator,
OverloadedOperatorKind OK,
SVal Offset) const {
if (!Offset.getAs<DefinedSVal>())
if (!isa<DefinedSVal>(Offset))
return;

QualType PtrType = Iterator->getType();
Expand Down Expand Up @@ -799,8 +799,8 @@ ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1,
SVB.evalBinOp(State, BO_EQ, nonloc::SymbolVal(Sym1),
nonloc::SymbolVal(Sym2), SVB.getConditionType());

assert(comparison.getAs<DefinedSVal>() &&
"Symbol comparison must be a `DefinedSVal`");
assert(isa<DefinedSVal>(comparison) &&
"Symbol comparison must be a `DefinedSVal`");

auto NewState = State->assume(comparison.castAs<DefinedSVal>(), Equal);
if (!NewState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void MPIChecker::allRegionsUsedByWait(
Ctx.getState(), SuperRegion, Ctx.getSValBuilder(),
CE.getArgExpr(1)->getType()->getPointeeType());
const llvm::APSInt &ArrSize =
ElementCount.getAs<nonloc::ConcreteInt>()->getValue();
ElementCount.castAs<nonloc::ConcreteInt>().getValue();

for (size_t i = 0; i < ArrSize; ++i) {
const NonLoc Idx = Ctx.getSValBuilder().makeArrayIndex(i);
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ MallocChecker::performKernelMalloc(const CallEvent &Call, CheckerContext &C,

const Expr *FlagsEx = Call.getArgExpr(Call.getNumArgs() - 1);
const SVal V = C.getSVal(FlagsEx);
if (!V.getAs<NonLoc>()) {
if (!isa<NonLoc>(V)) {
// The case where 'V' can be a location can only be due to a bad header,
// so in this case bail out.
return None;
Expand Down Expand Up @@ -1907,12 +1907,12 @@ ProgramStateRef MallocChecker::FreeMemAux(
return nullptr;

SVal ArgVal = C.getSVal(ArgExpr);
if (!ArgVal.getAs<DefinedOrUnknownSVal>())
if (!isa<DefinedOrUnknownSVal>(ArgVal))
return nullptr;
DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>();

// Check for null dereferences.
if (!location.getAs<Loc>())
if (!isa<Loc>(location))
return nullptr;

// The explicit NULL case, no operation is performed.
Expand Down Expand Up @@ -2582,7 +2582,7 @@ MallocChecker::ReallocMemAux(CheckerContext &C, const CallEvent &Call,

const Expr *arg0Expr = CE->getArg(0);
SVal Arg0Val = C.getSVal(arg0Expr);
if (!Arg0Val.getAs<DefinedOrUnknownSVal>())
if (!isa<DefinedOrUnknownSVal>(Arg0Val))
return nullptr;
DefinedOrUnknownSVal arg0Val = Arg0Val.castAs<DefinedOrUnknownSVal>();

Expand All @@ -2598,7 +2598,7 @@ MallocChecker::ReallocMemAux(CheckerContext &C, const CallEvent &Call,
SVal TotalSize = C.getSVal(Arg1);
if (SuffixWithN)
TotalSize = evalMulForBufferSize(C, Arg1, CE->getArg(2));
if (!TotalSize.getAs<DefinedOrUnknownSVal>())
if (!isa<DefinedOrUnknownSVal>(TotalSize))
return nullptr;

// Compare the size argument to 0.
Expand Down Expand Up @@ -2908,7 +2908,7 @@ void MallocChecker::checkPreCall(const CallEvent &Call,
// Check arguments for being used after free.
for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) {
SVal ArgSVal = Call.getArgSVal(I);
if (ArgSVal.getAs<Loc>()) {
if (isa<Loc>(ArgSVal)) {
SymbolRef Sym = ArgSVal.getAsSymbol();
if (!Sym)
continue;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
CheckerContext &C) const {
if (matchesAny(Call, MmapFn, MprotectFn)) {
SVal ProtVal = Call.getArgSVal(2);
Optional<nonloc::ConcreteInt> ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
int64_t Prot = ProtLoc->getValue().getSExtValue();
auto ProtLoc = ProtVal.castAs<nonloc::ConcreteInt>();
int64_t Prot = ProtLoc.getValue().getSExtValue();
if (ProtExecOv != ProtExec)
ProtExec = ProtExecOv;
if (ProtReadOv != ProtRead)
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
CheckerContext &C) const {
if (!isLoad)
return;
if (loc.isUndef() || !loc.getAs<Loc>())
if (loc.isUndef() || !isa<Loc>(loc))
return;

ASTContext &Ctx = C.getASTContext();
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ void NonNullParamChecker::checkPreCall(const CallEvent &Call,
if (!DV)
continue;

assert(!HasRefTypeParam || DV->getAs<Loc>());
assert(!HasRefTypeParam || isa<Loc>(DV.getValue()));

// Process the case when the argument is not a location.
if (ExpectedToBeNonNull && !DV->getAs<Loc>()) {
if (ExpectedToBeNonNull && !isa<Loc>(DV.getValue())) {
// If the argument is a union type, we want to handle a potential
// transparent_union GCC extension.
if (!ArgE)
Expand All @@ -161,7 +161,7 @@ void NonNullParamChecker::checkPreCall(const CallEvent &Call,
assert(++CSV->begin() == CSV->end());
// FIXME: Handle (some_union){ some_other_union_val }, which turns into
// a LazyCompoundVal inside a CompoundVal.
if (!V.getAs<Loc>())
if (!isa<Loc>(V))
continue;

// Retrieve the corresponding expression.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/ObjCAtSyncChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
SVal V = C.getSVal(Ex);

// Uninitialized value used for the mutex?
if (V.getAs<UndefinedVal>()) {
if (isa<UndefinedVal>(V)) {
if (ExplodedNode *N = C.generateErrorNode()) {
if (!BT_undef)
BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ static bool isSelfVar(SVal location, CheckerContext &C) {
AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
if (!analCtx->getSelfDecl())
return false;
if (!location.getAs<loc::MemRegionVal>())
if (!isa<loc::MemRegionVal>(location))
return false;

loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/STLAlgorithmModeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void STLAlgorithmModeling::Find(CheckerContext &C, const CallExpr *CE,
nonloc::SymbolVal(NewPos->getOffset()),
nonloc::SymbolVal(Pos->getOffset()),
SVB.getConditionType());
assert(GreaterOrEqual.getAs<DefinedSVal>() &&
assert(isa<DefinedSVal>(GreaterOrEqual) &&
"Symbol comparison must be a `DefinedSVal`");
StateFound = StateFound->assume(GreaterOrEqual.castAs<DefinedSVal>(), true);
}
Expand All @@ -153,7 +153,7 @@ void STLAlgorithmModeling::Find(CheckerContext &C, const CallExpr *CE,
nonloc::SymbolVal(NewPos->getOffset()),
nonloc::SymbolVal(Pos->getOffset()),
SVB.getConditionType());
assert(Less.getAs<DefinedSVal>() &&
assert(isa<DefinedSVal>(Less) &&
"Symbol comparison must be a `DefinedSVal`");
StateFound = StateFound->assume(Less.castAs<DefinedSVal>(), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class StdLibraryFunctionsChecker
return State;

DefinedOrUnknownSVal L = V.castAs<DefinedOrUnknownSVal>();
if (!L.getAs<Loc>())
if (!isa<Loc>(L))
return State;

return State->assume(L, CannotBeNull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,

SVal V = State->getSVal(FieldVal);

if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
if (isDereferencableType(T) || isa<nonloc::LocAsInteger>(V)) {
if (isDereferencableUninit(FR, LocalChain))
ContainsUninitField = true;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ bool FindUninitializedFields::isDereferencableUninit(
SVal V = State->getSVal(FR);

assert((isDereferencableType(FR->getDecl()->getType()) ||
V.getAs<nonloc::LocAsInteger>()) &&
isa<nonloc::LocAsInteger>(V)) &&
"This method only checks dereferenceable objects!");

if (V.isUnknown() || V.getAs<loc::ConcreteInt>()) {
if (V.isUnknown() || isa<loc::ConcreteInt>(V)) {
IsAnyFieldInitialized = true;
return false;
}
Expand Down Expand Up @@ -230,8 +230,8 @@ static llvm::Optional<DereferenceInfo> dereference(ProgramStateRef State,
// If the static type of the field is a void pointer, or it is a
// nonloc::LocAsInteger, we need to cast it back to the dynamic type before
// dereferencing.
bool NeedsCastBack = isVoidPointer(FR->getDecl()->getType()) ||
V.getAs<nonloc::LocAsInteger>();
bool NeedsCastBack =
isVoidPointer(FR->getDecl()->getType()) || isa<nonloc::LocAsInteger>(V);

// The region we'd like to acquire.
const auto *R = V.getAsRegion()->getAs<TypedValueRegion>();
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
// Now check if oflags has O_CREAT set.
const Expr *oflagsEx = CE->getArg(FlagsArgIndex);
const SVal V = C.getSVal(oflagsEx);
if (!V.getAs<NonLoc>()) {
if (!isa<NonLoc>(V)) {
// The case where 'V' can be a location can only be due to a bad header,
// so in this case bail out.
return;
Expand Down
Loading

0 comments on commit 96ccb69

Please sign in to comment.