blob: fe9cecadf3d929956c8c458e50896ce0999273b3 [file] [log] [blame]
Ted Kremenekf7c3bec2008-01-13 04:56:13 +00001//=-- 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 Kremenek31aeb1e2008-04-16 15:51:26 +000016#include "clang/AST/Stmt.h"
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000017#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000020#include <vector>
Ted Kremenek83f04aa2008-03-12 17:18:20 +000021#include <list>
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000022
23using namespace clang;
24
Ted Kremeneke9772a22008-08-27 01:56:11 +000025//===----------------------------------------------------------------------===//
26// Node auditing.
27//===----------------------------------------------------------------------===//
28
29// An out of line virtual method to provide a home for the class vtable.
30ExplodedNodeImpl::Auditor::~Auditor() {}
31
32#ifndef NDEBUG
33static ExplodedNodeImpl::Auditor* NodeAuditor = 0;
34#endif
35
36void ExplodedNodeImpl::SetAuditor(ExplodedNodeImpl::Auditor* A) {
37#ifndef NDEBUG
38 NodeAuditor = A;
39#endif
40}
41
42//===----------------------------------------------------------------------===//
43// ExplodedNodeImpl.
44//===----------------------------------------------------------------------===//
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000045
46static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
47 return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
48}
49
Ted Kremenek0bd7ddd2008-08-27 01:27:52 +000050void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) {
51 assert (!V->isSink());
52 Preds.addNode(V);
53 V->Succs.addNode(this);
Ted Kremeneke9772a22008-08-27 01:56:11 +000054#ifndef NDEBUG
55 if (NodeAuditor) NodeAuditor->AddEdge(V, this);
56#endif
Ted Kremenek0bd7ddd2008-08-27 01:27:52 +000057}
58
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000059void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek399e9012008-03-05 19:08:55 +000060
61 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000062 assert (!getFlag());
Ted Kremenek399e9012008-03-05 19:08:55 +000063
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000064 if (getKind() == Size1) {
65 if (ExplodedNodeImpl* NOld = getNode()) {
66 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek399e9012008-03-05 19:08:55 +000067 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000068 V->push_back(NOld);
69 V->push_back(N);
Ted Kremenek4ecd9bb2008-01-29 23:31:09 +000070 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek399e9012008-03-05 19:08:55 +000071 assert (getPtr() == (void*) V);
72 assert (getKind() == SizeOther);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000073 }
Ted Kremenek399e9012008-03-05 19:08:55 +000074 else {
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000075 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek399e9012008-03-05 19:08:55 +000076 assert (getKind() == Size1);
77 }
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000078 }
Ted Kremenek399e9012008-03-05 19:08:55 +000079 else {
80 assert (getKind() == SizeOther);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000081 getVector(getPtr()).push_back(N);
Ted Kremenek399e9012008-03-05 19:08:55 +000082 }
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000083}
84
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000085
86unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000087 if (getFlag())
88 return 0;
89
Ted Kremenekf7c3bec2008-01-13 04:56:13 +000090 if (getKind() == Size1)
91 return getNode() ? 1 : 0;
92 else
93 return getVector(getPtr()).size();
94}
95
96ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +000097 if (getFlag())
98 return NULL;
99
Ted Kremenekf7c3bec2008-01-13 04:56:13 +0000100 if (getKind() == Size1)
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000101 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenekf7c3bec2008-01-13 04:56:13 +0000102 else
103 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
104}
105
106ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000107 if (getFlag())
108 return NULL;
109
Ted Kremenekf7c3bec2008-01-13 04:56:13 +0000110 if (getKind() == Size1)
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000111 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenekd1b6ef62008-04-20 23:54:24 +0000112 else {
113 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argiris Kirtzidisefb4bd02008-04-22 07:37:18 +0000114 // we can dereference the last elem and then add 1 to the result.
115 return const_cast<ExplodedNodeImpl**>(&getVector(getPtr()).back()) + 1;
Ted Kremenekd1b6ef62008-04-20 23:54:24 +0000116 }
Ted Kremenekf7c3bec2008-01-13 04:56:13 +0000117}
118
119ExplodedNodeImpl::NodeGroup::~NodeGroup() {
120 if (getKind() == SizeOther) delete &getVector(getPtr());
121}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000122
Ted Kremenek3f6c6802009-01-24 00:55:43 +0000123ExplodedGraphImpl*
124ExplodedGraphImpl::Trim(const ExplodedNodeImpl* const* BeginSources,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000125 const ExplodedNodeImpl* const* EndSources,
Ted Kremenekc26c4692009-02-18 03:48:14 +0000126 InterExplodedGraphMapImpl* M,
127 llvm::DenseMap<const void*, const void*> *InverseMap)
128const {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000129
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000130 typedef llvm::DenseMap<const ExplodedNodeImpl*,
131 const ExplodedNodeImpl*> Pass1Ty;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000132 Pass1Ty Pass1;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000133
134 typedef llvm::DenseMap<const ExplodedNodeImpl*,
135 ExplodedNodeImpl*> Pass2Ty;
136
137 Pass2Ty& Pass2 = M->M;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000138
Ted Kremenek3f6c6802009-01-24 00:55:43 +0000139 llvm::SmallVector<const ExplodedNodeImpl*, 10> WL2;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000140
141 { // ===- Pass 1 (reverse BFS) -===
142
143 // Enqueue the source nodes to the first worklist.
144
Ted Kremenek3f6c6802009-01-24 00:55:43 +0000145 std::list<std::pair<const ExplodedNodeImpl*,
146 const ExplodedNodeImpl*> > WL1, WL1_Loops;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000147
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000148 for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I){
149 assert(*I);
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000150 WL1.push_back(std::make_pair(*I, *I));
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000151 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000152
153 // Process the worklist.
154
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000155 while (!WL1.empty() || !WL1_Loops.empty()) {
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000156 // Only dequeue from the "loops" worklist if WL1 has no items.
157 // Thus we prioritize for paths that don't span loop boundaries.
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000158 const ExplodedNodeImpl *N = 0, *Src = 0;
Ted Kremenek3f6c6802009-01-24 00:55:43 +0000159
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000160 if (WL1.empty()) {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000161 assert(!WL1_Loops.empty());
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000162 N = WL1_Loops.back().first;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000163 assert(N);
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000164 Src = WL1_Loops.back().second;
165 WL1_Loops.pop_back();
166 }
167 else {
168 N = WL1.back().first;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000169 assert(N);
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000170 Src = WL1.back().second;
171 WL1.pop_back();
172 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000173
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000174 if (Pass1.find(N) != Pass1.end())
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000175 continue;
176
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000177 bool PredHasSameSource = false;
178 bool VisitPreds = true;
179
180 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
181 I!=E; ++I) {
182
183 Pass1Ty::iterator pi = Pass1.find(*I);
184
185 if (pi == Pass1.end())
186 continue;
187
188 VisitPreds = false;
189
190 if (pi->second == Src) {
191 PredHasSameSource = true;
192 break;
193 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000194 }
195
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000196 if (VisitPreds || !PredHasSameSource) {
197
198 Pass1[N] = Src;
199
200 if (N->Preds.empty()) {
201 WL2.push_back(N);
202 continue;
203 }
204 }
205 else
206 Pass1[N] = NULL;
207
208 if (VisitPreds)
209 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000210 I!=E; ++I) {
211
212 ProgramPoint P = Src->getLocation();
213
214 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
215 if (Stmt* T = BE->getSrc()->getTerminator())
216 switch (T->getStmtClass()) {
217 default: break;
218 case Stmt::ForStmtClass:
219 case Stmt::WhileStmtClass:
220 case Stmt::DoStmtClass:
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000221 assert(*I);
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000222 WL1_Loops.push_front(std::make_pair(*I, Src));
223 continue;
224
225 }
226
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000227 assert(*I);
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000228 WL1.push_front(std::make_pair(*I, Src));
Ted Kremenek31aeb1e2008-04-16 15:51:26 +0000229 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000230 }
231 }
232
233 if (WL2.empty())
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000234 return 0;
235
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000236 ExplodedGraphImpl* G = MakeEmptyGraph();
237
238 // ===- Pass 2 (forward DFS to construct the new graph) -===
239
240 while (!WL2.empty()) {
241
Ted Kremenek3f6c6802009-01-24 00:55:43 +0000242 const ExplodedNodeImpl* N = WL2.back();
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000243 WL2.pop_back();
244
245 // Skip this node if we have already processed it.
246
247 if (Pass2.find(N) != Pass2.end())
248 continue;
249
250 // Create the corresponding node in the new graph.
251
252 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
253 Pass2[N] = NewN;
Ted Kremenekc26c4692009-02-18 03:48:14 +0000254 if (InverseMap) (*InverseMap)[NewN] = N;
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000255
256 if (N->Preds.empty())
257 G->addRoot(NewN);
258
259 // In the case that some of the intended predecessors of NewN have already
260 // been created, we should hook them up as predecessors.
261
262 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
263
264 Pass2Ty::iterator PI = Pass2.find(*I);
265
266 if (PI == Pass2.end())
267 continue;
268
269 NewN->addPredecessor(PI->second);
270 }
271
272 // In the case that some of the intended successors of NewN have already
273 // been created, we should hook them up as successors. Otherwise, enqueue
274 // the new nodes from the original graph that should have nodes created
275 // in the new graph.
276
277 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
278
279 Pass2Ty::iterator PI = Pass2.find(*I);
280
281 if (PI != Pass2.end()) {
282 PI->second->addPredecessor(NewN);
283 continue;
284 }
285
286 // Enqueue nodes to the worklist that were marked during pass 1.
287
Ted Kremenek83f04aa2008-03-12 17:18:20 +0000288 Pass1Ty::iterator pi = Pass1.find(*I);
289
290 if (pi == Pass1.end() || pi->second == NULL)
291 continue;
292
293 WL2.push_back(*I);
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000294 }
295
296 if (N->isSink())
297 NewN->markAsSink();
298 }
299
300 return G;
301}
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000302
303ExplodedNodeImpl*
304InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNodeImpl* N) const {
305 llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*>::iterator I =
306 M.find(N);
307
308 return I == M.end() ? 0 : I->second;
309}
310
311InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {}
312