blob: c0c0f0636baee4805f623ca579297e39bba6cbc9 [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);
54 state->Profile(ID);
55}
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
Ted Kremenek3148eb42009-01-24 00:55:43 +0000130ExplodedGraphImpl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000131ExplodedGraphImpl::Trim(const ExplodedNode* const* BeginSources,
132 const ExplodedNode* const* EndSources,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000133 InterExplodedGraphMapImpl* M,
134 llvm::DenseMap<const void*, const void*> *InverseMap)
135const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000136
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000137 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000138 Pass1Ty Pass1;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000139
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000140 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000141 Pass2Ty& Pass2 = M->M;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000142
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000143 llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000144
Ted Kremenek10aa5542009-03-12 23:41:59 +0000145 // ===- Pass 1 (reverse DFS) -===
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000146 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000147 assert(*I);
148 WL1.push_back(*I);
149 }
Ted Kremenekffe0f432008-03-07 22:58:01 +0000150
Ted Kremenekdf969292009-02-20 21:10:26 +0000151 // Process the first worklist until it is empty. Because it is a std::list
152 // it acts like a FIFO queue.
153 while (!WL1.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000154 const ExplodedNode *N = WL1.back();
Ted Kremenekdf969292009-02-20 21:10:26 +0000155 WL1.pop_back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000156
Ted Kremenekdf969292009-02-20 21:10:26 +0000157 // Have we already visited this node? If so, continue to the next one.
158 if (Pass1.count(N))
159 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000160
Ted Kremenekdf969292009-02-20 21:10:26 +0000161 // Otherwise, mark this node as visited.
162 Pass1.insert(N);
163
164 // If this is a root enqueue it to the second worklist.
165 if (N->Preds.empty()) {
166 WL2.push_back(N);
167 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000168 }
Ted Kremenekdf969292009-02-20 21:10:26 +0000169
170 // Visit our predecessors and enqueue them.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000171 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000172 WL1.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000173 }
174
Ted Kremenekdf969292009-02-20 21:10:26 +0000175 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000176 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000177 return 0;
178
Ted Kremenekdf969292009-02-20 21:10:26 +0000179 // Create an empty graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000180 ExplodedGraphImpl* G = MakeEmptyGraph();
181
Ted Kremenekdf969292009-02-20 21:10:26 +0000182 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000183 while (!WL2.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000184 const ExplodedNode* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000185 WL2.pop_back();
186
187 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000188 if (Pass2.find(N) != Pass2.end())
189 continue;
190
Ted Kremenekdf969292009-02-20 21:10:26 +0000191 // Create the corresponding node in the new graph and record the mapping
192 // from the old node to the new node.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000193 ExplodedNode* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000194 Pass2[N] = NewN;
Ted Kremenekdf969292009-02-20 21:10:26 +0000195
196 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000197 if (InverseMap) (*InverseMap)[NewN] = N;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000198
Ted Kremenekdf969292009-02-20 21:10:26 +0000199 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000200 if (N->Preds.empty())
201 G->addRoot(NewN);
202
203 // In the case that some of the intended predecessors of NewN have already
204 // been created, we should hook them up as predecessors.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000205
Ted Kremenekdf969292009-02-20 21:10:26 +0000206 // Walk through the predecessors of 'N' and hook up their corresponding
207 // nodes in the new graph (if any) to the freshly created node.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000208 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000209 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000210 if (PI == Pass2.end())
211 continue;
212
213 NewN->addPredecessor(PI->second);
214 }
215
216 // In the case that some of the intended successors of NewN have already
217 // been created, we should hook them up as successors. Otherwise, enqueue
218 // the new nodes from the original graph that should have nodes created
219 // in the new graph.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000220 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000221 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000222 if (PI != Pass2.end()) {
223 PI->second->addPredecessor(NewN);
224 continue;
225 }
226
227 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000228 if (Pass1.count(*I))
229 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000230 }
231
Ted Kremenekdf969292009-02-20 21:10:26 +0000232 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000233 if (N->isSink())
234 NewN->markAsSink();
235 }
236
237 return G;
238}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000239
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000240ExplodedNode*
241InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNode* N) const {
242 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::iterator I =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000243 M.find(N);
244
245 return I == M.end() ? 0 : I->second;
246}
247
248InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {}
249