blob: cd20e3ca6654cd15ff8566ceada726972fcbc398 [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 Xue1190f72009-08-15 03:17:38 +0000149bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, 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.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000167 BlockEdge StartLoc(Entry, Succ, L);
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()))
Zhongxing Xue1190f72009-08-15 03:17:38 +0000233 GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()), 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
Zhongxing Xue1190f72009-08-15 03:17:38 +0000338 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
339 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000340}
341
Zhongxing Xu82003da2009-08-06 10:00:15 +0000342void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
343 ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000344 assert (B->succ_size() == 2);
345
Zhongxing Xu107f7592009-08-06 12:48:26 +0000346 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
347 Pred, this);
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000348
349 ProcessBranch(Cond, Term, Builder);
350}
351
Zhongxing Xu82003da2009-08-06 10:00:15 +0000352void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000353 unsigned StmtIdx, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000354
355 assert (!B->empty());
356
Ted Kremeneke5843592008-01-15 00:24:08 +0000357 if (StmtIdx == B->size())
358 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000359 else {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000360 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
361 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000362 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000363 }
364}
365
Ted Kremenek3e743662008-01-14 23:24:37 +0000366/// GenerateNode - Utility method to generate nodes, hook up successors,
367/// and add nodes to the worklist.
Zhongxing Xu82003da2009-08-06 10:00:15 +0000368void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
369 const GRState* State, ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000370
371 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000372 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000373
374 if (Pred)
375 Node->addPredecessor(Pred); // Link 'Node' with its predecessor.
376 else {
377 assert (IsNew);
378 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
379 }
380
381 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000382 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000383}
384
Zhongxing Xu107f7592009-08-06 12:48:26 +0000385GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
386 ExplodedNode* N, GRCoreEngine* e,
387 GRStateManager &mgr)
388 : Eng(*e), B(*b), Idx(idx), Pred(N), LastNode(N), Mgr(mgr), Auditor(0),
389 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
390 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000391 Deferred.insert(N);
Zhongxing Xu107f7592009-08-06 12:48:26 +0000392 CleanedState = getLastNode()->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000393}
394
Zhongxing Xu107f7592009-08-06 12:48:26 +0000395GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000396 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000397 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000398 GenerateAutoTransition(*I);
399}
400
Zhongxing Xu107f7592009-08-06 12:48:26 +0000401void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000402 assert (!N->isSink());
Ted Kremenek3e743662008-01-14 23:24:37 +0000403
Zhongxing Xue1190f72009-08-15 03:17:38 +0000404 PostStmt Loc(getStmt(), N->getLocationContext());
Ted Kremenek3e743662008-01-14 23:24:37 +0000405
406 if (Loc == N->getLocation()) {
407 // Note: 'N' should be a fresh node because otherwise it shouldn't be
408 // a member of Deferred.
409 Eng.WList->Enqueue(N, B, Idx+1);
410 return;
411 }
412
413 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000414 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000415 Succ->addPredecessor(N);
416
417 if (IsNew)
418 Eng.WList->Enqueue(Succ, B, Idx+1);
419}
420
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000421static inline PostStmt GetPostLoc(const Stmt* S, ProgramPoint::Kind K,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000422 const LocationContext *L, const void *tag) {
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000423 switch (K) {
424 default:
425 assert(false && "Invalid PostXXXKind.");
426
427 case ProgramPoint::PostStmtKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000428 return PostStmt(S, L, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000429
430 case ProgramPoint::PostLoadKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000431 return PostLoad(S, L, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000432
433 case ProgramPoint::PostUndefLocationCheckFailedKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000434 return PostUndefLocationCheckFailed(S, L, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000435
436 case ProgramPoint::PostLocationChecksSucceedKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000437 return PostLocationChecksSucceed(S, L, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000438
439 case ProgramPoint::PostOutOfBoundsCheckFailedKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000440 return PostOutOfBoundsCheckFailed(S, L, tag);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000441
442 case ProgramPoint::PostNullCheckFailedKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000443 return PostNullCheckFailed(S, L, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000444
Ted Kremeneka1966182008-10-17 20:49:23 +0000445 case ProgramPoint::PostStoreKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000446 return PostStore(S, L, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000447
Ted Kremeneka6e08322009-05-07 18:27:16 +0000448 case ProgramPoint::PostLValueKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000449 return PostLValue(S, L, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000450
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000451 case ProgramPoint::PostPurgeDeadSymbolsKind:
Zhongxing Xue1190f72009-08-15 03:17:38 +0000452 return PostPurgeDeadSymbols(S, L, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000453 }
454}
455
Zhongxing Xu20227f72009-08-06 01:32:16 +0000456ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000457GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000458 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000459 ProgramPoint::Kind K,
460 const void *tag) {
Ted Kremenek27760792009-07-22 21:40:46 +0000461 return K == ProgramPoint::PreStmtKind
Zhongxing Xue1190f72009-08-15 03:17:38 +0000462 ? generateNodeInternal(PreStmt(S, Pred->getLocationContext(),tag),
463 State, Pred)
464 : generateNodeInternal(GetPostLoc(S, K, Pred->getLocationContext(), tag),
465 State, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000466}
467
Zhongxing Xu20227f72009-08-06 01:32:16 +0000468ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000469GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000470 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000471 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000472 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000473 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenek3e743662008-01-14 23:24:37 +0000474 N->addPredecessor(Pred);
475 Deferred.erase(Pred);
476
Ted Kremenek3e743662008-01-14 23:24:37 +0000477 if (IsNew) {
478 Deferred.insert(N);
479 LastNode = N;
480 return N;
481 }
482
483 LastNode = NULL;
484 return NULL;
485}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000486
Zhongxing Xu107f7592009-08-06 12:48:26 +0000487ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
488 bool branch) {
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000489
490 // If the branch has been marked infeasible we should not generate a node.
491 if (!isFeasible(branch))
492 return NULL;
493
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000494 bool IsNew;
495
Zhongxing Xu20227f72009-08-06 01:32:16 +0000496 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000497 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
498 State, &IsNew);
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000499
500 Succ->addPredecessor(Pred);
501
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000502 if (branch)
503 GeneratedTrue = true;
504 else
505 GeneratedFalse = true;
Ted Kremenek7ff18932008-01-29 23:32:35 +0000506
Ted Kremeneka50d9852008-01-30 23:03:39 +0000507 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000508 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000509 return Succ;
510 }
511
512 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000513}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000514
Zhongxing Xu107f7592009-08-06 12:48:26 +0000515GRBranchNodeBuilder::~GRBranchNodeBuilder() {
516 if (!GeneratedTrue) generateNode(Pred->State, true);
517 if (!GeneratedFalse) generateNode(Pred->State, false);
Ted Kremenek2531fce2008-01-30 23:24:39 +0000518
519 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000520 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000521}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000522
Ted Kremenek7022efb2008-02-13 00:24:44 +0000523
Zhongxing Xu20227f72009-08-06 01:32:16 +0000524ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000525GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
526 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000527 bool IsNew;
528
Zhongxing Xue1190f72009-08-15 03:17:38 +0000529 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
530 Pred->getLocationContext()), St, &IsNew);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000531
532 Succ->addPredecessor(Pred);
533
534 if (IsNew) {
535
536 if (isSink)
537 Succ->markAsSink();
538 else
539 Eng.WList->Enqueue(Succ);
540
541 return Succ;
542 }
543
544 return NULL;
545}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000546
547
Zhongxing Xu20227f72009-08-06 01:32:16 +0000548ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000549GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000550
551 bool IsNew;
552
Zhongxing Xue1190f72009-08-15 03:17:38 +0000553 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
554 Pred->getLocationContext()), St, &IsNew);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000555 Succ->addPredecessor(Pred);
556
557 if (IsNew) {
558 Eng.WList->Enqueue(Succ);
559 return Succ;
560 }
561
562 return NULL;
563}
564
565
Zhongxing Xu20227f72009-08-06 01:32:16 +0000566ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000567GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000568
569 // Get the block for the default case.
570 assert (Src->succ_rbegin() != Src->succ_rend());
571 CFGBlock* DefaultBlock = *Src->succ_rbegin();
572
573 bool IsNew;
574
Zhongxing Xue1190f72009-08-15 03:17:38 +0000575 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
576 Pred->getLocationContext()), St, &IsNew);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000577 Succ->addPredecessor(Pred);
578
579 if (IsNew) {
580 if (isSink)
581 Succ->markAsSink();
582 else
583 Eng.WList->Enqueue(Succ);
584
585 return Succ;
586 }
587
588 return NULL;
589}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000590
Zhongxing Xu107f7592009-08-06 12:48:26 +0000591GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000592 // Auto-generate an EOP node if one has not been generated.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000593 if (!HasGeneratedNode) generateNode(Pred->State);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000594}
595
Zhongxing Xu20227f72009-08-06 01:32:16 +0000596ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000597GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
598 ExplodedNode* P) {
Ted Kremenekc218c842009-05-08 23:08:34 +0000599 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000600 bool IsNew;
601
Zhongxing Xue1190f72009-08-15 03:17:38 +0000602 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
603 Pred->getLocationContext(), tag), State, &IsNew);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000604
Ted Kremenekc218c842009-05-08 23:08:34 +0000605 Node->addPredecessor(P ? P : Pred);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000606
607 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000608 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000609 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000610 }
611
Ted Kremenekd004c412008-04-18 16:30:14 +0000612 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000613}