blob: b0be7096615d2f2b6c6c9ff094ffce51f35c014c [file] [log] [blame]
Ted Kremenek17810802008-11-12 19:24:17 +00001//==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-//
Mike Stump11289f42009-09-09 15:08:12 +00002//
Ted Kremenek3e743662008-01-14 23:24:37 +00003// 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
Zhongxing Xuadf644d2010-07-22 13:52:13 +000015#include "clang/Checker/PathSensitive/AnalysisManager.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRCoreEngine.h"
17#include "clang/Checker/PathSensitive/GRExprEngine.h"
Zhongxing Xuadf644d2010-07-22 13:52:13 +000018#include "clang/Index/TranslationUnit.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000019#include "clang/AST/Expr.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000020#include "llvm/Support/Casting.h"
21#include "llvm/ADT/DenseMap.h"
22#include <vector>
Ted Kremenekd9de9f12008-12-16 22:13:33 +000023#include <queue>
Ted Kremenek3e743662008-01-14 23:24:37 +000024
25using llvm::cast;
26using llvm::isa;
27using namespace clang;
28
Zhongxing Xuadf644d2010-07-22 13:52:13 +000029// This should be removed in the future.
30namespace clang {
31GRTransferFuncs* MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
32 const LangOptions& lopts);
33}
34
Ted Kremenekd9de9f12008-12-16 22:13:33 +000035//===----------------------------------------------------------------------===//
36// Worklist classes for exploration of reachable states.
37//===----------------------------------------------------------------------===//
38
Ted Kremenek3e743662008-01-14 23:24:37 +000039namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000040class DFS : public GRWorkList {
Ted Kremenek3e743662008-01-14 23:24:37 +000041 llvm::SmallVector<GRWorkListUnit,20> Stack;
42public:
43 virtual bool hasWork() const {
44 return !Stack.empty();
45 }
46
47 virtual void Enqueue(const GRWorkListUnit& U) {
48 Stack.push_back(U);
49 }
50
51 virtual GRWorkListUnit Dequeue() {
52 assert (!Stack.empty());
53 const GRWorkListUnit& U = Stack.back();
54 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
55 return U;
56 }
57};
Mike Stump11289f42009-09-09 15:08:12 +000058
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000059class BFS : public GRWorkList {
Ted Kremenek2c327732009-05-01 22:18:46 +000060 std::queue<GRWorkListUnit> Queue;
61public:
62 virtual bool hasWork() const {
63 return !Queue.empty();
64 }
Mike Stump11289f42009-09-09 15:08:12 +000065
Ted Kremenek2c327732009-05-01 22:18:46 +000066 virtual void Enqueue(const GRWorkListUnit& U) {
67 Queue.push(U);
68 }
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek2c327732009-05-01 22:18:46 +000070 virtual GRWorkListUnit Dequeue() {
71 // Don't use const reference. The subsequent pop_back() might make it
72 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +000073 GRWorkListUnit U = Queue.front();
Ted Kremenek2c327732009-05-01 22:18:46 +000074 Queue.pop();
75 return U;
76 }
77};
Mike Stump11289f42009-09-09 15:08:12 +000078
Ted Kremenek3e743662008-01-14 23:24:37 +000079} // end anonymous namespace
80
Ted Kremenek2e12c2e2008-01-16 18:18:48 +000081// Place the dstor for GRWorkList here because it contains virtual member
82// functions, and we the code for the dstor generated in one compilation unit.
83GRWorkList::~GRWorkList() {}
84
Ted Kremenek2c327732009-05-01 22:18:46 +000085GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
86GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
Ted Kremenek3e743662008-01-14 23:24:37 +000087
Ted Kremenekd9de9f12008-12-16 22:13:33 +000088namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000089 class BFSBlockDFSContents : public GRWorkList {
Ted Kremenekd9de9f12008-12-16 22:13:33 +000090 std::queue<GRWorkListUnit> Queue;
91 llvm::SmallVector<GRWorkListUnit,20> Stack;
92 public:
93 virtual bool hasWork() const {
94 return !Queue.empty() || !Stack.empty();
95 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Ted Kremenekd9de9f12008-12-16 22:13:33 +000097 virtual void Enqueue(const GRWorkListUnit& U) {
98 if (isa<BlockEntrance>(U.getNode()->getLocation()))
99 Queue.push(U);
100 else
101 Stack.push_back(U);
102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000104 virtual GRWorkListUnit Dequeue() {
105 // Process all basic blocks to completion.
106 if (!Stack.empty()) {
107 const GRWorkListUnit& U = Stack.back();
108 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
109 return U;
110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000112 assert(!Queue.empty());
113 // Don't use const reference. The subsequent pop_back() might make it
114 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +0000115 GRWorkListUnit U = Queue.front();
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000116 Queue.pop();
Mike Stump11289f42009-09-09 15:08:12 +0000117 return U;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000118 }
119 };
120} // end anonymous namespace
121
122GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
123 return new BFSBlockDFSContents();
124}
125
126//===----------------------------------------------------------------------===//
127// Core analysis engine.
128//===----------------------------------------------------------------------===//
Douglas Gregora2fbc942010-02-25 19:01:53 +0000129
Ted Kremenek3e743662008-01-14 23:24:37 +0000130/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000131bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
132 const GRState *InitState) {
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenek3e743662008-01-14 23:24:37 +0000134 if (G->num_roots() == 0) { // Initialize the analysis by constructing
135 // the root if none exists.
Mike Stump11289f42009-09-09 15:08:12 +0000136
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000137 const CFGBlock* Entry = &(L->getCFG()->getEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000138
139 assert (Entry->empty() &&
Ted Kremenek3e743662008-01-14 23:24:37 +0000140 "Entry block must be empty.");
Mike Stump11289f42009-09-09 15:08:12 +0000141
Ted Kremenek3e743662008-01-14 23:24:37 +0000142 assert (Entry->succ_size() == 1 &&
143 "Entry block must have 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000144
Ted Kremenek3e743662008-01-14 23:24:37 +0000145 // Get the solitary successor.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000146 const CFGBlock* Succ = *(Entry->succ_begin());
Mike Stump11289f42009-09-09 15:08:12 +0000147
Ted Kremenek3e743662008-01-14 23:24:37 +0000148 // Construct an edge representing the
149 // starting location in the function.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000150 BlockEdge StartLoc(Entry, Succ, L);
Mike Stump11289f42009-09-09 15:08:12 +0000151
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000152 // Set the current block counter to being empty.
153 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
Mike Stump11289f42009-09-09 15:08:12 +0000154
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000155 if (!InitState)
156 // Generate the root.
157 GenerateNode(StartLoc, getInitialState(L), 0);
158 else
159 GenerateNode(StartLoc, InitState, 0);
Ted Kremenek3e743662008-01-14 23:24:37 +0000160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremenek3e743662008-01-14 23:24:37 +0000162 while (Steps && WList->hasWork()) {
163 --Steps;
164 const GRWorkListUnit& WU = WList->Dequeue();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000166 // Set the current block counter.
167 WList->setBlockCounter(WU.getBlockCounter());
168
169 // Retrieve the node.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000170 ExplodedNode* Node = WU.getNode();
Mike Stump11289f42009-09-09 15:08:12 +0000171
Ted Kremenek3e743662008-01-14 23:24:37 +0000172 // Dispatch on the location type.
173 switch (Node->getLocation().getKind()) {
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000174 case ProgramPoint::BlockEdgeKind:
Ted Kremenek3e743662008-01-14 23:24:37 +0000175 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
176 break;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Ted Kremenek3e743662008-01-14 23:24:37 +0000178 case ProgramPoint::BlockEntranceKind:
179 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
180 break;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenek3e743662008-01-14 23:24:37 +0000182 case ProgramPoint::BlockExitKind:
Ted Kremeneke5843592008-01-15 00:24:08 +0000183 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000184 break;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000185
Douglas Gregora2fbc942010-02-25 19:01:53 +0000186 case ProgramPoint::CallEnterKind:
187 HandleCallEnter(cast<CallEnter>(Node->getLocation()), WU.getBlock(),
188 WU.getIndex(), Node);
189 break;
190
191 case ProgramPoint::CallExitKind:
192 HandleCallExit(cast<CallExit>(Node->getLocation()), Node);
193 break;
194
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000195 default:
196 assert(isa<PostStmt>(Node->getLocation()));
Ted Kremenek3e743662008-01-14 23:24:37 +0000197 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
198 WU.getIndex(), Node);
Mike Stump11289f42009-09-09 15:08:12 +0000199 break;
Ted Kremenek3e743662008-01-14 23:24:37 +0000200 }
201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek090d62e2010-06-29 21:58:54 +0000203 SubEngine.ProcessEndWorklist(WList->hasWork() || BlockAborted);
Ted Kremenek3e743662008-01-14 23:24:37 +0000204 return WList->hasWork();
205}
206
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000207void GRCoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
208 unsigned Steps,
209 const GRState *InitState,
210 ExplodedNodeSet &Dst) {
211 ExecuteWorkList(L, Steps, InitState);
212 for (llvm::SmallVectorImpl<ExplodedNode*>::iterator I = G->EndNodes.begin(),
213 E = G->EndNodes.end(); I != E; ++I) {
214 Dst.Add(*I);
215 }
216}
217
Douglas Gregora2fbc942010-02-25 19:01:53 +0000218void GRCoreEngine::HandleCallEnter(const CallEnter &L, const CFGBlock *Block,
219 unsigned Index, ExplodedNode *Pred) {
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000220 GRCallEnterNodeBuilder Builder(*this, Pred, L.getCallExpr(),
221 L.getCalleeContext(), Block, Index);
Douglas Gregora2fbc942010-02-25 19:01:53 +0000222 ProcessCallEnter(Builder);
223}
224
225void GRCoreEngine::HandleCallExit(const CallExit &L, ExplodedNode *Pred) {
226 GRCallExitNodeBuilder Builder(*this, Pred);
227 ProcessCallExit(Builder);
228}
Zhongxing Xu82003da2009-08-06 10:00:15 +0000229
230void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000231
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000232 const CFGBlock* Blk = L.getDst();
Mike Stump11289f42009-09-09 15:08:12 +0000233
234 // Check if we are entering the EXIT block.
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000235 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
Mike Stump11289f42009-09-09 15:08:12 +0000236
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000237 assert (L.getLocationContext()->getCFG()->getExit().size() == 0
Ted Kremenek997d8722008-01-29 00:33:40 +0000238 && "EXIT block cannot contain Stmts.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000239
Ted Kremenek811c2b42008-04-11 22:03:04 +0000240 // Process the final state transition.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000241 GREndPathNodeBuilder Builder(Blk, Pred, this);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000242 ProcessEndPath(Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000243
Ted Kremenek3e743662008-01-14 23:24:37 +0000244 // This path is done. Don't enqueue any more nodes.
245 return;
246 }
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000247
248 // FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
Mike Stump11289f42009-09-09 15:08:12 +0000249
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000250 if (ProcessBlockEntrance(Blk, Pred, WList->getBlockCounter()))
Ted Kremenek090d62e2010-06-29 21:58:54 +0000251 GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()),
252 Pred->State, Pred);
253 else
254 BlockAborted = true;
Ted Kremenek3e743662008-01-14 23:24:37 +0000255}
256
Zhongxing Xu82003da2009-08-06 10:00:15 +0000257void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000258 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000260 // Increment the block counter.
261 GRBlockCounter Counter = WList->getBlockCounter();
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000262 Counter = BCounterFactory.IncrementCount(Counter,
263 Pred->getLocationContext()->getCurrentStackFrame(),
264 L.getBlock()->getBlockID());
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000265 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000266
267 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000268 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000269 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000270 SubEngine.getStateManager());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000271 ProcessStmt(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000272 }
Mike Stump11289f42009-09-09 15:08:12 +0000273 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000274 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000275}
276
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000277void GRCoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000278
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000279 if (const Stmt* Term = B->getTerminator()) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000280 switch (Term->getStmtClass()) {
281 default:
282 assert(false && "Analysis for this terminator not implemented.");
283 break;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenek822f7372008-02-12 21:51:20 +0000285 case Stmt::BinaryOperatorClass: // '&&' and '||'
286 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
287 return;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000289 case Stmt::ConditionalOperatorClass:
290 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000291 return;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Ted Kremenek822f7372008-02-12 21:51:20 +0000293 // FIXME: Use constant-folding in CFG construction to simplify this
294 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000296 case Stmt::ChooseExprClass:
297 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000298 return;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek822f7372008-02-12 21:51:20 +0000300 case Stmt::DoStmtClass:
301 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
302 return;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremenek822f7372008-02-12 21:51:20 +0000304 case Stmt::ForStmtClass:
305 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
306 return;
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek632bcb82008-02-13 16:56:51 +0000308 case Stmt::ContinueStmtClass:
309 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000310 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000311 break;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000313 case Stmt::IfStmtClass:
314 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000315 return;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek7022efb2008-02-13 00:24:44 +0000317 case Stmt::IndirectGotoStmtClass: {
318 // Only 1 successor: the indirect goto dispatch block.
319 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000320
Zhongxing Xu107f7592009-08-06 12:48:26 +0000321 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000322 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
323 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek7022efb2008-02-13 00:24:44 +0000325 ProcessIndirectGoto(builder);
326 return;
327 }
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek17810802008-11-12 19:24:17 +0000329 case Stmt::ObjCForCollectionStmtClass: {
330 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
331 //
332 // (1) inside a basic block, which represents the binding of the
333 // 'element' variable to a value.
334 // (2) in a terminator, which represents the branch.
335 //
336 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
337 // whether or not collection contains any more elements. We cannot
338 // just test to see if the element is nil because a container can
339 // contain nil elements.
340 HandleBranch(Term, Term, B, Pred);
341 return;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000344 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000345 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
346 this);
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000348 ProcessSwitch(builder);
349 return;
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000352 case Stmt::WhileStmtClass:
353 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000354 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000355 }
356 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000357
358 assert (B->succ_size() == 1 &&
359 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000360
361 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000362 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000363}
364
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000365void GRCoreEngine::HandleBranch(const Stmt* Cond, const Stmt* Term,
366 const CFGBlock * B, ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000367 assert (B->succ_size() == 2);
368
Zhongxing Xu107f7592009-08-06 12:48:26 +0000369 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
370 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000372 ProcessBranch(Cond, Term, Builder);
373}
374
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000375void GRCoreEngine::HandlePostStmt(const PostStmt& L, const CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000376 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenek3e743662008-01-14 23:24:37 +0000378 assert (!B->empty());
379
Ted Kremeneke5843592008-01-15 00:24:08 +0000380 if (StmtIdx == B->size())
381 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000382 else {
Mike Stump11289f42009-09-09 15:08:12 +0000383 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000384 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000385 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000386 }
387}
388
Ted Kremenek3e743662008-01-14 23:24:37 +0000389/// GenerateNode - Utility method to generate nodes, hook up successors,
390/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000391void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000392 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenek3e743662008-01-14 23:24:37 +0000394 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000395 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000396
397 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000398 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000399 else {
400 assert (IsNew);
401 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek3e743662008-01-14 23:24:37 +0000404 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000405 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000406}
407
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000408GRStmtNodeBuilder::GRStmtNodeBuilder(const CFGBlock* b, unsigned idx,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000409 ExplodedNode* N, GRCoreEngine* e,
410 GRStateManager &mgr)
Zhongxing Xud041bc62010-02-26 02:38:09 +0000411 : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr), Auditor(0),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000412 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
413 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000414 Deferred.insert(N);
Zhongxing Xud041bc62010-02-26 02:38:09 +0000415 CleanedState = Pred->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000416}
417
Zhongxing Xu107f7592009-08-06 12:48:26 +0000418GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000419 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000420 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000421 GenerateAutoTransition(*I);
422}
423
Zhongxing Xu107f7592009-08-06 12:48:26 +0000424void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000425 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000426
Douglas Gregora2fbc942010-02-25 19:01:53 +0000427 // Check if this node entered a callee.
428 if (isa<CallEnter>(N->getLocation())) {
429 // Still use the index of the CallExpr. It's needed to create the callee
430 // StackFrameContext.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000431 Eng.WList->Enqueue(N, &B, Idx);
Douglas Gregora2fbc942010-02-25 19:01:53 +0000432 return;
433 }
434
Zhongxing Xue1190f72009-08-15 03:17:38 +0000435 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek3e743662008-01-14 23:24:37 +0000437 if (Loc == N->getLocation()) {
438 // Note: 'N' should be a fresh node because otherwise it shouldn't be
439 // a member of Deferred.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000440 Eng.WList->Enqueue(N, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000441 return;
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Ted Kremenek3e743662008-01-14 23:24:37 +0000444 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000445 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000446 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000447
448 if (IsNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000449 Eng.WList->Enqueue(Succ, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000450}
451
Zhongxing Xuc2acbe02010-07-20 02:41:28 +0000452ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, const Stmt* S,
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000453 ExplodedNode* Pred, const GRState* St,
454 ProgramPoint::Kind K) {
455 const GRState* PredState = GetState(Pred);
456
457 // If the state hasn't changed, don't generate a new node.
458 if (!BuildSinks && St == PredState && Auditor == 0) {
459 Dst.Add(Pred);
460 return NULL;
461 }
462
463 ExplodedNode* N = generateNode(S, St, Pred, K);
464
465 if (N) {
466 if (BuildSinks)
467 N->markAsSink();
468 else {
469 if (Auditor && Auditor->Audit(N, Mgr))
470 N->markAsSink();
471
472 Dst.Add(N);
473 }
474 }
475
476 return N;
477}
478
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000479static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
480 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000481 switch (K) {
482 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000483 assert(false && "Unhandled ProgramPoint kind");
484 case ProgramPoint::PreStmtKind:
485 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000486 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000487 return PostStmt(S, LC, tag);
488 case ProgramPoint::PreLoadKind:
489 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000490 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000491 return PostLoad(S, LC, tag);
492 case ProgramPoint::PreStoreKind:
493 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000494 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000495 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000496 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000497 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000498 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000499 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000500 }
501}
502
Zhongxing Xu20227f72009-08-06 01:32:16 +0000503ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000504GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000505 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000506 ProgramPoint::Kind K,
507 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000508
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000509 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000510 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000511}
512
Zhongxing Xu20227f72009-08-06 01:32:16 +0000513ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000514GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000515 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000516 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000517 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000518 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000519 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000520 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000521
Ted Kremenek3e743662008-01-14 23:24:37 +0000522 if (IsNew) {
523 Deferred.insert(N);
Ted Kremenek3e743662008-01-14 23:24:37 +0000524 return N;
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Mike Stump11289f42009-09-09 15:08:12 +0000527 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000528}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000529
Zhongxing Xu107f7592009-08-06 12:48:26 +0000530ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
531 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000532
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000533 // If the branch has been marked infeasible we should not generate a node.
534 if (!isFeasible(branch))
535 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000537 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Zhongxing Xu20227f72009-08-06 01:32:16 +0000539 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000540 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
541 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenekc3661de2009-10-07 00:42:52 +0000543 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000544
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000545 if (branch)
546 GeneratedTrue = true;
547 else
Mike Stump11289f42009-09-09 15:08:12 +0000548 GeneratedFalse = true;
549
Ted Kremeneka50d9852008-01-30 23:03:39 +0000550 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000551 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000552 return Succ;
553 }
Mike Stump11289f42009-09-09 15:08:12 +0000554
Ted Kremeneka50d9852008-01-30 23:03:39 +0000555 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000556}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000557
Zhongxing Xu107f7592009-08-06 12:48:26 +0000558GRBranchNodeBuilder::~GRBranchNodeBuilder() {
559 if (!GeneratedTrue) generateNode(Pred->State, true);
560 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000561
Ted Kremenek2531fce2008-01-30 23:24:39 +0000562 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000563 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000564}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000565
Ted Kremenek7022efb2008-02-13 00:24:44 +0000566
Zhongxing Xu20227f72009-08-06 01:32:16 +0000567ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000568GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
569 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000570 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000571
572 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000573 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremenekc3661de2009-10-07 00:42:52 +0000575 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Ted Kremenek7022efb2008-02-13 00:24:44 +0000577 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek7022efb2008-02-13 00:24:44 +0000579 if (isSink)
580 Succ->markAsSink();
581 else
582 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek7022efb2008-02-13 00:24:44 +0000584 return Succ;
585 }
Mike Stump11289f42009-09-09 15:08:12 +0000586
Ted Kremenek7022efb2008-02-13 00:24:44 +0000587 return NULL;
588}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000589
590
Zhongxing Xu20227f72009-08-06 01:32:16 +0000591ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000592GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000593
594 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Zhongxing Xue1190f72009-08-15 03:17:38 +0000596 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
597 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000598 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000599
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000600 if (IsNew) {
601 Eng.WList->Enqueue(Succ);
602 return Succ;
603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000605 return NULL;
606}
607
608
Zhongxing Xu20227f72009-08-06 01:32:16 +0000609ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000610GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000611
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000612 // Get the block for the default case.
613 assert (Src->succ_rbegin() != Src->succ_rend());
614 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000615
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000616 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000617
Zhongxing Xue1190f72009-08-15 03:17:38 +0000618 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
619 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000620 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000622 if (IsNew) {
623 if (isSink)
624 Succ->markAsSink();
625 else
626 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000628 return Succ;
629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000631 return NULL;
632}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000633
Zhongxing Xu107f7592009-08-06 12:48:26 +0000634GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000635 // Auto-generate an EOP node if one has not been generated.
Douglas Gregora2fbc942010-02-25 19:01:53 +0000636 if (!HasGeneratedNode) {
637 // If we are in an inlined call, generate CallExit node.
638 if (Pred->getLocationContext()->getParent())
639 GenerateCallExitNode(Pred->State);
640 else
641 generateNode(Pred->State);
642 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000643}
644
Zhongxing Xu20227f72009-08-06 01:32:16 +0000645ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000646GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
647 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000648 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000649 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000650
651 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000652 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenekc3661de2009-10-07 00:42:52 +0000654 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Ted Kremenek811c2b42008-04-11 22:03:04 +0000656 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000657 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000658 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenekd004c412008-04-18 16:30:14 +0000661 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000662}
Douglas Gregora2fbc942010-02-25 19:01:53 +0000663
664void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
665 HasGeneratedNode = true;
666 // Create a CallExit node and enqueue it.
667 const StackFrameContext *LocCtx
668 = cast<StackFrameContext>(Pred->getLocationContext());
669 const Stmt *CE = LocCtx->getCallSite();
670
671 // Use the the callee location context.
672 CallExit Loc(CE, LocCtx);
673
674 bool isNew;
675 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
676 Node->addPredecessor(Pred, *Eng.G);
677
678 if (isNew)
679 Eng.WList->Enqueue(Node);
680}
681
682
683void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
684 const LocationContext *LocCtx) {
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000685 // Check if the callee is in the same translation unit.
686 if (CalleeCtx->getTranslationUnit() !=
687 Pred->getLocationContext()->getTranslationUnit()) {
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000688 // Create a new engine. We must be careful that the new engine should not
689 // reference data structures owned by the old engine.
690
691 AnalysisManager &OldMgr = Eng.SubEngine.getAnalysisManager();
692
693 // Get the callee's translation unit.
694 idx::TranslationUnit *TU = CalleeCtx->getTranslationUnit();
695
696 // Create a new AnalysisManager with components of the callee's
697 // TranslationUnit.
698 // The Diagnostic is actually shared when we create ASTUnits from PCH files.
699 AnalysisManager AMgr(TU->getASTContext(), TU->getDiagnostic(),
700 OldMgr.getLangOptions(),
701 OldMgr.getPathDiagnosticClient(),
702 OldMgr.getStoreManagerCreator(),
703 OldMgr.getConstraintManagerCreator(),
704 OldMgr.getIndexer(),
705 OldMgr.getMaxNodes(), OldMgr.getMaxLoop(),
706 OldMgr.shouldVisualizeGraphviz(),
707 OldMgr.shouldVisualizeUbigraph(),
708 OldMgr.shouldPurgeDead(),
709 OldMgr.shouldEagerlyAssume(),
710 OldMgr.shouldTrimGraph(),
711 OldMgr.shouldInlineCall());
712 llvm::OwningPtr<GRTransferFuncs> TF(MakeCFRefCountTF(AMgr.getASTContext(),
713 /* GCEnabled */ false,
714 AMgr.getLangOptions()));
715 // Create the new engine.
716 GRExprEngine NewEng(AMgr, TF.take());
717
718 // Create the new LocationContext.
719 AnalysisContext *NewAnaCtx = AMgr.getAnalysisContext(CalleeCtx->getDecl(),
720 CalleeCtx->getTranslationUnit());
721 const StackFrameContext *OldLocCtx = cast<StackFrameContext>(LocCtx);
722 const StackFrameContext *NewLocCtx = AMgr.getStackFrame(NewAnaCtx,
723 OldLocCtx->getParent(),
724 OldLocCtx->getCallSite(),
725 OldLocCtx->getCallSiteBlock(),
726 OldLocCtx->getIndex());
727
728 // Now create an initial state for the new engine.
729 const GRState *NewState = NewEng.getStateManager().MarshalState(state,
730 NewLocCtx);
731 ExplodedNodeSet ReturnNodes;
732 NewEng.ExecuteWorkListWithInitialState(NewLocCtx, AMgr.getMaxNodes(),
733 NewState, ReturnNodes);
734 return;
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000735 }
736
Douglas Gregora2fbc942010-02-25 19:01:53 +0000737 // Get the callee entry block.
738 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
739 assert(Entry->empty());
740 assert(Entry->succ_size() == 1);
741
742 // Get the solitary successor.
743 const CFGBlock *SuccB = *(Entry->succ_begin());
744
745 // Construct an edge representing the starting location in the callee.
746 BlockEdge Loc(Entry, SuccB, LocCtx);
747
748 bool isNew;
749 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
750 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
751
752 if (isNew)
753 Eng.WList->Enqueue(Node);
754}
755
756void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
757 // Get the callee's location context.
758 const StackFrameContext *LocCtx
759 = cast<StackFrameContext>(Pred->getLocationContext());
760
761 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
762 bool isNew;
763 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
764 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
765 if (isNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000766 Eng.WList->Enqueue(Node, LocCtx->getCallSiteBlock(),
Douglas Gregora2fbc942010-02-25 19:01:53 +0000767 LocCtx->getIndex() + 1);
768}