Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h =================================================================== --- clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h +++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h @@ -380,6 +380,10 @@ return loc::ConcreteInt(BasicVals.getValue(integer)); } + /// Return MemRegionVal on success cast, otherwise return None. + Optional getCastedMemRegionVal(const MemRegion *region, + QualType type); + /// Make an SVal that represents the given symbol. This follows the convention /// of representing Loc-type symbols (symbolic pointers and references) /// as Loc values wrapping the symbol rather than as plain symbol values. Index: clang/lib/StaticAnalyzer/Core/SValBuilder.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/SValBuilder.cpp +++ clang/lib/StaticAnalyzer/Core/SValBuilder.cpp @@ -268,6 +268,13 @@ return loc::MemRegionVal(BD); } +Optional +SValBuilder::getCastedMemRegionVal(const MemRegion *R, QualType Ty) { + if (auto OptR = StateMgr.getStoreManager().castRegion(R, Ty)) + return loc::MemRegionVal(*OptR); + return None; +} + /// Return a memory region for the 'this' object reference. loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D, const StackFrameContext *SFC) { @@ -753,16 +760,16 @@ if (const auto *SR = dyn_cast(R)) { QualType SRTy = SR->getSymbol()->getType(); if (!hasSameUnqualifiedPointeeType(SRTy, CastTy)) { - if (auto OptR = StateMgr.getStoreManager().castRegion(SR, CastTy)) - return loc::MemRegionVal(*OptR); + if (auto OptMemRegV = getCastedMemRegionVal(SR, CastTy)) + return *OptMemRegV; } } } // Next fixes pointer dereference using type different from its initial // one. See PR37503 and PR49007 for details. if (const auto *ER = dyn_cast(R)) { - if (auto OptR = StateMgr.getStoreManager().castRegion(ER, CastTy)) - return loc::MemRegionVal(*OptR); + if (auto OptMemRegV = getCastedMemRegionVal(ER, CastTy)) + return *OptMemRegV; } return V; @@ -807,8 +814,8 @@ // Get the result of casting a region to a different type. const MemRegion *R = V.getRegion(); - if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy)) - return loc::MemRegionVal(*OptR); + if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy)) + return *OptMemRegV; } // Pointer to whatever else. @@ -873,8 +880,8 @@ if (!IsUnknownOriginalType && Loc::isLocType(CastTy) && OriginalTy->isIntegralOrEnumerationType()) { if (const MemRegion *R = L.getAsRegion()) - if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy)) - return loc::MemRegionVal(*OptR); + if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy)) + return *OptMemRegV; return L; } @@ -890,8 +897,8 @@ // Delegate to store manager to get the result of casting a region to a // different type. If the MemRegion* returned is NULL, this expression // Evaluates to UnknownVal. - if (auto OptR = StateMgr.getStoreManager().castRegion(R, CastTy)) - return loc::MemRegionVal(*OptR); + if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy)) + return *OptMemRegV; } } else { if (Loc::isLocType(CastTy)) {