From bb7c96f290453104ec35ca17111a5165f68a4697 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 23 Jun 2009 18:17:08 +0000 Subject: [PATCH] - Add MemRegion::getMemorySpace() - Change implementation of MemRegion::hasStackStorage()/hasHeapStorage() to use 'getMemorySpace()'. This avoids a double traversal up the region hierarchy and is simpler. - Add MemRegion::hasHeapOrStackStorage() as a slightly more efficient alternative to 'hasStackStorage() || hasHeapStorage()'. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73977 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/Analysis/PathSensitive/MemRegion.h | 6 ++- lib/Analysis/MemRegion.cpp | 49 +++++++++---------- lib/Analysis/RegionStore.cpp | 2 +- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/clang/Analysis/PathSensitive/MemRegion.h b/include/clang/Analysis/PathSensitive/MemRegion.h index b531375be2..65ac0b5302 100644 --- a/include/clang/Analysis/PathSensitive/MemRegion.h +++ b/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/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp index f018c83b91..c8e027579a 100644 --- a/lib/Analysis/MemRegion.cpp +++ b/lib/Analysis/MemRegion.cpp @@ -313,45 +313,40 @@ AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { return getRegion(E, cnt); } -bool MemRegion::hasStackStorage() const { - // Only subregions can have stack storage. - const SubRegion* SR = dyn_cast(this); - - if (!SR) - return false; - MemSpaceRegion* S = getMemRegionManager()->getStackRegion(); +const MemSpaceRegion *MemRegion::getMemorySpace() const { + const MemRegion *R = this; + const SubRegion* SR = dyn_cast(this); while (SR) { - const MemRegion *R = SR->getSuperRegion(); - if (R == S) - return true; - - SR = dyn_cast(R); + R = SR->getSuperRegion(); + SR = dyn_cast(R); } - - return false; + + return dyn_cast(R); } -bool MemRegion::hasHeapStorage() const { - // Only subregions can have stack storage. - const SubRegion* SR = dyn_cast(this); +bool MemRegion::hasStackStorage() const { + if (const MemSpaceRegion *MS = getMemorySpace()) + return MS == getMemRegionManager()->getStackRegion(); - if (!SR) - return false; + return false; +} - MemSpaceRegion* H = getMemRegionManager()->getHeapRegion(); +bool MemRegion::hasHeapStorage() const { + if (const MemSpaceRegion *MS = getMemorySpace()) + return MS == getMemRegionManager()->getHeapRegion(); - while (SR) { - const MemRegion *R = SR->getSuperRegion(); - if (R == H) - return true; + return false; +} - SR = dyn_cast(R); +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/lib/Analysis/RegionStore.cpp b/lib/Analysis/RegionStore.cpp index 17e332387f..77f5b7cb39 100644 --- a/lib/Analysis/RegionStore.cpp +++ b/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 -- 2.40.0