Skip to content

Commit

Permalink
[analyzer][NFC] Change return value of StoreManager::attemptDownCast …
Browse files Browse the repository at this point in the history
…function from SVal to Optional<SVal>

Summary: Refactor return value of `StoreManager::attemptDownCast` function by removing the last parameter `bool &Failed` and replace the return value `SVal` with `Optional<SVal>`.  Make the function consistent with the family of `evalDerivedToBase` by renaming it to `evalBaseToDerived`. Aligned the code on the call side with these changes.

Differential Revision: https://reviews.llvm.org/
  • Loading branch information
ASDenysPetrov committed Dec 17, 2021
1 parent c680fb6 commit da8bd97
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h
Expand Up @@ -172,9 +172,9 @@ class StoreManager {
/// dynamic_cast.
/// - We don't know (base is a symbolic region and we don't have
/// enough info to determine if the cast will succeed at run time).
/// The function returns an SVal representing the derived class; it's
/// valid only if Failed flag is set to false.
SVal attemptDownCast(SVal Base, QualType DerivedPtrType, bool &Failed);
/// The function returns an optional with SVal representing the derived class
/// in case of a successful cast and `None` otherwise.
Optional<SVal> evalBaseToDerived(SVal Base, QualType DerivedPtrType);

const ElementRegion *GetElementZeroRegion(const SubRegion *R, QualType T);

Expand Down
9 changes: 5 additions & 4 deletions clang/lib/StaticAnalyzer/Core/CallEvent.cpp
Expand Up @@ -762,17 +762,18 @@ void CXXInstanceCall::getInitialStackFrameContents(
QualType Ty = Ctx.getPointerType(Ctx.getRecordType(Class));

// FIXME: CallEvent maybe shouldn't be directly accessing StoreManager.
bool Failed;
ThisVal = StateMgr.getStoreManager().attemptDownCast(ThisVal, Ty, Failed);
if (Failed) {
Optional<SVal> V =
StateMgr.getStoreManager().evalBaseToDerived(ThisVal, Ty);
if (!V.hasValue()) {
// We might have suffered some sort of placement new earlier, so
// we're constructing in a completely unexpected storage.
// Fall back to a generic pointer cast for this-value.
const CXXMethodDecl *StaticMD = cast<CXXMethodDecl>(getDecl());
const CXXRecordDecl *StaticClass = StaticMD->getParent();
QualType StaticTy = Ctx.getPointerType(Ctx.getRecordType(StaticClass));
ThisVal = SVB.evalCast(ThisVal, Ty, StaticTy);
}
} else
ThisVal = *V;
}

if (!ThisVal.isUnknown())
Expand Down
24 changes: 12 additions & 12 deletions clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
Expand Up @@ -439,14 +439,15 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
if (CastE->isGLValue())
resultType = getContext().getPointerType(resultType);

bool Failed = false;

// Check if the value being cast evaluates to 0.
if (val.isZeroConstant())
Failed = true;
// Else, evaluate the cast.
else
val = getStoreManager().attemptDownCast(val, T, Failed);
bool Failed = true;

// Check if the value being cast does not evaluates to 0.
if (!val.isZeroConstant())
if (Optional<SVal> V =
StateMgr.getStoreManager().evalBaseToDerived(val, T)) {
val = *V;
Failed = false;
}

if (Failed) {
if (T->isReferenceType()) {
Expand Down Expand Up @@ -478,14 +479,13 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
if (CastE->isGLValue())
resultType = getContext().getPointerType(resultType);

bool Failed = false;

if (!val.isConstant()) {
val = getStoreManager().attemptDownCast(val, T, Failed);
Optional<SVal> V = getStoreManager().evalBaseToDerived(val, T);
val = V ? *V : UnknownVal();
}

// Failed to cast or the result is unknown, fall back to conservative.
if (Failed || val.isUnknown()) {
if (val.isUnknown()) {
val =
svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
currBldrCtx->blockCount());
Expand Down
9 changes: 4 additions & 5 deletions clang/lib/StaticAnalyzer/Core/Store.cpp
Expand Up @@ -314,10 +314,7 @@ static const CXXRecordDecl *getCXXRecordType(const MemRegion *MR) {
return nullptr;
}

SVal StoreManager::attemptDownCast(SVal Base, QualType TargetType,
bool &Failed) {
Failed = false;

Optional<SVal> StoreManager::evalBaseToDerived(SVal Base, QualType TargetType) {
const MemRegion *MR = Base.getAsRegion();
if (!MR)
return UnknownVal();
Expand Down Expand Up @@ -392,7 +389,9 @@ SVal StoreManager::attemptDownCast(SVal Base, QualType TargetType,
}

// We failed if the region we ended up with has perfect type info.
Failed = isa<TypedValueRegion>(MR);
if (isa<TypedValueRegion>(MR))
return None;

return UnknownVal();
}

Expand Down

0 comments on commit da8bd97

Please sign in to comment.