Started partitioning of transfer function logic (and thus the policy behind 
these operations) into GRTransferFuncs and its subclasses.  Originally all
of this logic was handled by the class RValue, but in reality different
analyses will want more flexibility on how they evaluate different values.

Transfer functions migrated so far: "Cast"


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@47125 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Analysis/GRExprEngine.cpp b/Analysis/GRExprEngine.cpp
index 16abcc4..3ff4c4b 100644
--- a/Analysis/GRExprEngine.cpp
+++ b/Analysis/GRExprEngine.cpp
@@ -18,6 +18,9 @@
 #include "ValueState.h"
 
 #include "clang/Analysis/PathSensitive/GRCoreEngine.h"
+#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
+#include "GRSimpleVals.h"
+
 #include "clang/AST/Expr.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/Analysis/Analyses/LiveVariables.h"
@@ -73,7 +76,6 @@
   typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
   typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder;
   
-  
   class NodeSet {
     typedef llvm::SmallVector<NodeTy*,3> ImplTy;
     ImplTy Impl;
@@ -115,6 +117,10 @@
   /// ValueMgr - Object that manages the data for all created RValues.
   ValueManager& ValMgr;
   
+  /// TF - Object that represents a bundle of transfer functions
+  ///  for manipulating and creating RValues.
+  GRTransferFuncs& TF;
+  
   /// SymMgr - Object that manages the symbol information.
   SymbolManager& SymMgr;
   
@@ -139,10 +145,12 @@
   bool StateCleaned;
   
 public:
-  GRExprEngine(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
+  GRExprEngine(GraphTy& g) : 
+      G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
       Builder(NULL),
       StateMgr(G.getContext(), G.getAllocator()),
       ValMgr(StateMgr.getValueManager()),
+      TF(*(new GRSimpleVals())), // FIXME.
       SymMgr(StateMgr.getSymbolManager()),
       StmtEntryNode(NULL), CurrentStmt(NULL) {
     
@@ -312,6 +320,11 @@
   /// VisitUnaryOperator - Transfer function logic for unary operators.
   void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
   
+  
+  inline RValue EvalCast(ValueManager& ValMgr, RValue R, Expr* CastExpr) {
+    return TF.EvalCast(ValMgr, R, CastExpr);
+  }
+  
 };
 } // end anonymous namespace
 
@@ -702,7 +715,7 @@
     NodeTy* N = *I1;
     StateTy St = N->getState();
     const RValue& V = GetValue(St, E);
-    Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
+    Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
   }
 }
 
