CoreEngine.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #define DEBUG_TYPE "CoreEngine"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  18. #include "clang/AST/Expr.h"
  19. #include "clang/AST/StmtCXX.h"
  20. #include "llvm/Support/Casting.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/Statistic.h"
  23. using namespace clang;
  24. using namespace ento;
  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. virtual bool hasWork() const {
  40. return !Stack.empty();
  41. }
  42. virtual void enqueue(const WorkListUnit& U) {
  43. Stack.push_back(U);
  44. }
  45. virtual WorkListUnit dequeue() {
  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. virtual bool visitItemsInWorkList(Visitor &V) {
  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. virtual bool hasWork() const {
  64. return !Queue.empty();
  65. }
  66. virtual void enqueue(const WorkListUnit& U) {
  67. Queue.push_back(U);
  68. }
  69. virtual WorkListUnit dequeue() {
  70. WorkListUnit U = Queue.front();
  71. Queue.pop_front();
  72. return U;
  73. }
  74. virtual bool visitItemsInWorkList(Visitor &V) {
  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. virtual bool hasWork() const {
  95. return !Queue.empty() || !Stack.empty();
  96. }
  97. virtual void enqueue(const WorkListUnit& U) {
  98. if (isa<BlockEntrance>(U.getNode()->getLocation()))
  99. Queue.push_front(U);
  100. else
  101. Stack.push_back(U);
  102. }
  103. virtual WorkListUnit dequeue() {
  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. virtual bool visitItemsInWorkList(Visitor &V) {
  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), 0);
  162. else
  163. generateNode(StartLoc, InitState, 0);
  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(cast<BlockEdge>(Loc), Pred);
  192. break;
  193. case ProgramPoint::BlockEntranceKind:
  194. HandleBlockEntrance(cast<BlockEntrance>(Loc), 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 = cast<CallEnter>(Loc);
  201. if (AnalyzedCallees)
  202. if (const CallExpr* CE =
  203. dyn_cast_or_null<CallExpr>(CEnter.getCallExpr()))
  204. if (const Decl *CD = CE->getCalleeDecl())
  205. AnalyzedCallees->insert(CD);
  206. SubEng.processCallEnter(CEnter, Pred);
  207. break;
  208. }
  209. case ProgramPoint::CallExitBeginKind:
  210. SubEng.processCallExit(Pred);
  211. break;
  212. case ProgramPoint::EpsilonKind: {
  213. assert(Pred->hasSinglePred() &&
  214. "Assume epsilon has exactly one predecessor by construction");
  215. ExplodedNode *PNode = Pred->getFirstPred();
  216. dispatchWorkItem(Pred, PNode->getLocation(), WU);
  217. break;
  218. }
  219. default:
  220. assert(isa<PostStmt>(Loc) ||
  221. isa<PostInitializer>(Loc) ||
  222. isa<CallExitEnd>(Loc));
  223. HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
  224. break;
  225. }
  226. }
  227. bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
  228. unsigned Steps,
  229. ProgramStateRef InitState,
  230. ExplodedNodeSet &Dst) {
  231. bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
  232. for (ExplodedGraph::eop_iterator I = G->eop_begin(),
  233. E = G->eop_end(); I != E; ++I) {
  234. Dst.Add(*I);
  235. }
  236. return DidNotFinish;
  237. }
  238. void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
  239. const CFGBlock *Blk = L.getDst();
  240. NodeBuilderContext BuilderCtx(*this, Blk, Pred);
  241. // Mark this block as visited.
  242. const LocationContext *LC = Pred->getLocationContext();
  243. FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
  244. LC->getDecl(),
  245. LC->getCFG()->getNumBlockIDs());
  246. // Check if we are entering the EXIT block.
  247. if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
  248. assert (L.getLocationContext()->getCFG()->getExit().size() == 0
  249. && "EXIT block cannot contain Stmts.");
  250. // Process the final state transition.
  251. SubEng.processEndOfFunction(BuilderCtx);
  252. // This path is done. Don't enqueue any more nodes.
  253. return;
  254. }
  255. // Call into the SubEngine to process entering the CFGBlock.
  256. ExplodedNodeSet dstNodes;
  257. BlockEntrance BE(Blk, Pred->getLocationContext());
  258. NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
  259. SubEng.processCFGBlockEntrance(L, nodeBuilder);
  260. // Auto-generate a node.
  261. if (!nodeBuilder.hasGeneratedNodes()) {
  262. nodeBuilder.generateNode(Pred->State, Pred);
  263. }
  264. // Enqueue nodes onto the worklist.
  265. enqueue(dstNodes);
  266. }
  267. void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
  268. ExplodedNode *Pred) {
  269. // Increment the block counter.
  270. const LocationContext *LC = Pred->getLocationContext();
  271. unsigned BlockId = L.getBlock()->getBlockID();
  272. BlockCounter Counter = WList->getBlockCounter();
  273. Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(),
  274. BlockId);
  275. WList->setBlockCounter(Counter);
  276. // Process the entrance of the block.
  277. if (CFGElement E = L.getFirstElement()) {
  278. NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
  279. SubEng.processCFGElement(E, Pred, 0, &Ctx);
  280. }
  281. else
  282. HandleBlockExit(L.getBlock(), Pred);
  283. }
  284. void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
  285. if (const Stmt *Term = B->getTerminator()) {
  286. switch (Term->getStmtClass()) {
  287. default:
  288. llvm_unreachable("Analysis for this terminator not implemented.");
  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::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
  380. ExplodedNode *Pred) {
  381. assert(B);
  382. assert(!B->empty());
  383. if (StmtIdx == B->size())
  384. HandleBlockExit(B, Pred);
  385. else {
  386. NodeBuilderContext Ctx(*this, B, Pred);
  387. SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
  388. }
  389. }
  390. /// generateNode - Utility method to generate nodes, hook up successors,
  391. /// and add nodes to the worklist.
  392. void CoreEngine::generateNode(const ProgramPoint &Loc,
  393. ProgramStateRef State,
  394. ExplodedNode *Pred) {
  395. bool IsNew;
  396. ExplodedNode *Node = G->getNode(Loc, State, false, &IsNew);
  397. if (Pred)
  398. Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
  399. else {
  400. assert (IsNew);
  401. G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
  402. }
  403. // Only add 'Node' to the worklist if it was freshly generated.
  404. if (IsNew) WList->enqueue(Node);
  405. }
  406. void CoreEngine::enqueueStmtNode(ExplodedNode *N,
  407. const CFGBlock *Block, unsigned Idx) {
  408. assert(Block);
  409. assert (!N->isSink());
  410. // Check if this node entered a callee.
  411. if (isa<CallEnter>(N->getLocation())) {
  412. // Still use the index of the CallExpr. It's needed to create the callee
  413. // StackFrameContext.
  414. WList->enqueue(N, Block, Idx);
  415. return;
  416. }
  417. // Do not create extra nodes. Move to the next CFG element.
  418. if (isa<PostInitializer>(N->getLocation())) {
  419. WList->enqueue(N, Block, Idx+1);
  420. return;
  421. }
  422. if (isa<EpsilonPoint>(N->getLocation())) {
  423. WList->enqueue(N, Block, Idx);
  424. return;
  425. }
  426. const CFGStmt *CS = (*Block)[Idx].getAs<CFGStmt>();
  427. const Stmt *St = CS ? CS->getStmt() : 0;
  428. PostStmt Loc(St, N->getLocationContext());
  429. if (Loc == N->getLocation()) {
  430. // Note: 'N' should be a fresh node because otherwise it shouldn't be
  431. // a member of Deferred.
  432. WList->enqueue(N, Block, Idx+1);
  433. return;
  434. }
  435. bool IsNew;
  436. ExplodedNode *Succ = G->getNode(Loc, N->getState(), false, &IsNew);
  437. Succ->addPredecessor(N, *G);
  438. if (IsNew)
  439. WList->enqueue(Succ, Block, Idx+1);
  440. }
  441. ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N) {
  442. // Create a CallExitBegin node and enqueue it.
  443. const StackFrameContext *LocCtx
  444. = cast<StackFrameContext>(N->getLocationContext());
  445. // Use the the callee location context.
  446. CallExitBegin Loc(LocCtx);
  447. bool isNew;
  448. ExplodedNode *Node = G->getNode(Loc, N->getState(), false, &isNew);
  449. Node->addPredecessor(N, *G);
  450. return isNew ? Node : 0;
  451. }
  452. void CoreEngine::enqueue(ExplodedNodeSet &Set) {
  453. for (ExplodedNodeSet::iterator I = Set.begin(),
  454. E = Set.end(); I != E; ++I) {
  455. WList->enqueue(*I);
  456. }
  457. }
  458. void CoreEngine::enqueue(ExplodedNodeSet &Set,
  459. const CFGBlock *Block, unsigned Idx) {
  460. for (ExplodedNodeSet::iterator I = Set.begin(),
  461. E = Set.end(); I != E; ++I) {
  462. enqueueStmtNode(*I, Block, Idx);
  463. }
  464. }
  465. void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set) {
  466. for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
  467. ExplodedNode *N = *I;
  468. // If we are in an inlined call, generate CallExitBegin node.
  469. if (N->getLocationContext()->getParent()) {
  470. N = generateCallExitBeginNode(N);
  471. if (N)
  472. WList->enqueue(N);
  473. } else {
  474. // TODO: We should run remove dead bindings here.
  475. G->addEndOfPath(N);
  476. NumPathsExplored++;
  477. }
  478. }
  479. }
  480. void NodeBuilder::anchor() { }
  481. ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
  482. ProgramStateRef State,
  483. ExplodedNode *FromN,
  484. bool MarkAsSink) {
  485. HasGeneratedNodes = true;
  486. bool IsNew;
  487. ExplodedNode *N = C.Eng.G->getNode(Loc, State, MarkAsSink, &IsNew);
  488. N->addPredecessor(FromN, *C.Eng.G);
  489. Frontier.erase(FromN);
  490. if (!IsNew)
  491. return 0;
  492. if (!MarkAsSink)
  493. Frontier.Add(N);
  494. return N;
  495. }
  496. void NodeBuilderWithSinks::anchor() { }
  497. StmtNodeBuilder::~StmtNodeBuilder() {
  498. if (EnclosingBldr)
  499. for (ExplodedNodeSet::iterator I = Frontier.begin(),
  500. E = Frontier.end(); I != E; ++I )
  501. EnclosingBldr->addNodes(*I);
  502. }
  503. void BranchNodeBuilder::anchor() { }
  504. ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
  505. bool branch,
  506. ExplodedNode *NodePred) {
  507. // If the branch has been marked infeasible we should not generate a node.
  508. if (!isFeasible(branch))
  509. return NULL;
  510. ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
  511. NodePred->getLocationContext());
  512. ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
  513. return Succ;
  514. }
  515. ExplodedNode*
  516. IndirectGotoNodeBuilder::generateNode(const iterator &I,
  517. ProgramStateRef St,
  518. bool IsSink) {
  519. bool IsNew;
  520. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  521. Pred->getLocationContext()), St,
  522. IsSink, &IsNew);
  523. Succ->addPredecessor(Pred, *Eng.G);
  524. if (!IsNew)
  525. return 0;
  526. if (!IsSink)
  527. Eng.WList->enqueue(Succ);
  528. return Succ;
  529. }
  530. ExplodedNode*
  531. SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
  532. ProgramStateRef St) {
  533. bool IsNew;
  534. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  535. Pred->getLocationContext()), St,
  536. false, &IsNew);
  537. Succ->addPredecessor(Pred, *Eng.G);
  538. if (!IsNew)
  539. return 0;
  540. Eng.WList->enqueue(Succ);
  541. return Succ;
  542. }
  543. ExplodedNode*
  544. SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
  545. bool IsSink) {
  546. // Get the block for the default case.
  547. assert(Src->succ_rbegin() != Src->succ_rend());
  548. CFGBlock *DefaultBlock = *Src->succ_rbegin();
  549. // Sanity check for default blocks that are unreachable and not caught
  550. // by earlier stages.
  551. if (!DefaultBlock)
  552. return NULL;
  553. bool IsNew;
  554. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
  555. Pred->getLocationContext()), St,
  556. IsSink, &IsNew);
  557. Succ->addPredecessor(Pred, *Eng.G);
  558. if (!IsNew)
  559. return 0;
  560. if (!IsSink)
  561. Eng.WList->enqueue(Succ);
  562. return Succ;
  563. }