blob: e7720d97a3412580c4e4d47fef6bc83a5055201f [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
Ted Kremenek7c647412008-01-29 00:33:40 +000059 CFGBlock* Entry = &getCFG().getEntry();
Ted Kremenekef27b4b2008-01-14 23:24:37 +000060
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.
Ted Kremenek7c647412008-01-29 00:33:40 +000072 BlockEdge StartLoc(getCFG(), Entry, Succ);
Ted Kremenekef27b4b2008-01-14 23:24:37 +000073
Ted Kremenek4b170e52008-02-12 18:08:17 +000074 // Set the current block counter to being empty.
75 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
76
Ted Kremenekef27b4b2008-01-14 23:24:37 +000077 // Generate the root.
78 GenerateNode(StartLoc, getInitialState());
79 }
80
81 while (Steps && WList->hasWork()) {
82 --Steps;
83 const GRWorkListUnit& WU = WList->Dequeue();
Ted Kremenek4b170e52008-02-12 18:08:17 +000084
85 // Set the current block counter.
86 WList->setBlockCounter(WU.getBlockCounter());
87
88 // Retrieve the node.
Ted Kremenekef27b4b2008-01-14 23:24:37 +000089 ExplodedNodeImpl* Node = WU.getNode();
90
91 // Dispatch on the location type.
92 switch (Node->getLocation().getKind()) {
93 default:
94 assert (isa<BlockEdge>(Node->getLocation()));
95 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
96 break;
97
98 case ProgramPoint::BlockEntranceKind:
99 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
100 break;
101
102 case ProgramPoint::BlockExitKind:
Ted Kremenek3226a652008-01-15 00:24:08 +0000103 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000104 break;
105
106 case ProgramPoint::PostStmtKind:
107 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
108 WU.getIndex(), Node);
109 break;
110 }
111 }
112
113 return WList->hasWork();
114}
115
116void GREngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) {
117
118 CFGBlock* Blk = L.getDst();
119
120 // Check if we are entering the EXIT block.
Ted Kremenek7c647412008-01-29 00:33:40 +0000121 if (Blk == &getCFG().getExit()) {
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000122
Ted Kremenek7c647412008-01-29 00:33:40 +0000123 assert (getCFG().getExit().size() == 0
124 && "EXIT block cannot contain Stmts.");
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000125
126 // Process the final state transition.
127 void* State = ProcessEOP(Blk, Pred->State);
128
129 bool IsNew;
130 ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew);
131 Node->addPredecessor(Pred);
132
133 // If the node was freshly created, mark it as an "End-Of-Path" node.
134 if (IsNew) G->addEndOfPath(Node);
135
136 // This path is done. Don't enqueue any more nodes.
137 return;
138 }
139
140 // FIXME: we will dispatch to a function that
141 // manipulates the state at the entrance to a block.
142
Ted Kremenek3226a652008-01-15 00:24:08 +0000143 GenerateNode(BlockEntrance(Blk), Pred->State, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000144}
145
146void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L,
147 ExplodedNodeImpl* Pred) {
148
Ted Kremenek4b170e52008-02-12 18:08:17 +0000149 // Increment the block counter.
150 GRBlockCounter Counter = WList->getBlockCounter();
151 Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID());
152 WList->setBlockCounter(Counter);
153
154 // Process the entrance of the block.
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000155 if (Stmt* S = L.getFirstStmt()) {
Ted Kremenek1118e582008-01-29 22:11:49 +0000156 GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000157 ProcessStmt(S, Builder);
158 }
Ted Kremenek3226a652008-01-15 00:24:08 +0000159 else
160 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000161}
162
163
Ted Kremenek3226a652008-01-15 00:24:08 +0000164void GREngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) {
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000165
Ted Kremenek19fbb102008-01-29 22:56:11 +0000166 if (Stmt* Term = B->getTerminator()) {
167 switch (Term->getStmtClass()) {
168 default:
169 assert(false && "Analysis for this terminator not implemented.");
170 break;
Ted Kremenek6ff52182008-02-12 21:51:20 +0000171
172 case Stmt::BinaryOperatorClass: // '&&' and '||'
173 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
174 return;
Ted Kremenek19fbb102008-01-29 22:56:11 +0000175
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000176 case Stmt::ConditionalOperatorClass:
177 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek6ff52182008-02-12 21:51:20 +0000178 return;
179
180 // FIXME: Use constant-folding in CFG construction to simplify this
181 // case.
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000182
183 case Stmt::ChooseExprClass:
184 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek6ff52182008-02-12 21:51:20 +0000185 return;
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000186
Ted Kremenek6ff52182008-02-12 21:51:20 +0000187 case Stmt::DoStmtClass:
188 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
189 return;
190
191 case Stmt::ForStmtClass:
192 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
193 return;
194
195 case Stmt::GotoStmtClass:
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000196 break;
197
Ted Kremenek19fbb102008-01-29 22:56:11 +0000198 case Stmt::IfStmtClass:
199 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek6ff52182008-02-12 21:51:20 +0000200 return;
Ted Kremenek19fbb102008-01-29 22:56:11 +0000201
202 case Stmt::WhileStmtClass:
203 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek6ff52182008-02-12 21:51:20 +0000204 return;
Ted Kremenek19fbb102008-01-29 22:56:11 +0000205 }
206 }
Ted Kremenek6ff52182008-02-12 21:51:20 +0000207
208 assert (B->succ_size() == 1 &&
209 "Blocks with no terminator should have at most 1 successor.");
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000210
Ted Kremenek6ff52182008-02-12 21:51:20 +0000211 GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000212}
213
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000214void GREngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B,
Ted Kremenek19fbb102008-01-29 22:56:11 +0000215 ExplodedNodeImpl* Pred) {
216 assert (B->succ_size() == 2);
217
Ted Kremenek4b170e52008-02-12 18:08:17 +0000218 GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
Ted Kremenek19fbb102008-01-29 22:56:11 +0000219 Pred, this);
220
221 ProcessBranch(Cond, Term, Builder);
222}
223
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000224void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B,
225 unsigned StmtIdx, ExplodedNodeImpl* Pred) {
226
227 assert (!B->empty());
228
Ted Kremenek3226a652008-01-15 00:24:08 +0000229 if (StmtIdx == B->size())
230 HandleBlockExit(B, Pred);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000231 else {
Ted Kremenek1118e582008-01-29 22:11:49 +0000232 GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this);
Ted Kremenekc0f1aae2008-01-16 22:13:19 +0000233 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000234 }
235}
236
237typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy;
238/// PopulateParentMap - Recurse the AST starting at 'Parent' and add the
239/// mappings between child and parent to ParentMap.
240static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) {
241 for (Stmt::child_iterator I=Parent->child_begin(),
242 E=Parent->child_end(); I!=E; ++I) {
243
244 assert (M.find(*I) == M.end());
245 M[*I] = Parent;
246 PopulateParentMap(*I, M);
247 }
248}
249
250/// GenerateNode - Utility method to generate nodes, hook up successors,
251/// and add nodes to the worklist.
252void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State,
253 ExplodedNodeImpl* Pred) {
254
255 bool IsNew;
256 ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew);
257
258 if (Pred)
259 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
260 else {
261 assert (IsNew);
262 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
263 }
264
265 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000266 if (IsNew) WList->Enqueue(Node);
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000267}
268
Ted Kremenek1118e582008-01-29 22:11:49 +0000269GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx,
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000270 ExplodedNodeImpl* N, GREngineImpl* e)
271 : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) {
272 Deferred.insert(N);
273}
274
Ted Kremenek1118e582008-01-29 22:11:49 +0000275GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() {
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000276 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90960972008-01-30 23:03:39 +0000277 if (!(*I)->isSink())
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000278 GenerateAutoTransition(*I);
279}
280
Ted Kremenek1118e582008-01-29 22:11:49 +0000281void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) {
Ted Kremenek90960972008-01-30 23:03:39 +0000282 assert (!N->isSink());
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000283
284 PostStmt Loc(getStmt());
285
286 if (Loc == N->getLocation()) {
287 // Note: 'N' should be a fresh node because otherwise it shouldn't be
288 // a member of Deferred.
289 Eng.WList->Enqueue(N, B, Idx+1);
290 return;
291 }
292
293 bool IsNew;
294 ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew);
295 Succ->addPredecessor(N);
296
297 if (IsNew)
298 Eng.WList->Enqueue(Succ, B, Idx+1);
299}
300
Ted Kremenek1118e582008-01-29 22:11:49 +0000301ExplodedNodeImpl* GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State,
Ted Kremenekef27b4b2008-01-14 23:24:37 +0000302 ExplodedNodeImpl* Pred) {
303
304 bool IsNew;
305 ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew);
306 N->addPredecessor(Pred);
307 Deferred.erase(Pred);
308
309 HasGeneratedNode = true;
310
311 if (IsNew) {
312 Deferred.insert(N);
313 LastNode = N;
314 return N;
315 }
316
317 LastNode = NULL;
318 return NULL;
319}
Ted Kremenek19fbb102008-01-29 22:56:11 +0000320
Ted Kremenek90960972008-01-30 23:03:39 +0000321ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(void* State,
322 bool branch) {
Ted Kremenek19fbb102008-01-29 22:56:11 +0000323 bool IsNew;
324
325 ExplodedNodeImpl* Succ =
326 Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, branch ? DstT : DstF),
327 State, &IsNew);
328
329 Succ->addPredecessor(Pred);
330
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000331 if (branch) GeneratedTrue = true;
332 else GeneratedFalse = true;
333
Ted Kremenek90960972008-01-30 23:03:39 +0000334 if (IsNew) {
Ted Kremenek428d39e2008-01-30 23:24:39 +0000335 Deferred.push_back(Succ);
Ted Kremenek90960972008-01-30 23:03:39 +0000336 return Succ;
337 }
338
339 return NULL;
Ted Kremenek19fbb102008-01-29 22:56:11 +0000340}
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000341
342GRBranchNodeBuilderImpl::~GRBranchNodeBuilderImpl() {
343 if (!GeneratedTrue) generateNodeImpl(Pred->State, true);
344 if (!GeneratedFalse) generateNodeImpl(Pred->State, false);
Ted Kremenek428d39e2008-01-30 23:24:39 +0000345
346 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek4b170e52008-02-12 18:08:17 +0000347 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000348}