blob: 20de6c48c387ec59f649f2b5b6d7f783de47fac0 [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>
21
22using namespace clang;
23
Ted Kremenek2e287542008-08-27 01:56:11 +000024//===----------------------------------------------------------------------===//
25// Node auditing.
26//===----------------------------------------------------------------------===//
27
28// An out of line virtual method to provide a home for the class vtable.
29ExplodedNodeImpl::Auditor::~Auditor() {}
30
31#ifndef NDEBUG
32static ExplodedNodeImpl::Auditor* NodeAuditor = 0;
33#endif
34
35void ExplodedNodeImpl::SetAuditor(ExplodedNodeImpl::Auditor* A) {
36#ifndef NDEBUG
37 NodeAuditor = A;
38#endif
39}
40
41//===----------------------------------------------------------------------===//
42// ExplodedNodeImpl.
43//===----------------------------------------------------------------------===//
Ted Kremenek9eb49a42008-01-13 04:56:13 +000044
45static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
46 return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
47}
48
Ted Kremenek45b87892008-08-27 01:27:52 +000049void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) {
50 assert (!V->isSink());
51 Preds.addNode(V);
52 V->Succs.addNode(this);
Ted Kremenek2e287542008-08-27 01:56:11 +000053#ifndef NDEBUG
54 if (NodeAuditor) NodeAuditor->AddEdge(V, this);
55#endif
Ted Kremenek45b87892008-08-27 01:27:52 +000056}
57
Ted Kremenek9eb49a42008-01-13 04:56:13 +000058void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
Ted Kremenek596f0a12008-03-05 19:08:55 +000059
60 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenekffe0f432008-03-07 22:58:01 +000061 assert (!getFlag());
Ted Kremenek596f0a12008-03-05 19:08:55 +000062
Ted Kremenek9eb49a42008-01-13 04:56:13 +000063 if (getKind() == Size1) {
64 if (ExplodedNodeImpl* NOld = getNode()) {
65 std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
Ted Kremenek596f0a12008-03-05 19:08:55 +000066 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000067 V->push_back(NOld);
68 V->push_back(N);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000069 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek596f0a12008-03-05 19:08:55 +000070 assert (getPtr() == (void*) V);
71 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000072 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000073 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000074 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000075 assert (getKind() == Size1);
76 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000077 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000078 else {
79 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000080 getVector(getPtr()).push_back(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000081 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000082}
83
Ted Kremenek9eb49a42008-01-13 04:56:13 +000084
85unsigned ExplodedNodeImpl::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000086 if (getFlag())
87 return 0;
88
Ted Kremenek9eb49a42008-01-13 04:56:13 +000089 if (getKind() == Size1)
90 return getNode() ? 1 : 0;
91 else
92 return getVector(getPtr()).size();
93}
94
95ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000096 if (getFlag())
97 return NULL;
98
Ted Kremenek9eb49a42008-01-13 04:56:13 +000099 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +0000100 return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000101 else
102 return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
103}
104
105ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000106 if (getFlag())
107 return NULL;
108
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000109 if (getKind() == Size1)
Ted Kremenekffe0f432008-03-07 22:58:01 +0000110 return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +0000111 else {
112 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-04-22 07:37:18 +0000113 // we can dereference the last elem and then add 1 to the result.
114 return const_cast<ExplodedNodeImpl**>(&getVector(getPtr()).back()) + 1;
Ted Kremenek7b989572008-04-20 23:54:24 +0000115 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000116}
117
118ExplodedNodeImpl::NodeGroup::~NodeGroup() {
119 if (getKind() == SizeOther) delete &getVector(getPtr());
120}
Ted Kremenekffe0f432008-03-07 22:58:01 +0000121
Ted Kremenek3148eb42009-01-24 00:55:43 +0000122ExplodedGraphImpl*
123ExplodedGraphImpl::Trim(const ExplodedNodeImpl* const* BeginSources,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124 const ExplodedNodeImpl* const* EndSources,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000125 InterExplodedGraphMapImpl* M,
126 llvm::DenseMap<const void*, const void*> *InverseMap)
127const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000128
Ted Kremenekdf969292009-02-20 21:10:26 +0000129 typedef llvm::DenseSet<const ExplodedNodeImpl*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000130 Pass1Ty Pass1;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000131
Ted Kremenekdf969292009-02-20 21:10:26 +0000132 typedef llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000133 Pass2Ty& Pass2 = M->M;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000134
Ted Kremenek10aa5542009-03-12 23:41:59 +0000135 llvm::SmallVector<const ExplodedNodeImpl*, 10> WL1, WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000136
Ted Kremenek10aa5542009-03-12 23:41:59 +0000137 // ===- Pass 1 (reverse DFS) -===
Ted Kremenekdf969292009-02-20 21:10:26 +0000138 for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I) {
139 assert(*I);
140 WL1.push_back(*I);
141 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000142
Ted Kremenekdf969292009-02-20 21:10:26 +0000143 // Process the first worklist until it is empty. Because it is a std::list
144 // it acts like a FIFO queue.
145 while (!WL1.empty()) {
146 const ExplodedNodeImpl *N = WL1.back();
147 WL1.pop_back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000148
Ted Kremenekdf969292009-02-20 21:10:26 +0000149 // Have we already visited this node? If so, continue to the next one.
150 if (Pass1.count(N))
151 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000152
Ted Kremenekdf969292009-02-20 21:10:26 +0000153 // Otherwise, mark this node as visited.
154 Pass1.insert(N);
155
156 // If this is a root enqueue it to the second worklist.
157 if (N->Preds.empty()) {
158 WL2.push_back(N);
159 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000160 }
Ted Kremenekdf969292009-02-20 21:10:26 +0000161
162 // Visit our predecessors and enqueue them.
163 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000164 WL1.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000165 }
166
Ted Kremenekdf969292009-02-20 21:10:26 +0000167 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000168 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000169 return 0;
170
Ted Kremenekdf969292009-02-20 21:10:26 +0000171 // Create an empty graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000172 ExplodedGraphImpl* G = MakeEmptyGraph();
173
Ted Kremenekdf969292009-02-20 21:10:26 +0000174 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000175 while (!WL2.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000176 const ExplodedNodeImpl* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000177 WL2.pop_back();
178
179 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000180 if (Pass2.find(N) != Pass2.end())
181 continue;
182
Ted Kremenekdf969292009-02-20 21:10:26 +0000183 // Create the corresponding node in the new graph and record the mapping
184 // from the old node to the new node.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000185 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
186 Pass2[N] = NewN;
Ted Kremenekdf969292009-02-20 21:10:26 +0000187
188 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000189 if (InverseMap) (*InverseMap)[NewN] = N;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000190
Ted Kremenekdf969292009-02-20 21:10:26 +0000191 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000192 if (N->Preds.empty())
193 G->addRoot(NewN);
194
195 // In the case that some of the intended predecessors of NewN have already
196 // been created, we should hook them up as predecessors.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000197
Ted Kremenekdf969292009-02-20 21:10:26 +0000198 // Walk through the predecessors of 'N' and hook up their corresponding
199 // nodes in the new graph (if any) to the freshly created node.
200 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
201 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000202 if (PI == Pass2.end())
203 continue;
204
205 NewN->addPredecessor(PI->second);
206 }
207
208 // In the case that some of the intended successors of NewN have already
209 // been created, we should hook them up as successors. Otherwise, enqueue
210 // the new nodes from the original graph that should have nodes created
211 // in the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000212 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000213 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000214 if (PI != Pass2.end()) {
215 PI->second->addPredecessor(NewN);
216 continue;
217 }
218
219 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000220 if (Pass1.count(*I))
221 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000222 }
223
Ted Kremenekdf969292009-02-20 21:10:26 +0000224 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000225 if (N->isSink())
226 NewN->markAsSink();
227 }
228
229 return G;
230}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000231
232ExplodedNodeImpl*
233InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNodeImpl* N) const {
234 llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*>::iterator I =
235 M.find(N);
236
237 return I == M.end() ? 0 : I->second;
238}
239
240InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {}
241