[cfe-commits] r53252 - in /cfe/trunk: include/clang/Analysis/PathSensitive/Environment.h include/clang/Analysis/PathSensitive/ValueState.h lib/Analysis/Environment.cpp lib/Analysis/ValueState.cpp

Ted Kremenek kremenek at apple.com
Tue Jul 8 14:46:56 PDT 2008


Author: kremenek
Date: Tue Jul  8 16:46:56 2008
New Revision: 53252

URL: http://llvm.org/viewvc/llvm-project?rev=53252&view=rev
Log:
Initial work on splitting the ValueState into an Environment, Store, and
Constraints. These concepts are already present in the current ValueState, but
the implementation is monolothic. Making ValueState more modular opens up new
design choices for customizing the analysis engine.

In the context of the analysis engine, the "Environment" is the binding between
Expr* (expressions) and intermediate symbolic values (RValues).

Added:
    cfe/trunk/include/clang/Analysis/PathSensitive/Environment.h
    cfe/trunk/lib/Analysis/Environment.cpp
Modified:
    cfe/trunk/include/clang/Analysis/PathSensitive/ValueState.h
    cfe/trunk/lib/Analysis/ValueState.cpp

Added: cfe/trunk/include/clang/Analysis/PathSensitive/Environment.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/Environment.h?rev=53252&view=auto

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/Environment.h (added)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/Environment.h Tue Jul  8 16:46:56 2008
@@ -0,0 +1,127 @@
+//== Environment.h - Map from Expr* to Locations/Values ---------*- C++ -*--==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defined the Environment and EnvironmentManager classes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
+#define LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
+
+#include "llvm/ADT/ImmutableMap.h"
+#include "clang/Analysis/PathSensitive/RValues.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/ADT/FoldingSet.h"
+
+namespace clang {
+
+class EnvironmentManager;
+  
+class Environment : public llvm::FoldingSetNode {
+private:
+    
+  friend class EnvironmentManager;
+  
+  // Type definitions.
+  typedef llvm::ImmutableMap<Expr*,RVal> BindingsTy;
+
+  // Data.
+  BindingsTy SubExprBindings;
+  BindingsTy BlkExprBindings;
+  
+  Environment(BindingsTy seb, BindingsTy beb)
+    : SubExprBindings(seb), BlkExprBindings(beb) {}
+  
+public:
+    
+  typedef BindingsTy::iterator seb_iterator;
+  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
+  seb_iterator seb_end() const { return SubExprBindings.end(); }
+  
+  typedef BindingsTy::iterator beb_iterator;
+  beb_iterator beb_begin() const { return BlkExprBindings.begin(); }
+  beb_iterator beb_end() const { return BlkExprBindings.end(); }      
+  
+  RVal LookupSubExpr(Expr* E) const {
+    const RVal* X = SubExprBindings.lookup(E);
+    return X ? *X : UnknownVal();
+  }
+  
+  RVal LookupBlkExpr(Expr* E) const {
+    const RVal* X = BlkExprBindings.lookup(E);
+    return X ? *X : UnknownVal();
+  }
+  
+  RVal LookupExpr(Expr* E) const {
+    const RVal* X = SubExprBindings.lookup(E);
+    if (X) return *X;
+    X = BlkExprBindings.lookup(E);
+    return X ? *X : UnknownVal();
+  }
+  
+  /// Profile - Profile the contents of an Environment object for use
+  ///  in a FoldingSet.
+  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
+    E->SubExprBindings.Profile(ID);
+    E->BlkExprBindings.Profile(ID);
+  }
+  
+  /// Profile - Used to profile the contents of this object for inclusion
+  ///  in a FoldingSet.
+  void Profile(llvm::FoldingSetNodeID& ID) const {
+    Profile(ID, this);
+  }  
+};
+  
+class EnvironmentManager {
+private:
+  typedef Environment::BindingsTy::Factory FactoryTy;
+  FactoryTy F;
+  
+public:
+  
+  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
+  ~EnvironmentManager() {}
+
+  /// RemoveBlkExpr - Return a new environment object with the same bindings as
+  ///  the provided environment except with any bindings for the provided Expr*
+  ///  removed.  This method only removes bindings for block-level expressions.
+  ///  Using this method on a non-block level expression will return the
+  ///  same environment object.
+  Environment RemoveBlkExpr(const Environment& Env, Expr* E) {
+    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
+  }
+  
+  Environment RemoveSubExpr(const Environment& Env, Expr* E) {
+    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
+  }
+  
+  Environment AddBlkExpr(const Environment& Env, Expr* E, RVal V) {
+    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));    
+  }
+  
+  Environment AddSubExpr(const Environment& Env, Expr* E, RVal V) {
+    return Environment(F.Add(Env.SubExprBindings, E, V), Env.BlkExprBindings);
+  }
+  
+  /// RemoveSubExprBindings - Return a new environment object with
+  ///  the same bindings as the provided environment except with all the
+  ///  subexpression bindings removed.
+  Environment RemoveSubExprBindings(const Environment& Env) {
+    return Environment(F.GetEmptyMap(), Env.BlkExprBindings);
+  }
+  
+  Environment getInitialEnvironment() {
+    return Environment(F.GetEmptyMap(), F.GetEmptyMap());
+  }
+};
+  
+} // end clang namespace
+
+#endif

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

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/ValueState.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/ValueState.h Tue Jul  8 16:46:56 2008
@@ -16,6 +16,7 @@
 
 // FIXME: Reduce the number of includes.
 
