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