blob: 66c12fd00f7810fca12c0895ec467864e5aa4823 [file] [log] [blame]
Ted Kremenek17810802008-11-12 19:24:17 +00001//==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-//
Ted Kremenek3e743662008-01-14 23:24:37 +00002//
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
Ted Kremenekf6c62f32008-02-13 17:41:41 +000015#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
Zhongxing Xu82003da2009-08-06 10:00:15 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000017#include "clang/AST/Expr.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/Casting.h"
20#include "llvm/ADT/DenseMap.h"
21#include <vector>
Ted Kremenekd9de9f12008-12-16 22:13:33 +000022#include <queue>
Ted Kremenek3e743662008-01-14 23:24:37 +000023
24using llvm::cast;
25using llvm::isa;
26using namespace clang;
27
Ted Kremenekd9de9f12008-12-16 22:13:33 +000028//===----------------------------------------------------------------------===//
29// Worklist classes for exploration of reachable states.
30//===----------------------------------------------------------------------===//
31
Ted Kremenek3e743662008-01-14 23:24:37 +000032namespace {
Zhongxing Xu69cc15e2009-07-15 09:04:01 +000033class VISIBILITY_HIDDEN DFS : public GRWorkList {
Ted Kremenek3e743662008-01-14 23:24:37 +000034 llvm::SmallVector<GRWorkListUnit,20> Stack;
35public:
36 virtual bool hasWork() const {
37 return !Stack.empty();
38 }
39
40 virtual void Enqueue(const GRWorkListUnit& U) {
41 Stack.push_back(U);
42 }
43
44 virtual GRWorkListUnit Dequeue() {
45 assert (!Stack.empty());
46 const GRWorkListUnit& U = Stack.back();
47 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
48 return U;
49 }
50};
Ted Kremenek2c327732009-05-01 22:18:46 +000051
52class VISIBILITY_HIDDEN BFS : public GRWorkList {
53 std::queue<GRWorkListUnit> Queue;
54public:
55 virtual bool hasWork() const {
56 return !Queue.empty();
57 }
58
59 virtual void Enqueue(const GRWorkListUnit& U) {
60 Queue.push(U);
61 }
62
63 virtual GRWorkListUnit Dequeue() {
64 // Don't use const reference. The subsequent pop_back() might make it
65 // unsafe.
66 GRWorkListUnit U = Queue.front();
67 Queue.pop();
68 return U;
69 }
70};
71
Ted Kremenek3e743662008-01-14 23:24:37 +000072} // end anonymous namespace
73
Ted Kremenek2e12c2e2008-01-16 18:18:48 +000074// Place the dstor for GRWorkList here because it contains virtual member
75// functions, and we the code for the dstor generated in one compilation unit.
76GRWorkList::~GRWorkList() {}
77
Ted Kremenek2c327732009-05-01 22:18:46 +000078GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
79GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
Ted Kremenek3e743662008-01-14 23:24:37 +000080
Ted Kremenekd9de9f12008-12-16 22:13:33 +000081namespace {
82 class VISIBILITY_HIDDEN BFSBlockDFSContents : public GRWorkList {
83 std::queue<GRWorkListUnit> Queue;
84 llvm::SmallVector<GRWorkListUnit,20> Stack;
85 public:
86 virtual bool hasWork() const {
87 return !Queue.empty() || !Stack.empty();
88 }
89
90 virtual void Enqueue(const GRWorkListUnit& U) {
91 if (isa<BlockEntrance>(U.getNode()->getLocation()))
92 Queue.push(U);
93 else
94 Stack.push_back(U);
95 }
96
97 virtual GRWorkListUnit Dequeue() {
98 // Process all basic blocks to completion.
99 if (!Stack.empty()) {
100 const GRWorkListUnit& U = Stack.back();
101 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
102 return U;
103 }
104
105 assert(!Queue.empty());
106 // Don't use const reference. The subsequent pop_back() might make it
107 // unsafe.
108 GRWorkListUnit U = Queue.front();
109 Queue.pop();
110 return U;
111 }
112 };
113} // end anonymous namespace
114
115GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
116 return new BFSBlockDFSContents();
117}
118
119//===----------------------------------------------------------------------===//
120// Core analysis engine.
121//===----------------------------------------------------------------------===//
Zhongxing Xu107f7592009-08-06 12:48:26 +0000122void GRCoreEngine::ProcessEndPath(GREndPathNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000123 SubEngine.ProcessEndPath(Builder);
124}
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000125
Zhongxing Xu107f7592009-08-06 12:48:26 +0000126void GRCoreEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000127 SubEngine.ProcessStmt(S, Builder);
128}
129
130bool GRCoreEngine::ProcessBlockEntrance(CFGBlock* Blk, const GRState* State,
131 GRBlockCounter BC) {
132 return SubEngine.ProcessBlockEntrance(Blk, State, BC);
133}
134
135void GRCoreEngine::ProcessBranch(Stmt* Condition, Stmt* Terminator,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000136 GRBranchNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000137 SubEngine.ProcessBranch(Condition, Terminator, Builder);
138}
139
Zhongxing Xu107f7592009-08-06 12:48:26 +0000140void GRCoreEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000141 SubEngine.ProcessIndirectGoto(Builder);
142}
143
Zhongxing Xu107f7592009-08-06 12:48:26 +0000144void GRCoreEngine::ProcessSwitch(GRSwitchNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000145 SubEngine.ProcessSwitch(Builder);
146}
Zhongxing Xu107f7592009-08-06 12:48:26 +0000147
Ted Kremenek3e743662008-01-14 23:24:37 +0000148/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Zhongxing Xu82003da2009-08-06 10:00:15 +0000149bool GRCoreEngine::ExecuteWorkList(unsigned Steps) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000150
151 if (G->num_roots() == 0) { // Initialize the analysis by constructing
152 // the root if none exists.
153
Ted Kremenek997d8722008-01-29 00:33:40 +0000154 CFGBlock* Entry = &getCFG().getEntry();
Ted Kremenek3e743662008-01-14 23:24:37 +0000155
156 assert (Entry->empty() &&
157 "Entry block must be empty.");
158
159 assert (Entry->succ_size() == 1 &&
160 "Entry block must have 1 successor.");
161
162 // Get the solitary successor.
163 CFGBlock* Succ = *(Entry->succ_begin());
164
165 // Construct an edge representing the
166 // starting location in the function.
Ted Kremenek0ecb53a2008-09-16 18:44:52 +0000167 BlockEdge StartLoc(Entry, Succ);
Ted Kremenek3e743662008-01-14 23:24:37 +0000168
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000169 // Set the current block counter to being empty.
170 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
171
Ted Kremenek3e743662008-01-14 23:24:37 +0000172 // Generate the root.
Ted Kremenekaf665822008-08-26 22:34:23 +0000173 GenerateNode(StartLoc, getInitialState(), 0);
Ted Kremenek3e743662008-01-14 23:24:37 +0000174 }
175
176 while (Steps && WList->hasWork()) {
177 --Steps;
178 const GRWorkListUnit& WU = WList->Dequeue();
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000179
180 // Set the current block counter.
181 WList->setBlockCounter(WU.getBlockCounter());
182
183 // Retrieve the node.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000184 ExplodedNode* Node = WU.getNode();
Ted Kremenek3e743662008-01-14 23:24:37 +0000185
186 // Dispatch on the location type.
187 switch (Node->getLocation().getKind()) {
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000188 case ProgramPoint::BlockEdgeKind:
Ted Kremenek3e743662008-01-14 23:24:37 +0000189 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
190 break;
191
192 case ProgramPoint::BlockEntranceKind:
193 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
194 break;
195
196 case ProgramPoint::BlockExitKind:
Ted Kremeneke5843592008-01-15 00:24:08 +0000197 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000198 break;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000199
200 default:
201 assert(isa<PostStmt>(Node->getLocation()));
Ted Kremenek3e743662008-01-14 23:24:37 +0000202 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
203 WU.getIndex(), Node);
204 break;
205 }
206 }
207
208 return WList->hasWork();
209}
210
Zhongxing Xu82003da2009-08-06 10:00:15 +0000211
212void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000213
214 CFGBlock* Blk = L.getDst();
215
216 // Check if we are entering the EXIT block.
Ted Kremenek997d8722008-01-29 00:33:40 +0000217 if (Blk == &getCFG().getExit()) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000218
Ted Kremenek997d8722008-01-29 00:33:40 +0000219 assert (getCFG().getExit().size() == 0
220 && "EXIT block cannot contain Stmts.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000221
Ted Kremenek811c2b42008-04-11 22:03:04 +0000222 // Process the final state transition.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000223 GREndPathNodeBuilder Builder(Blk, Pred, this);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000224 ProcessEndPath(Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000225
Ted Kremenek3e743662008-01-14 23:24:37 +0000226 // This path is done. Don't enqueue any more nodes.
227 return;
228 }
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000229
230 // FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
Ted Kremenek3e743662008-01-14 23:24:37 +0000231
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000232 if (ProcessBlockEntrance(Blk, Pred->State, WList->getBlockCounter()))
233 GenerateNode(BlockEntrance(Blk), Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000234}
235
Zhongxing Xu82003da2009-08-06 10:00:15 +0000236void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000237 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000238
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000239 // Increment the block counter.
240 GRBlockCounter Counter = WList->getBlockCounter();
241 Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID());
242 WList->setBlockCounter(Counter);
243
244 // Process the entrance of the block.
Ted Kremenek3e743662008-01-14 23:24:37 +0000245 if (Stmt* S = L.getFirstStmt()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000246 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
247 SubEngine.getStateManager());
Ted Kremenek3e743662008-01-14 23:24:37 +0000248 ProcessStmt(S, Builder);
249 }
Ted Kremeneke5843592008-01-15 00:24:08 +0000250 else
251 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000252}
253
Zhongxing Xu82003da2009-08-06 10:00:15 +0000254void GRCoreEngine::HandleBlockExit(CFGBlock * B, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000255
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000256 if (Stmt* Term = B->getTerminator()) {
257 switch (Term->getStmtClass()) {
258 default:
259 assert(false && "Analysis for this terminator not implemented.");
260 break;
Ted Kremenek822f7372008-02-12 21:51:20 +0000261
262 case Stmt::BinaryOperatorClass: // '&&' and '||'
263 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
264 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000265
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000266 case Stmt::ConditionalOperatorClass:
267 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000268 return;
269
270 // FIXME: Use constant-folding in CFG construction to simplify this
271 // case.
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000272
273 case Stmt::ChooseExprClass:
274 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000275 return;
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000276
Ted Kremenek822f7372008-02-12 21:51:20 +0000277 case Stmt::DoStmtClass:
278 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
279 return;
280
281 case Stmt::ForStmtClass:
282 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
283 return;
Ted Kremenek632bcb82008-02-13 16:56:51 +0000284
285 case Stmt::ContinueStmtClass:
286 case Stmt::BreakStmtClass:
287 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000288 break;
289
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000290 case Stmt::IfStmtClass:
291 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000292 return;
Ted Kremenek7022efb2008-02-13 00:24:44 +0000293
294 case Stmt::IndirectGotoStmtClass: {
295 // Only 1 successor: the indirect goto dispatch block.
296 assert (B->succ_size() == 1);
297
Zhongxing Xu107f7592009-08-06 12:48:26 +0000298 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000299 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
300 *(B->succ_begin()), this);
301
302 ProcessIndirectGoto(builder);
303 return;
304 }
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000305
Ted Kremenek17810802008-11-12 19:24:17 +0000306 case Stmt::ObjCForCollectionStmtClass: {
307 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
308 //
309 // (1) inside a basic block, which represents the binding of the
310 // 'element' variable to a value.
311 // (2) in a terminator, which represents the branch.
312 //
313 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
314 // whether or not collection contains any more elements. We cannot
315 // just test to see if the element is nil because a container can
316 // contain nil elements.
317 HandleBranch(Term, Term, B, Pred);
318 return;
319 }
320
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000321 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000322 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
323 this);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000324
325 ProcessSwitch(builder);
326 return;
327 }
328
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000329 case Stmt::WhileStmtClass:
330 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000331 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000332 }
333 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000334
335 assert (B->succ_size() == 1 &&
336 "Blocks with no terminator should have at most 1 successor.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000337
Ted Kremenek0ecb53a2008-09-16 18:44:52 +0000338 GenerateNode(BlockEdge(B, *(B->succ_begin())), Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000339}
340
Zhongxing Xu82003da2009-08-06 10:00:15 +0000341void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
342 ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000343 assert (B->succ_size() == 2);
344
Zhongxing Xu107f7592009-08-06 12:48:26 +0000345 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
346 Pred, this);
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000347
348 ProcessBranch(Cond, Term, Builder);
349}
350
Zhongxing Xu82003da2009-08-06 10:00:15 +0000351void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000352 unsigned StmtIdx, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000353
354 assert (!B->empty());
355
Ted Kremeneke5843592008-01-15 00:24:08 +0000356 if (StmtIdx == B->size())
357 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000358 else {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000359 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
360 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000361 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000362 }
363}
364
Ted Kremenek3e743662008-01-14 23:24:37 +0000365/// GenerateNode - Utility method to generate nodes, hook up successors,
366/// and add nodes to the worklist.
Zhongxing Xu82003da2009-08-06 10:00:15 +0000367void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
368 const GRState* State, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000369
370 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000371 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000372
373 if (Pred)
374 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
375 else {
376 assert (IsNew);
377 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
378 }
379
380 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000381 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000382}
383
Zhongxing Xu107f7592009-08-06 12:48:26 +0000384GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
385 ExplodedNode* N, GRCoreEngine* e,
386 GRStateManager &mgr)
387 : Eng(*e), B(*b), Idx(idx), Pred(N), LastNode(N), Mgr(mgr), Auditor(0),
388 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
389 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000390 Deferred.insert(N);
Zhongxing Xu107f7592009-08-06 12:48:26 +0000391 CleanedState = getLastNode()->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000392}
393
Zhongxing Xu107f7592009-08-06 12:48:26 +0000394GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000395 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000396 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000397 GenerateAutoTransition(*I);
398}
399
Zhongxing Xu107f7592009-08-06 12:48:26 +0000400void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000401 assert (!N->isSink());
Ted Kremenek3e743662008-01-14 23:24:37 +0000402
403 PostStmt Loc(getStmt());
404
405 if (Loc == N->getLocation()) {
406 // Note: 'N' should be a fresh node because otherwise it shouldn't be
407 // a member of Deferred.
408 Eng.WList->Enqueue(N, B, Idx+1);
409 return;
410 }
411
412 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000413 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000414 Succ->addPredecessor(N);
415
416 if (IsNew)
417 Eng.WList->Enqueue(Succ, B, Idx+1);
418}
419
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000420static inline PostStmt GetPostLoc(const Stmt* S, ProgramPoint::Kind K,
Ted Kremenekdf240002009-04-11 00:11:10 +0000421 const void *tag) {
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000422 switch (K) {
423 default:
424 assert(false && "Invalid PostXXXKind.");
425
426 case ProgramPoint::PostStmtKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000427 return PostStmt(S, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000428
429 case ProgramPoint::PostLoadKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000430 return PostLoad(S, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000431
432 case ProgramPoint::PostUndefLocationCheckFailedKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000433 return PostUndefLocationCheckFailed(S, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000434
435 case ProgramPoint::PostLocationChecksSucceedKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000436 return PostLocationChecksSucceed(S, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000437
438 case ProgramPoint::PostOutOfBoundsCheckFailedKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000439 return PostOutOfBoundsCheckFailed(S, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000440
441 case ProgramPoint::PostNullCheckFailedKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000442 return PostNullCheckFailed(S, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000443
Ted Kremeneka1966182008-10-17 20:49:23 +0000444 case ProgramPoint::PostStoreKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000445 return PostStore(S, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000446
Ted Kremeneka6e08322009-05-07 18:27:16 +0000447 case ProgramPoint::PostLValueKind:
448 return PostLValue(S, tag);
449
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000450 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekdf240002009-04-11 00:11:10 +0000451 return PostPurgeDeadSymbols(S, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000452 }
453}
454
Zhongxing Xu20227f72009-08-06 01:32:16 +0000455ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000456GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000457 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000458 ProgramPoint::Kind K,
459 const void *tag) {
Ted Kremenek27760792009-07-22 21:40:46 +0000460 return K == ProgramPoint::PreStmtKind
Zhongxing Xu107f7592009-08-06 12:48:26 +0000461 ? generateNodeInternal(PreStmt(S, tag), State, Pred)
462 : generateNodeInternal(GetPostLoc(S, K, tag), State, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000463}
464
Zhongxing Xu20227f72009-08-06 01:32:16 +0000465ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000466GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000467 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000468 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000469 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000470 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000471 N->addPredecessor(Pred);
472 Deferred.erase(Pred);
473
Ted Kremenek3e743662008-01-14 23:24:37 +0000474 if (IsNew) {
475 Deferred.insert(N);
476 LastNode = N;
477 return N;
478 }
479
480 LastNode = NULL;
481 return NULL;
482}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000483
Zhongxing Xu107f7592009-08-06 12:48:26 +0000484ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
485 bool branch) {
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000486
487 // If the branch has been marked infeasible we should not generate a node.
488 if (!isFeasible(branch))
489 return NULL;
490
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000491 bool IsNew;
492
Zhongxing Xu20227f72009-08-06 01:32:16 +0000493 ExplodedNode* Succ =
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000494 Eng.G->getNode(BlockEdge(Src, branch ? DstT : DstF), State, &IsNew);
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000495
496 Succ->addPredecessor(Pred);
497
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000498 if (branch)
499 GeneratedTrue = true;
500 else
501 GeneratedFalse = true;
Ted Kremenek7ff18932008-01-29 23:32:35 +0000502
Ted Kremeneka50d9852008-01-30 23:03:39 +0000503 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000504 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000505 return Succ;
506 }
507
508 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000509}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000510
Zhongxing Xu107f7592009-08-06 12:48:26 +0000511GRBranchNodeBuilder::~GRBranchNodeBuilder() {
512 if (!GeneratedTrue) generateNode(Pred->State, true);
513 if (!GeneratedFalse) generateNode(Pred->State, false);
Ted Kremenek2531fce2008-01-30 23:24:39 +0000514
515 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000516 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000517}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000518
Ted Kremenek7022efb2008-02-13 00:24:44 +0000519
Zhongxing Xu20227f72009-08-06 01:32:16 +0000520ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000521GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
522 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000523 bool IsNew;
524
Zhongxing Xu107f7592009-08-06 12:48:26 +0000525 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock()), St, &IsNew);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000526
527 Succ->addPredecessor(Pred);
528
529 if (IsNew) {
530
531 if (isSink)
532 Succ->markAsSink();
533 else
534 Eng.WList->Enqueue(Succ);
535
536 return Succ;
537 }
538
539 return NULL;
540}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000541
542
Zhongxing Xu20227f72009-08-06 01:32:16 +0000543ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000544GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000545
546 bool IsNew;
547
Zhongxing Xu107f7592009-08-06 12:48:26 +0000548 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock()), St, &IsNew);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000549 Succ->addPredecessor(Pred);
550
551 if (IsNew) {
552 Eng.WList->Enqueue(Succ);
553 return Succ;
554 }
555
556 return NULL;
557}
558
559
Zhongxing Xu20227f72009-08-06 01:32:16 +0000560ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000561GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000562
563 // Get the block for the default case.
564 assert (Src->succ_rbegin() != Src->succ_rend());
565 CFGBlock* DefaultBlock = *Src->succ_rbegin();
566
567 bool IsNew;
568
Zhongxing Xu107f7592009-08-06 12:48:26 +0000569 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock), St, &IsNew);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000570 Succ->addPredecessor(Pred);
571
572 if (IsNew) {
573 if (isSink)
574 Succ->markAsSink();
575 else
576 Eng.WList->Enqueue(Succ);
577
578 return Succ;
579 }
580
581 return NULL;
582}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000583
Zhongxing Xu107f7592009-08-06 12:48:26 +0000584GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000585 // Auto-generate an EOP node if one has not been generated.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000586 if (!HasGeneratedNode) generateNode(Pred->State);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000587}
588
Zhongxing Xu20227f72009-08-06 01:32:16 +0000589ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000590GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
591 ExplodedNode* P) {
Ted Kremenekc218c842009-05-08 23:08:34 +0000592 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000593 bool IsNew;
594
Zhongxing Xu107f7592009-08-06 12:48:26 +0000595 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B, tag), State, &IsNew);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000596
Ted Kremenekc218c842009-05-08 23:08:34 +0000597 Node->addPredecessor(P ? P : Pred);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000598
599 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000600 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000601 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000602 }
603
Ted Kremenekd004c412008-04-18 16:30:14 +0000604 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000605}