aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2008-10-27 20:57:58 +0000
committerTed Kremenek <kremenek@apple.com>2008-10-27 20:57:58 +0000
commit329d6fde79254503b14724e1231a9d70fa6b387f (patch)
tree3a79c6cf173dea3eb5d3457cf01b2c6d29ff5588
parent80c28553e08d5f294801bc51f0e91df43f64a940 (diff)
Added CompoundLiteralRegion to represent the (temporary) memory allocated for a compound literal.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58270 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Analysis/PathSensitive/MemRegion.h46
-rw-r--r--lib/Analysis/MemRegion.cpp41
2 files changed, 80 insertions, 7 deletions
diff --git a/include/clang/Analysis/PathSensitive/MemRegion.h b/include/clang/Analysis/PathSensitive/MemRegion.h
index 4465796f9c..ffbef1b122 100644
--- a/include/clang/Analysis/PathSensitive/MemRegion.h
+++ b/include/clang/Analysis/PathSensitive/MemRegion.h
@@ -36,14 +36,17 @@ class MemRegionManager;
class MemRegion : public llvm::FoldingSetNode {
public:
enum Kind { MemSpaceRegionKind, SymbolicRegionKind,
+ // Subregions.
+ CompoundLiteralRegionKind,
// Typed regions.
BEG_TYPED_REGIONS,
- StringRegionKind, ElementRegionKind,
- BEG_DECL_REGIONS,
- VarRegionKind, FieldRegionKind,
- ObjCIvarRegionKind, ObjCObjectRegionKind,
- END_DECL_REGIONS,
- AnonTypedRegionKind, AnonPointeeRegionKind,
+ StringRegionKind, ElementRegionKind,
+ // Decl Regions.
+ BEG_DECL_REGIONS,
+ VarRegionKind, FieldRegionKind,
+ ObjCIvarRegionKind, ObjCObjectRegionKind,
+ END_DECL_REGIONS,
+ AnonTypedRegionKind, AnonPointeeRegionKind,
END_TYPED_REGIONS };
private:
const Kind kind;
@@ -213,6 +216,30 @@ public:
/// AnonHeapRegion - anonymous region created by malloc().
class AnonHeapRegion : public AnonTypedRegion {
};
+
+/// CompoundLiteralRegion - A memory region representing a compound literal.
+/// Compound literals are essentially temporaries that are stack allocated
+/// or in the global constant pool.
+class CompoundLiteralRegion : public SubRegion {
+private:
+ friend class MemRegionManager;
+ const CompoundLiteralExpr* CL;
+
+ CompoundLiteralRegion(const CompoundLiteralExpr* cl, const MemRegion* sReg)
+ : SubRegion(sReg,CompoundLiteralRegionKind), CL(cl) {}
+
+ static void ProfileRegion(llvm::FoldingSetNodeID& ID,
+ const CompoundLiteralExpr* CL,
+ const MemRegion* superRegion);
+public:
+ void Profile(llvm::FoldingSetNodeID& ID) const;
+
+ void print(llvm::raw_ostream& os) const;
+
+ static bool classof(const MemRegion* R) {
+ return R->getKind() == CompoundLiteralRegionKind;
+ }
+};
class DeclRegion : public TypedRegion {
protected:
@@ -392,6 +419,11 @@ public:
/// memory space.
MemSpaceRegion* getUnknownRegion();
+ /// getCompoundLiteralRegion - Retrieve the region associated with a
+ /// given CompoundLiteral.
+ CompoundLiteralRegion*
+ getCompoundLiteralRegion(const CompoundLiteralExpr* CL);
+
/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
SymbolicRegion* getSymbolicRegion(const SymbolID sym);
@@ -406,7 +438,7 @@ public:
return getVarRegion(vd, vd->hasLocalStorage() ? getStackRegion()
: getGlobalsRegion());
}
-
+
ElementRegion* getElementRegion(SVal Idx, const MemRegion* superRegion);
/// getFieldRegion - Retrieve or create the memory region associated with
diff --git a/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp
index 1db330cf09..8a7be0d564 100644
--- a/lib/Analysis/MemRegion.cpp
+++ b/lib/Analysis/MemRegion.cpp
@@ -53,6 +53,18 @@ void AnonTypedRegion::Profile(llvm::FoldingSetNodeID& ID) const {
AnonTypedRegion::ProfileRegion(ID, T, superRegion);
}
+void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
+ CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
+}
+
+void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
+ const CompoundLiteralExpr* CL,
+ const MemRegion* superRegion) {
+ ID.AddInteger((unsigned) CompoundLiteralRegionKind);
+ ID.AddPointer(CL);
+ ID.AddPointer(superRegion);
+}
+
void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
const MemRegion* superRegion, Kind k) {
ID.AddInteger((unsigned) k);
@@ -123,6 +135,11 @@ void ElementRegion::print(llvm::raw_ostream& os) const {
os << '['; Index.print(os); os << ']';
}
+void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
+ // FIXME: More elaborate pretty-printing.
+ os << "{ " << (void*) CL << " }";
+}
+
//===----------------------------------------------------------------------===//
// MemRegionManager methods.
//===----------------------------------------------------------------------===//
@@ -190,6 +207,30 @@ VarRegion* MemRegionManager::getVarRegion(const VarDecl* d,
return R;
}
+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;
+}
+
ElementRegion* MemRegionManager::getElementRegion(SVal Idx,
const MemRegion* superRegion){
llvm::FoldingSetNodeID ID;