blob: 4e41b60da3776330d3afabb2d7c0fe0b39a97756 [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
Ted Kremenek3148eb42009-01-24 00:55:43 +0000123ExplodedGraphImpl*
124ExplodedGraphImpl::Trim(const ExplodedNodeImpl* const* BeginSources,
125 const ExplodedNodeImpl* const* EndSources) const{
Ted Kremenekffe0f432008-03-07 22:58:01 +0000126
Ted Kremenek3148eb42009-01-24 00:55:43 +0000127 typedef llvm::DenseMap<const ExplodedNodeImpl*, const ExplodedNodeImpl*> Pass1Ty;
128 typedef llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000129
130 Pass1Ty Pass1;
131 Pass2Ty Pass2;
132
Ted Kremenek3148eb42009-01-24 00:55:43 +0000133 llvm::SmallVector<const ExplodedNodeImpl*, 10> WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000134
135 { // ===- Pass 1 (reverse BFS) -===
136
137 // Enqueue the source nodes to the first worklist.
138
Ted Kremenek3148eb42009-01-24 00:55:43 +0000139 std::list<std::pair<const ExplodedNodeImpl*,
140 const ExplodedNodeImpl*> > WL1, WL1_Loops;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000141
Ted Kremenek3148eb42009-01-24 00:55:43 +0000142 for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I)
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000143 WL1.push_back(std::make_pair(*I, *I));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000144
145 // Process the worklist.
146
Ted Kremenek33c63692008-04-16 15:51:26 +0000147 while (! (WL1.empty() && WL1_Loops.empty())) {
Ted Kremenek33c63692008-04-16 15:51:26 +0000148 // Only dequeue from the "loops" worklist if WL1 has no items.
149 // Thus we prioritize for paths that don't span loop boundaries.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000150 const ExplodedNodeImpl *N, *Src;
151
Ted Kremenek33c63692008-04-16 15:51:26 +0000152 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 Kremenekffe0f432008-03-07 22:58:01 +0000162
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000163 if (Pass1.find(N) != Pass1.end())
Ted Kremenekffe0f432008-03-07 22:58:01 +0000164 continue;
165
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000166 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 Kremenekffe0f432008-03-07 22:58:01 +0000183 }
184
Ted Kremenek7ec07fd2008-03-12 17:18:20 +0000185 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 Kremenek33c63692008-04-16 15:51:26 +0000199 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 Kremenek7ec07fd2008-03-12 17:18:20 +0000215 WL1.push_front(std::make_pair(*I, Src));
Ted Kremenek33c63692008-04-16 15:51:26 +0000216 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000217 }
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 Kremenek3148eb42009-01-24 00:55:43 +0000229 const ExplodedNodeImpl* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000230 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 Kremenek7ec07fd2008-03-12 17:18:20 +0000274 Pass1Ty::iterator pi = Pass1.find(*I);
275
276 if (pi == Pass1.end() || pi->second == NULL)
277 continue;
278
279 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000280 }
281
282 if (N->isSink())
283 NewN->markAsSink();
284 }
285
286 return G;
287}