blob: 5125f366de2a67e6b924c5dbb79fcde9b0a6d41c [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 Kremenek2b4adff2010-08-11 00:03:02 +0000203 SubEngine.ProcessEndWorklist(hasWorkRemaining());
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);
Ted Kremenek2b4adff2010-08-11 00:03:02 +0000253 else {
254 blocksAborted.push_back(std::make_pair(L, Pred));
255 }
Ted Kremenek3e743662008-01-14 23:24:37 +0000256}
257
Zhongxing Xu82003da2009-08-06 10:00:15 +0000258void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000259 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000260
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000261 // Increment the block counter.
262 GRBlockCounter Counter = WList->getBlockCounter();
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000263 Counter = BCounterFactory.IncrementCount(Counter,
264 Pred->getLocationContext()->getCurrentStackFrame(),
265 L.getBlock()->getBlockID());
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000266 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000267
268 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000269 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000270 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000271 SubEngine.getStateManager());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000272 ProcessStmt(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000275 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000276}
277
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000278void GRCoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000279
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000280 if (const Stmt* Term = B->getTerminator()) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000281 switch (Term->getStmtClass()) {
282 default:
283 assert(false && "Analysis for this terminator not implemented.");
284 break;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek822f7372008-02-12 21:51:20 +0000286 case Stmt::BinaryOperatorClass: // '&&' and '||'
287 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
288 return;
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000290 case Stmt::ConditionalOperatorClass:
291 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000292 return;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenek822f7372008-02-12 21:51:20 +0000294 // FIXME: Use constant-folding in CFG construction to simplify this
295 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000297 case Stmt::ChooseExprClass:
298 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000299 return;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenek822f7372008-02-12 21:51:20 +0000301 case Stmt::DoStmtClass:
302 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
303 return;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek822f7372008-02-12 21:51:20 +0000305 case Stmt::ForStmtClass:
306 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
307 return;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Ted Kremenek632bcb82008-02-13 16:56:51 +0000309 case Stmt::ContinueStmtClass:
310 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000311 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000312 break;
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000314 case Stmt::IfStmtClass:
315 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000316 return;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Ted Kremenek7022efb2008-02-13 00:24:44 +0000318 case Stmt::IndirectGotoStmtClass: {
319 // Only 1 successor: the indirect goto dispatch block.
320 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000321
Zhongxing Xu107f7592009-08-06 12:48:26 +0000322 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000323 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
324 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek7022efb2008-02-13 00:24:44 +0000326 ProcessIndirectGoto(builder);
327 return;
328 }
Mike Stump11289f42009-09-09 15:08:12 +0000329
Ted Kremenek17810802008-11-12 19:24:17 +0000330 case Stmt::ObjCForCollectionStmtClass: {
331 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
332 //
333 // (1) inside a basic block, which represents the binding of the
334 // 'element' variable to a value.
335 // (2) in a terminator, which represents the branch.
336 //
337 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
338 // whether or not collection contains any more elements. We cannot
339 // just test to see if the element is nil because a container can
340 // contain nil elements.
341 HandleBranch(Term, Term, B, Pred);
342 return;
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000345 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000346 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
347 this);
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000349 ProcessSwitch(builder);
350 return;
351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000353 case Stmt::WhileStmtClass:
354 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000355 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000356 }
357 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000358
359 assert (B->succ_size() == 1 &&
360 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000361
362 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000363 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000364}
365
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000366void GRCoreEngine::HandleBranch(const Stmt* Cond, const Stmt* Term,
367 const CFGBlock * B, ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000368 assert (B->succ_size() == 2);
369
Zhongxing Xu107f7592009-08-06 12:48:26 +0000370 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
371 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000373 ProcessBranch(Cond, Term, Builder);
374}
375
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000376void GRCoreEngine::HandlePostStmt(const PostStmt& L, const CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000377 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek3e743662008-01-14 23:24:37 +0000379 assert (!B->empty());
380
Ted Kremeneke5843592008-01-15 00:24:08 +0000381 if (StmtIdx == B->size())
382 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000383 else {
Mike Stump11289f42009-09-09 15:08:12 +0000384 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000385 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000386 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000387 }
388}
389
Ted Kremenek3e743662008-01-14 23:24:37 +0000390/// GenerateNode - Utility method to generate nodes, hook up successors,
391/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000392void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000393 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremenek3e743662008-01-14 23:24:37 +0000395 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000396 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000397
398 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000399 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000400 else {
401 assert (IsNew);
402 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek3e743662008-01-14 23:24:37 +0000405 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000406 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000407}
408
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000409GRStmtNodeBuilder::GRStmtNodeBuilder(const CFGBlock* b, unsigned idx,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000410 ExplodedNode* N, GRCoreEngine* e,
411 GRStateManager &mgr)
Zhongxing Xud041bc62010-02-26 02:38:09 +0000412 : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr), Auditor(0),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000413 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
414 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000415 Deferred.insert(N);
Zhongxing Xud041bc62010-02-26 02:38:09 +0000416 CleanedState = Pred->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000417}
418
Zhongxing Xu107f7592009-08-06 12:48:26 +0000419GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000420 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000421 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000422 GenerateAutoTransition(*I);
423}
424
Zhongxing Xu107f7592009-08-06 12:48:26 +0000425void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000426 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000427
Douglas Gregora2fbc942010-02-25 19:01:53 +0000428 // Check if this node entered a callee.
429 if (isa<CallEnter>(N->getLocation())) {
430 // Still use the index of the CallExpr. It's needed to create the callee
431 // StackFrameContext.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000432 Eng.WList->Enqueue(N, &B, Idx);
Douglas Gregora2fbc942010-02-25 19:01:53 +0000433 return;
434 }
435
Zhongxing Xue1190f72009-08-15 03:17:38 +0000436 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek3e743662008-01-14 23:24:37 +0000438 if (Loc == N->getLocation()) {
439 // Note: 'N' should be a fresh node because otherwise it shouldn't be
440 // a member of Deferred.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000441 Eng.WList->Enqueue(N, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000442 return;
443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenek3e743662008-01-14 23:24:37 +0000445 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000446 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000447 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000448
449 if (IsNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000450 Eng.WList->Enqueue(Succ, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000451}
452
Zhongxing Xuc2acbe02010-07-20 02:41:28 +0000453ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, const Stmt* S,
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000454 ExplodedNode* Pred, const GRState* St,
455 ProgramPoint::Kind K) {
456 const GRState* PredState = GetState(Pred);
457
458 // If the state hasn't changed, don't generate a new node.
459 if (!BuildSinks && St == PredState && Auditor == 0) {
460 Dst.Add(Pred);
461 return NULL;
462 }
463
464 ExplodedNode* N = generateNode(S, St, Pred, K);
465
466 if (N) {
467 if (BuildSinks)
468 N->markAsSink();
469 else {
470 if (Auditor && Auditor->Audit(N, Mgr))
471 N->markAsSink();
472
473 Dst.Add(N);
474 }
475 }
476
477 return N;
478}
479
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000480static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
481 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000482 switch (K) {
483 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000484 assert(false && "Unhandled ProgramPoint kind");
485 case ProgramPoint::PreStmtKind:
486 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000487 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000488 return PostStmt(S, LC, tag);
489 case ProgramPoint::PreLoadKind:
490 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000491 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000492 return PostLoad(S, LC, tag);
493 case ProgramPoint::PreStoreKind:
494 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000495 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000496 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000497 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000498 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000499 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000500 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000501 }
502}
503
Zhongxing Xu20227f72009-08-06 01:32:16 +0000504ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000505GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000506 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000507 ProgramPoint::Kind K,
508 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000509
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000510 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000511 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000512}
513
Zhongxing Xu20227f72009-08-06 01:32:16 +0000514ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000515GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000516 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000517 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000518 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000519 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000520 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000521 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek3e743662008-01-14 23:24:37 +0000523 if (IsNew) {
524 Deferred.insert(N);
Ted Kremenek3e743662008-01-14 23:24:37 +0000525 return N;
526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Mike Stump11289f42009-09-09 15:08:12 +0000528 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000529}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000530
Zhongxing Xu107f7592009-08-06 12:48:26 +0000531ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
532 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000533
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000534 // If the branch has been marked infeasible we should not generate a node.
535 if (!isFeasible(branch))
536 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000538 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Zhongxing Xu20227f72009-08-06 01:32:16 +0000540 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000541 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
542 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000543
Ted Kremenekc3661de2009-10-07 00:42:52 +0000544 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000545
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000546 if (branch)
547 GeneratedTrue = true;
548 else
Mike Stump11289f42009-09-09 15:08:12 +0000549 GeneratedFalse = true;
550
Ted Kremeneka50d9852008-01-30 23:03:39 +0000551 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000552 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000553 return Succ;
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremeneka50d9852008-01-30 23:03:39 +0000556 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000557}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000558
Zhongxing Xu107f7592009-08-06 12:48:26 +0000559GRBranchNodeBuilder::~GRBranchNodeBuilder() {
560 if (!GeneratedTrue) generateNode(Pred->State, true);
561 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenek2531fce2008-01-30 23:24:39 +0000563 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000564 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000565}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000566
Ted Kremenek7022efb2008-02-13 00:24:44 +0000567
Zhongxing Xu20227f72009-08-06 01:32:16 +0000568ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000569GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
570 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000571 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000572
573 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000574 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Ted Kremenekc3661de2009-10-07 00:42:52 +0000576 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek7022efb2008-02-13 00:24:44 +0000578 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000579
Ted Kremenek7022efb2008-02-13 00:24:44 +0000580 if (isSink)
581 Succ->markAsSink();
582 else
583 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremenek7022efb2008-02-13 00:24:44 +0000585 return Succ;
586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Ted Kremenek7022efb2008-02-13 00:24:44 +0000588 return NULL;
589}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000590
591
Zhongxing Xu20227f72009-08-06 01:32:16 +0000592ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000593GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000594
595 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000596
Zhongxing Xue1190f72009-08-15 03:17:38 +0000597 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
598 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000599 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000600
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000601 if (IsNew) {
602 Eng.WList->Enqueue(Succ);
603 return Succ;
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000606 return NULL;
607}
608
609
Zhongxing Xu20227f72009-08-06 01:32:16 +0000610ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000611GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000612
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000613 // Get the block for the default case.
614 assert (Src->succ_rbegin() != Src->succ_rend());
615 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000616
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000617 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Zhongxing Xue1190f72009-08-15 03:17:38 +0000619 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
620 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000621 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000622
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000623 if (IsNew) {
624 if (isSink)
625 Succ->markAsSink();
626 else
627 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000629 return Succ;
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000632 return NULL;
633}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000634
Zhongxing Xu107f7592009-08-06 12:48:26 +0000635GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000636 // Auto-generate an EOP node if one has not been generated.
Douglas Gregora2fbc942010-02-25 19:01:53 +0000637 if (!HasGeneratedNode) {
638 // If we are in an inlined call, generate CallExit node.
639 if (Pred->getLocationContext()->getParent())
640 GenerateCallExitNode(Pred->State);
641 else
642 generateNode(Pred->State);
643 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000644}
645
Zhongxing Xu20227f72009-08-06 01:32:16 +0000646ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000647GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
648 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000649 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000650 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000651
652 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000653 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000654
Ted Kremenekc3661de2009-10-07 00:42:52 +0000655 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek811c2b42008-04-11 22:03:04 +0000657 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000658 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000659 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Ted Kremenekd004c412008-04-18 16:30:14 +0000662 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000663}
Douglas Gregora2fbc942010-02-25 19:01:53 +0000664
665void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
666 HasGeneratedNode = true;
667 // Create a CallExit node and enqueue it.
668 const StackFrameContext *LocCtx
669 = cast<StackFrameContext>(Pred->getLocationContext());
670 const Stmt *CE = LocCtx->getCallSite();
671
672 // Use the the callee location context.
673 CallExit Loc(CE, LocCtx);
674
675 bool isNew;
676 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
677 Node->addPredecessor(Pred, *Eng.G);
678
679 if (isNew)
680 Eng.WList->Enqueue(Node);
681}
682
683
684void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
685 const LocationContext *LocCtx) {
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000686 // Check if the callee is in the same translation unit.
687 if (CalleeCtx->getTranslationUnit() !=
688 Pred->getLocationContext()->getTranslationUnit()) {
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000689 // Create a new engine. We must be careful that the new engine should not
690 // reference data structures owned by the old engine.
691
692 AnalysisManager &OldMgr = Eng.SubEngine.getAnalysisManager();
693
694 // Get the callee's translation unit.
695 idx::TranslationUnit *TU = CalleeCtx->getTranslationUnit();
696
697 // Create a new AnalysisManager with components of the callee's
698 // TranslationUnit.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000699 // The Diagnostic is actually shared when we create ASTUnits from AST files.
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000700 AnalysisManager AMgr(TU->getASTContext(), TU->getDiagnostic(),
701 OldMgr.getLangOptions(),
702 OldMgr.getPathDiagnosticClient(),
703 OldMgr.getStoreManagerCreator(),
704 OldMgr.getConstraintManagerCreator(),
705 OldMgr.getIndexer(),
706 OldMgr.getMaxNodes(), OldMgr.getMaxLoop(),
707 OldMgr.shouldVisualizeGraphviz(),
708 OldMgr.shouldVisualizeUbigraph(),
709 OldMgr.shouldPurgeDead(),
710 OldMgr.shouldEagerlyAssume(),
711 OldMgr.shouldTrimGraph(),
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000712 OldMgr.shouldInlineCall(),
713 OldMgr.getAnalysisContextManager().getUseUnoptimizedCFG());
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000714 llvm::OwningPtr<GRTransferFuncs> TF(MakeCFRefCountTF(AMgr.getASTContext(),
715 /* GCEnabled */ false,
716 AMgr.getLangOptions()));
717 // Create the new engine.
718 GRExprEngine NewEng(AMgr, TF.take());
719
720 // Create the new LocationContext.
721 AnalysisContext *NewAnaCtx = AMgr.getAnalysisContext(CalleeCtx->getDecl(),
722 CalleeCtx->getTranslationUnit());
723 const StackFrameContext *OldLocCtx = cast<StackFrameContext>(LocCtx);
724 const StackFrameContext *NewLocCtx = AMgr.getStackFrame(NewAnaCtx,
725 OldLocCtx->getParent(),
726 OldLocCtx->getCallSite(),
727 OldLocCtx->getCallSiteBlock(),
728 OldLocCtx->getIndex());
729
730 // Now create an initial state for the new engine.
731 const GRState *NewState = NewEng.getStateManager().MarshalState(state,
732 NewLocCtx);
733 ExplodedNodeSet ReturnNodes;
734 NewEng.ExecuteWorkListWithInitialState(NewLocCtx, AMgr.getMaxNodes(),
735 NewState, ReturnNodes);
736 return;
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000737 }
738
Douglas Gregora2fbc942010-02-25 19:01:53 +0000739 // Get the callee entry block.
740 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
741 assert(Entry->empty());
742 assert(Entry->succ_size() == 1);
743
744 // Get the solitary successor.
745 const CFGBlock *SuccB = *(Entry->succ_begin());
746
747 // Construct an edge representing the starting location in the callee.
748 BlockEdge Loc(Entry, SuccB, LocCtx);
749
750 bool isNew;
751 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
752 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
753
754 if (isNew)
755 Eng.WList->Enqueue(Node);
756}
757
758void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
759 // Get the callee's location context.
760 const StackFrameContext *LocCtx
761 = cast<StackFrameContext>(Pred->getLocationContext());
762
763 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
764 bool isNew;
765 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
766 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
767 if (isNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000768 Eng.WList->Enqueue(Node, LocCtx->getCallSiteBlock(),
Douglas Gregora2fbc942010-02-25 19:01:53 +0000769 LocCtx->getIndex() + 1);
770}