CoreEngine.cpp 27 KB

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