blob: 01d254c8f476476a9ec3796c83bc32dff23f1b5d [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 Kremenekb02788d2010-11-13 05:04:49 +000039GRWorkList::Visitor::~Visitor() {}
40
Ted Kremenek3e743662008-01-14 23:24:37 +000041namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000042class DFS : public GRWorkList {
Ted Kremenek3e743662008-01-14 23:24:37 +000043 llvm::SmallVector<GRWorkListUnit,20> Stack;
44public:
45 virtual bool hasWork() const {
46 return !Stack.empty();
47 }
48
49 virtual void Enqueue(const GRWorkListUnit& U) {
50 Stack.push_back(U);
51 }
52
53 virtual GRWorkListUnit Dequeue() {
54 assert (!Stack.empty());
55 const GRWorkListUnit& U = Stack.back();
56 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
57 return U;
58 }
Ted Kremenekb02788d2010-11-13 05:04:49 +000059
60 virtual bool VisitItemsInWorkList(Visitor &V) {
61 for (llvm::SmallVectorImpl<GRWorkListUnit>::iterator
62 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
63 if (V.Visit(*I))
64 return true;
65 }
66 return false;
67 }
Ted Kremenek3e743662008-01-14 23:24:37 +000068};
Mike Stump11289f42009-09-09 15:08:12 +000069
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000070class BFS : public GRWorkList {
Ted Kremenekb02788d2010-11-13 05:04:49 +000071 std::deque<GRWorkListUnit> Queue;
Ted Kremenek2c327732009-05-01 22:18:46 +000072public:
73 virtual bool hasWork() const {
74 return !Queue.empty();
75 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenek2c327732009-05-01 22:18:46 +000077 virtual void Enqueue(const GRWorkListUnit& U) {
Ted Kremenekb02788d2010-11-13 05:04:49 +000078 Queue.push_front(U);
Ted Kremenek2c327732009-05-01 22:18:46 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Ted Kremenek2c327732009-05-01 22:18:46 +000081 virtual GRWorkListUnit Dequeue() {
Mike Stump11289f42009-09-09 15:08:12 +000082 GRWorkListUnit U = Queue.front();
Ted Kremenekb02788d2010-11-13 05:04:49 +000083 Queue.pop_front();
Ted Kremenek2c327732009-05-01 22:18:46 +000084 return U;
85 }
Ted Kremenekb02788d2010-11-13 05:04:49 +000086
87 virtual bool VisitItemsInWorkList(Visitor &V) {
88 for (std::deque<GRWorkListUnit>::iterator
89 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
90 if (V.Visit(*I))
91 return true;
92 }
93 return false;
94 }
Ted Kremenek2c327732009-05-01 22:18:46 +000095};
Mike Stump11289f42009-09-09 15:08:12 +000096
Ted Kremenek3e743662008-01-14 23:24:37 +000097} // end anonymous namespace
98
Ted Kremenek2e12c2e2008-01-16 18:18:48 +000099// Place the dstor for GRWorkList here because it contains virtual member
100// functions, and we the code for the dstor generated in one compilation unit.
101GRWorkList::~GRWorkList() {}
102
Ted Kremenek2c327732009-05-01 22:18:46 +0000103GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
104GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
Ted Kremenek3e743662008-01-14 23:24:37 +0000105
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000106namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000107 class BFSBlockDFSContents : public GRWorkList {
Ted Kremenekb02788d2010-11-13 05:04:49 +0000108 std::deque<GRWorkListUnit> Queue;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000109 llvm::SmallVector<GRWorkListUnit,20> Stack;
110 public:
111 virtual bool hasWork() const {
112 return !Queue.empty() || !Stack.empty();
113 }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000115 virtual void Enqueue(const GRWorkListUnit& U) {
116 if (isa<BlockEntrance>(U.getNode()->getLocation()))
Ted Kremenekb02788d2010-11-13 05:04:49 +0000117 Queue.push_front(U);
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000118 else
119 Stack.push_back(U);
120 }
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000122 virtual GRWorkListUnit Dequeue() {
123 // Process all basic blocks to completion.
124 if (!Stack.empty()) {
125 const GRWorkListUnit& U = Stack.back();
126 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
127 return U;
128 }
Mike Stump11289f42009-09-09 15:08:12 +0000129
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000130 assert(!Queue.empty());
131 // Don't use const reference. The subsequent pop_back() might make it
132 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +0000133 GRWorkListUnit U = Queue.front();
Ted Kremenekb02788d2010-11-13 05:04:49 +0000134 Queue.pop_front();
Mike Stump11289f42009-09-09 15:08:12 +0000135 return U;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000136 }
Ted Kremenekb02788d2010-11-13 05:04:49 +0000137 virtual bool VisitItemsInWorkList(Visitor &V) {
138 for (llvm::SmallVectorImpl<GRWorkListUnit>::iterator
139 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
140 if (V.Visit(*I))
141 return true;
142 }
143 for (std::deque<GRWorkListUnit>::iterator
144 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
145 if (V.Visit(*I))
146 return true;
147 }
148 return false;
149 }
150
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000151 };
152} // end anonymous namespace
153
154GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
155 return new BFSBlockDFSContents();
156}
157
158//===----------------------------------------------------------------------===//
159// Core analysis engine.
160//===----------------------------------------------------------------------===//
Douglas Gregora2fbc942010-02-25 19:01:53 +0000161
Ted Kremenek3e743662008-01-14 23:24:37 +0000162/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000163bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
164 const GRState *InitState) {
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek3e743662008-01-14 23:24:37 +0000166 if (G->num_roots() == 0) { // Initialize the analysis by constructing
167 // the root if none exists.
Mike Stump11289f42009-09-09 15:08:12 +0000168
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000169 const CFGBlock* Entry = &(L->getCFG()->getEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000170
171 assert (Entry->empty() &&
Ted Kremenek3e743662008-01-14 23:24:37 +0000172 "Entry block must be empty.");
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek3e743662008-01-14 23:24:37 +0000174 assert (Entry->succ_size() == 1 &&
175 "Entry block must have 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000176
Ted Kremenek3e743662008-01-14 23:24:37 +0000177 // Get the solitary successor.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000178 const CFGBlock* Succ = *(Entry->succ_begin());
Mike Stump11289f42009-09-09 15:08:12 +0000179
Ted Kremenek3e743662008-01-14 23:24:37 +0000180 // Construct an edge representing the
181 // starting location in the function.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000182 BlockEdge StartLoc(Entry, Succ, L);
Mike Stump11289f42009-09-09 15:08:12 +0000183
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000184 // Set the current block counter to being empty.
185 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
Mike Stump11289f42009-09-09 15:08:12 +0000186
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000187 if (!InitState)
188 // Generate the root.
189 GenerateNode(StartLoc, getInitialState(L), 0);
190 else
191 GenerateNode(StartLoc, InitState, 0);
Ted Kremenek3e743662008-01-14 23:24:37 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Tom Care472205b2010-09-29 23:48:13 +0000194 // Check if we have a steps limit
195 bool UnlimitedSteps = Steps == 0;
196
197 while (WList->hasWork()) {
198 if (!UnlimitedSteps) {
199 if (Steps == 0)
200 break;
201 --Steps;
202 }
203
Ted Kremenek3e743662008-01-14 23:24:37 +0000204 const GRWorkListUnit& WU = WList->Dequeue();
Mike Stump11289f42009-09-09 15:08:12 +0000205
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000206 // Set the current block counter.
207 WList->setBlockCounter(WU.getBlockCounter());
208
209 // Retrieve the node.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000210 ExplodedNode* Node = WU.getNode();
Mike Stump11289f42009-09-09 15:08:12 +0000211
Ted Kremenek3e743662008-01-14 23:24:37 +0000212 // Dispatch on the location type.
213 switch (Node->getLocation().getKind()) {
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000214 case ProgramPoint::BlockEdgeKind:
Ted Kremenek3e743662008-01-14 23:24:37 +0000215 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
216 break;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek3e743662008-01-14 23:24:37 +0000218 case ProgramPoint::BlockEntranceKind:
219 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
220 break;
Mike Stump11289f42009-09-09 15:08:12 +0000221
Ted Kremenek3e743662008-01-14 23:24:37 +0000222 case ProgramPoint::BlockExitKind:
Ted Kremeneke5843592008-01-15 00:24:08 +0000223 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000224 break;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000225
Douglas Gregora2fbc942010-02-25 19:01:53 +0000226 case ProgramPoint::CallEnterKind:
227 HandleCallEnter(cast<CallEnter>(Node->getLocation()), WU.getBlock(),
228 WU.getIndex(), Node);
229 break;
230
231 case ProgramPoint::CallExitKind:
232 HandleCallExit(cast<CallExit>(Node->getLocation()), Node);
233 break;
234
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000235 default:
236 assert(isa<PostStmt>(Node->getLocation()));
Ted Kremenek3e743662008-01-14 23:24:37 +0000237 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
238 WU.getIndex(), Node);
Mike Stump11289f42009-09-09 15:08:12 +0000239 break;
Ted Kremenek3e743662008-01-14 23:24:37 +0000240 }
241 }
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenek2b4adff2010-08-11 00:03:02 +0000243 SubEngine.ProcessEndWorklist(hasWorkRemaining());
Ted Kremenek3e743662008-01-14 23:24:37 +0000244 return WList->hasWork();
245}
246
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000247void GRCoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
248 unsigned Steps,
249 const GRState *InitState,
250 ExplodedNodeSet &Dst) {
251 ExecuteWorkList(L, Steps, InitState);
252 for (llvm::SmallVectorImpl<ExplodedNode*>::iterator I = G->EndNodes.begin(),
253 E = G->EndNodes.end(); I != E; ++I) {
254 Dst.Add(*I);
255 }
256}
257
Douglas Gregora2fbc942010-02-25 19:01:53 +0000258void GRCoreEngine::HandleCallEnter(const CallEnter &L, const CFGBlock *Block,
259 unsigned Index, ExplodedNode *Pred) {
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000260 GRCallEnterNodeBuilder Builder(*this, Pred, L.getCallExpr(),
261 L.getCalleeContext(), Block, Index);
Douglas Gregora2fbc942010-02-25 19:01:53 +0000262 ProcessCallEnter(Builder);
263}
264
265void GRCoreEngine::HandleCallExit(const CallExit &L, ExplodedNode *Pred) {
266 GRCallExitNodeBuilder Builder(*this, Pred);
267 ProcessCallExit(Builder);
268}
Zhongxing Xu82003da2009-08-06 10:00:15 +0000269
270void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000271
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000272 const CFGBlock* Blk = L.getDst();
Mike Stump11289f42009-09-09 15:08:12 +0000273
274 // Check if we are entering the EXIT block.
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000275 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
Mike Stump11289f42009-09-09 15:08:12 +0000276
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000277 assert (L.getLocationContext()->getCFG()->getExit().size() == 0
Ted Kremenek997d8722008-01-29 00:33:40 +0000278 && "EXIT block cannot contain Stmts.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000279
Ted Kremenek811c2b42008-04-11 22:03:04 +0000280 // Process the final state transition.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000281 GREndPathNodeBuilder Builder(Blk, Pred, this);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000282 ProcessEndPath(Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000283
Ted Kremenek3e743662008-01-14 23:24:37 +0000284 // This path is done. Don't enqueue any more nodes.
285 return;
286 }
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000287
288 // FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
Mike Stump11289f42009-09-09 15:08:12 +0000289
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000290 if (ProcessBlockEntrance(Blk, Pred, WList->getBlockCounter()))
Ted Kremenek090d62e2010-06-29 21:58:54 +0000291 GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()),
292 Pred->State, Pred);
Ted Kremenek2b4adff2010-08-11 00:03:02 +0000293 else {
294 blocksAborted.push_back(std::make_pair(L, Pred));
295 }
Ted Kremenek3e743662008-01-14 23:24:37 +0000296}
297
Zhongxing Xu82003da2009-08-06 10:00:15 +0000298void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000299 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000301 // Increment the block counter.
302 GRBlockCounter Counter = WList->getBlockCounter();
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000303 Counter = BCounterFactory.IncrementCount(Counter,
304 Pred->getLocationContext()->getCurrentStackFrame(),
305 L.getBlock()->getBlockID());
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000306 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000307
308 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000309 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000310 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000311 SubEngine.getStateManager());
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000312 ProcessElement(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000315 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000316}
317
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000318void GRCoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000319
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000320 if (const Stmt* Term = B->getTerminator()) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000321 switch (Term->getStmtClass()) {
322 default:
323 assert(false && "Analysis for this terminator not implemented.");
324 break;
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek822f7372008-02-12 21:51:20 +0000326 case Stmt::BinaryOperatorClass: // '&&' and '||'
327 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
328 return;
Mike Stump11289f42009-09-09 15:08:12 +0000329
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000330 case Stmt::ConditionalOperatorClass:
331 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000332 return;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenek822f7372008-02-12 21:51:20 +0000334 // FIXME: Use constant-folding in CFG construction to simplify this
335 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000337 case Stmt::ChooseExprClass:
338 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000339 return;
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek822f7372008-02-12 21:51:20 +0000341 case Stmt::DoStmtClass:
342 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
343 return;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Ted Kremenek822f7372008-02-12 21:51:20 +0000345 case Stmt::ForStmtClass:
346 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
347 return;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenek632bcb82008-02-13 16:56:51 +0000349 case Stmt::ContinueStmtClass:
350 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000351 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000352 break;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000354 case Stmt::IfStmtClass:
355 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000356 return;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenek7022efb2008-02-13 00:24:44 +0000358 case Stmt::IndirectGotoStmtClass: {
359 // Only 1 successor: the indirect goto dispatch block.
360 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000361
Zhongxing Xu107f7592009-08-06 12:48:26 +0000362 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000363 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
364 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek7022efb2008-02-13 00:24:44 +0000366 ProcessIndirectGoto(builder);
367 return;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenek17810802008-11-12 19:24:17 +0000370 case Stmt::ObjCForCollectionStmtClass: {
371 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
372 //
373 // (1) inside a basic block, which represents the binding of the
374 // 'element' variable to a value.
375 // (2) in a terminator, which represents the branch.
376 //
377 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
378 // whether or not collection contains any more elements. We cannot
379 // just test to see if the element is nil because a container can
380 // contain nil elements.
381 HandleBranch(Term, Term, B, Pred);
382 return;
383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000385 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000386 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
387 this);
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000389 ProcessSwitch(builder);
390 return;
391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000393 case Stmt::WhileStmtClass:
394 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000395 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000396 }
397 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000398
399 assert (B->succ_size() == 1 &&
400 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000401
402 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000403 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000404}
405
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000406void GRCoreEngine::HandleBranch(const Stmt* Cond, const Stmt* Term,
407 const CFGBlock * B, ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000408 assert (B->succ_size() == 2);
409
Zhongxing Xu107f7592009-08-06 12:48:26 +0000410 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
411 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000412
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000413 ProcessBranch(Cond, Term, Builder);
414}
415
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000416void GRCoreEngine::HandlePostStmt(const PostStmt& L, const CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000417 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000418
Ted Kremenek3e743662008-01-14 23:24:37 +0000419 assert (!B->empty());
420
Ted Kremeneke5843592008-01-15 00:24:08 +0000421 if (StmtIdx == B->size())
422 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000423 else {
Mike Stump11289f42009-09-09 15:08:12 +0000424 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000425 SubEngine.getStateManager());
Zhongxing Xu5d30c692010-11-15 08:48:43 +0000426 ProcessElement((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000427 }
428}
429
Ted Kremenek3e743662008-01-14 23:24:37 +0000430/// GenerateNode - Utility method to generate nodes, hook up successors,
431/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000432void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000433 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek3e743662008-01-14 23:24:37 +0000435 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000436 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000437
438 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000439 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000440 else {
441 assert (IsNew);
442 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenek3e743662008-01-14 23:24:37 +0000445 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000446 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000447}
448
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000449GRStmtNodeBuilder::GRStmtNodeBuilder(const CFGBlock* b, unsigned idx,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000450 ExplodedNode* N, GRCoreEngine* e,
451 GRStateManager &mgr)
Ted Kremenek982b32b2010-10-20 23:48:34 +0000452 : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000453 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
454 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000455 Deferred.insert(N);
Zhongxing Xud041bc62010-02-26 02:38:09 +0000456 CleanedState = Pred->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000457}
458
Zhongxing Xu107f7592009-08-06 12:48:26 +0000459GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000460 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000461 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000462 GenerateAutoTransition(*I);
463}
464
Zhongxing Xu107f7592009-08-06 12:48:26 +0000465void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000466 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregora2fbc942010-02-25 19:01:53 +0000468 // Check if this node entered a callee.
469 if (isa<CallEnter>(N->getLocation())) {
470 // Still use the index of the CallExpr. It's needed to create the callee
471 // StackFrameContext.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000472 Eng.WList->Enqueue(N, &B, Idx);
Douglas Gregora2fbc942010-02-25 19:01:53 +0000473 return;
474 }
475
Zhongxing Xue1190f72009-08-15 03:17:38 +0000476 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000477
Ted Kremenek3e743662008-01-14 23:24:37 +0000478 if (Loc == N->getLocation()) {
479 // Note: 'N' should be a fresh node because otherwise it shouldn't be
480 // a member of Deferred.
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000481 Eng.WList->Enqueue(N, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000482 return;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek3e743662008-01-14 23:24:37 +0000485 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000486 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000487 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000488
489 if (IsNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000490 Eng.WList->Enqueue(Succ, &B, Idx+1);
Ted Kremenek3e743662008-01-14 23:24:37 +0000491}
492
Zhongxing Xuc2acbe02010-07-20 02:41:28 +0000493ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, const Stmt* S,
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000494 ExplodedNode* Pred, const GRState* St,
495 ProgramPoint::Kind K) {
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000496
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000497 ExplodedNode* N = generateNode(S, St, Pred, K);
498
499 if (N) {
500 if (BuildSinks)
501 N->markAsSink();
Ted Kremenek982b32b2010-10-20 23:48:34 +0000502 else
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000503 Dst.Add(N);
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000504 }
505
506 return N;
507}
508
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000509static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
510 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000511 switch (K) {
512 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000513 assert(false && "Unhandled ProgramPoint kind");
514 case ProgramPoint::PreStmtKind:
515 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000516 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000517 return PostStmt(S, LC, tag);
518 case ProgramPoint::PreLoadKind:
519 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000520 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000521 return PostLoad(S, LC, tag);
522 case ProgramPoint::PreStoreKind:
523 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000524 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000525 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000526 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000527 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000528 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000529 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000530 }
531}
532
Zhongxing Xu20227f72009-08-06 01:32:16 +0000533ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000534GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000535 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000536 ProgramPoint::Kind K,
537 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000538
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000539 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000540 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000541}
542
Zhongxing Xu20227f72009-08-06 01:32:16 +0000543ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000544GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000545 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000546 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000547 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000548 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000549 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000550 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Ted Kremenek3e743662008-01-14 23:24:37 +0000552 if (IsNew) {
553 Deferred.insert(N);
Ted Kremenek3e743662008-01-14 23:24:37 +0000554 return N;
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Mike Stump11289f42009-09-09 15:08:12 +0000557 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000558}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000559
Zhongxing Xu107f7592009-08-06 12:48:26 +0000560ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
561 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000563 // If the branch has been marked infeasible we should not generate a node.
564 if (!isFeasible(branch))
565 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000566
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000567 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000568
Zhongxing Xu20227f72009-08-06 01:32:16 +0000569 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000570 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
571 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000572
Ted Kremenekc3661de2009-10-07 00:42:52 +0000573 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000575 if (branch)
576 GeneratedTrue = true;
577 else
Mike Stump11289f42009-09-09 15:08:12 +0000578 GeneratedFalse = true;
579
Ted Kremeneka50d9852008-01-30 23:03:39 +0000580 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000581 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000582 return Succ;
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremeneka50d9852008-01-30 23:03:39 +0000585 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000586}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000587
Zhongxing Xu107f7592009-08-06 12:48:26 +0000588GRBranchNodeBuilder::~GRBranchNodeBuilder() {
589 if (!GeneratedTrue) generateNode(Pred->State, true);
590 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000591
Ted Kremenek2531fce2008-01-30 23:24:39 +0000592 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000593 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000594}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000595
Ted Kremenek7022efb2008-02-13 00:24:44 +0000596
Zhongxing Xu20227f72009-08-06 01:32:16 +0000597ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000598GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
599 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000600 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000601
602 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000603 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenekc3661de2009-10-07 00:42:52 +0000605 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek7022efb2008-02-13 00:24:44 +0000607 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000608
Ted Kremenek7022efb2008-02-13 00:24:44 +0000609 if (isSink)
610 Succ->markAsSink();
611 else
612 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000613
Ted Kremenek7022efb2008-02-13 00:24:44 +0000614 return Succ;
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Ted Kremenek7022efb2008-02-13 00:24:44 +0000617 return NULL;
618}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000619
620
Zhongxing Xu20227f72009-08-06 01:32:16 +0000621ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000622GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000623
624 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000625
Zhongxing Xue1190f72009-08-15 03:17:38 +0000626 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
627 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000628 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000629
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000630 if (IsNew) {
631 Eng.WList->Enqueue(Succ);
632 return Succ;
633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000635 return NULL;
636}
637
638
Zhongxing Xu20227f72009-08-06 01:32:16 +0000639ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000640GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000641
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000642 // Get the block for the default case.
643 assert (Src->succ_rbegin() != Src->succ_rend());
644 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000645
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000646 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Zhongxing Xue1190f72009-08-15 03:17:38 +0000648 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
649 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000650 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000652 if (IsNew) {
653 if (isSink)
654 Succ->markAsSink();
655 else
656 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000658 return Succ;
659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000661 return NULL;
662}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000663
Zhongxing Xu107f7592009-08-06 12:48:26 +0000664GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000665 // Auto-generate an EOP node if one has not been generated.
Douglas Gregora2fbc942010-02-25 19:01:53 +0000666 if (!HasGeneratedNode) {
667 // If we are in an inlined call, generate CallExit node.
668 if (Pred->getLocationContext()->getParent())
669 GenerateCallExitNode(Pred->State);
670 else
671 generateNode(Pred->State);
672 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000673}
674
Zhongxing Xu20227f72009-08-06 01:32:16 +0000675ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000676GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
677 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000678 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000679 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000680
681 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000682 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenekc3661de2009-10-07 00:42:52 +0000684 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Ted Kremenek811c2b42008-04-11 22:03:04 +0000686 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000687 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000688 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Ted Kremenekd004c412008-04-18 16:30:14 +0000691 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000692}
Douglas Gregora2fbc942010-02-25 19:01:53 +0000693
694void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
695 HasGeneratedNode = true;
696 // Create a CallExit node and enqueue it.
697 const StackFrameContext *LocCtx
698 = cast<StackFrameContext>(Pred->getLocationContext());
699 const Stmt *CE = LocCtx->getCallSite();
700
701 // Use the the callee location context.
702 CallExit Loc(CE, LocCtx);
703
704 bool isNew;
705 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
706 Node->addPredecessor(Pred, *Eng.G);
707
708 if (isNew)
709 Eng.WList->Enqueue(Node);
710}
711
712
713void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
714 const LocationContext *LocCtx) {
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000715 // Check if the callee is in the same translation unit.
716 if (CalleeCtx->getTranslationUnit() !=
717 Pred->getLocationContext()->getTranslationUnit()) {
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000718 // Create a new engine. We must be careful that the new engine should not
719 // reference data structures owned by the old engine.
720
721 AnalysisManager &OldMgr = Eng.SubEngine.getAnalysisManager();
722
723 // Get the callee's translation unit.
724 idx::TranslationUnit *TU = CalleeCtx->getTranslationUnit();
725
726 // Create a new AnalysisManager with components of the callee's
727 // TranslationUnit.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000728 // The Diagnostic is actually shared when we create ASTUnits from AST files.
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000729 AnalysisManager AMgr(TU->getASTContext(), TU->getDiagnostic(),
730 OldMgr.getLangOptions(),
731 OldMgr.getPathDiagnosticClient(),
732 OldMgr.getStoreManagerCreator(),
733 OldMgr.getConstraintManagerCreator(),
734 OldMgr.getIndexer(),
Tom Carec88ed952010-09-14 21:35:27 +0000735 OldMgr.getMaxNodes(), OldMgr.getMaxVisit(),
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000736 OldMgr.shouldVisualizeGraphviz(),
737 OldMgr.shouldVisualizeUbigraph(),
738 OldMgr.shouldPurgeDead(),
739 OldMgr.shouldEagerlyAssume(),
740 OldMgr.shouldTrimGraph(),
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000741 OldMgr.shouldInlineCall(),
Marcin Swiderski99a90402010-09-30 07:41:24 +0000742 OldMgr.getAnalysisContextManager().getUseUnoptimizedCFG(),
743 OldMgr.getAnalysisContextManager().getAddImplicitDtors(),
744 OldMgr.getAnalysisContextManager().getAddInitializers());
Zhongxing Xuadf644d2010-07-22 13:52:13 +0000745 llvm::OwningPtr<GRTransferFuncs> TF(MakeCFRefCountTF(AMgr.getASTContext(),
746 /* GCEnabled */ false,
747 AMgr.getLangOptions()));
748 // Create the new engine.
749 GRExprEngine NewEng(AMgr, TF.take());
750
751 // Create the new LocationContext.
752 AnalysisContext *NewAnaCtx = AMgr.getAnalysisContext(CalleeCtx->getDecl(),
753 CalleeCtx->getTranslationUnit());
754 const StackFrameContext *OldLocCtx = cast<StackFrameContext>(LocCtx);
755 const StackFrameContext *NewLocCtx = AMgr.getStackFrame(NewAnaCtx,
756 OldLocCtx->getParent(),
757 OldLocCtx->getCallSite(),
758 OldLocCtx->getCallSiteBlock(),
759 OldLocCtx->getIndex());
760
761 // Now create an initial state for the new engine.
762 const GRState *NewState = NewEng.getStateManager().MarshalState(state,
763 NewLocCtx);
764 ExplodedNodeSet ReturnNodes;
765 NewEng.ExecuteWorkListWithInitialState(NewLocCtx, AMgr.getMaxNodes(),
766 NewState, ReturnNodes);
767 return;
Zhongxing Xu84f65e02010-07-19 01:31:21 +0000768 }
769
Douglas Gregora2fbc942010-02-25 19:01:53 +0000770 // Get the callee entry block.
771 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
772 assert(Entry->empty());
773 assert(Entry->succ_size() == 1);
774
775 // Get the solitary successor.
776 const CFGBlock *SuccB = *(Entry->succ_begin());
777
778 // Construct an edge representing the starting location in the callee.
779 BlockEdge Loc(Entry, SuccB, LocCtx);
780
781 bool isNew;
782 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
783 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
784
785 if (isNew)
786 Eng.WList->Enqueue(Node);
787}
788
789void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
790 // Get the callee's location context.
791 const StackFrameContext *LocCtx
792 = cast<StackFrameContext>(Pred->getLocationContext());
793
794 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
795 bool isNew;
796 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
797 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
798 if (isNew)
Zhongxing Xuedb77fe2010-07-20 06:22:24 +0000799 Eng.WList->Enqueue(Node, LocCtx->getCallSiteBlock(),
Douglas Gregora2fbc942010-02-25 19:01:53 +0000800 LocCtx->getIndex() + 1);
801}