blob: e3b238aebe9af86bf5958840a55ef11983bdf294 [file] [log] [blame]
Ted Kremenekf24af5b2008-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 Kremenekee985462008-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 Kremenekf24af5b2008-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 Kremenekcb48b9c2008-01-29 00:33:40 +000059 CFGBlock* Entry = &getCFG().getEntry();
Ted Kremenekf24af5b2008-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 Kremenekcb48b9c2008-01-29 00:33:40 +000072 BlockEdge StartLoc(getCFG(), Entry, Succ);
Ted Kremenekf24af5b2008-01-14 23:24:37 +000073
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 Kremenek425c08c2008-01-15 00:24:08 +000095 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenekf24af5b2008-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.
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000113 if (Blk == &getCFG().getExit()) {
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000114
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000115 assert (getCFG().getExit().size() == 0
116 && "EXIT block cannot contain Stmts.");
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000117
118 // Process the final state transition.
119 void* State = ProcessEOP(Blk, Pred->State);
120
121 bool IsNew;
122 ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew);
123 Node->addPredecessor(Pred);
124
125 // If the node was freshly created, mark it as an "End-Of-Path" node.
126 if (IsNew) G->addEndOfPath(Node);
127
128 // This path is done. Don't enqueue any more nodes.
129 return;
130 }
131
132 // FIXME: we will dispatch to a function that
133 // manipulates the state at the entrance to a block.
134
Ted Kremenek425c08c2008-01-15 00:24:08 +0000135 GenerateNode(BlockEntrance(Blk), Pred->State, Pred);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000136}
137
138void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L,
139 ExplodedNodeImpl* Pred) {
140
141 if (Stmt* S = L.getFirstStmt()) {
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000142 GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000143 ProcessStmt(S, Builder);
144 }
Ted Kremenek425c08c2008-01-15 00:24:08 +0000145 else
146 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000147}
148
149
Ted Kremenek425c08c2008-01-15 00:24:08 +0000150void GREngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) {
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000151
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000152 if (Stmt* Term = B->getTerminator()) {
153 switch (Term->getStmtClass()) {
154 default:
155 assert(false && "Analysis for this terminator not implemented.");
156 break;
157
Ted Kremenekf233d482008-02-05 00:26:40 +0000158 case Stmt::ConditionalOperatorClass:
159 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
160 break;
161
162 case Stmt::ChooseExprClass:
163 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
164 break;
165
166 case Stmt::BinaryOperatorClass: // '&&' and '||'
167 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
168 break;
169
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000170 case Stmt::IfStmtClass:
171 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
172 break;
173
174 case Stmt::ForStmtClass:
175 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
176 break;
177
178 case Stmt::WhileStmtClass:
179 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
180 break;
181
182 case Stmt::DoStmtClass:
183 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
184 break;
185 }
186 }
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000187 else {
188 assert (B->succ_size() == 1 &&
189 "Blocks with no terminator should have at most 1 successor.");
190
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000191 GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000192 }
193}
194
Ted Kremenekf233d482008-02-05 00:26:40 +0000195void GREngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B,
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000196 ExplodedNodeImpl* Pred) {
197 assert (B->succ_size() == 2);
198
199 GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
200 Pred, this);
201
202 ProcessBranch(Cond, Term, Builder);
203}
204
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000205void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B,
206 unsigned StmtIdx, ExplodedNodeImpl* Pred) {
207
208 assert (!B->empty());
209
Ted Kremenek425c08c2008-01-15 00:24:08 +0000210 if (StmtIdx == B->size())
211 HandleBlockExit(B, Pred);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000212 else {
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000213 GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this);
Ted Kremenek160760e2008-01-16 22:13:19 +0000214 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000215 }
216}
217
218typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy;
219/// PopulateParentMap - Recurse the AST starting at 'Parent' and add the
220/// mappings between child and parent to ParentMap.
221static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) {
222 for (Stmt::child_iterator I=Parent->child_begin(),
223 E=Parent->child_end(); I!=E; ++I) {
224
225 assert (M.find(*I) == M.end());
226 M[*I] = Parent;
227 PopulateParentMap(*I, M);
228 }
229}
230
231/// GenerateNode - Utility method to generate nodes, hook up successors,
232/// and add nodes to the worklist.
233void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State,
234 ExplodedNodeImpl* Pred) {
235
236 bool IsNew;
237 ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew);
238
239 if (Pred)
240 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
241 else {
242 assert (IsNew);
243 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
244 }
245
246 // Only add 'Node' to the worklist if it was freshly generated.
247 if (IsNew) WList->Enqueue(GRWorkListUnit(Node));
248}
249
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000250GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx,
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000251 ExplodedNodeImpl* N, GREngineImpl* e)
252 : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) {
253 Deferred.insert(N);
254}
255
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000256GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() {
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000257 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000258 if (!(*I)->isSink())
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000259 GenerateAutoTransition(*I);
260}
261
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000262void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000263 assert (!N->isSink());
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000264
265 PostStmt Loc(getStmt());
266
267 if (Loc == N->getLocation()) {
268 // Note: 'N' should be a fresh node because otherwise it shouldn't be
269 // a member of Deferred.
270 Eng.WList->Enqueue(N, B, Idx+1);
271 return;
272 }
273
274 bool IsNew;
275 ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew);
276 Succ->addPredecessor(N);
277
278 if (IsNew)
279 Eng.WList->Enqueue(Succ, B, Idx+1);
280}
281
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000282ExplodedNodeImpl* GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State,
Ted Kremenekf24af5b2008-01-14 23:24:37 +0000283 ExplodedNodeImpl* Pred) {
284
285 bool IsNew;
286 ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew);
287 N->addPredecessor(Pred);
288 Deferred.erase(Pred);
289
290 HasGeneratedNode = true;
291
292 if (IsNew) {
293 Deferred.insert(N);
294 LastNode = N;
295 return N;
296 }
297
298 LastNode = NULL;
299 return NULL;
300}
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000301
Ted Kremenekb38911f2008-01-30 23:03:39 +0000302ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(void* State,
303 bool branch) {
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000304 bool IsNew;
305
306 ExplodedNodeImpl* Succ =
307 Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, branch ? DstT : DstF),
308 State, &IsNew);
309
310 Succ->addPredecessor(Pred);
311
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000312 if (branch) GeneratedTrue = true;
313 else GeneratedFalse = true;
314
Ted Kremenekb38911f2008-01-30 23:03:39 +0000315 if (IsNew) {
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000316 Deferred.push_back(Succ);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000317 return Succ;
318 }
319
320 return NULL;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000321}
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000322
323GRBranchNodeBuilderImpl::~GRBranchNodeBuilderImpl() {
324 if (!GeneratedTrue) generateNodeImpl(Pred->State, true);
325 if (!GeneratedFalse) generateNodeImpl(Pred->State, false);
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000326
327 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
328 if (!(*I)->isSink()) Eng.WList->Enqueue(GRWorkListUnit(*I));
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000329}