From: Ted Kremenek Date: Thu, 14 Feb 2008 18:28:23 +0000 (+0000) Subject: Started partitioning of transfer function logic (and thus the policy behind X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d59cccc0b6cd4485897526699fe98514805c1883;p=clang 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 16abcc425b..3ff4c4bdfe 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 @@ public: typedef GRIndirectGotoNodeBuilder IndirectGotoNodeBuilder; typedef GRSwitchNodeBuilder SwitchNodeBuilder; - class NodeSet { typedef llvm::SmallVector 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 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 0000000000..447eb44f0e --- /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(X)) + return UnknownVal(); + + llvm::APSInt V = cast(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(X)) + return UnknownVal(); + + llvm::APSInt V = cast(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 0000000000..4c9c6890ba --- /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 0000000000..3716ed9691 --- /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(X), CastExpr); + + case RValue::NonLValueKind: + return EvalCast(ValMgr, cast(X), CastExpr); + + case RValue::UninitializedKind: + case RValue::UnknownKind: break; + } + + return X; +} diff --git a/Analysis/RValues.cpp b/Analysis/RValues.cpp index 965400a3e0..06d29550a1 100644 --- a/Analysis/RValues.cpp +++ b/Analysis/RValues.cpp @@ -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(this)->EvalCast(ValMgr, CastExpr); - case NonLValueKind: return cast(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(this)) - return UnknownVal(); - - APSInt V = cast(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(*this)) - return UnknownVal(); - - APSInt V = cast(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 index 0000000000..7e8b38d86c --- /dev/null +++ b/include/clang/Analysis/PathSensitive/GRTransferFuncs.h @@ -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 diff --git a/include/clang/Analysis/PathSensitive/RValues.h b/include/clang/Analysis/PathSensitive/RValues.h index 2f500fea68..8d84498a0b 100644 --- a/include/clang/Analysis/PathSensitive/RValues.h +++ b/include/clang/Analysis/PathSensitive/RValues.h @@ -270,9 +270,6 @@ public: /// BufferTy - A temporary buffer to hold a set of RValues. typedef llvm::SmallVector 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 support.