CoreEngine.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. StmtNodeBuilder Builder(L.getBlock(), 0, Pred, this);
  261. SubEng.processCFGElement(E, Builder);
  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);
  343. SubEng.processBranch(Cond, Term, Ctx, Pred,
  344. *(B->succ_begin()), *(B->succ_begin()+1));
  345. }
  346. void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
  347. ExplodedNode *Pred) {
  348. assert(B);
  349. assert(!B->empty());
  350. if (StmtIdx == B->size())
  351. HandleBlockExit(B, Pred);
  352. else {
  353. StmtNodeBuilder Builder(B, StmtIdx, Pred, this);
  354. SubEng.processCFGElement((*B)[StmtIdx], Builder);
  355. }
  356. }
  357. /// generateNode - Utility method to generate nodes, hook up successors,
  358. /// and add nodes to the worklist.
  359. void CoreEngine::generateNode(const ProgramPoint &Loc,
  360. const ProgramState *State,
  361. ExplodedNode *Pred) {
  362. bool IsNew;
  363. ExplodedNode *Node = G->getNode(Loc, State, &IsNew);
  364. if (Pred)
  365. Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
  366. else {
  367. assert (IsNew);
  368. G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
  369. }
  370. // Only add 'Node' to the worklist if it was freshly generated.
  371. if (IsNew) WList->enqueue(Node);
  372. }
  373. void CoreEngine::enqueue(NodeBuilder &NB) {
  374. for (NodeBuilder::iterator I = NB.results_begin(),
  375. E = NB.results_end(); I != E; ++I) {
  376. WList->enqueue(*I);
  377. }
  378. }
  379. ExplodedNode *
  380. GenericNodeBuilderImpl::generateNodeImpl(const ProgramState *state,
  381. ExplodedNode *pred,
  382. ProgramPoint programPoint,
  383. bool asSink) {
  384. hasGeneratedNode = true;
  385. bool isNew;
  386. ExplodedNode *node = engine.getGraph().getNode(programPoint, state, &isNew);
  387. if (pred)
  388. node->addPredecessor(pred, engine.getGraph());
  389. if (isNew) {
  390. if (asSink) {
  391. node->markAsSink();
  392. sinksGenerated.push_back(node);
  393. }
  394. return node;
  395. }
  396. return 0;
  397. }
  398. ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
  399. const ProgramState *State,
  400. ExplodedNode *FromN,
  401. bool MarkAsSink) {
  402. assert(Finalized == false &&
  403. "We cannot create new nodes after the results have been finalized.");
  404. bool IsNew;
  405. ExplodedNode *N = C.Eng.G->getNode(Loc, State, &IsNew);
  406. N->addPredecessor(FromN, *C.Eng.G);
  407. Deferred.erase(FromN);
  408. if (MarkAsSink)
  409. N->markAsSink();
  410. if (IsNew && !N->isSink())
  411. Deferred.insert(N);
  412. return (IsNew ? N : 0);
  413. }
  414. StmtNodeBuilder::StmtNodeBuilder(const CFGBlock *b,
  415. unsigned idx,
  416. ExplodedNode *N,
  417. CoreEngine* e)
  418. : CommonNodeBuilder(e, N), B(*b), Idx(idx),
  419. PurgingDeadSymbols(false), BuildSinks(false), hasGeneratedNode(false),
  420. PointKind(ProgramPoint::PostStmtKind), Tag(0) {
  421. Deferred.insert(N);
  422. }
  423. StmtNodeBuilder::~StmtNodeBuilder() {
  424. for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
  425. if (!(*I)->isSink())
  426. GenerateAutoTransition(*I);
  427. }
  428. void StmtNodeBuilder::GenerateAutoTransition(ExplodedNode *N) {
  429. assert (!N->isSink());
  430. // Check if this node entered a callee.
  431. if (isa<CallEnter>(N->getLocation())) {
  432. // Still use the index of the CallExpr. It's needed to create the callee
  433. // StackFrameContext.
  434. Eng.WList->enqueue(N, &B, Idx);
  435. return;
  436. }
  437. // Do not create extra nodes. Move to the next CFG element.
  438. if (isa<PostInitializer>(N->getLocation())) {
  439. Eng.WList->enqueue(N, &B, Idx+1);
  440. return;
  441. }
  442. PostStmt Loc(getStmt(), N->getLocationContext());
  443. if (Loc == N->getLocation()) {
  444. // Note: 'N' should be a fresh node because otherwise it shouldn't be
  445. // a member of Deferred.
  446. Eng.WList->enqueue(N, &B, Idx+1);
  447. return;
  448. }
  449. bool IsNew;
  450. ExplodedNode *Succ = Eng.G->getNode(Loc, N->State, &IsNew);
  451. Succ->addPredecessor(N, *Eng.G);
  452. if (IsNew)
  453. Eng.WList->enqueue(Succ, &B, Idx+1);
  454. }
  455. ExplodedNode *StmtNodeBuilder::MakeNode(ExplodedNodeSet &Dst,
  456. const Stmt *S,
  457. ExplodedNode *Pred,
  458. const ProgramState *St,
  459. ProgramPoint::Kind K) {
  460. ExplodedNode *N = generateNode(S, St, Pred, K);
  461. if (N) {
  462. if (BuildSinks)
  463. N->markAsSink();
  464. else
  465. Dst.Add(N);
  466. }
  467. return N;
  468. }
  469. ExplodedNode*
  470. StmtNodeBuilder::generateNodeInternal(const Stmt *S,
  471. const ProgramState *state,
  472. ExplodedNode *Pred,
  473. ProgramPoint::Kind K,
  474. const ProgramPointTag *tag) {
  475. const ProgramPoint &L = ProgramPoint::getProgramPoint(S, K,
  476. Pred->getLocationContext(), tag);
  477. return generateNodeInternal(L, state, Pred);
  478. }
  479. ExplodedNode*
  480. StmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
  481. const ProgramState *State,
  482. ExplodedNode *Pred) {
  483. bool IsNew;
  484. ExplodedNode *N = Eng.G->getNode(Loc, State, &IsNew);
  485. N->addPredecessor(Pred, *Eng.G);
  486. Deferred.erase(Pred);
  487. if (IsNew) {
  488. Deferred.insert(N);
  489. return N;
  490. }
  491. return NULL;
  492. }
  493. // This function generate a new ExplodedNode but not a new branch(block edge).
  494. // Creates a transition from the Builder's top predecessor.
  495. ExplodedNode *BranchNodeBuilder::generateNode(const Stmt *Condition,
  496. const ProgramState *State,
  497. const ProgramPointTag *Tag,
  498. bool MarkAsSink) {
  499. ProgramPoint PP = PostCondition(Condition,
  500. BuilderPred->getLocationContext(), Tag);
  501. ExplodedNode *N = generateNodeImpl(PP, State, BuilderPred, MarkAsSink);
  502. assert(N);
  503. // TODO: This needs to go - we should not change Pred!!!
  504. BuilderPred = N;
  505. return N;
  506. }
  507. ExplodedNode *BranchNodeBuilder::generateNode(const ProgramState *State,
  508. bool branch,
  509. ExplodedNode *NodePred) {
  510. // If the branch has been marked infeasible we should not generate a node.
  511. if (!isFeasible(branch))
  512. return NULL;
  513. if (!NodePred)
  514. NodePred = BuilderPred;
  515. ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
  516. NodePred->getLocationContext());
  517. ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
  518. if (branch)
  519. GeneratedTrue = true;
  520. else
  521. GeneratedFalse = true;
  522. return Succ;
  523. }
  524. ExplodedNode*
  525. IndirectGotoNodeBuilder::generateNode(const iterator &I,
  526. const ProgramState *St,
  527. bool isSink) {
  528. bool IsNew;
  529. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  530. Pred->getLocationContext()), St, &IsNew);
  531. Succ->addPredecessor(Pred, *Eng.G);
  532. if (IsNew) {
  533. if (isSink)
  534. Succ->markAsSink();
  535. else
  536. Eng.WList->enqueue(Succ);
  537. return Succ;
  538. }
  539. return NULL;
  540. }
  541. ExplodedNode*
  542. SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
  543. const ProgramState *St) {
  544. bool IsNew;
  545. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  546. Pred->getLocationContext()),
  547. St, &IsNew);
  548. Succ->addPredecessor(Pred, *Eng.G);
  549. if (IsNew) {
  550. Eng.WList->enqueue(Succ);
  551. return Succ;
  552. }
  553. return NULL;
  554. }
  555. ExplodedNode*
  556. SwitchNodeBuilder::generateDefaultCaseNode(const ProgramState *St,
  557. bool isSink) {
  558. // Get the block for the default case.
  559. assert(Src->succ_rbegin() != Src->succ_rend());
  560. CFGBlock *DefaultBlock = *Src->succ_rbegin();
  561. // Sanity check for default blocks that are unreachable and not caught
  562. // by earlier stages.
  563. if (!DefaultBlock)
  564. return NULL;
  565. bool IsNew;
  566. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
  567. Pred->getLocationContext()), St, &IsNew);
  568. Succ->addPredecessor(Pred, *Eng.G);
  569. if (IsNew) {
  570. if (isSink)
  571. Succ->markAsSink();
  572. else
  573. Eng.WList->enqueue(Succ);
  574. return Succ;
  575. }
  576. return NULL;
  577. }
  578. EndOfFunctionNodeBuilder::~EndOfFunctionNodeBuilder() {
  579. // Auto-generate an EOP node if one has not been generated.
  580. if (!hasGeneratedNode) {
  581. // If we are in an inlined call, generate CallExit node.
  582. if (Pred->getLocationContext()->getParent())
  583. GenerateCallExitNode(Pred->State);
  584. else
  585. generateNode(Pred->State);
  586. }
  587. }
  588. ExplodedNode*
  589. EndOfFunctionNodeBuilder::generateNode(const ProgramState *State,
  590. ExplodedNode *P,
  591. const ProgramPointTag *tag) {
  592. hasGeneratedNode = true;
  593. bool IsNew;
  594. ExplodedNode *Node = Eng.G->getNode(BlockEntrance(&B,
  595. Pred->getLocationContext(), tag ? tag : Tag),
  596. State, &IsNew);
  597. Node->addPredecessor(P ? P : Pred, *Eng.G);
  598. if (IsNew) {
  599. Eng.G->addEndOfPath(Node);
  600. return Node;
  601. }
  602. return NULL;
  603. }
  604. void EndOfFunctionNodeBuilder::GenerateCallExitNode(const ProgramState *state) {
  605. hasGeneratedNode = true;
  606. // Create a CallExit node and enqueue it.
  607. const StackFrameContext *LocCtx
  608. = cast<StackFrameContext>(Pred->getLocationContext());
  609. const Stmt *CE = LocCtx->getCallSite();
  610. // Use the the callee location context.
  611. CallExit Loc(CE, LocCtx);
  612. bool isNew;
  613. ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
  614. Node->addPredecessor(Pred, *Eng.G);
  615. if (isNew)
  616. Eng.WList->enqueue(Node);
  617. }
  618. void CallEnterNodeBuilder::generateNode(const ProgramState *state) {
  619. // Check if the callee is in the same translation unit.
  620. if (CalleeCtx->getTranslationUnit() !=
  621. Pred->getLocationContext()->getTranslationUnit()) {
  622. // Create a new engine. We must be careful that the new engine should not
  623. // reference data structures owned by the old engine.
  624. AnalysisManager &OldMgr = Eng.SubEng.getAnalysisManager();
  625. // Get the callee's translation unit.
  626. idx::TranslationUnit *TU = CalleeCtx->getTranslationUnit();
  627. // Create a new AnalysisManager with components of the callee's
  628. // TranslationUnit.
  629. // The Diagnostic is actually shared when we create ASTUnits from AST files.
  630. AnalysisManager AMgr(TU->getASTContext(), TU->getDiagnostic(), OldMgr);
  631. // Create the new engine.
  632. // FIXME: This cast isn't really safe.
  633. bool GCEnabled = static_cast<ExprEngine&>(Eng.SubEng).isObjCGCEnabled();
  634. ExprEngine NewEng(AMgr, GCEnabled);
  635. // Create the new LocationContext.
  636. AnalysisContext *NewAnaCtx = AMgr.getAnalysisContext(CalleeCtx->getDecl(),
  637. CalleeCtx->getTranslationUnit());
  638. const StackFrameContext *OldLocCtx = CalleeCtx;
  639. const StackFrameContext *NewLocCtx = AMgr.getStackFrame(NewAnaCtx,
  640. OldLocCtx->getParent(),
  641. OldLocCtx->getCallSite(),
  642. OldLocCtx->getCallSiteBlock(),
  643. OldLocCtx->getIndex());
  644. // Now create an initial state for the new engine.
  645. const ProgramState *NewState =
  646. NewEng.getStateManager().MarshalState(state, NewLocCtx);
  647. ExplodedNodeSet ReturnNodes;
  648. NewEng.ExecuteWorkListWithInitialState(NewLocCtx, AMgr.getMaxNodes(),
  649. NewState, ReturnNodes);
  650. return;
  651. }
  652. // Get the callee entry block.
  653. const CFGBlock *Entry = &(CalleeCtx->getCFG()->getEntry());
  654. assert(Entry->empty());
  655. assert(Entry->succ_size() == 1);
  656. // Get the solitary successor.
  657. const CFGBlock *SuccB = *(Entry->succ_begin());
  658. // Construct an edge representing the starting location in the callee.
  659. BlockEdge Loc(Entry, SuccB, CalleeCtx);
  660. bool isNew;
  661. ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
  662. Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
  663. if (isNew)
  664. Eng.WList->enqueue(Node);
  665. }
  666. void CallExitNodeBuilder::generateNode(const ProgramState *state) {
  667. // Get the callee's location context.
  668. const StackFrameContext *LocCtx
  669. = cast<StackFrameContext>(Pred->getLocationContext());
  670. // When exiting an implicit automatic obj dtor call, the callsite is the Stmt
  671. // that triggers the dtor.
  672. PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
  673. bool isNew;
  674. ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
  675. Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
  676. if (isNew)
  677. Eng.WList->enqueue(Node, LocCtx->getCallSiteBlock(),
  678. LocCtx->getIndex() + 1);
  679. }