From: Ted Kremenek Date: Thu, 10 Jul 2008 17:19:18 +0000 (+0000) Subject: Move some environment methods from ValueState/ValueStateManager to Environment/Enviro... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d72ee907f76000446c706471e93d1f299104f9a7;p=clang Move some environment methods from ValueState/ValueStateManager to Environment/EnvironmentManager. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53412 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Analysis/PathSensitive/Environment.h b/include/clang/Analysis/PathSensitive/Environment.h index 1eb8e72718..ba06286657 100644 --- a/include/clang/Analysis/PathSensitive/Environment.h +++ b/include/clang/Analysis/PathSensitive/Environment.h @@ -22,6 +22,7 @@ namespace clang { class EnvironmentManager; +class BasicValueFactory; class Environment : public llvm::FoldingSetNode { private: @@ -65,6 +66,9 @@ public: return X ? *X : UnknownVal(); } + RVal GetRVal(Expr* Ex, BasicValueFactory& BasicVals) const; + RVal GetBlkExprRVal(Expr* Ex, BasicValueFactory& BasicVals) const; + /// Profile - Profile the contents of an Environment object for use /// in a FoldingSet. static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) { @@ -76,7 +80,12 @@ public: /// in a FoldingSet. void Profile(llvm::FoldingSetNodeID& ID) const { Profile(ID, this); - } + } + + bool operator==(const Environment& RHS) const { + return SubExprBindings == RHS.SubExprBindings && + BlkExprBindings == RHS.BlkExprBindings; + } }; class EnvironmentManager { @@ -120,6 +129,9 @@ public: Environment getInitialEnvironment() { return Environment(F.GetEmptyMap(), F.GetEmptyMap()); } + + Environment SetRVal(const Environment& Env, Expr* E, RVal V, + bool isBlkExpr, bool Invalidate); }; } // end clang namespace diff --git a/include/clang/Analysis/PathSensitive/ValueState.h b/include/clang/Analysis/PathSensitive/ValueState.h index ce25dd95bc..ab9b4a8b32 100644 --- a/include/clang/Analysis/PathSensitive/ValueState.h +++ b/include/clang/Analysis/PathSensitive/ValueState.h @@ -235,17 +235,37 @@ public: NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env); return getPersistentState(NewSt); } + + // Methods that query & manipulate the Environment. - ValueState* SetRVal(ValueState* St, Expr* E, RVal V, bool isBlkExpr, - bool Invalidate); + RVal GetRVal(ValueState* St, Expr* Ex) { + return St->getEnvironment().GetRVal(Ex, BasicVals); + } - ValueState* SetRVal(ValueState* St, LVal LV, RVal V); + RVal GetBlkExprRVal(ValueState* St, Expr* Ex) { + return St->getEnvironment().GetBlkExprRVal(Ex, BasicVals); + } + + ValueState* SetRVal(ValueState* St, Expr* Ex, RVal V, bool isBlkExpr, + bool Invalidate) { + + const Environment& OldEnv = St->getEnvironment(); + Environment NewEnv = EnvMgr.SetRVal(OldEnv, Ex, V, isBlkExpr, Invalidate); + + if (NewEnv == OldEnv) + return St; + + ValueState NewSt = *St; + NewSt.Env = NewEnv; + return getPersistentState(NewSt); + } + + // Methods that query & manipulate the Store. - RVal GetRVal(ValueState* St, Expr* E); RVal GetRVal(ValueState* St, LVal LV, QualType T = QualType()); - RVal GetBlkExprRVal(ValueState* St, Expr* Ex); - + ValueState* SetRVal(ValueState* St, LVal LV, RVal V); + void BindVar(ValueState& StImpl, VarDecl* D, RVal V); void Unbind(ValueState& StImpl, LVal LV); diff --git a/lib/Analysis/Environment.cpp b/lib/Analysis/Environment.cpp index 86fd12c2a5..dd5caef7f0 100644 --- a/lib/Analysis/Environment.cpp +++ b/lib/Analysis/Environment.cpp @@ -16,14 +16,101 @@ using namespace clang; -//===----------------------------------------------------------------------===// -// Environment. -//===----------------------------------------------------------------------===// - +RVal Environment::GetRVal(Expr* E, BasicValueFactory& BasicVals) const { + + for (;;) { + + switch (E->getStmtClass()) { + + case Stmt::AddrLabelExprClass: + return LVal::MakeVal(cast(E)); + + // ParenExprs are no-ops. + + case Stmt::ParenExprClass: + E = cast(E)->getSubExpr(); + continue; + + case Stmt::CharacterLiteralClass: { + CharacterLiteral* C = cast(E); + return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); + } + + case Stmt::IntegerLiteralClass: { + return NonLVal::MakeVal(BasicVals, cast(E)); + } + + case Stmt::StringLiteralClass: + return LVal::MakeVal(cast(E)); + + // Casts where the source and target type are the same + // are no-ops. We blast through these to get the descendant + // subexpression that has a value. + + case Stmt::ImplicitCastExprClass: { + ImplicitCastExpr* C = cast(E); + QualType CT = C->getType(); + + if (CT->isVoidType()) + return UnknownVal(); + + QualType ST = C->getSubExpr()->getType(); + + break; + } + + case Stmt::CastExprClass: { + CastExpr* C = cast(E); + QualType CT = C->getType(); + QualType ST = C->getSubExpr()->getType(); + + if (CT->isVoidType()) + return UnknownVal(); + + break; + } + + // Handle all other Expr* using a lookup. + + default: + break; + }; + + break; + } + + return LookupExpr(E); +} +RVal Environment::GetBlkExprRVal(Expr* E, BasicValueFactory& BasicVals) const { + + E = E->IgnoreParens(); + + switch (E->getStmtClass()) { + case Stmt::CharacterLiteralClass: { + CharacterLiteral* C = cast(E); + return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); + } + + case Stmt::IntegerLiteralClass: { + return NonLVal::MakeVal(BasicVals, cast(E)); + } + + default: + return LookupBlkExpr(E); + } +} +Environment EnvironmentManager::SetRVal(const Environment& Env, Expr* E, RVal V, + bool isBlkExpr, bool Invalidate) { + assert (E); + + if (V.isUnknown()) { + if (Invalidate) + return isBlkExpr ? RemoveBlkExpr(Env, E) : RemoveSubExpr(Env, E); + else + return Env; + } - -//===----------------------------------------------------------------------===// -// Environment Manager. -//===----------------------------------------------------------------------===// \ No newline at end of file + return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V); +} diff --git a/lib/Analysis/ValueState.cpp b/lib/Analysis/ValueState.cpp index 23c93ae1d6..cc77edc826 100644 --- a/lib/Analysis/ValueState.cpp +++ b/lib/Analysis/ValueState.cpp @@ -258,125 +258,6 @@ ValueState* ValueStateManager::AddEQ(ValueState* St, SymbolID sym, return getPersistentState(NewSt); } -// FIXME: This should all go into the environment. -RVal ValueStateManager::GetRVal(ValueState* St, Expr* E) { - - for (;;) { - - switch (E->getStmtClass()) { - - case Stmt::AddrLabelExprClass: - return LVal::MakeVal(cast(E)); - - // ParenExprs are no-ops. - - case Stmt::ParenExprClass: - E = cast(E)->getSubExpr(); - continue; - - case Stmt::CharacterLiteralClass: { - CharacterLiteral* C = cast(E); - return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); - } - - case Stmt::IntegerLiteralClass: { - return NonLVal::MakeVal(BasicVals, cast(E)); - } - - case Stmt::StringLiteralClass: - return LVal::MakeVal(cast(E)); - - // Casts where the source and target type are the same - // are no-ops. We blast through these to get the descendant - // subexpression that has a value. - - case Stmt::ImplicitCastExprClass: { - ImplicitCastExpr* C = cast(E); - QualType CT = C->getType(); - - if (CT->isVoidType()) - return UnknownVal(); - - QualType ST = C->getSubExpr()->getType(); - - break; - } - - case Stmt::CastExprClass: { - CastExpr* C = cast(E); - QualType CT = C->getType(); - QualType ST = C->getSubExpr()->getType(); - - if (CT->isVoidType()) - return UnknownVal(); - - break; - } - - // Handle all other Expr* using a lookup. - - default: - break; - }; - - break; - } - - return St->LookupExpr(E); -} - -RVal ValueStateManager::GetBlkExprRVal(ValueState* St, Expr* E) { - - E = E->IgnoreParens(); - - switch (E->getStmtClass()) { - case Stmt::CharacterLiteralClass: { - CharacterLiteral* C = cast(E); - return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType()); - } - - case Stmt::IntegerLiteralClass: { - return NonLVal::MakeVal(BasicVals, cast(E)); - } - - default: - return St->getEnvironment().LookupBlkExpr(E); - } -} - -ValueState* -ValueStateManager::SetRVal(ValueState* St, Expr* E, RVal V, - bool isBlkExpr, bool Invalidate) { - - assert (E); - - if (V.isUnknown()) { - - if (Invalidate) { - - ValueState NewSt = *St; - - if (isBlkExpr) - NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, E); - else - NewSt.Env = EnvMgr.RemoveSubExpr(NewSt.Env, E); - - return getPersistentState(NewSt); - } - - return St; - } - - ValueState NewSt = *St; - - if (isBlkExpr) - NewSt.Env = EnvMgr.AddBlkExpr(NewSt.Env, E, V); - else - NewSt.Env = EnvMgr.AddSubExpr(NewSt.Env, E, V); - - return getPersistentState(NewSt); -} - ValueState* ValueStateManager::SetRVal(ValueState* St, LVal LV, RVal V) {