diff options
Diffstat (limited to 'lib/Analysis/MemRegion.cpp')
-rw-r--r-- | lib/Analysis/MemRegion.cpp | 254 |
1 files changed, 63 insertions, 191 deletions
diff --git a/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp index 9e11a26..9bd93cd 100644 --- a/lib/Analysis/MemRegion.cpp +++ b/lib/Analysis/MemRegion.cpp @@ -18,6 +18,9 @@ using namespace clang; +//===----------------------------------------------------------------------===// +// Basic methods. +//===----------------------------------------------------------------------===// MemRegion::~MemRegion() {} @@ -34,6 +37,19 @@ bool SubRegion::isSubRegionOf(const MemRegion* R) const { return false; } + +MemRegionManager* SubRegion::getMemRegionManager() const { + const SubRegion* r = this; + do { + const MemRegion *superRegion = r->getSuperRegion(); + if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) { + r = sr; + continue; + } + return superRegion->getMemRegionManager(); + } while (1); +} + void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger((unsigned)getKind()); } @@ -47,14 +63,15 @@ void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, } void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, - const Expr* Ex, unsigned cnt) { + const Expr* Ex, unsigned cnt, + const MemRegion *) { ID.AddInteger((unsigned) AllocaRegionKind); ID.AddPointer(Ex); ID.AddInteger(cnt); } void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { - ProfileRegion(ID, Ex, Cnt); + ProfileRegion(ID, Ex, Cnt, superRegion); } void TypedViewRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T, @@ -87,13 +104,15 @@ void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); } -void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) { +void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym, + const MemRegion *sreg) { ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); ID.Add(sym); + ID.AddPointer(sreg); } void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { - SymbolicRegion::ProfileRegion(ID, sym); + SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion()); } void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, @@ -110,14 +129,14 @@ void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { } void CodeTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const void* data, - QualType t) { + QualType t, const MemRegion*) { ID.AddInteger(MemRegion::CodeTextRegionKind); ID.AddPointer(data); ID.Add(t); } void CodeTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { - CodeTextRegion::ProfileRegion(ID, Data, LocationType); + CodeTextRegion::ProfileRegion(ID, Data, LocationType, superRegion); } //===----------------------------------------------------------------------===// @@ -186,13 +205,12 @@ void VarRegion::print(llvm::raw_ostream& os) const { // MemRegionManager methods. //===----------------------------------------------------------------------===// -MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { - +MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { if (!region) { region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); - new (region) MemSpaceRegion(); + new (region) MemSpaceRegion(this); } - + return region; } @@ -216,82 +234,21 @@ MemSpaceRegion* MemRegionManager::getCodeRegion() { return LazyAllocate(code); } -bool MemRegionManager::onStack(const MemRegion* R) { - while (const SubRegion* SR = dyn_cast<SubRegion>(R)) - R = SR->getSuperRegion(); - - return (R != 0) && (R == stack); -} - -bool MemRegionManager::onHeap(const MemRegion* R) { - while (const SubRegion* SR = dyn_cast<SubRegion>(R)) - R = SR->getSuperRegion(); - - return (R != 0) && (R == heap); -} +//===----------------------------------------------------------------------===// +// Constructing regions. +//===----------------------------------------------------------------------===// StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) { - llvm::FoldingSetNodeID ID; - MemSpaceRegion* GlobalsR = getGlobalsRegion(); - - StringRegion::ProfileRegion(ID, Str, GlobalsR); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - StringRegion* R = cast_or_null<StringRegion>(data); - - if (!R) { - R = (StringRegion*) A.Allocate<StringRegion>(); - new (R) StringRegion(Str, GlobalsR); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<StringRegion>(Str); } VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) { - - const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion() - : getGlobalsRegion(); - - llvm::FoldingSetNodeID ID; - DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - VarRegion* R = cast_or_null<VarRegion>(data); - - if (!R) { - R = (VarRegion*) A.Allocate<VarRegion>(); - new (R) VarRegion(d, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<VarRegion>(d); } CompoundLiteralRegion* MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) { - // Is this compound literal allocated on the stack or is part of the - // global constant pool? - const MemRegion* superRegion = CL->isFileScope() ? - getGlobalsRegion() : getStackRegion(); - - // Profile the compound literal. - llvm::FoldingSetNodeID ID; - CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data); - - if (!R) { - R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>(); - new (R) CompoundLiteralRegion(CL, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<CompoundLiteralRegion>(CL); } ElementRegion* @@ -318,149 +275,45 @@ MemRegionManager::getElementRegion(QualType elementType, SVal Idx, CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd, QualType t) { - llvm::FoldingSetNodeID ID; - CodeTextRegion::ProfileRegion(ID, fd, t); - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - CodeTextRegion* R = cast_or_null<CodeTextRegion>(data); - - if (!R) { - R = (CodeTextRegion*) A.Allocate<CodeTextRegion>(); - new (R) CodeTextRegion(fd, t, getCodeRegion()); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<CodeTextRegion>(fd, t); } CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) { - llvm::FoldingSetNodeID ID; - CodeTextRegion::ProfileRegion(ID, sym, t); - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - CodeTextRegion* R = cast_or_null<CodeTextRegion>(data); - - if (!R) { - R = (CodeTextRegion*) A.Allocate<CodeTextRegion>(); - new (R) CodeTextRegion(sym, t, getCodeRegion()); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<CodeTextRegion>(sym, t); } /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) { - llvm::FoldingSetNodeID ID; - SymbolicRegion::ProfileRegion(ID, sym); - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); - - if (!R) { - R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); - // SymbolicRegion's storage class is usually unknown. - new (R) SymbolicRegion(sym, getUnknownRegion()); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<SymbolicRegion>(sym); } FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, const MemRegion* superRegion) { - llvm::FoldingSetNodeID ID; - DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - FieldRegion* R = cast_or_null<FieldRegion>(data); - - if (!R) { - R = (FieldRegion*) A.Allocate<FieldRegion>(); - new (R) FieldRegion(d, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<FieldRegion>(d, superRegion); } ObjCIvarRegion* MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, const MemRegion* superRegion) { - llvm::FoldingSetNodeID ID; - DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); - - if (!R) { - R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); - new (R) ObjCIvarRegion(d, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<ObjCIvarRegion>(d, superRegion); } ObjCObjectRegion* MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d, - const MemRegion* superRegion) { - llvm::FoldingSetNodeID ID; - DeclRegion::ProfileRegion(ID, d, superRegion, - MemRegion::ObjCObjectRegionKind); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data); - - if (!R) { - R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>(); - new (R) ObjCObjectRegion(d, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + const MemRegion* superRegion) { + return getRegion<ObjCObjectRegion>(d, superRegion); } TypedViewRegion* MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) { - llvm::FoldingSetNodeID ID; - TypedViewRegion::ProfileRegion(ID, t, superRegion); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - TypedViewRegion* R = cast_or_null<TypedViewRegion>(data); - - if (!R) { - R = (TypedViewRegion*) A.Allocate<TypedViewRegion>(); - new (R) TypedViewRegion(t, superRegion); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<TypedViewRegion>(t, superRegion); } AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { - llvm::FoldingSetNodeID ID; - AllocaRegion::ProfileRegion(ID, E, cnt); - - void* InsertPos; - MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - AllocaRegion* R = cast_or_null<AllocaRegion>(data); - - if (!R) { - R = (AllocaRegion*) A.Allocate<AllocaRegion>(); - new (R) AllocaRegion(E, cnt, getStackRegion()); - Regions.InsertNode(R, InsertPos); - } - - return R; + return getRegion<AllocaRegion>(E, cnt); } bool MemRegionManager::hasStackStorage(const MemRegion* R) { - // Only subregions can have stack storage. const SubRegion* SR = dyn_cast<SubRegion>(R); @@ -476,10 +329,29 @@ bool MemRegionManager::hasStackStorage(const MemRegion* R) { SR = dyn_cast<SubRegion>(R); } - + return false; } +bool MemRegionManager::hasHeapStorage(const MemRegion* R) { + // Only subregions can have stack storage. + const SubRegion* SR = dyn_cast<SubRegion>(R); + + if (!SR) + return false; + + MemSpaceRegion* H = getHeapRegion(); + + while (SR) { + R = SR->getSuperRegion(); + if (R == H) + return true; + + SR = dyn_cast<SubRegion>(R); + } + + return false; +} //===----------------------------------------------------------------------===// // View handling. |