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