CoreEngine.cpp 26 KB

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