From e9f4e5420895a75dd788e9891921e7781c1823b9 Mon Sep 17 00:00:00 2001 From: Zhongxing Xu Date: Sat, 25 Oct 2008 14:13:41 +0000 Subject: [PATCH] Add StringRegion to MemRegions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58137 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/Analysis/PathSensitive/MemRegion.h | 43 ++++++++++++++++++- lib/Analysis/MemRegion.cpp | 27 ++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/include/clang/Analysis/PathSensitive/MemRegion.h b/include/clang/Analysis/PathSensitive/MemRegion.h index 514760a4ac..2a20b6320e 100644 --- a/include/clang/Analysis/PathSensitive/MemRegion.h +++ b/include/clang/Analysis/PathSensitive/MemRegion.h @@ -38,8 +38,11 @@ public: enum Kind { MemSpaceRegionKind, SymbolicRegionKind, // Typed regions. BEG_TYPED_REGIONS, - VarRegionKind, FieldRegionKind, ElementRegionKind, + StringRegionKind, ElementRegionKind, + BEG_DECL_REGIONS, + VarRegionKind, FieldRegionKind, ObjCIvarRegionKind, ObjCObjectRegionKind, + END_DECL_REGIONS, AnonTypedRegionKind, AnonPointeeRegionKind, END_TYPED_REGIONS }; private: @@ -135,6 +138,35 @@ public: } }; +/// StringRegion - Region associated with a StringLiteral. +class StringRegion : public TypedRegion { + friend class MemRegionManager; + + const StringLiteral* Str; + +protected: + + StringRegion(const StringLiteral* str, MemRegion* sreg) + : TypedRegion(sreg, StringRegionKind), Str(str) {} + + static void ProfileRegion(llvm::FoldingSetNodeID& ID, + const StringLiteral* Str, + const MemRegion* superRegion); + +public: + QualType getType(ASTContext&) const { + return Str->getType(); + } + + void Profile(llvm::FoldingSetNodeID& ID) const { + ProfileRegion(ID, Str, superRegion); + } + + static bool classof(const MemRegion* R) { + return R->getKind() == StringRegionKind; + } +}; + /// AnonTypedRegion - An "anonymous" region that simply types a chunk /// of memory. class AnonTypedRegion : public TypedRegion { @@ -195,6 +227,11 @@ protected: public: const Decl* getDecl() const { return D; } void Profile(llvm::FoldingSetNodeID& ID) const; + + static bool classof(const MemRegion* R) { + unsigned k = R->getKind(); + return k > BEG_DECL_REGIONS && k < END_DECL_REGIONS; + } }; class VarRegion : public DeclRegion { @@ -351,7 +388,9 @@ public: /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. SymbolicRegion* getSymbolicRegion(const SymbolID sym); - + + StringRegion* getStringRegion(const StringLiteral* Str); + /// getVarRegion - Retrieve or create the memory region associated with /// a specified VarDecl. 'superRegion' corresponds to the containing /// memory region, and 'off' is the offset within the containing region. diff --git a/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp index 5dad3560f0..f14e0cba44 100644 --- a/lib/Analysis/MemRegion.cpp +++ b/lib/Analysis/MemRegion.cpp @@ -25,6 +25,14 @@ void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger((unsigned)getKind()); } +void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, + const StringLiteral* Str, + const MemRegion* superRegion) { + ID.AddInteger((unsigned) StringRegionKind); + ID.AddPointer(Str); + ID.AddPointer(superRegion); +} + void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T, const MemRegion* superRegion) { ID.AddInteger((unsigned) AnonTypedRegionKind); @@ -138,6 +146,25 @@ MemSpaceRegion* MemRegionManager::getUnknownRegion() { return LazyAllocate(unknown); } +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(data); + + if (!R) { + R = (StringRegion*) A.Allocate(); + new (R) StringRegion(Str, GlobalsR); + Regions.InsertNode(R, InsertPos); + } + + return R; +} + VarRegion* MemRegionManager::getVarRegion(const VarDecl* d, const MemRegion* superRegion) { llvm::FoldingSetNodeID ID; -- 2.40.0