blob: 945416b1b56e882cc38c9dde5a63360cde3084be [file] [log] [blame]
Ted Kremenek9eb49a42008-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 Kremenek33c63692008-04-16 15:51:26 +000016#include "clang/AST/Stmt.h"
Ted Kremenekffe0f432008-03-07 22:58:01 +000017#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
Ted Kremenek9eb49a42008-01-13 04:56:13 +000020#include <vector>
Ted Kremenek7ec07fd2008-03-12 17:18:20 +000021#include <list>
Ted Kremenek9eb49a42008-01-13 04:56:13 +000022
23using namespace clang;
24
Ted Kremenek2e287542008-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 Kremenek9eb49a42008-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 Kremenek45b87892008-08-27 01:27:52 +000050void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) {
51 assert (!V->isSink());
52 Preds.addNode(V);
53 V->Succs.addNode(this);
Ted Kremenek2e287542008-08-27 01:56:11 +000054#ifndef NDEBUG
55 if (NodeAuditor) NodeAuditor->AddEdge(V, this);
56#endif
Ted Kremenek45b87892008-08-27 01:27:52 +000057}
58
Ted Kremenek9eb49a42008-01-13 04:56:13 +000059void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek596f0a12008-03-05 19:08:55 +000060
61 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenekffe0f432008-03-07 22:58:01 +000062 assert (!getFlag());
Ted Kremenek596f0a12008-03-05 19:08:55 +000063
Ted Kremenek9eb49a42008-01-13 04:56:13 +000064 if (getKind() == Size1) {
65 if (ExplodedNodeImpl* NOld = getNode()) {
66 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek596f0a12008-03-05 19:08:55 +000067 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000068 V->push_back(NOld);
69 V->push_back(N);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000070 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek596f0a12008-03-05 19:08:55 +000071 assert (getPtr() == (void*) V);
72 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000073 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000074 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000075 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000076 assert (getKind() == Size1);
77 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000078 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000079 else {
80 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000081 getVector(getPtr()).push_back(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000082 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000083}
84
Ted Kremenek9eb49a42008-01-13 04:56:13 +000085
86unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000087 if (getFlag())
88 return 0;
89
Ted Kremenek9eb49a42008-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 Kremenekffe0f432008-03-07 22:58:01 +000097 if (getFlag())
98 return NULL;
99
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000100 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +0000101 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000102 else
103 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
104}
105
106ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000107 if (getFlag())
108 return NULL;
109
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000110 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +0000111 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +0000112 else {
113 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-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 Kremenek7b989572008-04-20 23:54:24 +0000116 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000117}
118
119ExplodedNodeImpl::NodeGroup::~NodeGroup() {
120 if (getKind() == SizeOther) delete &getVector(getPtr());
121}
Ted Kremenekffe0f432008-03-07 22:58:01 +0000122
123ExplodedGraphImpl* ExplodedGraphImpl::Trim(ExplodedNodeImpl** BeginSources,
124 ExplodedNodeImpl** EndSources) const{
125
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000126 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000127 typedef llvm::DenseMap<ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
128
129 Pass1Ty Pass1;
130 Pass2Ty Pass2;
131
132 llvm::SmallVector<ExplodedNodeImpl*, 10> WL2;
133
134 { // ===- Pass 1 (reverse BFS) -===
135
136 // Enqueue the source nodes to the first worklist.
137
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000138 std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1;
Ted Kremenek33c63692008-04-16 15:51:26 +0000139 std::list<std::pair<ExplodedNodeImpl*, ExplodedNodeImpl*> > WL1_Loops;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000140
141 for (ExplodedNodeImpl** I = BeginSources; I != EndSources; ++I)
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000142 WL1.push_back(std::make_pair(*I, *I));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000143
144 // Process the worklist.
145
Ted Kremenek33c63692008-04-16 15:51:26 +0000146 while (! (WL1.empty() && WL1_Loops.empty())) {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000147
Ted Kremenek33c63692008-04-16 15:51:26 +0000148 ExplodedNodeImpl *N, *Src;
149
150 // Only dequeue from the "loops" worklist if WL1 has no items.
151 // Thus we prioritize for paths that don't span loop boundaries.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000152
Ted Kremenek33c63692008-04-16 15:51:26 +0000153 if (WL1.empty()) {
154 N = WL1_Loops.back().first;
155 Src = WL1_Loops.back().second;
156 WL1_Loops.pop_back();
157 }
158 else {
159 N = WL1.back().first;
160 Src = WL1.back().second;
161 WL1.pop_back();
162 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000163
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000164 if (Pass1.find(N) != Pass1.end())
Ted Kremenekffe0f432008-03-07 22:58:01 +0000165 continue;
166
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000167 bool PredHasSameSource = false;
168 bool VisitPreds = true;
169
170 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
171 I!=E; ++I) {
172
173 Pass1Ty::iterator pi = Pass1.find(*I);
174
175 if (pi == Pass1.end())
176 continue;
177
178 VisitPreds = false;
179
180 if (pi->second == Src) {
181 PredHasSameSource = true;
182 break;
183 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000184 }
185
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000186 if (VisitPreds || !PredHasSameSource) {
187
188 Pass1[N] = Src;
189
190 if (N->Preds.empty()) {
191 WL2.push_back(N);
192 continue;
193 }
194 }
195 else
196 Pass1[N] = NULL;
197
198 if (VisitPreds)
199 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end();
Ted Kremenek33c63692008-04-16 15:51:26 +0000200 I!=E; ++I) {
201
202 ProgramPoint P = Src->getLocation();
203
204 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
205 if (Stmt* T = BE->getSrc()->getTerminator())
206 switch (T->getStmtClass()) {
207 default: break;
208 case Stmt::ForStmtClass:
209 case Stmt::WhileStmtClass:
210 case Stmt::DoStmtClass:
211 WL1_Loops.push_front(std::make_pair(*I, Src));
212 continue;
213
214 }
215
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000216 WL1.push_front(std::make_pair(*I, Src));
Ted Kremenek33c63692008-04-16 15:51:26 +0000217 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000218 }
219 }
220
221 if (WL2.empty())
222 return NULL;
223
224 ExplodedGraphImpl* G = MakeEmptyGraph();
225
226 // ===- Pass 2 (forward DFS to construct the new graph) -===
227
228 while (!WL2.empty()) {
229
230 ExplodedNodeImpl* N = WL2.back();
231 WL2.pop_back();
232
233 // Skip this node if we have already processed it.
234
235 if (Pass2.find(N) != Pass2.end())
236 continue;
237
238 // Create the corresponding node in the new graph.
239
240 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
241 Pass2[N] = NewN;
242
243 if (N->Preds.empty())
244 G->addRoot(NewN);
245
246 // In the case that some of the intended predecessors of NewN have already
247 // been created, we should hook them up as predecessors.
248
249 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
250
251 Pass2Ty::iterator PI = Pass2.find(*I);
252
253 if (PI == Pass2.end())
254 continue;
255
256 NewN->addPredecessor(PI->second);
257 }
258
259 // In the case that some of the intended successors of NewN have already
260 // been created, we should hook them up as successors. Otherwise, enqueue
261 // the new nodes from the original graph that should have nodes created
262 // in the new graph.
263
264 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
265
266 Pass2Ty::iterator PI = Pass2.find(*I);
267
268 if (PI != Pass2.end()) {
269 PI->second->addPredecessor(NewN);
270 continue;
271 }
272
273 // Enqueue nodes to the worklist that were marked during pass 1.
274
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000275 Pass1Ty::iterator pi = Pass1.find(*I);
276
277 if (pi == Pass1.end() || pi->second == NULL)
278 continue;
279
280 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000281 }
282
283 if (N->isSink())
284 NewN->markAsSink();
285 }
286
287 return G;
288}