+#include "clang/Analysis/PathSensitive/Environment.h"
 #include "clang/Analysis/PathSensitive/RValues.h"
 #include "clang/Analysis/PathSensitive/GRCoreEngine.h"
 #include "clang/AST/Expr.h"
@@ -39,6 +40,8 @@
 
 namespace clang {
 
+class ValueStateManager;
+  
 //===----------------------------------------------------------------------===//
 // ValueState- An ImmutableMap type Stmt*/Decl*/Symbols to RVals.
 //===----------------------------------------------------------------------===//
@@ -48,25 +51,22 @@
 ///  used as a functional object; that is once it is created and made
 ///  "persistent" in a FoldingSet its values will never change.
 class ValueState : public llvm::FoldingSetNode {
-public:
-  
-  // Typedefs.
-  
+public:  
+  // Typedefs.  
   typedef llvm::ImmutableSet<llvm::APSInt*>                IntSetTy;
-  typedef llvm::ImmutableMap<Expr*,RVal>                   ExprBindingsTy;
   typedef llvm::ImmutableMap<VarDecl*,RVal>                VarBindingsTy;  
   typedef llvm::ImmutableMap<SymbolID,IntSetTy>            ConstNotEqTy;
   typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy;
 
 private:
-
   void operator=(const ValueState& R) const;
   
-  // FIXME: Make these private.
+  friend class ValueStateManager;
+  
+  Environment Env;
 
+  // FIXME: Make these private.
 public:
-  ExprBindingsTy   SubExprBindings;
-  ExprBindingsTy   BlockExprBindings;  
   VarBindingsTy    VarBindings;
   ConstNotEqTy     ConstNotEq;
   ConstEqTy        ConstEq;
@@ -75,10 +75,9 @@
 public:
   
   /// This ctor is used when creating the first ValueState object.
-  ValueState(ExprBindingsTy  EB,  VarBindingsTy VB,
-                 ConstNotEqTy CNE, ConstEqTy  CE)
-    : SubExprBindings(EB), 
-      BlockExprBindings(EB),
+  ValueState(const Environment& env,  VarBindingsTy VB,
+             ConstNotEqTy CNE, ConstEqTy  CE)
+    : Env(env),
       VarBindings(VB),
       ConstNotEq(CNE),
       ConstEq(CE),
@@ -88,18 +87,20 @@
   ///  in FoldingSetNode will also get copied.
   ValueState(const ValueState& RHS)
     : llvm::FoldingSetNode(),
-      SubExprBindings(RHS.SubExprBindings),
-      BlockExprBindings(RHS.BlockExprBindings),
+      Env(RHS.Env),
       VarBindings(RHS.VarBindings),
       ConstNotEq(RHS.ConstNotEq),
       ConstEq(RHS.ConstEq),
       CheckerState(RHS.CheckerState) {} 
   
+  /// getEnvironment - Return the environment associated with this state.
+  ///  The environment is the mapping from expressions to values.
+  const Environment& getEnvironment() const { return Env; }
+  
   /// Profile - Profile the contents of a ValueState object for use
   ///  in a FoldingSet.
   static void Profile(llvm::FoldingSetNodeID& ID, const ValueState* V) {
-    V->SubExprBindings.Profile(ID);
-    V->BlockExprBindings.Profile(ID);
+    V->Env.Profile(ID);
     V->VarBindings.Profile(ID);
     V->ConstNotEq.Profile(ID);
     V->ConstEq.Profile(ID);
@@ -116,20 +117,24 @@
   
   bool isNotEqual(SymbolID sym, const llvm::APSInt& V) const;
   const llvm::APSInt* getSymVal(SymbolID sym) const;
-
+ 
+  RVal LookupExpr(Expr* E) const {
+    return Env.LookupExpr(E);
+  }
+  
   // Iterators.
 
   typedef VarBindingsTy::iterator vb_iterator;
   vb_iterator vb_begin() const { return VarBindings.begin(); }
   vb_iterator vb_end() const { return VarBindings.end(); }
     
-  typedef ExprBindingsTy::iterator seb_iterator;
-  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
-  seb_iterator seb_end() const { return SubExprBindings.end(); }
-  
-  typedef ExprBindingsTy::iterator beb_iterator;
-  beb_iterator beb_begin() const { return BlockExprBindings.begin(); }
-  beb_iterator beb_end() const { return BlockExprBindings.end(); }
+  typedef Environment::seb_iterator seb_iterator;
+  seb_iterator seb_begin() const { return Env.seb_begin(); }
+  seb_iterator seb_end() const { return Env.beb_end(); }
+  
+  typedef Environment::beb_iterator beb_iterator;
+  beb_iterator beb_begin() const { return Env.beb_begin(); }
+  beb_iterator beb_end() const { return Env.beb_end(); }
   
   typedef ConstNotEqTy::iterator cne_iterator;
   cne_iterator cne_begin() const { return ConstNotEq.begin(); }
@@ -167,8 +172,8 @@
   
 class ValueStateManager {
 private:
+  EnvironmentManager                   EnvMgr;
   ValueState::IntSetTy::Factory        ISetFactory;
-  ValueState::ExprBindingsTy::Factory  EXFactory;
   ValueState::VarBindingsTy::Factory   VBFactory;
   ValueState::ConstNotEqTy::Factory    CNEFactory;
   ValueState::ConstEqTy::Factory       CEFactory;
@@ -187,20 +192,16 @@
   llvm::BumpPtrAllocator& Alloc;
   
 private:
+
+  Environment RemoveBlkExpr(const Environment& Env, Expr* E) {
+    return EnvMgr.RemoveBlkExpr(Env, E);
+  }
   
-  ValueState::ExprBindingsTy Remove(ValueState::ExprBindingsTy B, Expr* E) {
-    return EXFactory.Remove(B, E);
-  }    
-    
-  ValueState::VarBindingsTy  Remove(ValueState::VarBindingsTy B, VarDecl* V) {
+  ValueState::VarBindingsTy Remove(ValueState::VarBindingsTy B, VarDecl* V) {
     return VBFactory.Remove(B, V);
   }
-
-  inline ValueState::ExprBindingsTy Remove(const ValueState& V, Expr* E) {
-    return Remove(V.BlockExprBindings, E);
-  }
   
-  inline ValueState::VarBindingsTy Remove(const ValueState& V, VarDecl* D) {
+  ValueState::VarBindingsTy Remove(const ValueState& V, VarDecl* D) {
     return Remove(V.VarBindings, D);
   }
                   
@@ -209,8 +210,8 @@
   
 public:  
   ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc) 
-    : ISetFactory(alloc), 
-      EXFactory(alloc),
+    : EnvMgr(alloc),
+      ISetFactory(alloc), 
       VBFactory(alloc),
       CNEFactory(alloc),
       CEFactory(alloc),
@@ -228,10 +229,10 @@
   ValueState* RemoveDeadBindings(ValueState* St, Stmt* Loc, 
                                  const LiveVariables& Liveness,
                                  DeadSymbolsTy& DeadSymbols);
-  
+
   ValueState* RemoveSubExprBindings(ValueState* St) {
     ValueState NewSt = *St;
-    NewSt.SubExprBindings = EXFactory.GetEmptyMap();
+    NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env);
     return getPersistentState(NewSt);    
   }
   

Added: cfe/trunk/lib/Analysis/Environment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/Environment.cpp?rev=53252&view=auto

==============================================================================
--- cfe/trunk/lib/Analysis/Environment.cpp (added)
+++ cfe/trunk/lib/Analysis/Environment.cpp Tue Jul  8 16:46:56 2008
@@ -0,0 +1,29 @@
+//== Environment.cpp - Map from Expr* to Locations/Values -------*- C++ -*--==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defined the Environment and EnvironmentManager classes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/PathSensitive/Environment.h"
+#include "llvm/ADT/ImmutableMap.h"
+
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Environment.
+//===----------------------------------------------------------------------===//
+
+
+
+
+
+//===----------------------------------------------------------------------===//
+// Environment Manager.
+//===----------------------------------------------------------------------===//
\ No newline at end of file

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

==============================================================================
--- cfe/trunk/lib/Analysis/ValueState.cpp (original)
+++ cfe/trunk/lib/Analysis/ValueState.cpp Tue Jul  8 16:46:56 2008
@@ -49,7 +49,7 @@
   ValueState NewSt = *St;
   
   // Drop bindings for subexpressions.
-  NewSt.SubExprBindings = EXFactory.GetEmptyMap();
+  NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env);
   
   // Iterate over the block-expr bindings.
 
@@ -76,7 +76,7 @@
       if (X.isUndef() && cast<UndefinedVal>(X).getData())
         continue;
       
-      NewSt.BlockExprBindings = Remove(NewSt, BlkExpr);
+      NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, BlkExpr);
     }
   }
 
