blob: 88bb120f5da59f5205647872ecc89cef9f516575 [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"
Zhongxing Xuc5619d92009-08-06 01:32:16 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek33c63692008-04-16 15:51:26 +000017#include "clang/AST/Stmt.h"
Ted Kremenekffe0f432008-03-07 22:58:01 +000018#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
Ted Kremenek9eb49a42008-01-13 04:56:13 +000021#include <vector>
22
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.
Zhongxing Xuc5619d92009-08-06 01:32:16 +000030ExplodedNode::Auditor::~Auditor() {}
Ted Kremenek2e287542008-08-27 01:56:11 +000031
32#ifndef NDEBUG
Zhongxing Xuc5619d92009-08-06 01:32:16 +000033static ExplodedNode::Auditor* NodeAuditor = 0;
Ted Kremenek2e287542008-08-27 01:56:11 +000034#endif
35
Zhongxing Xuc5619d92009-08-06 01:32:16 +000036void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) {
Ted Kremenek2e287542008-08-27 01:56:11 +000037#ifndef NDEBUG
38 NodeAuditor = A;
39#endif
40}
41
42//===----------------------------------------------------------------------===//
Zhongxing Xuc5619d92009-08-06 01:32:16 +000043// ExplodedNode.
Ted Kremenek2e287542008-08-27 01:56:11 +000044//===----------------------------------------------------------------------===//
Ted Kremenek9eb49a42008-01-13 04:56:13 +000045
Zhongxing Xuc5619d92009-08-06 01:32:16 +000046static inline std::vector<ExplodedNode*>& getVector(void* P) {
47 return *reinterpret_cast<std::vector<ExplodedNode*>*>(P);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000048}
49
Zhongxing Xuc5619d92009-08-06 01:32:16 +000050void ExplodedNode::Profile(llvm::FoldingSetNodeID& ID,
51 const ProgramPoint& Loc,
52 const GRState* state) {
53 ID.Add(Loc);
Ted Kremenek619d3022009-08-06 03:39:20 +000054 ID.AddPointer(state);
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055}
56
57void ExplodedNode::addPredecessor(ExplodedNode* V) {
Ted Kremenek45b87892008-08-27 01:27:52 +000058 assert (!V->isSink());
59 Preds.addNode(V);
60 V->Succs.addNode(this);
Ted Kremenek2e287542008-08-27 01:56:11 +000061#ifndef NDEBUG
62 if (NodeAuditor) NodeAuditor->AddEdge(V, this);
63#endif
Ted Kremenek45b87892008-08-27 01:27:52 +000064}
65
Zhongxing Xuc5619d92009-08-06 01:32:16 +000066void ExplodedNode::NodeGroup::addNode(ExplodedNode* N) {
Ted Kremenek596f0a12008-03-05 19:08:55 +000067
68 assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
Ted Kremenekffe0f432008-03-07 22:58:01 +000069 assert (!getFlag());
Ted Kremenek596f0a12008-03-05 19:08:55 +000070
Ted Kremenek9eb49a42008-01-13 04:56:13 +000071 if (getKind() == Size1) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +000072 if (ExplodedNode* NOld = getNode()) {
73 std::vector<ExplodedNode*>* V = new std::vector<ExplodedNode*>();
Ted Kremenek596f0a12008-03-05 19:08:55 +000074 assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000075 V->push_back(NOld);
76 V->push_back(N);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000077 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek596f0a12008-03-05 19:08:55 +000078 assert (getPtr() == (void*) V);
79 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000080 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000081 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000082 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000083 assert (getKind() == Size1);
84 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000085 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000086 else {
87 assert (getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000088 getVector(getPtr()).push_back(N);
Ted Kremenek596f0a12008-03-05 19:08:55 +000089 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000090}
91
Ted Kremenek9eb49a42008-01-13 04:56:13 +000092
Zhongxing Xuc5619d92009-08-06 01:32:16 +000093unsigned ExplodedNode::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000094 if (getFlag())
95 return 0;
96
Ted Kremenek9eb49a42008-01-13 04:56:13 +000097 if (getKind() == Size1)
98 return getNode() ? 1 : 0;
99 else
100 return getVector(getPtr()).size();
101}
102
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000103ExplodedNode** ExplodedNode::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000104 if (getFlag())
105 return NULL;
106
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000107 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000108 return (ExplodedNode**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000109 else
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000110 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin()));
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000111}
112
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000113ExplodedNode** ExplodedNode::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000114 if (getFlag())
115 return NULL;
116
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000117 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000118 return (ExplodedNode**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +0000119 else {
120 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-04-22 07:37:18 +0000121 // we can dereference the last elem and then add 1 to the result.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000122 return const_cast<ExplodedNode**>(&getVector(getPtr()).back()) + 1;
Ted Kremenek7b989572008-04-20 23:54:24 +0000123 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000124}
125
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000126ExplodedNode::NodeGroup::~NodeGroup() {
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000127 if (getKind() == SizeOther) delete &getVector(getPtr());
128}
Ted Kremenekffe0f432008-03-07 22:58:01 +0000129
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000130ExplodedNode *ExplodedGraph::getNode(const ProgramPoint& L,
131 const GRState* State, bool* IsNew) {
132 // Profile 'State' to determine if we already have an existing node.
133 llvm::FoldingSetNodeID profile;
134 void* InsertPos = 0;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000135
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000136 NodeTy::Profile(profile, L, State);
137 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
138
139 if (!V) {
140 // Allocate a new node.
141 V = (NodeTy*) Allocator.Allocate<NodeTy>();
142 new (V) NodeTy(L, State);
143
144 // Insert the node into the node set and return it.
145 Nodes.InsertNode(V, InsertPos);
146
147 ++NumNodes;
148
149 if (IsNew) *IsNew = true;
150 }
151 else
152 if (IsNew) *IsNew = false;
153
154 return V;
155}
156
157std::pair<ExplodedGraph*, InterExplodedGraphMap*>
158ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
159 llvm::DenseMap<const void*, const void*> *InverseMap) const {
160
161 if (NBeg == NEnd)
162 return std::make_pair((ExplodedGraph*) 0,
163 (InterExplodedGraphMap*) 0);
164
165 assert (NBeg < NEnd);
166
167 llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap());
168
169 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap);
170
171 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take());
172}
173
174ExplodedGraph*
175ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources,
176 const ExplodedNode* const* EndSources,
177 InterExplodedGraphMap* M,
178 llvm::DenseMap<const void*, const void*> *InverseMap) const {
179
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000180 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000181 Pass1Ty Pass1;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000183 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000184 Pass2Ty& Pass2 = M->M;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000185
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000186 llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000187
Ted Kremenek10aa5542009-03-12 23:41:59 +0000188 // ===- Pass 1 (reverse DFS) -===
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000189 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000190 assert(*I);
191 WL1.push_back(*I);
192 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000193
Ted Kremenekdf969292009-02-20 21:10:26 +0000194 // Process the first worklist until it is empty. Because it is a std::list
195 // it acts like a FIFO queue.
196 while (!WL1.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000197 const ExplodedNode *N = WL1.back();
Ted Kremenekdf969292009-02-20 21:10:26 +0000198 WL1.pop_back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000199
Ted Kremenekdf969292009-02-20 21:10:26 +0000200 // Have we already visited this node? If so, continue to the next one.
201 if (Pass1.count(N))
202 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000203
Ted Kremenekdf969292009-02-20 21:10:26 +0000204 // Otherwise, mark this node as visited.
205 Pass1.insert(N);
206
207 // If this is a root enqueue it to the second worklist.
208 if (N->Preds.empty()) {
209 WL2.push_back(N);
210 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000211 }
Ted Kremenekdf969292009-02-20 21:10:26 +0000212
213 // Visit our predecessors and enqueue them.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000214 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000215 WL1.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000216 }
217
Ted Kremenekdf969292009-02-20 21:10:26 +0000218 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000219 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000220 return 0;
221
Ted Kremenekdf969292009-02-20 21:10:26 +0000222 // Create an empty graph.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000223 ExplodedGraph* G = MakeEmptyGraph();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000224
Ted Kremenekdf969292009-02-20 21:10:26 +0000225 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000226 while (!WL2.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000227 const ExplodedNode* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000228 WL2.pop_back();
229
230 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000231 if (Pass2.find(N) != Pass2.end())
232 continue;
233
Ted Kremenekdf969292009-02-20 21:10:26 +0000234 // Create the corresponding node in the new graph and record the mapping
235 // from the old node to the new node.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000236 ExplodedNode* NewN = G->getNode(N->getLocation(), N->State, NULL);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000237 Pass2[N] = NewN;
Ted Kremenekdf969292009-02-20 21:10:26 +0000238
239 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000240 if (InverseMap) (*InverseMap)[NewN] = N;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000241
Ted Kremenekdf969292009-02-20 21:10:26 +0000242 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000243 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.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000248
Ted Kremenekdf969292009-02-20 21:10:26 +0000249 // Walk through the predecessors of 'N' and hook up their corresponding
250 // nodes in the new graph (if any) to the freshly created node.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000251 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000252 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000253 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.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000263 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000264 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000265 if (PI != Pass2.end()) {
266 PI->second->addPredecessor(NewN);
267 continue;
268 }
269
270 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000271 if (Pass1.count(*I))
272 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000273 }
274
Ted Kremenekdf969292009-02-20 21:10:26 +0000275 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000276 if (N->isSink())
277 NewN->markAsSink();
278 }
279
280 return G;
281}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000282
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000283ExplodedNode*
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000284InterExplodedGraphMap::getMappedNode(const ExplodedNode* N) const {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000285 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::iterator I =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000286 M.find(N);
287
288 return I == M.end() ? 0 : I->second;
289}
290