]> granicus.if.org Git - clang/commitdiff
Add ElementRegion to represent memory chunks for array elements.
authorZhongxing Xu <xuzhongxing@gmail.com>
Tue, 21 Oct 2008 05:27:10 +0000 (05:27 +0000)
committerZhongxing Xu <xuzhongxing@gmail.com>
Tue, 21 Oct 2008 05:27:10 +0000 (05:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57891 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Analysis/PathSensitive/MemRegion.h
lib/Analysis/MemRegion.cpp

index 69e4abb7bd545ec4fd8a3de6068a3b224e1a12a0..ed9c6e2bd47382457dad463a5072b83ce62ab944 100644 (file)
@@ -19,6 +19,7 @@
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/Analysis/PathSensitive/SymbolManager.h"
+#include "clang/Analysis/PathSensitive/RValues.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/Support/Allocator.h"
@@ -37,7 +38,8 @@ public:
   enum Kind { MemSpaceRegionKind, SymbolicRegionKind,
               // Typed regions.
               BEG_TYPED_REGIONS,
-              VarRegionKind, FieldRegionKind, ObjCIvarRegionKind,
+              VarRegionKind, FieldRegionKind, ElementRegionKind,
+              ObjCIvarRegionKind,
               AnonTypedRegionKind, AnonPointeeRegionKind,
               END_TYPED_REGIONS };  
 private:
@@ -257,7 +259,27 @@ public:
     return R->getKind() == ObjCIvarRegionKind;
   }
 };
-  
+
+class ElementRegion : public SubRegion {
+  friend class MemRegionManager;
+
+  SVal Index;
+
+  ElementRegion(SVal Idx, const MemRegion* sReg)
+    : SubRegion(sReg, ElementRegionKind), Index(Idx) {}
+
+  static void ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx, 
+                            const MemRegion* superRegion);
+
+public:
+
+  void Profile(llvm::FoldingSetNodeID& ID) const;
+
+  static bool classof(const MemRegion* R) {
+    return R->getKind() == ElementRegionKind;
+  }
+};
+
 //===----------------------------------------------------------------------===//
 // MemRegionManager - Factory object for creating regions.
 //===----------------------------------------------------------------------===//
@@ -305,7 +327,9 @@ public:
     return getVarRegion(vd, vd->hasLocalStorage() ? getStackRegion() 
                         : getGlobalsRegion());
   }
-  
+
+  ElementRegion* getElementRegion(SVal Idx, const MemRegion* superRegion);
+
   /// getFieldRegion - Retrieve or create the memory region associated with
   ///  a specified FieldDecl.  'superRegion' corresponds to the containing
   ///  memory region (which typically represents the memory representing
index d8efef2e675d934e0d9a35a16568860e50019c05..cae053f595829caf3cf446a346396ca1a5871c4a 100644 (file)
@@ -65,6 +65,16 @@ void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
   SymbolicRegion::ProfileRegion(ID, sym);
 }
 
+void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx, 
+                                  const MemRegion* superRegion) {
+  ID.AddInteger(MemRegion::ElementRegionKind);
+  ID.AddPointer(superRegion);
+  Idx.Profile(ID);
+}
+
+void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
+  ElementRegion::ProfileRegion(ID, Index, superRegion);
+}
 //===----------------------------------------------------------------------===//
 // Region pretty-printing.
 //===----------------------------------------------------------------------===//
@@ -141,6 +151,24 @@ VarRegion* MemRegionManager::getVarRegion(const VarDecl* d,
   return R;
 }
 
+ElementRegion* MemRegionManager::getElementRegion(SVal Idx,
+                                                  const MemRegion* superRegion){
+  llvm::FoldingSetNodeID ID;
+  ElementRegion::ProfileRegion(ID, Idx, superRegion);
+
+  void* InsertPos;
+  MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
+  ElementRegion* R = cast_or_null<ElementRegion>(data);
+
+  if (!R) {
+    R = (ElementRegion*) A.Allocate<ElementRegion>();
+    new (R) ElementRegion(Idx, superRegion);
+    Regions.InsertNode(R, InsertPos);
+  }
+
+  return R;
+}
+
 /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
 SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) {