@@ -258,6 +258,7 @@
   return getPersistentState(NewSt);
 }
 
+// FIXME: This should all go into the environment.
 RVal ValueStateManager::GetRVal(ValueState* St, Expr* E) {
 
   for (;;) {
@@ -321,13 +322,7 @@
     break;
   }
   
-  ValueState::ExprBindingsTy::data_type* T = St->SubExprBindings.lookup(E);
-  
-  if (T)
-    return *T;
-  
-  T = St->BlockExprBindings.lookup(E);
-  return T ? *T : UnknownVal();
+  return St->LookupExpr(E);
 }
 
 RVal ValueStateManager::GetBlkExprRVal(ValueState* St, Expr* E) {
@@ -344,10 +339,8 @@
       return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E));
     }
       
-    default: {
-      ValueState::ExprBindingsTy::data_type* T=St->BlockExprBindings.lookup(E);    
-      return T ? *T : UnknownVal();
-    }
+    default:
+      return St->getEnvironment().LookupBlkExpr(E);
   }
 }
 
@@ -364,9 +357,9 @@
       ValueState NewSt = *St;
       
       if (isBlkExpr)
-        NewSt.BlockExprBindings = EXFactory.Remove(NewSt.BlockExprBindings, E);
+        NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, E);
       else
-        NewSt.SubExprBindings = EXFactory.Remove(NewSt.SubExprBindings, E);
+        NewSt.Env = EnvMgr.RemoveSubExpr(NewSt.Env, E);
       
       return getPersistentState(NewSt);
     }
