blob: 42951fc24fd8690072841ee7763e488deba62bfa [file] [log] [blame]
Ted Kremenekef27b4b2008-01-14 23:24:37 +00001//==- GREngine.cpp - Path-Sensitive Dataflow Engine ----------------*- 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 a generic engine for intraprocedural, path-sensitive,
11// dataflow analysis via graph reachability engine.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/GREngine.h"
16#include "clang/AST/Expr.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Casting.h"
19#include "llvm/ADT/DenseMap.h"
20#include <vector>
21
22using llvm::cast;
23using llvm::isa;
24using namespace clang;
25
26namespace {
27 class VISIBILITY_HIDDEN DFS : public GRWorkList {
28 llvm::SmallVector<GRWorkListUnit,20> Stack;
29public:
30 virtual bool hasWork() const {
31 return !Stack.empty();
32 }
33
34 virtual void Enqueue(const GRWorkListUnit& U) {
35 Stack.push_back(U);
36 }
37
38 virtual GRWorkListUnit Dequeue() {
39 assert (!Stack.empty());
40 const GRWorkListUnit& U = Stack.back();
41 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
42 return U;
43 }
44};
45} // end anonymous namespace
46
Ted Kremenekd2500ab2008-01-16 18:18:48 +000047// Place the dstor for GRWorkList here because it contains virtual member
48// functions, and we the code for the dstor generated in one compilation unit.
49GRWorkList::~GRWorkList() {}
50
Ted Kremenekef27b4b2008-01-14 23:24:37 +000051GRWorkList* GRWorkList::MakeDFS() { return new DFS(); }
52
53/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
54bool GREngineImpl::ExecuteWorkList(unsigned Steps) {
55
56 if (G->num_roots() == 0) { // Initialize the analysis by constructing
57 // the root if none exists.
58
59 CFGBlock* Entry = &cfg.getEntry();
60
61 assert (Entry->empty() &&
62 "Entry block must be empty.");
63
64 assert (Entry->succ_size() == 1 &&
65 "Entry block must have 1 successor.");
66
67 // Get the solitary successor.
68 CFGBlock* Succ = *(Entry->succ_begin());
69
70 // Construct an edge representing the
71 // starting location in the function.
72 BlockEdge StartLoc(cfg, Entry, Succ);
73
74 // Generate the root.
75 GenerateNode(StartLoc, getInitialState());
76 }
77
78 while (Steps && WList->hasWork()) {
79 --Steps;
80 const GRWorkListUnit& WU = WList->Dequeue();
81 ExplodedNodeImpl* Node = WU.getNode();
82
83 // Dispatch on the location type.
84 switch (Node->getLocation().getKind()) {
85 default:
86 assert (isa<BlockEdge>(Node->getLocation()));
87 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
88 break;
89
90 case ProgramPoint::BlockEntranceKind:
91 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
92 break;
93
94 case ProgramPoint::BlockExitKind:
Ted Kremenek3226a652008-01-15 00:24:08 +000095 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenekef27b4b2008-01-14 23:24:37 +000096 break;
97
98 case ProgramPoint::PostStmtKind:
99 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
100 WU.getIndex(), Node);
101 break;
102 }
103 }
104
105 return WList->hasWork();
106}
107
108void GREngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) {
109
110 CFGBlock* Blk = L.getDst();
111
112 // Check if we are entering the EXIT block.
113 if (Blk == &cfg.getExit()) {
114
115 assert (cfg.getExit().size() == 0 && "EXIT block cannot contain Stmts.");
116
117 // Process the final state transition.
118 void* State = ProcessEOP(Blk, Pred->State);
119
120 bool IsNew;
121 ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew);
122 Node->addPredecessor(Pred);
123
124 // If the node was freshly created, mark it as an "End-Of-Path" node.
125 if (IsNew) G->addEndOfPath(Node);
126
127 // This path is done. Don't enqueue any more nodes.
128 return;
129 }
130
131 // FIXME: we will dispatch to a function that
132 // manipulates the state at the entrance to a block.
133
Ted Kremenek3226a652008-01-15 00:24:08 +0000134 GenerateNode(BlockEntrance(Blk), Pred->State, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000135}
136
137void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L,
138 ExplodedNodeImpl* Pred) {
139
140 if (Stmt* S = L.getFirstStmt()) {
141 GRNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this);
142 ProcessStmt(S, Builder);
143 }
Ted Kremenek3226a652008-01-15 00:24:08 +0000144 else
145 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000146}
147
148
Ted Kremenek3226a652008-01-15 00:24:08 +0000149void GREngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) {
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000150
151 if (Stmt* Terminator = B->getTerminator())
152 ProcessTerminator(Terminator, B, Pred);
153 else {
154 assert (B->succ_size() == 1 &&
155 "Blocks with no terminator should have at most 1 successor.");
156
157 GenerateNode(BlockEdge(cfg,B,*(B->succ_begin())), Pred->State, Pred);
158 }
159}
160
161void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B,
162 unsigned StmtIdx, ExplodedNodeImpl* Pred) {
163
164 assert (!B->empty());
165
Ted Kremenek3226a652008-01-15 00:24:08 +0000166 if (StmtIdx == B->size())
167 HandleBlockExit(B, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000168 else {
169 GRNodeBuilderImpl Builder(B, StmtIdx, Pred, this);
170 ProcessStmt(L.getStmt(), Builder);
171 }
172}
173
174typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy;
175/// PopulateParentMap - Recurse the AST starting at 'Parent' and add the
176/// mappings between child and parent to ParentMap.
177static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) {
178 for (Stmt::child_iterator I=Parent->child_begin(),
179 E=Parent->child_end(); I!=E; ++I) {
180
181 assert (M.find(*I) == M.end());
182 M[*I] = Parent;
183 PopulateParentMap(*I, M);
184 }
185}
186
187/// GenerateNode - Utility method to generate nodes, hook up successors,
188/// and add nodes to the worklist.
189void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State,
190 ExplodedNodeImpl* Pred) {
191
192 bool IsNew;
193 ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew);
194
195 if (Pred)
196 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
197 else {
198 assert (IsNew);
199 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
200 }
201
202 // Only add 'Node' to the worklist if it was freshly generated.
203 if (IsNew) WList->Enqueue(GRWorkListUnit(Node));
204}
205
206GRNodeBuilderImpl::GRNodeBuilderImpl(CFGBlock* b, unsigned idx,
207 ExplodedNodeImpl* N, GREngineImpl* e)
208 : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) {
209 Deferred.insert(N);
210}
211
212GRNodeBuilderImpl::~GRNodeBuilderImpl() {
213 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
214 if (!(*I)->isInfeasible())
215 GenerateAutoTransition(*I);
216}
217
218void GRNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) {
219 assert (!N->isInfeasible());
220
221 PostStmt Loc(getStmt());
222
223 if (Loc == N->getLocation()) {
224 // Note: 'N' should be a fresh node because otherwise it shouldn't be
225 // a member of Deferred.
226 Eng.WList->Enqueue(N, B, Idx+1);
227 return;
228 }
229
230 bool IsNew;
231 ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew);
232 Succ->addPredecessor(N);
233
234 if (IsNew)
235 Eng.WList->Enqueue(Succ, B, Idx+1);
236}
237
238ExplodedNodeImpl* GRNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State,
239 ExplodedNodeImpl* Pred) {
240
241 bool IsNew;
242 ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew);
243 N->addPredecessor(Pred);
244 Deferred.erase(Pred);
245
246 HasGeneratedNode = true;
247
248 if (IsNew) {
249 Deferred.insert(N);
250 LastNode = N;
251 return N;
252 }
253
254 LastNode = NULL;
255 return NULL;
256}