CoreEngine.cpp 21 KB

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