[cfe-commits] r58270 - in /cfe/trunk: include/clang/Analysis/PathSensitive/MemRegion.h lib/Analysis/MemRegion.cpp

Ted Kremenek kremenek at apple.com
Mon Oct 27 13:57:58 PDT 2008


Author: kremenek
Date: Mon Oct 27 15:57:58 2008
New Revision: 58270

URL: http://llvm.org/viewvc/llvm-project?rev=58270&view=rev
Log:
Added CompoundLiteralRegion to represent the (temporary) memory allocated for a compound literal.

Modified:
    cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h
    cfe/trunk/lib/Analysis/MemRegion.cpp

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h?rev=58270&r1=58269&r2=58270&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h Mon Oct 27 15:57:58 2008
@@ -36,14 +36,17 @@
 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 @@
 /// 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 @@
   /// 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 @@
     return getVarRegion(vd, vd->hasLocalStorage() ? getStackRegion() 
                         : getGlobalsRegion());
   }
-
+  
   ElementRegion* getElementRegion(SVal Idx, const MemRegion* superRegion);
 
   /// getFieldRegion - Retrieve or create the memory region associated with

Modified: cfe/trunk/lib/Analysis/MemRegion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/MemRegion.cpp?rev=58270&r1=58269&r2=58270&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/MemRegion.cpp (original)
+++ cfe/trunk/lib/Analysis/MemRegion.cpp Mon Oct 27 15:57:58 2008
@@ -53,6 +53,18 @@
   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 @@
   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 @@
   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;





More information about the cfe-commits mailing list