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