]> granicus.if.org Git - clang/commitdiff
Started partitioning of transfer function logic (and thus the policy behind
authorTed Kremenek <kremenek@apple.com>
Thu, 14 Feb 2008 18:28:23 +0000 (18:28 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 14 Feb 2008 18:28:23 +0000 (18:28 +0000)
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

Analysis/GRExprEngine.cpp
Analysis/GRSimpleVals.cpp [new file with mode: 0644]
Analysis/GRSimpleVals.h [new file with mode: 0644]
Analysis/GRTransferFuncs.cpp [new file with mode: 0644]
Analysis/RValues.cpp
include/clang/Analysis/PathSensitive/GRTransferFuncs.h [new file with mode: 0644]
include/clang/Analysis/PathSensitive/RValues.h

index 16abcc425b00eb9a03bb71e852953d3fe69c786d..3ff4c4bdfe0deda09ad6d85c4cf6e64c9af75aa8 100644 (file)
@@ -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 @@ public:
   typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
   typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder;
   
-  
   class NodeSet {
     typedef llvm::SmallVector<NodeTy*,3> ImplTy;
     ImplTy Impl;
@@ -115,6 +117,10 @@ protected:
   /// 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 @@ protected:
   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 @@ public:
   /// 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 @@ void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
     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 @@ void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
                     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 (file)
index 0000000..447eb44
--- /dev/null
@@ -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 (file)
index 0000000..4c9c689
--- /dev/null
@@ -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 (file)
index 0000000..3716ed9
--- /dev/null
@@ -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;
+}
index 965400a3e0516c1bc90f4022178ecfe8895e8c4b..06d29550a10dab6541e9d735387b918f1bec37fd 100644 (file)
@@ -123,20 +123,7 @@ ValueManager::getConstraint(SymbolID sym, BinaryOperator::Opcode Op,
   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 @@ nonlval::ConcreteInt::EvalComplement(ValueManager& ValMgr) const {
   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 @@ NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
   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.
diff --git a/include/clang/Analysis/PathSensitive/GRTransferFuncs.h b/include/clang/Analysis/PathSensitive/GRTransferFuncs.h
new file mode 100644 (file)
index 0000000..7e8b38d
--- /dev/null
@@ -0,0 +1,37 @@
+//== GRTransferFuncs.h - 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.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_ANALYSIS_GRTF
+#define LLVM_CLANG_ANALYSIS_GRTF
+
+#include "clang/Analysis/PathSensitive/RValues.h"
+
+namespace clang {
+  
+class GRTransferFuncs {
+public:
+  GRTransferFuncs() {}
+  virtual ~GRTransferFuncs() {}
+  
+  RValue EvalCast(ValueManager& ValMgr, RValue V, Expr* CastExpr);
+  virtual RValue EvalCast(ValueManager& ValMgr, NonLValue V, Expr* CastExpr) =0;
+  virtual RValue EvalCast(ValueManager& ValMgr, LValue V, Expr* CastExpr) = 0;
+
+  
+  
+};
+  
+} // end clang namespace
+
+#endif
index 2f500fea681e20702b91b42e9b64f407646292ff..8d84498a0b8b1384a13cb0f97f69396765476c2d 100644 (file)
@@ -270,9 +270,6 @@ public:
   
   /// BufferTy - A temporary buffer to hold a set of RValues.
   typedef llvm::SmallVector<RValue,5> BufferTy;
-
-  
-  RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const;
   
   unsigned getRawKind() const { return Kind; }
   BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
@@ -329,7 +326,6 @@ public:
                                BinaryOperator::Opcode Op,
                                const NonLValue& RHS) const;
 
-  RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const;
   NonLValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const;
   NonLValue EvalComplement(ValueManager& ValMgr) const;
   
@@ -361,9 +357,7 @@ public:
 
   RValue EvalBinaryOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
                             const LValue& RHS) const;
-  
-  RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const;
-  
+    
   static LValue GetValue(AddrLabelExpr* E);
   
   // Implement isa<T> support.