Skip to content

Commit

Permalink
[analyzer] Convert more functions (ex:evalBind()) to iterative builders
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142829 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
AnnaZaks committed Oct 24, 2011
1 parent 8ad8c54 commit 6b6152b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
Expand Up @@ -301,6 +301,10 @@ class PureStmtNodeBuilder: public NodeBuilder {
const NodeBuilderContext &Ctx)
: NodeBuilder(SrcNode, DstSet, Ctx) {}

PureStmtNodeBuilder(ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
const NodeBuilderContext &Ctx)
: NodeBuilder(SrcSet, DstSet, Ctx) {}

ExplodedNode *generateNode(const Stmt *S,
ExplodedNode *Pred,
const ProgramState *St,
Expand Down
10 changes: 8 additions & 2 deletions lib/StaticAnalyzer/Core/ExprEngine.cpp
Expand Up @@ -1297,9 +1297,13 @@ void ExprEngine::evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE,
getCheckerManager().runCheckersForBind(CheckedSet, Pred, location, Val,
StoreE, *this);

// TODO: Remove TmpDst after NB refactoring is done.
ExplodedNodeSet TmpDst;
Builder->takeNodes(CheckedSet);
PureStmtNodeBuilder Bldr(CheckedSet, TmpDst, *currentBuilderContext);

for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
I!=E; ++I) {

const ProgramState *state = (*I)->getState();

if (atDeclInit) {
Expand All @@ -1311,8 +1315,10 @@ void ExprEngine::evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE,
state = state->bindLoc(location, Val);
}

MakeNode(Dst, StoreE, *I, state);
Bldr.generateNode(StoreE, *I, state);
}
Builder->addNodes(TmpDst);
Dst.insert(TmpDst);
}

/// evalStore - Handle the semantics of a store via an assignment.
Expand Down
39 changes: 26 additions & 13 deletions lib/StaticAnalyzer/Core/ExprEngineC.cpp
Expand Up @@ -373,7 +373,9 @@ void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,

void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
ExplodedNodeSet &Dst) {

Builder->takeNodes(Pred);
PureStmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);

assert(B->getOpcode() == BO_LAnd ||
B->getOpcode() == BO_LOr);

Expand All @@ -389,7 +391,8 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,

// Handle undefined values.
if (X.isUndef()) {
MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Bldr.generateNode(B, Pred, state->BindExpr(B, X));
Builder->addNodes(Dst);
return;
}

Expand All @@ -402,11 +405,11 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
// this right now, and since most logical expressions are used for branches,
// the payoff is not likely to be large. Instead, we do eager evaluation.
if (const ProgramState *newState = state->assume(XD, true))
MakeNode(Dst, B, Pred,
Bldr.generateNode(B, Pred,
newState->BindExpr(B, svalBuilder.makeIntVal(1U, B->getType())));

if (const ProgramState *newState = state->assume(XD, false))
MakeNode(Dst, B, Pred,
Bldr.generateNode(B, Pred,
newState->BindExpr(B, svalBuilder.makeIntVal(0U, B->getType())));
}
else {
Expand All @@ -415,13 +418,16 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
// the short-circuiting.
X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U,
B->getType());
MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Bldr.generateNode(B, Pred, state->BindExpr(B, X));
}
Builder->addNodes(Dst);
}

void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
ExplodedNode *Pred,
ExplodedNodeSet &Dst) {
Builder->takeNodes(Pred);
PureStmtNodeBuilder B(Pred, Dst, *currentBuilderContext);

const ProgramState *state = Pred->getState();
QualType T = getContext().getCanonicalType(IE->getType());
Expand All @@ -434,7 +440,8 @@ void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
// e.g: static int* myArray[] = {};
if (NumInitElements == 0) {
SVal V = svalBuilder.makeCompoundVal(T, vals);
MakeNode(Dst, IE, Pred, state->BindExpr(IE, V));
B.generateNode(IE, Pred, state->BindExpr(IE, V));
Builder->addNodes(Dst);
return;
}

Expand All @@ -443,15 +450,17 @@ void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
vals = getBasicVals().consVals(state->getSVal(cast<Expr>(*it)), vals);
}

MakeNode(Dst, IE, Pred,
state->BindExpr(IE, svalBuilder.makeCompoundVal(T, vals)));
B.generateNode(IE, Pred,
state->BindExpr(IE, svalBuilder.makeCompoundVal(T, vals)));
Builder->addNodes(Dst);
return;
}

if (Loc::isLocType(T) || T->isIntegerType()) {
assert(IE->getNumInits() == 1);
const Expr *initEx = IE->getInit(0);
MakeNode(Dst, IE, Pred, state->BindExpr(IE, state->getSVal(initEx)));
B.generateNode(IE, Pred, state->BindExpr(IE, state->getSVal(initEx)));
Builder->addNodes(Dst);
return;
}

Expand All @@ -463,6 +472,8 @@ void ExprEngine::VisitGuardedExpr(const Expr *Ex,
const Expr *R,
ExplodedNode *Pred,
ExplodedNodeSet &Dst) {
Builder->takeNodes(Pred);
PureStmtNodeBuilder B(Pred, Dst, *currentBuilderContext);

const ProgramState *state = Pred->getState();
SVal X = state->getSVal(Ex);
Expand All @@ -472,24 +483,26 @@ void ExprEngine::VisitGuardedExpr(const Expr *Ex,
X = state->getSVal(SE);

// Make sure that we invalidate the previous binding.
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
B.generateNode(Ex, Pred, state->BindExpr(Ex, X, true));
Builder->addNodes(Dst);
}

void ExprEngine::
VisitOffsetOfExpr(const OffsetOfExpr *OOE,
ExplodedNode *Pred, ExplodedNodeSet &Dst) {
Builder->takeNodes(Pred);
PureStmtNodeBuilder B(Pred, Dst, *currentBuilderContext);
Expr::EvalResult Res;
if (OOE->Evaluate(Res, getContext()) && Res.Val.isInt()) {
const APSInt &IV = Res.Val.getInt();
assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
assert(OOE->getType()->isIntegerType());
assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType());
SVal X = svalBuilder.makeIntVal(IV);
MakeNode(Dst, OOE, Pred, Pred->getState()->BindExpr(OOE, X));
return;
B.generateNode(OOE, Pred, Pred->getState()->BindExpr(OOE, X));
}
// FIXME: Handle the case where __builtin_offsetof is not a constant.
Dst.Add(Pred);
Builder->addNodes(Dst);
}


Expand Down

0 comments on commit 6b6152b

Please sign in to comment.