1 //==- CoreEngine.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/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/StmtCXX.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/Casting.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 #define DEBUG_TYPE "CoreEngine"
28 
29 STATISTIC(NumSteps,
30             "The # of steps executed.");
31 STATISTIC(NumReachedMaxSteps,
32             "The # of times we reached the max number of steps.");
33 STATISTIC(NumPathsExplored,
34             "The # of paths explored by the analyzer.");
35 
36 //===----------------------------------------------------------------------===//
37 // Worklist classes for exploration of reachable states.
38 //===----------------------------------------------------------------------===//
39 
40 namespace {
41 class DFS : public WorkList {
42   SmallVector<WorkListUnit,20> Stack;
43 public:
44   bool hasWork() const override {
45     return !Stack.empty();
46   }
47 
48   void enqueue(const WorkListUnit& U) override {
49     Stack.push_back(U);
50   }
51 
52   WorkListUnit dequeue() override {
53     assert (!Stack.empty());
54     const WorkListUnit& U = Stack.back();
55     Stack.pop_back(); // This technically "invalidates" U, but we are fine.
56     return U;
57   }
58 };
59 
60 class BFS : public WorkList {
61   std::deque<WorkListUnit> Queue;
62 public:
63   bool hasWork() const override {
64     return !Queue.empty();
65   }
66 
67   void enqueue(const WorkListUnit& U) override {
68     Queue.push_back(U);
69   }
70 
71   WorkListUnit dequeue() override {
72     WorkListUnit U = Queue.front();
73     Queue.pop_front();
74     return U;
75   }
76 
77 };
78 
79 } // end anonymous namespace
80 
81 // Place the dstor for WorkList here because it contains virtual member
82 // functions, and we the code for the dstor generated in one compilation unit.
83 WorkList::~WorkList() {}
84 
85 WorkList *WorkList::makeDFS() { return new DFS(); }
86 WorkList *WorkList::makeBFS() { return new BFS(); }
87 
88 namespace {
89   class BFSBlockDFSContents : public WorkList {
90     std::deque<WorkListUnit> Queue;
91     SmallVector<WorkListUnit,20> Stack;
92   public:
93     bool hasWork() const override {
94       return !Queue.empty() || !Stack.empty();
95     }
96 
97     void enqueue(const WorkListUnit& U) override {
98       if (U.getNode()->getLocation().getAs<BlockEntrance>())
99         Queue.push_front(U);
100       else
101         Stack.push_back(U);
102     }
103 
104     WorkListUnit dequeue() override {
105       // Process all basic blocks to completion.
106       if (!Stack.empty()) {
107         const WorkListUnit& U = Stack.back();
108         Stack.pop_back(); // This technically "invalidates" U, but we are fine.
109         return U;
110       }
111 
112       assert(!Queue.empty());
113       // Don't use const reference.  The subsequent pop_back() might make it
114       // unsafe.
115       WorkListUnit U = Queue.front();
116       Queue.pop_front();
117       return U;
118     }
119   };
120 } // end anonymous namespace
121 
122 WorkList* WorkList::makeBFSBlockDFSContents() {
123   return new BFSBlockDFSContents();
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // Core analysis engine.
128 //===----------------------------------------------------------------------===//
129 
130 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
131 bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
132                                    ProgramStateRef InitState) {
133 
134   if (G.num_roots() == 0) { // Initialize the analysis by constructing
135     // the root if none exists.
136 
137     const CFGBlock *Entry = &(L->getCFG()->getEntry());
138 
139     assert (Entry->empty() &&
140             "Entry block must be empty.");
141 
142     assert (Entry->succ_size() == 1 &&
143             "Entry block must have 1 successor.");
144 
145     // Mark the entry block as visited.
146     FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
147                                              L->getDecl(),
148                                              L->getCFG()->getNumBlockIDs());
149 
150     // Get the solitary successor.
151     const CFGBlock *Succ = *(Entry->succ_begin());
152 
153     // Construct an edge representing the
154     // starting location in the function.
155     BlockEdge StartLoc(Entry, Succ, L);
156 
157     // Set the current block counter to being empty.
158     WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
159 
160     if (!InitState)
161       InitState = SubEng.getInitialState(L);
162 
163     bool IsNew;
164     ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
165     assert (IsNew);
166     G.addRoot(Node);
167 
168     NodeBuilderContext BuilderCtx(*this, StartLoc.getDst(), Node);
169     ExplodedNodeSet DstBegin;
170     SubEng.processBeginOfFunction(BuilderCtx, Node, DstBegin, StartLoc);
171 
172     enqueue(DstBegin);
173   }
174 
175   // Check if we have a steps limit
176   bool UnlimitedSteps = Steps == 0;
177   // Cap our pre-reservation in the event that the user specifies
178   // a very large number of maximum steps.
179   const unsigned PreReservationCap = 4000000;
180   if(!UnlimitedSteps)
181     G.reserve(std::min(Steps,PreReservationCap));
182 
183   while (WList->hasWork()) {
184     if (!UnlimitedSteps) {
185       if (Steps == 0) {
186         NumReachedMaxSteps++;
187         break;
188       }
189       --Steps;
190     }
191 
192     NumSteps++;
193 
194     const WorkListUnit& WU = WList->dequeue();
195 
196     // Set the current block counter.
197     WList->setBlockCounter(WU.getBlockCounter());
198 
199     // Retrieve the node.
200     ExplodedNode *Node = WU.getNode();
201 
202     dispatchWorkItem(Node, Node->getLocation(), WU);
203   }
204   SubEng.processEndWorklist(hasWorkRemaining());
205   return WList->hasWork();
206 }
207 
208 void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
209                                   const WorkListUnit& WU) {
210   // Dispatch on the location type.
211   switch (Loc.getKind()) {
212     case ProgramPoint::BlockEdgeKind:
213       HandleBlockEdge(Loc.castAs<BlockEdge>(), Pred);
214       break;
215 
216     case ProgramPoint::BlockEntranceKind:
217       HandleBlockEntrance(Loc.castAs<BlockEntrance>(), Pred);
218       break;
219 
220     case ProgramPoint::BlockExitKind:
221       assert (false && "BlockExit location never occur in forward analysis.");
222       break;
223 
224     case ProgramPoint::CallEnterKind: {
225       HandleCallEnter(Loc.castAs<CallEnter>(), Pred);
226       break;
227     }
228 
229     case ProgramPoint::CallExitBeginKind:
230       SubEng.processCallExit(Pred);
231       break;
232 
233     case ProgramPoint::EpsilonKind: {
234       assert(Pred->hasSinglePred() &&
235              "Assume epsilon has exactly one predecessor by construction");
236       ExplodedNode *PNode = Pred->getFirstPred();
237       dispatchWorkItem(Pred, PNode->getLocation(), WU);
238       break;
239     }
240     default:
241       assert(Loc.getAs<PostStmt>() ||
242              Loc.getAs<PostInitializer>() ||
243              Loc.getAs<PostImplicitCall>() ||
244              Loc.getAs<CallExitEnd>() ||
245              Loc.getAs<LoopExit>() ||
246              Loc.getAs<PostAllocatorCall>());
247       HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
248       break;
249   }
250 }
251 
252 bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
253                                                  unsigned Steps,
254                                                  ProgramStateRef InitState,
255                                                  ExplodedNodeSet &Dst) {
256   bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
257   for (ExplodedGraph::eop_iterator I = G.eop_begin(), E = G.eop_end(); I != E;
258        ++I) {
259     Dst.Add(*I);
260   }
261   return DidNotFinish;
262 }
263 
264 void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
265 
266   const CFGBlock *Blk = L.getDst();
267   NodeBuilderContext BuilderCtx(*this, Blk, Pred);
268 
269   // Mark this block as visited.
270   const LocationContext *LC = Pred->getLocationContext();
271   FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
272                                            LC->getDecl(),
273                                            LC->getCFG()->getNumBlockIDs());
274 
275   // Check if we are entering the EXIT block.
276   if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
277 
278     assert (L.getLocationContext()->getCFG()->getExit().size() == 0
279             && "EXIT block cannot contain Stmts.");
280 
281     // Get return statement..
282     const ReturnStmt *RS = nullptr;
283     if (!L.getSrc()->empty()) {
284       if (Optional<CFGStmt> LastStmt = L.getSrc()->back().getAs<CFGStmt>()) {
285         if ((RS = dyn_cast<ReturnStmt>(LastStmt->getStmt()))) {
286           if (!RS->getRetValue())
287             RS = nullptr;
288         }
289       }
290     }
291 
292     // Process the final state transition.
293     SubEng.processEndOfFunction(BuilderCtx, Pred, RS);
294 
295     // This path is done. Don't enqueue any more nodes.
296     return;
297   }
298 
299   // Call into the SubEngine to process entering the CFGBlock.
300   ExplodedNodeSet dstNodes;
301   BlockEntrance BE(Blk, Pred->getLocationContext());
302   NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
303   SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred);
304 
305   // Auto-generate a node.
306   if (!nodeBuilder.hasGeneratedNodes()) {
307     nodeBuilder.generateNode(Pred->State, Pred);
308   }
309 
310   // Enqueue nodes onto the worklist.
311   enqueue(dstNodes);
312 }
313 
314 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
315                                        ExplodedNode *Pred) {
316 
317   // Increment the block counter.
318   const LocationContext *LC = Pred->getLocationContext();
319   unsigned BlockId = L.getBlock()->getBlockID();
320   BlockCounter Counter = WList->getBlockCounter();
321   Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(),
322                                            BlockId);
323   WList->setBlockCounter(Counter);
324 
325   // Process the entrance of the block.
326   if (Optional<CFGElement> E = L.getFirstElement()) {
327     NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
328     SubEng.processCFGElement(*E, Pred, 0, &Ctx);
329   }
330   else
331     HandleBlockExit(L.getBlock(), Pred);
332 }
333 
334 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
335 
336   if (const Stmt *Term = B->getTerminator()) {
337     switch (Term->getStmtClass()) {
338       default:
339         llvm_unreachable("Analysis for this terminator not implemented.");
340 
341       case Stmt::CXXBindTemporaryExprClass:
342         HandleCleanupTemporaryBranch(
343             cast<CXXBindTemporaryExpr>(B->getTerminator().getStmt()), B, Pred);
344         return;
345 
346       // Model static initializers.
347       case Stmt::DeclStmtClass:
348         HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
349         return;
350 
351       case Stmt::BinaryOperatorClass: // '&&' and '||'
352         HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
353         return;
354 
355       case Stmt::BinaryConditionalOperatorClass:
356       case Stmt::ConditionalOperatorClass:
357         HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
358                      Term, B, Pred);
359         return;
360 
361         // FIXME: Use constant-folding in CFG construction to simplify this
362         // case.
363 
364       case Stmt::ChooseExprClass:
365         HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
366         return;
367 
368       case Stmt::CXXTryStmtClass: {
369         // Generate a node for each of the successors.
370         // Our logic for EH analysis can certainly be improved.
371         for (CFGBlock::const_succ_iterator it = B->succ_begin(),
372              et = B->succ_end(); it != et; ++it) {
373           if (const CFGBlock *succ = *it) {
374             generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
375                          Pred->State, Pred);
376           }
377         }
378         return;
379       }
380 
381       case Stmt::DoStmtClass:
382         HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
383         return;
384 
385       case Stmt::CXXForRangeStmtClass:
386         HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
387         return;
388 
389       case Stmt::ForStmtClass:
390         HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
391         return;
392 
393       case Stmt::ContinueStmtClass:
394       case Stmt::BreakStmtClass:
395       case Stmt::GotoStmtClass:
396         break;
397 
398       case Stmt::IfStmtClass:
399         HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
400         return;
401 
402       case Stmt::IndirectGotoStmtClass: {
403         // Only 1 successor: the indirect goto dispatch block.
404         assert (B->succ_size() == 1);
405 
406         IndirectGotoNodeBuilder
407            builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
408                    *(B->succ_begin()), this);
409 
410         SubEng.processIndirectGoto(builder);
411         return;
412       }
413 
414       case Stmt::ObjCForCollectionStmtClass: {
415         // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
416         //
417         //  (1) inside a basic block, which represents the binding of the
418         //      'element' variable to a value.
419         //  (2) in a terminator, which represents the branch.
420         //
421         // For (1), subengines will bind a value (i.e., 0 or 1) indicating
422         // whether or not collection contains any more elements.  We cannot
423         // just test to see if the element is nil because a container can
424         // contain nil elements.
425         HandleBranch(Term, Term, B, Pred);
426         return;
427       }
428 
429       case Stmt::SwitchStmtClass: {
430         SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
431                                     this);
432 
433         SubEng.processSwitch(builder);
434         return;
435       }
436 
437       case Stmt::WhileStmtClass:
438         HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
439         return;
440     }
441   }
442 
443   assert (B->succ_size() == 1 &&
444           "Blocks with no terminator should have at most 1 successor.");
445 
446   generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
447                Pred->State, Pred);
448 }
449 
450 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) {
451   NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred);
452   SubEng.processCallEnter(BuilderCtx, CE, Pred);
453 }
454 
455 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
456                                 const CFGBlock * B, ExplodedNode *Pred) {
457   assert(B->succ_size() == 2);
458   NodeBuilderContext Ctx(*this, B, Pred);
459   ExplodedNodeSet Dst;
460   SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
461                        *(B->succ_begin()), *(B->succ_begin()+1));
462   // Enqueue the new frontier onto the worklist.
463   enqueue(Dst);
464 }
465 
466 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
467                                               const CFGBlock *B,
468                                               ExplodedNode *Pred) {
469   assert(B->succ_size() == 2);
470   NodeBuilderContext Ctx(*this, B, Pred);
471   ExplodedNodeSet Dst;
472   SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()),
473                                        *(B->succ_begin() + 1));
474   // Enqueue the new frontier onto the worklist.
475   enqueue(Dst);
476 }
477 
478 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
479                                   ExplodedNode *Pred) {
480   assert(B->succ_size() == 2);
481   NodeBuilderContext Ctx(*this, B, Pred);
482   ExplodedNodeSet Dst;
483   SubEng.processStaticInitializer(DS, Ctx, Pred, Dst,
484                                   *(B->succ_begin()), *(B->succ_begin()+1));
485   // Enqueue the new frontier onto the worklist.
486   enqueue(Dst);
487 }
488 
489 
490 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
491                                   ExplodedNode *Pred) {
492   assert(B);
493   assert(!B->empty());
494 
495   if (StmtIdx == B->size())
496     HandleBlockExit(B, Pred);
497   else {
498     NodeBuilderContext Ctx(*this, B, Pred);
499     SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
500   }
501 }
502 
503 /// generateNode - Utility method to generate nodes, hook up successors,
504 ///  and add nodes to the worklist.
505 void CoreEngine::generateNode(const ProgramPoint &Loc,
506                               ProgramStateRef State,
507                               ExplodedNode *Pred) {
508 
509   bool IsNew;
510   ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew);
511 
512   if (Pred)
513     Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor.
514   else {
515     assert (IsNew);
516     G.addRoot(Node); // 'Node' has no predecessor.  Make it a root.
517   }
518 
519   // Only add 'Node' to the worklist if it was freshly generated.
520   if (IsNew) WList->enqueue(Node);
521 }
522 
523 void CoreEngine::enqueueStmtNode(ExplodedNode *N,
524                                  const CFGBlock *Block, unsigned Idx) {
525   assert(Block);
526   assert (!N->isSink());
527 
528   // Check if this node entered a callee.
529   if (N->getLocation().getAs<CallEnter>()) {
530     // Still use the index of the CallExpr. It's needed to create the callee
531     // StackFrameContext.
532     WList->enqueue(N, Block, Idx);
533     return;
534   }
535 
536   // Do not create extra nodes. Move to the next CFG element.
537   if (N->getLocation().getAs<PostInitializer>() ||
538       N->getLocation().getAs<PostImplicitCall>()||
539       N->getLocation().getAs<LoopExit>()) {
540     WList->enqueue(N, Block, Idx+1);
541     return;
542   }
543 
544   if (N->getLocation().getAs<EpsilonPoint>()) {
545     WList->enqueue(N, Block, Idx);
546     return;
547   }
548 
549   if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) {
550     WList->enqueue(N, Block, Idx+1);
551     return;
552   }
553 
554   // At this point, we know we're processing a normal statement.
555   CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>();
556   PostStmt Loc(CS.getStmt(), N->getLocationContext());
557 
558   if (Loc == N->getLocation().withTag(nullptr)) {
559     // Note: 'N' should be a fresh node because otherwise it shouldn't be
560     // a member of Deferred.
561     WList->enqueue(N, Block, Idx+1);
562     return;
563   }
564 
565   bool IsNew;
566   ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew);
567   Succ->addPredecessor(N, G);
568 
569   if (IsNew)
570     WList->enqueue(Succ, Block, Idx+1);
571 }
572 
573 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N,
574                                                     const ReturnStmt *RS) {
575   // Create a CallExitBegin node and enqueue it.
576   const StackFrameContext *LocCtx
577                          = cast<StackFrameContext>(N->getLocationContext());
578 
579   // Use the callee location context.
580   CallExitBegin Loc(LocCtx, RS);
581 
582   bool isNew;
583   ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew);
584   Node->addPredecessor(N, G);
585   return isNew ? Node : nullptr;
586 }
587 
588 
589 void CoreEngine::enqueue(ExplodedNodeSet &Set) {
590   for (ExplodedNodeSet::iterator I = Set.begin(),
591                                  E = Set.end(); I != E; ++I) {
592     WList->enqueue(*I);
593   }
594 }
595 
596 void CoreEngine::enqueue(ExplodedNodeSet &Set,
597                          const CFGBlock *Block, unsigned Idx) {
598   for (ExplodedNodeSet::iterator I = Set.begin(),
599                                  E = Set.end(); I != E; ++I) {
600     enqueueStmtNode(*I, Block, Idx);
601   }
602 }
603 
604 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS) {
605   for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
606     ExplodedNode *N = *I;
607     // If we are in an inlined call, generate CallExitBegin node.
608     if (N->getLocationContext()->getParent()) {
609       N = generateCallExitBeginNode(N, RS);
610       if (N)
611         WList->enqueue(N);
612     } else {
613       // TODO: We should run remove dead bindings here.
614       G.addEndOfPath(N);
615       NumPathsExplored++;
616     }
617   }
618 }
619 
620 
621 void NodeBuilder::anchor() { }
622 
623 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
624                                             ProgramStateRef State,
625                                             ExplodedNode *FromN,
626                                             bool MarkAsSink) {
627   HasGeneratedNodes = true;
628   bool IsNew;
629   ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew);
630   N->addPredecessor(FromN, C.Eng.G);
631   Frontier.erase(FromN);
632 
633   if (!IsNew)
634     return nullptr;
635 
636   if (!MarkAsSink)
637     Frontier.Add(N);
638 
639   return N;
640 }
641 
642 void NodeBuilderWithSinks::anchor() { }
643 
644 StmtNodeBuilder::~StmtNodeBuilder() {
645   if (EnclosingBldr)
646     for (ExplodedNodeSet::iterator I = Frontier.begin(),
647                                    E = Frontier.end(); I != E; ++I )
648       EnclosingBldr->addNodes(*I);
649 }
650 
651 void BranchNodeBuilder::anchor() { }
652 
653 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
654                                               bool branch,
655                                               ExplodedNode *NodePred) {
656   // If the branch has been marked infeasible we should not generate a node.
657   if (!isFeasible(branch))
658     return nullptr;
659 
660   ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
661                                NodePred->getLocationContext());
662   ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
663   return Succ;
664 }
665 
666 ExplodedNode*
667 IndirectGotoNodeBuilder::generateNode(const iterator &I,
668                                       ProgramStateRef St,
669                                       bool IsSink) {
670   bool IsNew;
671   ExplodedNode *Succ =
672       Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
673                     St, IsSink, &IsNew);
674   Succ->addPredecessor(Pred, Eng.G);
675 
676   if (!IsNew)
677     return nullptr;
678 
679   if (!IsSink)
680     Eng.WList->enqueue(Succ);
681 
682   return Succ;
683 }
684 
685 
686 ExplodedNode*
687 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
688                                         ProgramStateRef St) {
689 
690   bool IsNew;
691   ExplodedNode *Succ =
692       Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
693                     St, false, &IsNew);
694   Succ->addPredecessor(Pred, Eng.G);
695   if (!IsNew)
696     return nullptr;
697 
698   Eng.WList->enqueue(Succ);
699   return Succ;
700 }
701 
702 
703 ExplodedNode*
704 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
705                                            bool IsSink) {
706   // Get the block for the default case.
707   assert(Src->succ_rbegin() != Src->succ_rend());
708   CFGBlock *DefaultBlock = *Src->succ_rbegin();
709 
710   // Sanity check for default blocks that are unreachable and not caught
711   // by earlier stages.
712   if (!DefaultBlock)
713     return nullptr;
714 
715   bool IsNew;
716   ExplodedNode *Succ =
717       Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()),
718                     St, IsSink, &IsNew);
719   Succ->addPredecessor(Pred, Eng.G);
720 
721   if (!IsNew)
722     return nullptr;
723 
724   if (!IsSink)
725     Eng.WList->enqueue(Succ);
726 
727   return Succ;
728 }
729