blob: 0dc81a4225a8ba5e50b5dccdc773b7e8361cba14 [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
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000046static inline BumpVector<ExplodedNode*>& getVector(void* P) {
47 return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000048}
49
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000050void ExplodedNode::addPredecessor(ExplodedNode* V, ExplodedGraph &G) {
Ted Kremenek45b87892008-08-27 01:27:52 +000051 assert (!V->isSink());
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000052 Preds.addNode(V, G);
53 V->Succs.addNode(this, G);
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 Kremenek5fe4d9d2009-10-07 00:42:52 +000059void ExplodedNode::NodeGroup::addNode(ExplodedNode* N, ExplodedGraph &G) {
60 assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
61 assert(!getFlag());
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek9eb49a42008-01-13 04:56:13 +000063 if (getKind() == Size1) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064 if (ExplodedNode* NOld = getNode()) {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000065 BumpVectorContext &Ctx = G.getNodeAllocator();
66 BumpVector<ExplodedNode*> *V =
67 G.getAllocator().Allocate<BumpVector<ExplodedNode*> >();
68 new (V) BumpVector<ExplodedNode*>(Ctx, 4);
69
70 assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
71 V->push_back(NOld, Ctx);
72 V->push_back(N, Ctx);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000073 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000074 assert(getPtr() == (void*) V);
75 assert(getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000076 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000077 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000078 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000079 assert(getKind() == Size1);
Ted Kremenek596f0a12008-03-05 19:08:55 +000080 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000081 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000082 else {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000083 assert(getKind() == SizeOther);
84 getVector(getPtr()).push_back(N, G.getNodeAllocator());
Ted Kremenek596f0a12008-03-05 19:08:55 +000085 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000086}
87
Zhongxing Xuc5619d92009-08-06 01:32:16 +000088unsigned ExplodedNode::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000089 if (getFlag())
90 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenek9eb49a42008-01-13 04:56:13 +000092 if (getKind() == Size1)
93 return getNode() ? 1 : 0;
94 else
95 return getVector(getPtr()).size();
96}
97
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000098ExplodedNode **ExplodedNode::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000099 if (getFlag())
100 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000102 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000103 return (ExplodedNode**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000104 else
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000105 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin()));
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000106}
107
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000108ExplodedNode** ExplodedNode::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000109 if (getFlag())
110 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000112 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000113 return (ExplodedNode**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +0000114 else {
115 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-04-22 07:37:18 +0000116 // we can dereference the last elem and then add 1 to the result.
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000117 return const_cast<ExplodedNode**>(getVector(getPtr()).end());
Ted Kremenek7b989572008-04-20 23:54:24 +0000118 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121ExplodedNode *ExplodedGraph::getNode(const ProgramPoint& L,
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000122 const GRState* State, bool* IsNew) {
123 // Profile 'State' to determine if we already have an existing node.
Mike Stump1eb44332009-09-09 15:08:12 +0000124 llvm::FoldingSetNodeID profile;
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000125 void* InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000127 NodeTy::Profile(profile, L, State);
128 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000130 if (!V) {
131 // Allocate a new node.
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000132 V = (NodeTy*) getAllocator().Allocate<NodeTy>();
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000133 new (V) NodeTy(L, State);
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000135 // Insert the node into the node set and return it.
136 Nodes.InsertNode(V, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000138 ++NumNodes;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000140 if (IsNew) *IsNew = true;
141 }
142 else
143 if (IsNew) *IsNew = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000145 return V;
146}
147
148std::pair<ExplodedGraph*, InterExplodedGraphMap*>
149ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
150 llvm::DenseMap<const void*, const void*> *InverseMap) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000152 if (NBeg == NEnd)
153 return std::make_pair((ExplodedGraph*) 0,
154 (InterExplodedGraphMap*) 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000156 assert (NBeg < NEnd);
157
158 llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap());
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000160 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000162 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take());
163}
164
165ExplodedGraph*
166ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources,
167 const ExplodedNode* const* EndSources,
168 InterExplodedGraphMap* M,
169 llvm::DenseMap<const void*, const void*> *InverseMap) const {
170
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000171 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000172 Pass1Ty Pass1;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000174 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000175 Pass2Ty& Pass2 = M->M;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000177 llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000178
Ted Kremenek10aa5542009-03-12 23:41:59 +0000179 // ===- Pass 1 (reverse DFS) -===
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000180 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000181 assert(*I);
182 WL1.push_back(*I);
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenekdf969292009-02-20 21:10:26 +0000185 // Process the first worklist until it is empty. Because it is a std::list
186 // it acts like a FIFO queue.
187 while (!WL1.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000188 const ExplodedNode *N = WL1.back();
Ted Kremenekdf969292009-02-20 21:10:26 +0000189 WL1.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenekdf969292009-02-20 21:10:26 +0000191 // Have we already visited this node? If so, continue to the next one.
192 if (Pass1.count(N))
193 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000194
Ted Kremenekdf969292009-02-20 21:10:26 +0000195 // Otherwise, mark this node as visited.
196 Pass1.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenekdf969292009-02-20 21:10:26 +0000198 // If this is a root enqueue it to the second worklist.
199 if (N->Preds.empty()) {
200 WL2.push_back(N);
201 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenekdf969292009-02-20 21:10:26 +0000204 // Visit our predecessors and enqueue them.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000205 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000206 WL1.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremenekdf969292009-02-20 21:10:26 +0000209 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000210 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000211 return 0;
212
Ted Kremenekdf969292009-02-20 21:10:26 +0000213 // Create an empty graph.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000214 ExplodedGraph* G = MakeEmptyGraph();
Mike Stump1eb44332009-09-09 15:08:12 +0000215
216 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000217 while (!WL2.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000218 const ExplodedNode* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000219 WL2.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenekffe0f432008-03-07 22:58:01 +0000221 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000222 if (Pass2.find(N) != Pass2.end())
223 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenekdf969292009-02-20 21:10:26 +0000225 // Create the corresponding node in the new graph and record the mapping
226 // from the old node to the new node.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000227 ExplodedNode* NewN = G->getNode(N->getLocation(), N->State, NULL);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000228 Pass2[N] = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenekdf969292009-02-20 21:10:26 +0000230 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000231 if (InverseMap) (*InverseMap)[NewN] = N;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremenekdf969292009-02-20 21:10:26 +0000233 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000234 if (N->Preds.empty())
235 G->addRoot(NewN);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenekffe0f432008-03-07 22:58:01 +0000237 // In the case that some of the intended predecessors of NewN have already
238 // been created, we should hook them up as predecessors.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000239
Ted Kremenekdf969292009-02-20 21:10:26 +0000240 // Walk through the predecessors of 'N' and hook up their corresponding
241 // nodes in the new graph (if any) to the freshly created node.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000242 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000243 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000244 if (PI == Pass2.end())
245 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000247 NewN->addPredecessor(PI->second, *G);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000248 }
249
250 // In the case that some of the intended successors of NewN have already
251 // been created, we should hook them up as successors. Otherwise, enqueue
252 // the new nodes from the original graph that should have nodes created
253 // in the new graph.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000254 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000256 if (PI != Pass2.end()) {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000257 PI->second->addPredecessor(NewN, *G);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000258 continue;
259 }
260
261 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000262 if (Pass1.count(*I))
263 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenekdf969292009-02-20 21:10:26 +0000266 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000267 if (N->isSink())
268 NewN->markAsSink();
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenekffe0f432008-03-07 22:58:01 +0000271 return G;
272}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000273
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000274ExplodedNode*
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000275InterExplodedGraphMap::getMappedNode(const ExplodedNode* N) const {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000276 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::iterator I =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000277 M.find(N);
278
279 return I == M.end() ? 0 : I->second;
280}
281