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" |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.h" |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 20 | #include <vector> |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 21 | #include <list> |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 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. |
| 30 | ExplodedNodeImpl::Auditor::~Auditor() {} |
| 31 | |
| 32 | #ifndef NDEBUG |
| 33 | static ExplodedNodeImpl::Auditor* NodeAuditor = 0; |
| 34 | #endif |
| 35 | |
| 36 | void ExplodedNodeImpl::SetAuditor(ExplodedNodeImpl::Auditor* A) { |
| 37 | #ifndef NDEBUG |
| 38 | NodeAuditor = A; |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // ExplodedNodeImpl. |
| 44 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 45 | |
| 46 | static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) { |
| 47 | return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P); |
| 48 | } |
| 49 | |
Ted Kremenek | 45b8789 | 2008-08-27 01:27:52 +0000 | [diff] [blame] | 50 | void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) { |
| 51 | assert (!V->isSink()); |
| 52 | Preds.addNode(V); |
| 53 | V->Succs.addNode(this); |
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 | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 59 | void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) { |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 60 | |
| 61 | assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 62 | assert (!getFlag()); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 64 | if (getKind() == Size1) { |
| 65 | if (ExplodedNodeImpl* NOld = getNode()) { |
| 66 | std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>(); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 67 | assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 68 | V->push_back(NOld); |
| 69 | V->push_back(N); |
Ted Kremenek | 45c63bd | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 70 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 71 | assert (getPtr() == (void*) V); |
| 72 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 73 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 74 | else { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 75 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 76 | assert (getKind() == Size1); |
| 77 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 78 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 79 | else { |
| 80 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 81 | getVector(getPtr()).push_back(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 85 | |
| 86 | unsigned ExplodedNodeImpl::NodeGroup::size() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 87 | if (getFlag()) |
| 88 | return 0; |
| 89 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 90 | if (getKind() == Size1) |
| 91 | return getNode() ? 1 : 0; |
| 92 | else |
| 93 | return getVector(getPtr()).size(); |
| 94 | } |
| 95 | |
| 96 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 97 | if (getFlag()) |
| 98 | return NULL; |
| 99 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 100 | if (getKind() == Size1) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 101 | return (ExplodedNodeImpl**) (getPtr() ? &P : NULL); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 102 | else |
| 103 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin())); |
| 104 | } |
| 105 | |
| 106 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 107 | if (getFlag()) |
| 108 | return NULL; |
| 109 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 110 | if (getKind() == Size1) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 111 | return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL); |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 112 | else { |
| 113 | // Dereferencing end() is undefined behaviour. The vector is not empty, so |
Argyrios Kyrtzidis | 5fc073f | 2008-04-22 07:37:18 +0000 | [diff] [blame] | 114 | // we can dereference the last elem and then add 1 to the result. |
| 115 | return const_cast<ExplodedNodeImpl**>(&getVector(getPtr()).back()) + 1; |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 116 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | ExplodedNodeImpl::NodeGroup::~NodeGroup() { |
| 120 | if (getKind() == SizeOther) delete &getVector(getPtr()); |
| 121 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 123 | ExplodedGraphImpl* |
| 124 | ExplodedGraphImpl::Trim(const ExplodedNodeImpl* const* BeginSources, |
| 125 | const ExplodedNodeImpl* const* EndSources) const{ |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 127 | typedef llvm::DenseMap<const ExplodedNodeImpl*, const ExplodedNodeImpl*> Pass1Ty; |
| 128 | typedef llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 129 | |
| 130 | Pass1Ty Pass1; |
| 131 | Pass2Ty Pass2; |
| 132 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 133 | llvm::SmallVector<const ExplodedNodeImpl*, 10> WL2; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 134 | |
| 135 | { // ===- Pass 1 (reverse BFS) -=== |
| 136 | |
| 137 | // Enqueue the source nodes to the first worklist. |
| 138 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 139 | std::list<std::pair<const ExplodedNodeImpl*, |
| 140 | const ExplodedNodeImpl*> > WL1, WL1_Loops; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 142 | for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I) |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 143 | WL1.push_back(std::make_pair(*I, *I)); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 144 | |
| 145 | // Process the worklist. |
| 146 | |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 147 | while (! (WL1.empty() && WL1_Loops.empty())) { |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 148 | // Only dequeue from the "loops" worklist if WL1 has no items. |
| 149 | // Thus we prioritize for paths that don't span loop boundaries. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 150 | const ExplodedNodeImpl *N, *Src; |
| 151 | |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 152 | if (WL1.empty()) { |
| 153 | N = WL1_Loops.back().first; |
| 154 | Src = WL1_Loops.back().second; |
| 155 | WL1_Loops.pop_back(); |
| 156 | } |
| 157 | else { |
| 158 | N = WL1.back().first; |
| 159 | Src = WL1.back().second; |
| 160 | WL1.pop_back(); |
| 161 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 163 | if (Pass1.find(N) != Pass1.end()) |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 164 | continue; |
| 165 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 166 | bool PredHasSameSource = false; |
| 167 | bool VisitPreds = true; |
| 168 | |
| 169 | for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); |
| 170 | I!=E; ++I) { |
| 171 | |
| 172 | Pass1Ty::iterator pi = Pass1.find(*I); |
| 173 | |
| 174 | if (pi == Pass1.end()) |
| 175 | continue; |
| 176 | |
| 177 | VisitPreds = false; |
| 178 | |
| 179 | if (pi->second == Src) { |
| 180 | PredHasSameSource = true; |
| 181 | break; |
| 182 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 185 | if (VisitPreds || !PredHasSameSource) { |
| 186 | |
| 187 | Pass1[N] = Src; |
| 188 | |
| 189 | if (N->Preds.empty()) { |
| 190 | WL2.push_back(N); |
| 191 | continue; |
| 192 | } |
| 193 | } |
| 194 | else |
| 195 | Pass1[N] = NULL; |
| 196 | |
| 197 | if (VisitPreds) |
| 198 | for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 199 | I!=E; ++I) { |
| 200 | |
| 201 | ProgramPoint P = Src->getLocation(); |
| 202 | |
| 203 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) |
| 204 | if (Stmt* T = BE->getSrc()->getTerminator()) |
| 205 | switch (T->getStmtClass()) { |
| 206 | default: break; |
| 207 | case Stmt::ForStmtClass: |
| 208 | case Stmt::WhileStmtClass: |
| 209 | case Stmt::DoStmtClass: |
| 210 | WL1_Loops.push_front(std::make_pair(*I, Src)); |
| 211 | continue; |
| 212 | |
| 213 | } |
| 214 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 215 | WL1.push_front(std::make_pair(*I, Src)); |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 216 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | if (WL2.empty()) |
| 221 | return NULL; |
| 222 | |
| 223 | ExplodedGraphImpl* G = MakeEmptyGraph(); |
| 224 | |
| 225 | // ===- Pass 2 (forward DFS to construct the new graph) -=== |
| 226 | |
| 227 | while (!WL2.empty()) { |
| 228 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame^] | 229 | const ExplodedNodeImpl* N = WL2.back(); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 230 | WL2.pop_back(); |
| 231 | |
| 232 | // Skip this node if we have already processed it. |
| 233 | |
| 234 | if (Pass2.find(N) != Pass2.end()) |
| 235 | continue; |
| 236 | |
| 237 | // Create the corresponding node in the new graph. |
| 238 | |
| 239 | ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL); |
| 240 | Pass2[N] = NewN; |
| 241 | |
| 242 | if (N->Preds.empty()) |
| 243 | G->addRoot(NewN); |
| 244 | |
| 245 | // In the case that some of the intended predecessors of NewN have already |
| 246 | // been created, we should hook them up as predecessors. |
| 247 | |
| 248 | for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { |
| 249 | |
| 250 | Pass2Ty::iterator PI = Pass2.find(*I); |
| 251 | |
| 252 | if (PI == Pass2.end()) |
| 253 | continue; |
| 254 | |
| 255 | NewN->addPredecessor(PI->second); |
| 256 | } |
| 257 | |
| 258 | // In the case that some of the intended successors of NewN have already |
| 259 | // been created, we should hook them up as successors. Otherwise, enqueue |
| 260 | // the new nodes from the original graph that should have nodes created |
| 261 | // in the new graph. |
| 262 | |
| 263 | for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { |
| 264 | |
| 265 | Pass2Ty::iterator PI = Pass2.find(*I); |
| 266 | |
| 267 | if (PI != Pass2.end()) { |
| 268 | PI->second->addPredecessor(NewN); |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | // Enqueue nodes to the worklist that were marked during pass 1. |
| 273 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 274 | Pass1Ty::iterator pi = Pass1.find(*I); |
| 275 | |
| 276 | if (pi == Pass1.end() || pi->second == NULL) |
| 277 | continue; |
| 278 | |
| 279 | WL2.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (N->isSink()) |
| 283 | NewN->markAsSink(); |
| 284 | } |
| 285 | |
| 286 | return G; |
| 287 | } |