Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 1 | //=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the template classes ExplodedNode and ExplodedGraph, |
| 11 | // which represent a path-sensitive, intra-procedural "exploded graph." |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/ExplodedGraph.h" |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Node auditing. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | // An out of line virtual method to provide a home for the class vtable. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 30 | ExplodedNode::Auditor::~Auditor() {} |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 31 | |
| 32 | #ifndef NDEBUG |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 33 | static ExplodedNode::Auditor* NodeAuditor = 0; |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 36 | void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 37 | #ifndef NDEBUG |
| 38 | NodeAuditor = A; |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 43 | // ExplodedNode. |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 46 | static inline BumpVector<ExplodedNode*>& getVector(void* P) { |
| 47 | return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 50 | void ExplodedNode::addPredecessor(ExplodedNode* V, ExplodedGraph &G) { |
Ted Kremenek | 45b8789 | 2008-08-27 01:27:52 +0000 | [diff] [blame] | 51 | assert (!V->isSink()); |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 52 | Preds.addNode(V, G); |
| 53 | V->Succs.addNode(this, G); |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 54 | #ifndef NDEBUG |
| 55 | if (NodeAuditor) NodeAuditor->AddEdge(V, this); |
| 56 | #endif |
Ted Kremenek | 45b8789 | 2008-08-27 01:27:52 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 59 | void ExplodedNode::NodeGroup::addNode(ExplodedNode* N, ExplodedGraph &G) { |
| 60 | assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
| 61 | assert(!getFlag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 63 | if (getKind() == Size1) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 64 | if (ExplodedNode* NOld = getNode()) { |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 65 | BumpVectorContext &Ctx = G.getNodeAllocator(); |
| 66 | BumpVector<ExplodedNode*> *V = |
| 67 | G.getAllocator().Allocate<BumpVector<ExplodedNode*> >(); |
| 68 | new (V) BumpVector<ExplodedNode*>(Ctx, 4); |
| 69 | |
| 70 | assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
| 71 | V->push_back(NOld, Ctx); |
| 72 | V->push_back(N, Ctx); |
Ted Kremenek | 45c63bd | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 73 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 74 | assert(getPtr() == (void*) V); |
| 75 | assert(getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 76 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 77 | else { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 78 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 79 | assert(getKind() == Size1); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 81 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 82 | else { |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 83 | assert(getKind() == SizeOther); |
| 84 | getVector(getPtr()).push_back(N, G.getNodeAllocator()); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 85 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 88 | unsigned ExplodedNode::NodeGroup::size() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 89 | if (getFlag()) |
| 90 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 92 | if (getKind() == Size1) |
| 93 | return getNode() ? 1 : 0; |
| 94 | else |
| 95 | return getVector(getPtr()).size(); |
| 96 | } |
| 97 | |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 98 | ExplodedNode **ExplodedNode::NodeGroup::begin() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 99 | if (getFlag()) |
| 100 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 102 | if (getKind() == Size1) |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 103 | return (ExplodedNode**) (getPtr() ? &P : NULL); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 104 | else |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 105 | return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin())); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 108 | ExplodedNode** ExplodedNode::NodeGroup::end() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 109 | if (getFlag()) |
| 110 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 112 | if (getKind() == Size1) |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 113 | return (ExplodedNode**) (getPtr() ? &P+1 : NULL); |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 114 | else { |
| 115 | // Dereferencing end() is undefined behaviour. The vector is not empty, so |
Argyrios Kyrtzidis | 5fc073f | 2008-04-22 07:37:18 +0000 | [diff] [blame] | 116 | // we can dereference the last elem and then add 1 to the result. |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 117 | return const_cast<ExplodedNode**>(getVector(getPtr()).end()); |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 118 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | ExplodedNode *ExplodedGraph::getNode(const ProgramPoint& L, |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 122 | const GRState* State, bool* IsNew) { |
| 123 | // Profile 'State' to determine if we already have an existing node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | llvm::FoldingSetNodeID profile; |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 125 | void* InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 127 | NodeTy::Profile(profile, L, State); |
| 128 | NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 130 | if (!V) { |
| 131 | // Allocate a new node. |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 132 | V = (NodeTy*) getAllocator().Allocate<NodeTy>(); |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 133 | new (V) NodeTy(L, State); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 135 | // Insert the node into the node set and return it. |
| 136 | Nodes.InsertNode(V, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 138 | ++NumNodes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 140 | if (IsNew) *IsNew = true; |
| 141 | } |
| 142 | else |
| 143 | if (IsNew) *IsNew = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 145 | return V; |
| 146 | } |
| 147 | |
| 148 | std::pair<ExplodedGraph*, InterExplodedGraphMap*> |
| 149 | ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, |
| 150 | llvm::DenseMap<const void*, const void*> *InverseMap) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 152 | if (NBeg == NEnd) |
| 153 | return std::make_pair((ExplodedGraph*) 0, |
| 154 | (InterExplodedGraphMap*) 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 156 | assert (NBeg < NEnd); |
| 157 | |
| 158 | llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 160 | ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 162 | return std::make_pair(static_cast<ExplodedGraph*>(G), M.take()); |
| 163 | } |
| 164 | |
| 165 | ExplodedGraph* |
| 166 | ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources, |
| 167 | const ExplodedNode* const* EndSources, |
| 168 | InterExplodedGraphMap* M, |
| 169 | llvm::DenseMap<const void*, const void*> *InverseMap) const { |
| 170 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 171 | typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 172 | Pass1Ty Pass1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 174 | typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 175 | Pass2Ty& Pass2 = M->M; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 177 | llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 179 | // ===- Pass 1 (reverse DFS) -=== |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 180 | for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) { |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 181 | assert(*I); |
| 182 | WL1.push_back(*I); |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 185 | // Process the first worklist until it is empty. Because it is a std::list |
| 186 | // it acts like a FIFO queue. |
| 187 | while (!WL1.empty()) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 188 | const ExplodedNode *N = WL1.back(); |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 189 | WL1.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 191 | // Have we already visited this node? If so, continue to the next one. |
| 192 | if (Pass1.count(N)) |
| 193 | continue; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 195 | // Otherwise, mark this node as visited. |
| 196 | Pass1.insert(N); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 198 | // If this is a root enqueue it to the second worklist. |
| 199 | if (N->Preds.empty()) { |
| 200 | WL2.push_back(N); |
| 201 | continue; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 204 | // Visit our predecessors and enqueue them. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 205 | for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 206 | WL1.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 209 | // We didn't hit a root? Return with a null pointer for the new graph. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 210 | if (WL2.empty()) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 211 | return 0; |
| 212 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 213 | // Create an empty graph. |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 214 | ExplodedGraph* G = MakeEmptyGraph(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
| 216 | // ===- Pass 2 (forward DFS to construct the new graph) -=== |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 217 | while (!WL2.empty()) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 218 | const ExplodedNode* N = WL2.back(); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 219 | WL2.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 221 | // Skip this node if we have already processed it. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 222 | if (Pass2.find(N) != Pass2.end()) |
| 223 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 225 | // Create the corresponding node in the new graph and record the mapping |
| 226 | // from the old node to the new node. |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 227 | ExplodedNode* NewN = G->getNode(N->getLocation(), N->State, NULL); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 228 | Pass2[N] = NewN; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 230 | // Also record the reverse mapping from the new node to the old node. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 231 | if (InverseMap) (*InverseMap)[NewN] = N; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 233 | // If this node is a root, designate it as such in the graph. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 234 | if (N->Preds.empty()) |
| 235 | G->addRoot(NewN); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 237 | // In the case that some of the intended predecessors of NewN have already |
| 238 | // been created, we should hook them up as predecessors. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 240 | // Walk through the predecessors of 'N' and hook up their corresponding |
| 241 | // nodes in the new graph (if any) to the freshly created node. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 242 | for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 243 | Pass2Ty::iterator PI = Pass2.find(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 244 | if (PI == Pass2.end()) |
| 245 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 247 | NewN->addPredecessor(PI->second, *G); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // In the case that some of the intended successors of NewN have already |
| 251 | // been created, we should hook them up as successors. Otherwise, enqueue |
| 252 | // the new nodes from the original graph that should have nodes created |
| 253 | // in the new graph. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 254 | for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | Pass2Ty::iterator PI = Pass2.find(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 256 | if (PI != Pass2.end()) { |
Ted Kremenek | 5fe4d9d | 2009-10-07 00:42:52 +0000 | [diff] [blame^] | 257 | PI->second->addPredecessor(NewN, *G); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | |
| 261 | // Enqueue nodes to the worklist that were marked during pass 1. |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 262 | if (Pass1.count(*I)) |
| 263 | WL2.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 266 | // Finally, explictly mark all nodes without any successors as sinks. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 267 | if (N->isSink()) |
| 268 | NewN->markAsSink(); |
| 269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 271 | return G; |
| 272 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 273 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 274 | ExplodedNode* |
Zhongxing Xu | 38b02b9 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 275 | InterExplodedGraphMap::getMappedNode(const ExplodedNode* N) const { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 276 | llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::iterator I = |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 277 | M.find(N); |
| 278 | |
| 279 | return I == M.end() ? 0 : I->second; |
| 280 | } |
| 281 | |