CoreEngine.cpp 22 KB

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