blob: 0e72af91e136e6f4a5c639949677a6eeac21bd3e [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,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000125 const ExplodedNodeImpl* const* EndSources,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000126 InterExplodedGraphMapImpl* M,
127 llvm::DenseMap<const void*, const void*> *InverseMap)
128const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000129
Ted Kremenekdf969292009-02-20 21:10:26 +0000130 typedef llvm::DenseSet<const ExplodedNodeImpl*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000131 Pass1Ty Pass1;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000132
Ted Kremenekdf969292009-02-20 21:10:26 +0000133 typedef llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 Pass2Ty& Pass2 = M->M;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000135
Ted Kremenekdf969292009-02-20 21:10:26 +0000136 std::list<const ExplodedNodeImpl*> WL1;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000137 llvm::SmallVector<const ExplodedNodeImpl*, 10> WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000138
Ted Kremenekdf969292009-02-20 21:10:26 +0000139 // ===- Pass 1 (reverse BFS) -===
140 for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I) {
141 assert(*I);
142 WL1.push_back(*I);
143 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000144
Ted Kremenekdf969292009-02-20 21:10:26 +0000145 // Process the first worklist until it is empty. Because it is a std::list
146 // it acts like a FIFO queue.
147 while (!WL1.empty()) {
148 const ExplodedNodeImpl *N = WL1.back();
149 WL1.pop_back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000150
Ted Kremenekdf969292009-02-20 21:10:26 +0000151 // Have we already visited this node? If so, continue to the next one.
152 if (Pass1.count(N))
153 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000154
Ted Kremenekdf969292009-02-20 21:10:26 +0000155 // Otherwise, mark this node as visited.
156 Pass1.insert(N);
157
158 // If this is a root enqueue it to the second worklist.
159 if (N->Preds.empty()) {
160 WL2.push_back(N);
161 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000162 }
Ted Kremenekdf969292009-02-20 21:10:26 +0000163
164 // Visit our predecessors and enqueue them.
165 for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
166 WL1.push_front(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000167 }
168
Ted Kremenekdf969292009-02-20 21:10:26 +0000169 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000170 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000171 return 0;
172
Ted Kremenekdf969292009-02-20 21:10:26 +0000173 // Create an empty graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000174 ExplodedGraphImpl* G = MakeEmptyGraph();
175
Ted Kremenekdf969292009-02-20 21:10:26 +0000176 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000177 while (!WL2.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000178 const ExplodedNodeImpl* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000179 WL2.pop_back();
180
181 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000182 if (Pass2.find(N) != Pass2.end())
183 continue;
184
Ted Kremenekdf969292009-02-20 21:10:26 +0000185 // Create the corresponding node in the new graph and record the mapping
186 // from the old node to the new node.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000187 ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
188 Pass2[N] = NewN;
Ted Kremenekdf969292009-02-20 21:10:26 +0000189
190 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000191 if (InverseMap) (*InverseMap)[NewN] = N;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000192
Ted Kremenekdf969292009-02-20 21:10:26 +0000193 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000194 if (N->Preds.empty())
195 G->addRoot(NewN);
196
197 // In the case that some of the intended predecessors of NewN have already
198 // been created, we should hook them up as predecessors.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000199
Ted Kremenekdf969292009-02-20 21:10:26 +0000200 // Walk through the predecessors of 'N' and hook up their corresponding
201 // nodes in the new graph (if any) to the freshly created node.
202 for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
203 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000204 if (PI == Pass2.end())
205 continue;
206
207 NewN->addPredecessor(PI->second);
208 }
209
210 // In the case that some of the intended successors of NewN have already
211 // been created, we should hook them up as successors. Otherwise, enqueue
212 // the new nodes from the original graph that should have nodes created
213 // in the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000214 for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000215 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000216 if (PI != Pass2.end()) {
217 PI->second->addPredecessor(NewN);
218 continue;
219 }
220
221 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000222 if (Pass1.count(*I))
223 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000224 }
225
Ted Kremenekdf969292009-02-20 21:10:26 +0000226 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000227 if (N->isSink())
228 NewN->markAsSink();
229 }
230
231 return G;
232}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000233
234ExplodedNodeImpl*
235InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNodeImpl* N) const {
236 llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*>::iterator I =
237 M.find(N);
238
239 return I == M.end() ? 0 : I->second;
240}
241
242InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {}
243