@@ -1511,10 +1524,13 @@
                     Diagnostic& Diag) {
   
   GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
+  GRExprEngine* CheckerState = &Engine.getCheckerState();
+  
+  // Execute the worklist algorithm.
   Engine.ExecuteWorkList();
   
   // Look for explicit-Null dereferences and warn about them.
-  GRExprEngine* CheckerState = &Engine.getCheckerState();
+
   
   for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
                                   E=CheckerState->null_end(); I!=E; ++I) {
diff --git a/Analysis/GRSimpleVals.cpp b/Analysis/GRSimpleVals.cpp
new file mode 100644
index 0000000..447eb44
--- /dev/null
+++ b/Analysis/GRSimpleVals.cpp
@@ -0,0 +1,59 @@
+// GRSimpleVals.cpp - Transfer functions for tracking simple values -*- C++ -*--
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This files defines GRSimpleVals, a sub-class of GRTransferFuncs that
+//  provides transfer functions for performing simple value tracking with
+//  limited support for symbolics.
+//
+//===----------------------------------------------------------------------===//
+
+#include "GRSimpleVals.h"
+
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Transfer function for Casts.
+//===----------------------------------------------------------------------===//
+
+RValue GRSimpleVals::EvalCast(ValueManager& ValMgr, NonLValue X,
+                              Expr* CastExpr) {
+  
+  if (!isa<nonlval::ConcreteInt>(X))
+    return UnknownVal();
+  
+  llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue();
+  QualType T = CastExpr->getType();
+  V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
+  V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
+  
+  if (CastExpr->getType()->isPointerType())
+    return lval::ConcreteInt(ValMgr.getValue(V));
+  else
+    return nonlval::ConcreteInt(ValMgr.getValue(V));
+}
+
+// Casts.
+
+RValue GRSimpleVals::EvalCast(ValueManager& ValMgr, LValue X, Expr* CastExpr) {
+
+  if (CastExpr->getType()->isPointerType())
+    return X;
+  
+  assert (CastExpr->getType()->isIntegerType());
+  
+  if (!isa<lval::ConcreteInt>(X))
+    return UnknownVal();
+  
+  llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue();
+  QualType T = CastExpr->getType();
+  V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
+  V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
+
+  return nonlval::ConcreteInt(ValMgr.getValue(V));
+}
\ No newline at end of file
diff --git a/Analysis/GRSimpleVals.h b/Analysis/GRSimpleVals.h
new file mode 100644
index 0000000..4c9c689
--- /dev/null
+++ b/Analysis/GRSimpleVals.h
@@ -0,0 +1,35 @@
+// GRSimpleVals.h - Transfer functions for tracking simple values -*- C++ -*--//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This files defines GRSimpleVals, a sub-class of GRTransferFuncs that
+//  provides transfer functions for performing simple value tracking with
+//  limited support for symbolics.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_ANALYSIS_GRSIMPLEVALS
+#define LLVM_CLANG_ANALYSIS_GRSIMPLEVALS
+
+#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
+
+namespace clang {
+  
+class GRSimpleVals : public GRTransferFuncs {
+public:
+  GRSimpleVals() {}
+  virtual ~GRSimpleVals() {}
+  
+  virtual RValue EvalCast(ValueManager& ValMgr, NonLValue V, Expr* CastExpr);
+  virtual RValue EvalCast(ValueManager& ValMgr, LValue V, Expr* CastExpr);
+};
+  
+  
+} // end clang namespace
+
+#endif
diff --git a/Analysis/GRTransferFuncs.cpp b/Analysis/GRTransferFuncs.cpp
new file mode 100644
index 0000000..3716ed9
--- /dev/null
+++ b/Analysis/GRTransferFuncs.cpp
@@ -0,0 +1,41 @@
+//== GRTransferFuncs.cpp - Path-Sens. Transfer Functions Interface -*- C++ -*--=
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This files defines GRTransferFuncs, which provides a base-class that
+//  defines an interface for transfer functions used by GRExprEngine.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
+
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Transfer function for Casts.
+//===----------------------------------------------------------------------===//
+
+RValue GRTransferFuncs::EvalCast(ValueManager& ValMgr, RValue X,
+                                 Expr* CastExpr) {
+  
+  switch (X.getBaseKind()) {
+    default:
+      assert(false && "Invalid RValue."); break;
+
+    case RValue::LValueKind: 
+      return EvalCast(ValMgr, cast<LValue>(X), CastExpr);
+
+    case RValue::NonLValueKind:
+      return EvalCast(ValMgr, cast<NonLValue>(X), CastExpr);
+    
+    case RValue::UninitializedKind:
+    case RValue::UnknownKind: break;
+  }
+  
+  return X;
+}
diff --git a/Analysis/RValues.cpp b/Analysis/RValues.cpp
index 965400a..06d2955 100644
--- a/Analysis/RValues.cpp
+++ b/Analysis/RValues.cpp
@@ -123,20 +123,7 @@
   return *C;
 }
 
-//===----------------------------------------------------------------------===//
-// Transfer function for Casts.
-//===----------------------------------------------------------------------===//
 
-RValue RValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const {
-  switch (getBaseKind()) {
-    default: assert(false && "Invalid RValue."); break;
-    case LValueKind: return cast<LValue>(this)->EvalCast(ValMgr, CastExpr);
-    case NonLValueKind: return cast<NonLValue>(this)->EvalCast(ValMgr, CastExpr);      
-    case UninitializedKind: case UnknownKind: break;
-  }
-  
-  return *this;
-}
  
 
 //===----------------------------------------------------------------------===//
@@ -270,23 +257,6 @@
   return ValMgr.getValue(~getValue()); 
 }
 
-  // Casts.
-
-RValue NonLValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const {
-  if (!isa<nonlval::ConcreteInt>(this))
-    return UnknownVal();
-  
-  APSInt V = cast<nonlval::ConcreteInt>(this)->getValue();
-  QualType T = CastExpr->getType();
-  V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
-  V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
-  
-  if (CastExpr->getType()->isPointerType())
-    return lval::ConcreteInt(ValMgr.getValue(V));
-  else
-    return nonlval::ConcreteInt(ValMgr.getValue(V));
-}
-
   // Unary Minus.
 
 NonLValue NonLValue::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const {
@@ -445,23 +415,7 @@
   return NonLValue::GetIntTruthValue(ValMgr, true);
 }
 
-  // Casts.
 
-RValue LValue::EvalCast(ValueManager& ValMgr, Expr* CastExpr) const {
-  if (CastExpr->getType()->isPointerType())
-    return *this;
-  
-  assert (CastExpr->getType()->isIntegerType());
-  
-  if (!isa<lval::ConcreteInt>(*this))
-    return UnknownVal();
-  
-  APSInt V = cast<lval::ConcreteInt>(this)->getValue();
-  QualType T = CastExpr->getType();
-  V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
-  V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart()));
-  return nonlval::ConcreteInt(ValMgr.getValue(V));
-}
 
 //===----------------------------------------------------------------------===//
 // Utility methods for constructing Non-LValues.