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

Ted Kremenek kremenek at apple.com
Sat Nov 1 17:34:33 PDT 2008


Author: kremenek
Date: Sat Nov  1 19:34:33 2008
New Revision: 58551

URL: http://llvm.org/viewvc/llvm-project?rev=58551&view=rev
Log:
Added AllocaRegion, which represents regions created by calls to alloca().

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=58551&r1=58550&r2=58551&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/MemRegion.h Sat Nov  1 19:34:33 2008
@@ -37,6 +37,7 @@
 class MemRegion : public llvm::FoldingSetNode {
 public:
   enum Kind { MemSpaceRegionKind, SymbolicRegionKind,
+              AllocaRegionKind,
               // Typed regions.
               BEG_TYPED_REGIONS,
                CompoundLiteralRegionKind,
@@ -101,6 +102,34 @@
   }
 };
   
+/// AllocaRegion - A region that represents an untyped blob of bytes created
+///  by a call to 'alloca'.
+class AllocaRegion : public SubRegion {
+  friend class MemRegionManager;
+protected:
+  unsigned Cnt; // Block counter.  Used to distinguish different pieces of
+                // memory allocated by alloca at the same call site.
+  const Expr* Ex;
+
+  AllocaRegion(const Expr* ex, unsigned cnt, const MemRegion* superRegion)
+    : SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {}
+  
+public:
+  
+  const Expr* getExpr() const { return Ex; }
+  
+  void Profile(llvm::FoldingSetNodeID& ID) const;
+
+  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Expr* Ex,
+                            unsigned Cnt);
+  
+  void print(llvm::raw_ostream& os) const;
+  
+  static bool classof(const MemRegion* R) {
+    return R->getKind() == AllocaRegionKind;
+  }
+};    
+  
 /// SymbolicRegion - A special, "non-concrete" region. Unlike other region
 ///  clases, SymbolicRegion represents a region that serves as an alias for
 ///  either a real region, a NULL pointer, etc.  It essentially is used to
@@ -430,6 +459,9 @@
     return R == globals; 
   }
   
+  /// getAllocaRegion - Retrieve a region associated with a call to alloca().
+  AllocaRegion* getAllocaRegion(const Expr* Ex, unsigned Cnt);
+  
   /// getCompoundLiteralRegion - Retrieve the region associated with a
   ///  given CompoundLiteral.
   CompoundLiteralRegion*

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

==============================================================================
--- cfe/trunk/lib/Analysis/MemRegion.cpp (original)
+++ cfe/trunk/lib/Analysis/MemRegion.cpp Sat Nov  1 19:34:33 2008
@@ -33,6 +33,17 @@
   ID.AddPointer(superRegion);
 }
 
+void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
+                                 const Expr* Ex, unsigned cnt) {
+  ID.AddInteger((unsigned) AllocaRegionKind);
+  ID.AddPointer(Ex);
+  ID.AddInteger(cnt);
+}
+
+void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
+  ProfileRegion(ID, Ex, Cnt);
+}
+
 void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
                                     const MemRegion* superRegion) {
   ID.AddInteger((unsigned) AnonTypedRegionKind);
@@ -117,6 +128,10 @@
   os << "<Unknown Region>";
 }
 
+void AllocaRegion::print(llvm::raw_ostream& os) const {
+  os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
+}
+
 void VarRegion::print(llvm::raw_ostream& os) const {
   os << cast<VarDecl>(D)->getName();
 }
@@ -350,13 +365,31 @@
   return R;
 }
 
+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;
+}
+
 bool MemRegionManager::hasStackStorage(const MemRegion* R) {
-  const SubRegion* SR = dyn_cast<SubRegion>(R);
 
   // Only subregions can have stack storage.
+  const SubRegion* SR = dyn_cast<SubRegion>(R);
+
   if (!SR)
     return false;
-  
+
   MemSpaceRegion* S = getStackRegion();
   
   while (SR) {





More information about the cfe-commits mailing list