CoreEngine.cpp 21 KB

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