2008-01-13 12:56:13 +08:00
|
|
|
//=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the template classes ExplodedNode and ExplodedGraph,
|
|
|
|
// which represent a path-sensitive, intra-procedural "exploded graph."
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2008-03-08 06:58:01 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2008-03-08 06:58:01 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-06-23 06:08:12 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-01-13 12:56:13 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-01-13 12:56:13 +08:00
|
|
|
|
2008-08-27 09:56:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Node auditing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// An out of line virtual method to provide a home for the class vtable.
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode::Auditor::~Auditor() {}
|
2008-08-27 09:56:11 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2009-08-06 09:32:16 +08:00
|
|
|
static ExplodedNode::Auditor* NodeAuditor = 0;
|
2008-08-27 09:56:11 +08:00
|
|
|
#endif
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) {
|
2008-08-27 09:56:11 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
NodeAuditor = A;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-09 09:27:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cleanup.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-25 08:35:05 +08:00
|
|
|
ExplodedGraph::ExplodedGraph()
|
2012-10-24 07:59:05 +08:00
|
|
|
: NumNodes(0), ReclaimNodeInterval(0) {}
|
2012-02-17 04:19:25 +08:00
|
|
|
|
|
|
|
ExplodedGraph::~ExplodedGraph() {}
|
2011-02-09 09:27:33 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Node reclamation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-02-25 15:37:13 +08:00
|
|
|
bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) {
|
|
|
|
if (!Ex->isLValue())
|
|
|
|
return false;
|
|
|
|
return isa<DeclRefExpr>(Ex) ||
|
|
|
|
isa<MemberExpr>(Ex) ||
|
|
|
|
isa<ObjCIvarRefExpr>(Ex);
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:20:02 +08:00
|
|
|
bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
|
2013-02-26 05:32:40 +08:00
|
|
|
// First, we only consider nodes for reclamation of the following
|
|
|
|
// conditions apply:
|
2012-01-31 09:20:02 +08:00
|
|
|
//
|
|
|
|
// (1) 1 predecessor (that has one successor)
|
|
|
|
// (2) 1 successor (that has one predecessor)
|
2013-02-26 05:32:40 +08:00
|
|
|
//
|
|
|
|
// If a node has no successor it is on the "frontier", while a node
|
|
|
|
// with no predecessor is a root.
|
|
|
|
//
|
|
|
|
// After these prerequisites, we discard all "filler" nodes that
|
|
|
|
// are used only for intermediate processing, and are not essential
|
|
|
|
// for analyzer history:
|
|
|
|
//
|
|
|
|
// (a) PreStmtPurgeDeadSymbols
|
|
|
|
//
|
|
|
|
// We then discard all other nodes where *all* of the following conditions
|
|
|
|
// apply:
|
|
|
|
//
|
2012-10-30 01:31:53 +08:00
|
|
|
// (3) The ProgramPoint is for a PostStmt, but not a PostStore.
|
2012-01-31 09:20:02 +08:00
|
|
|
// (4) There is no 'tag' for the ProgramPoint.
|
|
|
|
// (5) The 'store' is the same as the predecessor.
|
|
|
|
// (6) The 'GDM' is the same as the predecessor.
|
|
|
|
// (7) The LocationContext is the same as the predecessor.
|
2013-02-24 15:21:01 +08:00
|
|
|
// (8) Expressions that are *not* lvalue expressions.
|
|
|
|
// (9) The PostStmt isn't for a non-consumed Stmt or Expr.
|
|
|
|
// (10) The successor is not a CallExpr StmtPoint (so that we would
|
|
|
|
// be able to find it when retrying a call with no inlining).
|
2012-07-11 06:07:42 +08:00
|
|
|
// FIXME: It may be safe to reclaim PreCall and PostCall nodes as well.
|
2012-01-31 09:20:02 +08:00
|
|
|
|
|
|
|
// Conditions 1 and 2.
|
|
|
|
if (node->pred_size() != 1 || node->succ_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ExplodedNode *pred = *(node->pred_begin());
|
|
|
|
if (pred->succ_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ExplodedNode *succ = *(node->succ_begin());
|
|
|
|
if (succ->pred_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
2013-02-26 05:32:40 +08:00
|
|
|
// Now reclaim any nodes that are (by definition) not essential to
|
|
|
|
// analysis history and are not consulted by any client code.
|
2012-01-31 09:20:02 +08:00
|
|
|
ProgramPoint progPoint = node->getLocation();
|
2013-02-26 05:32:40 +08:00
|
|
|
if (progPoint.getAs<PreStmtPurgeDeadSymbols>())
|
2013-02-27 09:26:58 +08:00
|
|
|
return !progPoint.getTag();
|
2013-02-26 05:32:40 +08:00
|
|
|
|
|
|
|
// Condition 3.
|
2013-02-22 06:23:56 +08:00
|
|
|
if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>())
|
2012-01-31 09:20:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Condition 4.
|
2013-03-28 01:36:01 +08:00
|
|
|
if (progPoint.getTag())
|
2012-01-31 09:20:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Conditions 5, 6, and 7.
|
|
|
|
ProgramStateRef state = node->getState();
|
|
|
|
ProgramStateRef pred_state = pred->getState();
|
|
|
|
if (state->store != pred_state->store || state->GDM != pred_state->GDM ||
|
|
|
|
progPoint.getLocationContext() != pred->getLocationContext())
|
|
|
|
return false;
|
2013-02-24 15:21:01 +08:00
|
|
|
|
2013-03-28 01:36:01 +08:00
|
|
|
// All further checks require expressions. As per #3, we know that we have
|
|
|
|
// a PostStmt.
|
|
|
|
const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt());
|
2013-02-25 15:37:13 +08:00
|
|
|
if (!Ex)
|
|
|
|
return false;
|
|
|
|
|
2012-01-31 09:20:02 +08:00
|
|
|
// Condition 8.
|
2013-02-25 15:37:13 +08:00
|
|
|
// Do not collect nodes for "interesting" lvalue expressions since they are
|
2013-02-24 15:21:01 +08:00
|
|
|
// used extensively for generating path diagnostics.
|
2013-02-25 15:37:13 +08:00
|
|
|
if (isInterestingLValueExpr(Ex))
|
2012-12-07 02:58:29 +08:00
|
|
|
return false;
|
|
|
|
|
2013-02-24 15:21:01 +08:00
|
|
|
// Condition 9.
|
|
|
|
// Do not collect nodes for non-consumed Stmt or Expr to ensure precise
|
|
|
|
// diagnostic generation; specifically, so that we could anchor arrows
|
|
|
|
// pointing to the beginning of statements (as written in code).
|
2012-12-07 02:58:29 +08:00
|
|
|
ParentMap &PM = progPoint.getLocationContext()->getParentMap();
|
|
|
|
if (!PM.isConsumedExpr(Ex))
|
2012-09-07 14:56:18 +08:00
|
|
|
return false;
|
2013-02-24 15:21:01 +08:00
|
|
|
|
|
|
|
// Condition 10.
|
2012-06-02 07:48:44 +08:00
|
|
|
const ProgramPoint SuccLoc = succ->getLocation();
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>())
|
2012-08-28 08:50:38 +08:00
|
|
|
if (CallEvent::isCallStmt(SP->getStmt()))
|
2012-06-02 07:48:44 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2012-01-31 09:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExplodedGraph::collectNode(ExplodedNode *node) {
|
|
|
|
// Removing a node means:
|
|
|
|
// (a) changing the predecessors successor to the successor of this node
|
|
|
|
// (b) changing the successors predecessor to the predecessor of this node
|
|
|
|
// (c) Putting 'node' onto freeNodes.
|
|
|
|
assert(node->pred_size() == 1 || node->succ_size() == 1);
|
|
|
|
ExplodedNode *pred = *(node->pred_begin());
|
|
|
|
ExplodedNode *succ = *(node->succ_begin());
|
|
|
|
pred->replaceSuccessor(succ);
|
|
|
|
succ->replacePredecessor(pred);
|
2012-02-17 04:19:25 +08:00
|
|
|
FreeNodes.push_back(node);
|
2012-01-31 09:20:02 +08:00
|
|
|
Nodes.RemoveNode(node);
|
|
|
|
--NumNodes;
|
|
|
|
node->~ExplodedNode();
|
|
|
|
}
|
|
|
|
|
2012-02-17 04:48:04 +08:00
|
|
|
void ExplodedGraph::reclaimRecentlyAllocatedNodes() {
|
2012-02-17 04:19:25 +08:00
|
|
|
if (ChangedNodes.empty())
|
2011-02-09 09:27:33 +08:00
|
|
|
return;
|
2012-01-25 08:35:05 +08:00
|
|
|
|
2012-10-24 07:59:05 +08:00
|
|
|
// Only periodically reclaim nodes so that we can build up a set of
|
2012-02-17 04:48:04 +08:00
|
|
|
// nodes that meet the reclamation criteria. Freshly created nodes
|
|
|
|
// by definition have no successor, and thus cannot be reclaimed (see below).
|
2012-10-24 07:59:05 +08:00
|
|
|
assert(ReclaimCounter > 0);
|
|
|
|
if (--ReclaimCounter != 0)
|
2012-02-17 04:48:04 +08:00
|
|
|
return;
|
2012-10-24 07:59:05 +08:00
|
|
|
ReclaimCounter = ReclaimNodeInterval;
|
2012-02-17 04:48:04 +08:00
|
|
|
|
|
|
|
for (NodeVector::iterator it = ChangedNodes.begin(), et = ChangedNodes.end();
|
2012-02-17 04:19:25 +08:00
|
|
|
it != et; ++it) {
|
|
|
|
ExplodedNode *node = *it;
|
2012-01-31 09:20:02 +08:00
|
|
|
if (shouldCollect(node))
|
|
|
|
collectNode(node);
|
2011-02-09 09:27:33 +08:00
|
|
|
}
|
2012-02-17 04:19:25 +08:00
|
|
|
ChangedNodes.clear();
|
2011-02-09 09:27:33 +08:00
|
|
|
}
|
|
|
|
|
2008-08-27 09:56:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-06 09:32:16 +08:00
|
|
|
// ExplodedNode.
|
2008-08-27 09:56:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-13 12:56:13 +08:00
|
|
|
|
2012-08-21 02:59:46 +08:00
|
|
|
// An NodeGroup's storage type is actually very much like a TinyPtrVector:
|
|
|
|
// it can be either a pointer to a single ExplodedNode, or a pointer to a
|
|
|
|
// BumpVector allocated with the ExplodedGraph's allocator. This allows the
|
|
|
|
// common case of single-node NodeGroups to be implemented with no extra memory.
|
|
|
|
//
|
|
|
|
// Consequently, each of the NodeGroup methods have up to four cases to handle:
|
|
|
|
// 1. The flag is set and this group does not actually contain any nodes.
|
|
|
|
// 2. The group is empty, in which case the storage value is null.
|
|
|
|
// 3. The group contains a single node.
|
|
|
|
// 4. The group contains more than one node.
|
2012-08-18 08:30:10 +08:00
|
|
|
typedef BumpVector<ExplodedNode *> ExplodedNodeVector;
|
|
|
|
typedef llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *> GroupStorage;
|
2008-01-13 12:56:13 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) {
|
2008-08-27 09:27:52 +08:00
|
|
|
assert (!V->isSink());
|
2009-10-07 08:42:52 +08:00
|
|
|
Preds.addNode(V, G);
|
|
|
|
V->Succs.addNode(this, G);
|
2008-08-27 09:56:11 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (NodeAuditor) NodeAuditor->AddEdge(V, this);
|
|
|
|
#endif
|
2008-08-27 09:27:52 +08:00
|
|
|
}
|
|
|
|
|
2011-02-09 09:27:33 +08:00
|
|
|
void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) {
|
2012-08-21 02:59:46 +08:00
|
|
|
assert(!getFlag());
|
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
|
|
|
|
assert(Storage.is<ExplodedNode *>());
|
|
|
|
Storage = node;
|
|
|
|
assert(Storage.is<ExplodedNode *>());
|
2011-02-09 09:27:33 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) {
|
2009-10-07 08:42:52 +08:00
|
|
|
assert(!getFlag());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
|
|
|
|
if (Storage.isNull()) {
|
|
|
|
Storage = N;
|
|
|
|
assert(Storage.is<ExplodedNode *>());
|
|
|
|
return;
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
2012-08-18 08:30:10 +08:00
|
|
|
|
|
|
|
ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>();
|
|
|
|
|
|
|
|
if (!V) {
|
|
|
|
// Switch from single-node to multi-node representation.
|
|
|
|
ExplodedNode *Old = Storage.get<ExplodedNode *>();
|
|
|
|
|
|
|
|
BumpVectorContext &Ctx = G.getNodeAllocator();
|
|
|
|
V = G.getAllocator().Allocate<ExplodedNodeVector>();
|
|
|
|
new (V) ExplodedNodeVector(Ctx, 4);
|
|
|
|
V->push_back(Old, Ctx);
|
|
|
|
|
|
|
|
Storage = V;
|
|
|
|
assert(!getFlag());
|
|
|
|
assert(Storage.is<ExplodedNodeVector *>());
|
2008-03-06 03:08:55 +08:00
|
|
|
}
|
2012-08-18 08:30:10 +08:00
|
|
|
|
|
|
|
V->push_back(N, G.getNodeAllocator());
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
unsigned ExplodedNode::NodeGroup::size() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
|
|
|
|
if (Storage.isNull())
|
|
|
|
return 0;
|
|
|
|
if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
|
|
|
|
return V->size();
|
|
|
|
return 1;
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
ExplodedNode * const *ExplodedNode::NodeGroup::begin() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
2012-08-18 08:30:10 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
|
|
|
|
if (Storage.isNull())
|
|
|
|
return 0;
|
|
|
|
if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
|
|
|
|
return V->begin();
|
|
|
|
return Storage.getAddrOfPtr1();
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
2012-08-18 08:30:10 +08:00
|
|
|
ExplodedNode * const *ExplodedNode::NodeGroup::end() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
2012-08-18 08:30:10 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
|
|
|
|
if (Storage.isNull())
|
|
|
|
return 0;
|
|
|
|
if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
|
|
|
|
return V->end();
|
|
|
|
return Storage.getAddrOfPtr1() + 1;
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State,
|
2011-11-02 06:41:19 +08:00
|
|
|
bool IsSink,
|
|
|
|
bool* IsNew) {
|
2009-08-06 14:28:40 +08:00
|
|
|
// Profile 'State' to determine if we already have an existing node.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::FoldingSetNodeID profile;
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-11-02 06:41:19 +08:00
|
|
|
NodeTy::Profile(profile, L, State, IsSink);
|
2009-08-06 14:28:40 +08:00
|
|
|
NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 14:28:40 +08:00
|
|
|
if (!V) {
|
2012-02-17 04:19:25 +08:00
|
|
|
if (!FreeNodes.empty()) {
|
|
|
|
V = FreeNodes.back();
|
|
|
|
FreeNodes.pop_back();
|
2011-02-09 09:27:33 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Allocate a new node.
|
|
|
|
V = (NodeTy*) getAllocator().Allocate<NodeTy>();
|
|
|
|
}
|
|
|
|
|
2011-11-02 06:41:19 +08:00
|
|
|
new (V) NodeTy(L, State, IsSink);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-24 07:59:05 +08:00
|
|
|
if (ReclaimNodeInterval)
|
2012-02-17 04:48:04 +08:00
|
|
|
ChangedNodes.push_back(V);
|
|
|
|
|
2009-08-06 14:28:40 +08:00
|
|
|
// Insert the node into the node set and return it.
|
|
|
|
Nodes.InsertNode(V, InsertPos);
|
|
|
|
++NumNodes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 14:28:40 +08:00
|
|
|
if (IsNew) *IsNew = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (IsNew) *IsNew = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 14:28:40 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2013-03-16 09:07:53 +08:00
|
|
|
ExplodedGraph *
|
2013-03-23 05:15:33 +08:00
|
|
|
ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
|
2013-03-16 09:07:53 +08:00
|
|
|
InterExplodedGraphMap *ForwardMap,
|
|
|
|
InterExplodedGraphMap *InverseMap) const{
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-16 09:07:53 +08:00
|
|
|
if (Nodes.empty())
|
|
|
|
return 0;
|
2009-08-06 14:28:40 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
|
2008-03-08 06:58:01 +08:00
|
|
|
Pass1Ty Pass1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-16 09:07:53 +08:00
|
|
|
typedef InterExplodedGraphMap Pass2Ty;
|
|
|
|
InterExplodedGraphMap Pass2Scratch;
|
|
|
|
Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const ExplodedNode*, 10> WL1, WL2;
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-03-13 07:41:59 +08:00
|
|
|
// ===- Pass 1 (reverse DFS) -===
|
2013-03-16 09:07:53 +08:00
|
|
|
for (ArrayRef<const NodeTy *>::iterator I = Sinks.begin(), E = Sinks.end();
|
|
|
|
I != E; ++I) {
|
2012-09-22 09:24:53 +08:00
|
|
|
if (*I)
|
|
|
|
WL1.push_back(*I);
|
2009-02-21 05:10:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-16 09:07:53 +08:00
|
|
|
// Process the first worklist until it is empty.
|
2009-02-21 05:10:26 +08:00
|
|
|
while (!WL1.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
const ExplodedNode *N = WL1.pop_back_val();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Have we already visited this node? If so, continue to the next one.
|
|
|
|
if (Pass1.count(N))
|
|
|
|
continue;
|
2009-01-24 08:55:43 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Otherwise, mark this node as visited.
|
|
|
|
Pass1.insert(N);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// If this is a root enqueue it to the second worklist.
|
|
|
|
if (N->Preds.empty()) {
|
|
|
|
WL2.push_back(N);
|
|
|
|
continue;
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Visit our predecessors and enqueue them.
|
2012-08-18 08:30:10 +08:00
|
|
|
for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
|
|
|
|
I != E; ++I)
|
2009-03-13 07:41:59 +08:00
|
|
|
WL1.push_back(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// We didn't hit a root? Return with a null pointer for the new graph.
|
2008-03-08 06:58:01 +08:00
|
|
|
if (WL2.empty())
|
2009-02-05 07:49:09 +08:00
|
|
|
return 0;
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Create an empty graph.
|
2009-08-06 14:28:40 +08:00
|
|
|
ExplodedGraph* G = MakeEmptyGraph();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// ===- Pass 2 (forward DFS to construct the new graph) -===
|
2008-03-08 06:58:01 +08:00
|
|
|
while (!WL2.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
const ExplodedNode *N = WL2.pop_back_val();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
// Skip this node if we have already processed it.
|
|
|
|
if (Pass2.find(N) != Pass2.end())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Create the corresponding node in the new graph and record the mapping
|
|
|
|
// from the old node to the new node.
|
2011-11-02 06:41:19 +08:00
|
|
|
ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(), 0);
|
2008-03-08 06:58:01 +08:00
|
|
|
Pass2[N] = NewN;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Also record the reverse mapping from the new node to the old node.
|
2009-02-18 11:48:14 +08:00
|
|
|
if (InverseMap) (*InverseMap)[NewN] = N;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// If this node is a root, designate it as such in the graph.
|
2008-03-08 06:58:01 +08:00
|
|
|
if (N->Preds.empty())
|
|
|
|
G->addRoot(NewN);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
// In the case that some of the intended predecessors of NewN have already
|
|
|
|
// been created, we should hook them up as predecessors.
|
2009-02-21 05:10:26 +08:00
|
|
|
|
|
|
|
// Walk through the predecessors of 'N' and hook up their corresponding
|
|
|
|
// nodes in the new graph (if any) to the freshly created node.
|
2012-08-18 08:30:10 +08:00
|
|
|
for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
|
|
|
|
I != E; ++I) {
|
2008-03-08 06:58:01 +08:00
|
|
|
Pass2Ty::iterator PI = Pass2.find(*I);
|
|
|
|
if (PI == Pass2.end())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-16 09:07:53 +08:00
|
|
|
NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// In the case that some of the intended successors of NewN have already
|
|
|
|
// been created, we should hook them up as successors. Otherwise, enqueue
|
|
|
|
// the new nodes from the original graph that should have nodes created
|
|
|
|
// in the new graph.
|
2012-08-18 08:30:10 +08:00
|
|
|
for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
|
|
|
|
I != E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Pass2Ty::iterator PI = Pass2.find(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
if (PI != Pass2.end()) {
|
2013-03-23 05:15:33 +08:00
|
|
|
const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
|
2008-03-08 06:58:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue nodes to the worklist that were marked during pass 1.
|
2009-02-21 05:10:26 +08:00
|
|
|
if (Pass1.count(*I))
|
|
|
|
WL2.push_back(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
return G;
|
|
|
|
}
|
2009-02-05 07:49:09 +08:00
|
|
|
|