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 Loc.getAs<LoopExit>() || 279 Loc.getAs<PostAllocatorCall>()); 280 HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred); 281 break; 282 } 283 } 284 285 bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L, 286 unsigned Steps, 287 ProgramStateRef InitState, 288 ExplodedNodeSet &Dst) { 289 bool DidNotFinish = ExecuteWorkList(L, Steps, InitState); 290 for (ExplodedGraph::eop_iterator I = G.eop_begin(), E = G.eop_end(); I != E; 291 ++I) { 292 Dst.Add(*I); 293 } 294 return DidNotFinish; 295 } 296 297 void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) { 298 299 const CFGBlock *Blk = L.getDst(); 300 NodeBuilderContext BuilderCtx(*this, Blk, Pred); 301 302 // Mark this block as visited. 303 const LocationContext *LC = Pred->getLocationContext(); 304 FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(), 305 LC->getDecl(), 306 LC->getCFG()->getNumBlockIDs()); 307 308 // Check if we are entering the EXIT block. 309 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) { 310 311 assert (L.getLocationContext()->getCFG()->getExit().size() == 0 312 && "EXIT block cannot contain Stmts."); 313 314 // Get return statement.. 315 const ReturnStmt *RS = nullptr; 316 if (!L.getSrc()->empty()) { 317 if (Optional<CFGStmt> LastStmt = L.getSrc()->back().getAs<CFGStmt>()) { 318 if ((RS = dyn_cast<ReturnStmt>(LastStmt->getStmt()))) { 319 if (!RS->getRetValue()) 320 RS = nullptr; 321 } 322 } 323 } 324 325 // Process the final state transition. 326 SubEng.processEndOfFunction(BuilderCtx, Pred, RS); 327 328 // This path is done. Don't enqueue any more nodes. 329 return; 330 } 331 332 // Call into the SubEngine to process entering the CFGBlock. 333 ExplodedNodeSet dstNodes; 334 BlockEntrance BE(Blk, Pred->getLocationContext()); 335 NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE); 336 SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred); 337 338 // Auto-generate a node. 339 if (!nodeBuilder.hasGeneratedNodes()) { 340 nodeBuilder.generateNode(Pred->State, Pred); 341 } 342 343 // Enqueue nodes onto the worklist. 344 enqueue(dstNodes); 345 } 346 347 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L, 348 ExplodedNode *Pred) { 349 350 // Increment the block counter. 351 const LocationContext *LC = Pred->getLocationContext(); 352 unsigned BlockId = L.getBlock()->getBlockID(); 353 BlockCounter Counter = WList->getBlockCounter(); 354 Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(), 355 BlockId); 356 WList->setBlockCounter(Counter); 357 358 // Process the entrance of the block. 359 if (Optional<CFGElement> E = L.getFirstElement()) { 360 NodeBuilderContext Ctx(*this, L.getBlock(), Pred); 361 SubEng.processCFGElement(*E, Pred, 0, &Ctx); 362 } 363 else 364 HandleBlockExit(L.getBlock(), Pred); 365 } 366 367 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) { 368 369 if (const Stmt *Term = B->getTerminator()) { 370 switch (Term->getStmtClass()) { 371 default: 372 llvm_unreachable("Analysis for this terminator not implemented."); 373 374 case Stmt::CXXBindTemporaryExprClass: 375 HandleCleanupTemporaryBranch( 376 cast<CXXBindTemporaryExpr>(B->getTerminator().getStmt()), B, Pred); 377 return; 378 379 // Model static initializers. 380 case Stmt::DeclStmtClass: 381 HandleStaticInit(cast<DeclStmt>(Term), B, Pred); 382 return; 383 384 case Stmt::BinaryOperatorClass: // '&&' and '||' 385 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); 386 return; 387 388 case Stmt::BinaryConditionalOperatorClass: 389 case Stmt::ConditionalOperatorClass: 390 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(), 391 Term, B, Pred); 392 return; 393 394 // FIXME: Use constant-folding in CFG construction to simplify this 395 // case. 396 397 case Stmt::ChooseExprClass: 398 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); 399 return; 400 401 case Stmt::CXXTryStmtClass: { 402 // Generate a node for each of the successors. 403 // Our logic for EH analysis can certainly be improved. 404 for (CFGBlock::const_succ_iterator it = B->succ_begin(), 405 et = B->succ_end(); it != et; ++it) { 406 if (const CFGBlock *succ = *it) { 407 generateNode(BlockEdge(B, succ, Pred->getLocationContext()), 408 Pred->State, Pred); 409 } 410 } 411 return; 412 } 413 414 case Stmt::DoStmtClass: 415 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); 416 return; 417 418 case Stmt::CXXForRangeStmtClass: 419 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred); 420 return; 421 422 case Stmt::ForStmtClass: 423 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); 424 return; 425 426 case Stmt::ContinueStmtClass: 427 case Stmt::BreakStmtClass: 428 case Stmt::GotoStmtClass: 429 break; 430 431 case Stmt::IfStmtClass: 432 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); 433 return; 434 435 case Stmt::IndirectGotoStmtClass: { 436 // Only 1 successor: the indirect goto dispatch block. 437 assert (B->succ_size() == 1); 438 439 IndirectGotoNodeBuilder 440 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(), 441 *(B->succ_begin()), this); 442 443 SubEng.processIndirectGoto(builder); 444 return; 445 } 446 447 case Stmt::ObjCForCollectionStmtClass: { 448 // In the case of ObjCForCollectionStmt, it appears twice in a CFG: 449 // 450 // (1) inside a basic block, which represents the binding of the 451 // 'element' variable to a value. 452 // (2) in a terminator, which represents the branch. 453 // 454 // For (1), subengines will bind a value (i.e., 0 or 1) indicating 455 // whether or not collection contains any more elements. We cannot 456 // just test to see if the element is nil because a container can 457 // contain nil elements. 458 HandleBranch(Term, Term, B, Pred); 459 return; 460 } 461 462 case Stmt::SwitchStmtClass: { 463 SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(), 464 this); 465 466 SubEng.processSwitch(builder); 467 return; 468 } 469 470 case Stmt::WhileStmtClass: 471 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); 472 return; 473 } 474 } 475 476 assert (B->succ_size() == 1 && 477 "Blocks with no terminator should have at most 1 successor."); 478 479 generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()), 480 Pred->State, Pred); 481 } 482 483 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) { 484 NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred); 485 SubEng.processCallEnter(BuilderCtx, CE, Pred); 486 } 487 488 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term, 489 const CFGBlock * B, ExplodedNode *Pred) { 490 assert(B->succ_size() == 2); 491 NodeBuilderContext Ctx(*this, B, Pred); 492 ExplodedNodeSet Dst; 493 SubEng.processBranch(Cond, Term, Ctx, Pred, Dst, 494 *(B->succ_begin()), *(B->succ_begin()+1)); 495 // Enqueue the new frontier onto the worklist. 496 enqueue(Dst); 497 } 498 499 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE, 500 const CFGBlock *B, 501 ExplodedNode *Pred) { 502 assert(B->succ_size() == 2); 503 NodeBuilderContext Ctx(*this, B, Pred); 504 ExplodedNodeSet Dst; 505 SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()), 506 *(B->succ_begin() + 1)); 507 // Enqueue the new frontier onto the worklist. 508 enqueue(Dst); 509 } 510 511 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B, 512 ExplodedNode *Pred) { 513 assert(B->succ_size() == 2); 514 NodeBuilderContext Ctx(*this, B, Pred); 515 ExplodedNodeSet Dst; 516 SubEng.processStaticInitializer(DS, Ctx, Pred, Dst, 517 *(B->succ_begin()), *(B->succ_begin()+1)); 518 // Enqueue the new frontier onto the worklist. 519 enqueue(Dst); 520 } 521 522 523 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx, 524 ExplodedNode *Pred) { 525 assert(B); 526 assert(!B->empty()); 527 528 if (StmtIdx == B->size()) 529 HandleBlockExit(B, Pred); 530 else { 531 NodeBuilderContext Ctx(*this, B, Pred); 532 SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx); 533 } 534 } 535 536 /// generateNode - Utility method to generate nodes, hook up successors, 537 /// and add nodes to the worklist. 538 void CoreEngine::generateNode(const ProgramPoint &Loc, 539 ProgramStateRef State, 540 ExplodedNode *Pred) { 541 542 bool IsNew; 543 ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew); 544 545 if (Pred) 546 Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor. 547 else { 548 assert (IsNew); 549 G.addRoot(Node); // 'Node' has no predecessor. Make it a root. 550 } 551 552 // Only add 'Node' to the worklist if it was freshly generated. 553 if (IsNew) WList->enqueue(Node); 554 } 555 556 void CoreEngine::enqueueStmtNode(ExplodedNode *N, 557 const CFGBlock *Block, unsigned Idx) { 558 assert(Block); 559 assert (!N->isSink()); 560 561 // Check if this node entered a callee. 562 if (N->getLocation().getAs<CallEnter>()) { 563 // Still use the index of the CallExpr. It's needed to create the callee 564 // StackFrameContext. 565 WList->enqueue(N, Block, Idx); 566 return; 567 } 568 569 // Do not create extra nodes. Move to the next CFG element. 570 if (N->getLocation().getAs<PostInitializer>() || 571 N->getLocation().getAs<PostImplicitCall>()|| 572 N->getLocation().getAs<LoopExit>()) { 573 WList->enqueue(N, Block, Idx+1); 574 return; 575 } 576 577 if (N->getLocation().getAs<EpsilonPoint>()) { 578 WList->enqueue(N, Block, Idx); 579 return; 580 } 581 582 if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) { 583 WList->enqueue(N, Block, Idx+1); 584 return; 585 } 586 587 // At this point, we know we're processing a normal statement. 588 CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>(); 589 PostStmt Loc(CS.getStmt(), N->getLocationContext()); 590 591 if (Loc == N->getLocation().withTag(nullptr)) { 592 // Note: 'N' should be a fresh node because otherwise it shouldn't be 593 // a member of Deferred. 594 WList->enqueue(N, Block, Idx+1); 595 return; 596 } 597 598 bool IsNew; 599 ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew); 600 Succ->addPredecessor(N, G); 601 602 if (IsNew) 603 WList->enqueue(Succ, Block, Idx+1); 604 } 605 606 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N, 607 const ReturnStmt *RS) { 608 // Create a CallExitBegin node and enqueue it. 609 const StackFrameContext *LocCtx 610 = cast<StackFrameContext>(N->getLocationContext()); 611 612 // Use the callee location context. 613 CallExitBegin Loc(LocCtx, RS); 614 615 bool isNew; 616 ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew); 617 Node->addPredecessor(N, G); 618 return isNew ? Node : nullptr; 619 } 620 621 622 void CoreEngine::enqueue(ExplodedNodeSet &Set) { 623 for (ExplodedNodeSet::iterator I = Set.begin(), 624 E = Set.end(); I != E; ++I) { 625 WList->enqueue(*I); 626 } 627 } 628 629 void CoreEngine::enqueue(ExplodedNodeSet &Set, 630 const CFGBlock *Block, unsigned Idx) { 631 for (ExplodedNodeSet::iterator I = Set.begin(), 632 E = Set.end(); I != E; ++I) { 633 enqueueStmtNode(*I, Block, Idx); 634 } 635 } 636 637 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS) { 638 for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) { 639 ExplodedNode *N = *I; 640 // If we are in an inlined call, generate CallExitBegin node. 641 if (N->getLocationContext()->getParent()) { 642 N = generateCallExitBeginNode(N, RS); 643 if (N) 644 WList->enqueue(N); 645 } else { 646 // TODO: We should run remove dead bindings here. 647 G.addEndOfPath(N); 648 NumPathsExplored++; 649 } 650 } 651 } 652 653 654 void NodeBuilder::anchor() { } 655 656 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc, 657 ProgramStateRef State, 658 ExplodedNode *FromN, 659 bool MarkAsSink) { 660 HasGeneratedNodes = true; 661 bool IsNew; 662 ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew); 663 N->addPredecessor(FromN, C.Eng.G); 664 Frontier.erase(FromN); 665 666 if (!IsNew) 667 return nullptr; 668 669 if (!MarkAsSink) 670 Frontier.Add(N); 671 672 return N; 673 } 674 675 void NodeBuilderWithSinks::anchor() { } 676 677 StmtNodeBuilder::~StmtNodeBuilder() { 678 if (EnclosingBldr) 679 for (ExplodedNodeSet::iterator I = Frontier.begin(), 680 E = Frontier.end(); I != E; ++I ) 681 EnclosingBldr->addNodes(*I); 682 } 683 684 void BranchNodeBuilder::anchor() { } 685 686 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State, 687 bool branch, 688 ExplodedNode *NodePred) { 689 // If the branch has been marked infeasible we should not generate a node. 690 if (!isFeasible(branch)) 691 return nullptr; 692 693 ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF, 694 NodePred->getLocationContext()); 695 ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred); 696 return Succ; 697 } 698 699 ExplodedNode* 700 IndirectGotoNodeBuilder::generateNode(const iterator &I, 701 ProgramStateRef St, 702 bool IsSink) { 703 bool IsNew; 704 ExplodedNode *Succ = 705 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()), 706 St, IsSink, &IsNew); 707 Succ->addPredecessor(Pred, Eng.G); 708 709 if (!IsNew) 710 return nullptr; 711 712 if (!IsSink) 713 Eng.WList->enqueue(Succ); 714 715 return Succ; 716 } 717 718 719 ExplodedNode* 720 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I, 721 ProgramStateRef St) { 722 723 bool IsNew; 724 ExplodedNode *Succ = 725 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()), 726 St, false, &IsNew); 727 Succ->addPredecessor(Pred, Eng.G); 728 if (!IsNew) 729 return nullptr; 730 731 Eng.WList->enqueue(Succ); 732 return Succ; 733 } 734 735 736 ExplodedNode* 737 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St, 738 bool IsSink) { 739 // Get the block for the default case. 740 assert(Src->succ_rbegin() != Src->succ_rend()); 741 CFGBlock *DefaultBlock = *Src->succ_rbegin(); 742 743 // Sanity check for default blocks that are unreachable and not caught 744 // by earlier stages. 745 if (!DefaultBlock) 746 return nullptr; 747 748 bool IsNew; 749 ExplodedNode *Succ = 750 Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()), 751 St, IsSink, &IsNew); 752 Succ->addPredecessor(Pred, Eng.G); 753 754 if (!IsNew) 755 return nullptr; 756 757 if (!IsSink) 758 Eng.WList->enqueue(Succ); 759 760 return Succ; 761 } 762