diff --git a/clang/include/clang/Analysis/PathSensitive/MemRegion.h b/clang/include/clang/Analysis/PathSensitive/MemRegion.h index b531375be2bc..65ac0b53027c 100644 --- a/clang/include/clang/Analysis/PathSensitive/MemRegion.h +++ b/clang/include/clang/Analysis/PathSensitive/MemRegion.h @@ -33,7 +33,7 @@ namespace llvm { class raw_ostream; } namespace clang { class MemRegionManager; - +class MemSpaceRegion; /// MemRegion - The root abstract class for all memory regions. class MemRegion : public llvm::FoldingSetNode { @@ -68,10 +68,14 @@ public: virtual MemRegionManager* getMemRegionManager() const = 0; std::string getString() const; + + const MemSpaceRegion *getMemorySpace() const; bool hasStackStorage() const; bool hasHeapStorage() const; + + bool hasHeapOrStackStorage() const; virtual void print(llvm::raw_ostream& os) const; diff --git a/clang/lib/Analysis/MemRegion.cpp b/clang/lib/Analysis/MemRegion.cpp index f018c83b910c..c8e027579a30 100644 --- a/clang/lib/Analysis/MemRegion.cpp +++ b/clang/lib/Analysis/MemRegion.cpp @@ -313,46 +313,41 @@ AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { return getRegion(E, cnt); } -bool MemRegion::hasStackStorage() const { - // Only subregions can have stack storage. + +const MemSpaceRegion *MemRegion::getMemorySpace() const { + const MemRegion *R = this; const SubRegion* SR = dyn_cast(this); - - if (!SR) - return false; - - MemSpaceRegion* S = getMemRegionManager()->getStackRegion(); while (SR) { - const MemRegion *R = SR->getSuperRegion(); - if (R == S) - return true; - - SR = dyn_cast(R); + R = SR->getSuperRegion(); + SR = dyn_cast(R); } - + + return dyn_cast(R); +} + +bool MemRegion::hasStackStorage() const { + if (const MemSpaceRegion *MS = getMemorySpace()) + return MS == getMemRegionManager()->getStackRegion(); + return false; } bool MemRegion::hasHeapStorage() const { - // Only subregions can have stack storage. - const SubRegion* SR = dyn_cast(this); - - if (!SR) - return false; - - MemSpaceRegion* H = getMemRegionManager()->getHeapRegion(); - - while (SR) { - const MemRegion *R = SR->getSuperRegion(); - if (R == H) - return true; - - SR = dyn_cast(R); - } + if (const MemSpaceRegion *MS = getMemorySpace()) + return MS == getMemRegionManager()->getHeapRegion(); return false; } +bool MemRegion::hasHeapOrStackStorage() const { + if (const MemSpaceRegion *MS = getMemorySpace()) { + MemRegionManager *Mgr = getMemRegionManager(); + return MS == Mgr->getHeapRegion() || MS == Mgr->getStackRegion(); + } + return false; +} + //===----------------------------------------------------------------------===// // View handling. //===----------------------------------------------------------------------===// diff --git a/clang/lib/Analysis/RegionStore.cpp b/clang/lib/Analysis/RegionStore.cpp index 17e332387f7c..77f5b7cb39bb 100644 --- a/clang/lib/Analysis/RegionStore.cpp +++ b/clang/lib/Analysis/RegionStore.cpp @@ -939,7 +939,7 @@ SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) { } } - if (R->hasStackStorage() || R->hasHeapStorage()) { + if (R->hasHeapOrStackStorage()) { // All stack variables are considered to have undefined values // upon creation. All heap allocated blocks are considered to // have undefined values as well unless they are explicitly bound