[cfe-commits] r95529 - in /cfe/trunk: lib/Checker/FlatStore.cpp test/Analysis/misc-ps-flat-store.c

Zhongxing Xu xuzhongxing at gmail.com
Sun Feb 7 21:40:08 PST 2010


Author: zhongxingxu
Date: Sun Feb  7 23:40:07 2010
New Revision: 95529

URL: http://llvm.org/viewvc/llvm-project?rev=95529&view=rev
Log:
Add support for binding and retrieving VarRegions in flat store.

Added:
    cfe/trunk/test/Analysis/misc-ps-flat-store.c
Modified:
    cfe/trunk/lib/Checker/FlatStore.cpp

Modified: cfe/trunk/lib/Checker/FlatStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/FlatStore.cpp?rev=95529&r1=95528&r2=95529&view=diff

==============================================================================
--- cfe/trunk/lib/Checker/FlatStore.cpp (original)
+++ cfe/trunk/lib/Checker/FlatStore.cpp Sun Feb  7 23:40:07 2010
@@ -11,6 +11,7 @@
 #include "llvm/ADT/ImmutableIntervalMap.h"
 
 using namespace clang;
+using llvm::Interval;
 
 // The actual store type.
 typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
@@ -27,8 +28,8 @@
       RBFactory(mgr.getAllocator()), 
       BVFactory(mgr.getAllocator()) {}
 
-  SVal Retrieve(Store store, Loc loc, QualType T);
-  Store Bind(Store store, Loc loc, SVal val);
+  SVal Retrieve(Store store, Loc L, QualType T);
+  Store Bind(Store store, Loc L, SVal val);
   Store Remove(Store St, Loc L);
   Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
                             const LocationContext *LC, SVal v);
@@ -41,7 +42,9 @@
     return 0;
   }
 
-  SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
+  SVal getLValueVar(const VarDecl *VD, const LocationContext *LC) {
+    return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
+  }
 
   SVal getLValueString(const StringLiteral* sl);
   SVal getLValueIvar(const ObjCIvarDecl* decl, SVal base);
@@ -65,6 +68,15 @@
   void print(Store store, llvm::raw_ostream& Out, const char* nl, 
              const char *sep);
   void iterBindings(Store store, BindingsHandler& f);
+
+private:
+  static RegionBindings getRegionBindings(Store store) {
+    return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
+  }
+
+  Interval RegionToInterval(const MemRegion *R);
+
+  SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
 };
 } // end anonymous namespace
 
@@ -72,12 +84,42 @@
   return new FlatStoreManager(StMgr);
 }
 
-SVal FlatStoreManager::Retrieve(Store store, Loc loc, QualType T) {
-  return UnknownVal();
+SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
+  const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
+  Interval I = RegionToInterval(R);
+  RegionBindings B = getRegionBindings(store);
+  const BindingVal *BV = B.lookup(R);
+  if (BV) {
+    const SVal *V = BVFactory.Lookup(*BV, I);
+    if (V)
+      return *V;
+    else
+      return RetrieveRegionWithNoBinding(R, T);
+  }
+  return RetrieveRegionWithNoBinding(R, T);
 }
 
-Store FlatStoreManager::Bind(Store store, Loc loc, SVal val) {
-  return store;
+SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
+                                                   QualType T) {
+  if (R->hasStackNonParametersStorage())
+    return UndefinedVal();
+  else
+    return ValMgr.getRegionValueSymbolVal(R, T);
+}
+
+Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
+  const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
+  RegionBindings B = getRegionBindings(store);
+  const BindingVal *V = B.lookup(R);
+
+  BindingVal BV = BVFactory.GetEmptyMap();
+  if (V)
+    BV = *V;
+
+  Interval I = RegionToInterval(R);
+  BV = BVFactory.Add(BV, I, val);
+  B = RBFactory.Add(B, R, BV);
+  return B.getRoot();
 }
 
 Store FlatStoreManager::Remove(Store store, Loc L) {
@@ -91,11 +133,6 @@
   return store;
 }
 
-SVal FlatStoreManager::getLValueVar(const VarDecl *VD, 
-                                    const LocationContext *LC) {
-  return UnknownVal();
-}
-
 SVal FlatStoreManager::getLValueString(const StringLiteral* sl) {
   return UnknownVal();
 }
@@ -138,3 +175,15 @@
 
 void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
 }
+
+Interval FlatStoreManager::RegionToInterval(const MemRegion *R) { 
+  switch (R->getKind()) {
+  case MemRegion::VarRegionKind: {
+    QualType T = cast<VarRegion>(R)->getValueType(StateMgr.getContext());
+    uint64_t Size = StateMgr.getContext().getTypeSize(T);
+    return Interval(0, Size-1);
+  }
+  default:
+    assert(0 && "Region kind unhandled.");
+  }
+}

Added: cfe/trunk/test/Analysis/misc-ps-flat-store.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-flat-store.c?rev=95529&view=auto

==============================================================================
--- cfe/trunk/test/Analysis/misc-ps-flat-store.c (added)
+++ cfe/trunk/test/Analysis/misc-ps-flat-store.c Sun Feb  7 23:40:07 2010
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=flat -verify %s
+
+void f1() {
+  int x;
+  int *p;
+  x = 1;
+  p = 0;
+  if (x != 1)
+    *p = 1; // no-warning
+}





More information about the cfe-commits mailing list