@@ -376,12 +369,10 @@
   
   ValueState NewSt = *St;
   
-  if (isBlkExpr) {
-    NewSt.BlockExprBindings = EXFactory.Add(NewSt.BlockExprBindings, E, V);
-  }
-  else {
-    NewSt.SubExprBindings = EXFactory.Add(NewSt.SubExprBindings, E, V);
-  }
+  if (isBlkExpr)
+    NewSt.Env = EnvMgr.AddBlkExpr(NewSt.Env, E, V);
+  else
+    NewSt.Env = EnvMgr.AddSubExpr(NewSt.Env, E, V);
 
   return getPersistentState(NewSt);
 }
@@ -437,10 +428,10 @@
 ValueState* ValueStateManager::getInitialState() {
 
   // Create a state with empty variable bindings.
-  ValueState StateImpl(EXFactory.GetEmptyMap(),
-                           VBFactory.GetEmptyMap(),
-                           CNEFactory.GetEmptyMap(),
-                           CEFactory.GetEmptyMap());
+  ValueState StateImpl(EnvMgr.getInitialEnvironment(),
+                       VBFactory.GetEmptyMap(),
+                       CNEFactory.GetEmptyMap(),
+                       CEFactory.GetEmptyMap());
   
   return getPersistentState(StateImpl);
 }





More information about the cfe-commits mailing list