blob: 23a87d303b4d447915fe9c9db2b289356a436108 [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
Ted Kremenekd6b87082010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/GRCoreEngine.h"
16#include "clang/Checker/PathSensitive/GRExprEngine.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000017#include "clang/AST/Expr.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000018#include "llvm/Support/Casting.h"
19#include "llvm/ADT/DenseMap.h"
20#include <vector>
Ted Kremenekd9de9f12008-12-16 22:13:33 +000021#include <queue>
Ted Kremenek3e743662008-01-14 23:24:37 +000022
23using llvm::cast;
24using llvm::isa;
25using namespace clang;
26
Ted Kremenekd9de9f12008-12-16 22:13:33 +000027//===----------------------------------------------------------------------===//
28// Worklist classes for exploration of reachable states.
29//===----------------------------------------------------------------------===//
30
Ted Kremenek3e743662008-01-14 23:24:37 +000031namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000032class DFS : public GRWorkList {
Ted Kremenek3e743662008-01-14 23:24:37 +000033 llvm::SmallVector<GRWorkListUnit,20> Stack;
34public:
35 virtual bool hasWork() const {
36 return !Stack.empty();
37 }
38
39 virtual void Enqueue(const GRWorkListUnit& U) {
40 Stack.push_back(U);
41 }
42
43 virtual GRWorkListUnit Dequeue() {
44 assert (!Stack.empty());
45 const GRWorkListUnit& U = Stack.back();
46 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
47 return U;
48 }
49};
Mike Stump11289f42009-09-09 15:08:12 +000050
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000051class BFS : public GRWorkList {
Ted Kremenek2c327732009-05-01 22:18:46 +000052 std::queue<GRWorkListUnit> Queue;
53public:
54 virtual bool hasWork() const {
55 return !Queue.empty();
56 }
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenek2c327732009-05-01 22:18:46 +000058 virtual void Enqueue(const GRWorkListUnit& U) {
59 Queue.push(U);
60 }
Mike Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenek2c327732009-05-01 22:18:46 +000062 virtual GRWorkListUnit Dequeue() {
63 // Don't use const reference. The subsequent pop_back() might make it
64 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +000065 GRWorkListUnit U = Queue.front();
Ted Kremenek2c327732009-05-01 22:18:46 +000066 Queue.pop();
67 return U;
68 }
69};
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenek3e743662008-01-14 23:24:37 +000071} // end anonymous namespace
72
Ted Kremenek2e12c2e2008-01-16 18:18:48 +000073// Place the dstor for GRWorkList here because it contains virtual member
74// functions, and we the code for the dstor generated in one compilation unit.
75GRWorkList::~GRWorkList() {}
76
Ted Kremenek2c327732009-05-01 22:18:46 +000077GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
78GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
Ted Kremenek3e743662008-01-14 23:24:37 +000079
Ted Kremenekd9de9f12008-12-16 22:13:33 +000080namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000081 class BFSBlockDFSContents : public GRWorkList {
Ted Kremenekd9de9f12008-12-16 22:13:33 +000082 std::queue<GRWorkListUnit> Queue;
83 llvm::SmallVector<GRWorkListUnit,20> Stack;
84 public:
85 virtual bool hasWork() const {
86 return !Queue.empty() || !Stack.empty();
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremenekd9de9f12008-12-16 22:13:33 +000089 virtual void Enqueue(const GRWorkListUnit& U) {
90 if (isa<BlockEntrance>(U.getNode()->getLocation()))
91 Queue.push(U);
92 else
93 Stack.push_back(U);
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Ted Kremenekd9de9f12008-12-16 22:13:33 +000096 virtual GRWorkListUnit Dequeue() {
97 // Process all basic blocks to completion.
98 if (!Stack.empty()) {
99 const GRWorkListUnit& U = Stack.back();
100 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
101 return U;
102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000104 assert(!Queue.empty());
105 // Don't use const reference. The subsequent pop_back() might make it
106 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +0000107 GRWorkListUnit U = Queue.front();
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000108 Queue.pop();
Mike Stump11289f42009-09-09 15:08:12 +0000109 return U;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000110 }
111 };
112} // end anonymous namespace
113
114GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
115 return new BFSBlockDFSContents();
116}
117
118//===----------------------------------------------------------------------===//
119// Core analysis engine.
120//===----------------------------------------------------------------------===//
Zhongxing Xu107f7592009-08-06 12:48:26 +0000121void GRCoreEngine::ProcessEndPath(GREndPathNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000122 SubEngine.ProcessEndPath(Builder);
123}
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000124
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000125void GRCoreEngine::ProcessStmt(CFGElement E, GRStmtNodeBuilder& Builder) {
126 SubEngine.ProcessStmt(E, Builder);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000127}
128
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000129bool GRCoreEngine::ProcessBlockEntrance(CFGBlock* Blk, const ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +0000130 GRBlockCounter BC) {
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000131 return SubEngine.ProcessBlockEntrance(Blk, Pred, BC);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000132}
133
134void GRCoreEngine::ProcessBranch(Stmt* Condition, Stmt* Terminator,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000135 GRBranchNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000136 SubEngine.ProcessBranch(Condition, Terminator, Builder);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000137}
138
Zhongxing Xu107f7592009-08-06 12:48:26 +0000139void GRCoreEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000140 SubEngine.ProcessIndirectGoto(Builder);
141}
142
Zhongxing Xu107f7592009-08-06 12:48:26 +0000143void GRCoreEngine::ProcessSwitch(GRSwitchNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000144 SubEngine.ProcessSwitch(Builder);
145}
Zhongxing Xu107f7592009-08-06 12:48:26 +0000146
Douglas Gregora2fbc942010-02-25 19:01:53 +0000147void GRCoreEngine::ProcessCallEnter(GRCallEnterNodeBuilder &Builder) {
148 SubEngine.ProcessCallEnter(Builder);
149}
150
151void GRCoreEngine::ProcessCallExit(GRCallExitNodeBuilder &Builder) {
152 SubEngine.ProcessCallExit(Builder);
153}
154
Ted Kremenek3e743662008-01-14 23:24:37 +0000155/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000156bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps) {
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek3e743662008-01-14 23:24:37 +0000158 if (G->num_roots() == 0) { // Initialize the analysis by constructing
159 // the root if none exists.
Mike Stump11289f42009-09-09 15:08:12 +0000160
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000161 CFGBlock* Entry = &(L->getCFG()->getEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000162
163 assert (Entry->empty() &&
Ted Kremenek3e743662008-01-14 23:24:37 +0000164 "Entry block must be empty.");
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek3e743662008-01-14 23:24:37 +0000166 assert (Entry->succ_size() == 1 &&
167 "Entry block must have 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenek3e743662008-01-14 23:24:37 +0000169 // Get the solitary successor.
Mike Stump11289f42009-09-09 15:08:12 +0000170 CFGBlock* Succ = *(Entry->succ_begin());
171
Ted Kremenek3e743662008-01-14 23:24:37 +0000172 // Construct an edge representing the
173 // starting location in the function.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000174 BlockEdge StartLoc(Entry, Succ, L);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000176 // Set the current block counter to being empty.
177 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
Mike Stump11289f42009-09-09 15:08:12 +0000178
Ted Kremenek3e743662008-01-14 23:24:37 +0000179 // Generate the root.
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000180 GenerateNode(StartLoc, getInitialState(L), 0);
Ted Kremenek3e743662008-01-14 23:24:37 +0000181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek3e743662008-01-14 23:24:37 +0000183 while (Steps && WList->hasWork()) {
184 --Steps;
185 const GRWorkListUnit& WU = WList->Dequeue();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000187 // Set the current block counter.
188 WList->setBlockCounter(WU.getBlockCounter());
189
190 // Retrieve the node.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000191 ExplodedNode* Node = WU.getNode();
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek3e743662008-01-14 23:24:37 +0000193 // Dispatch on the location type.
194 switch (Node->getLocation().getKind()) {
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000195 case ProgramPoint::BlockEdgeKind:
Ted Kremenek3e743662008-01-14 23:24:37 +0000196 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
197 break;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek3e743662008-01-14 23:24:37 +0000199 case ProgramPoint::BlockEntranceKind:
200 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
201 break;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek3e743662008-01-14 23:24:37 +0000203 case ProgramPoint::BlockExitKind:
Ted Kremeneke5843592008-01-15 00:24:08 +0000204 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000205 break;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000206
Douglas Gregora2fbc942010-02-25 19:01:53 +0000207 case ProgramPoint::CallEnterKind:
208 HandleCallEnter(cast<CallEnter>(Node->getLocation()), WU.getBlock(),
209 WU.getIndex(), Node);
210 break;
211
212 case ProgramPoint::CallExitKind:
213 HandleCallExit(cast<CallExit>(Node->getLocation()), Node);
214 break;
215
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000216 default:
217 assert(isa<PostStmt>(Node->getLocation()));
Ted Kremenek3e743662008-01-14 23:24:37 +0000218 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
219 WU.getIndex(), Node);
Mike Stump11289f42009-09-09 15:08:12 +0000220 break;
Ted Kremenek3e743662008-01-14 23:24:37 +0000221 }
222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremenek3e743662008-01-14 23:24:37 +0000224 return WList->hasWork();
225}
226
Douglas Gregora2fbc942010-02-25 19:01:53 +0000227void GRCoreEngine::HandleCallEnter(const CallEnter &L, const CFGBlock *Block,
228 unsigned Index, ExplodedNode *Pred) {
229 GRCallEnterNodeBuilder Builder(*this, Pred, L.getCallExpr(), L.getCallee(),
230 Block, Index);
231 ProcessCallEnter(Builder);
232}
233
234void GRCoreEngine::HandleCallExit(const CallExit &L, ExplodedNode *Pred) {
235 GRCallExitNodeBuilder Builder(*this, Pred);
236 ProcessCallExit(Builder);
237}
Zhongxing Xu82003da2009-08-06 10:00:15 +0000238
239void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000240
Ted Kremenek3e743662008-01-14 23:24:37 +0000241 CFGBlock* Blk = L.getDst();
Mike Stump11289f42009-09-09 15:08:12 +0000242
243 // Check if we are entering the EXIT block.
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000244 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
Mike Stump11289f42009-09-09 15:08:12 +0000245
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000246 assert (L.getLocationContext()->getCFG()->getExit().size() == 0
Ted Kremenek997d8722008-01-29 00:33:40 +0000247 && "EXIT block cannot contain Stmts.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000248
Ted Kremenek811c2b42008-04-11 22:03:04 +0000249 // Process the final state transition.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000250 GREndPathNodeBuilder Builder(Blk, Pred, this);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000251 ProcessEndPath(Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000252
Ted Kremenek3e743662008-01-14 23:24:37 +0000253 // This path is done. Don't enqueue any more nodes.
254 return;
255 }
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000256
257 // FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
Mike Stump11289f42009-09-09 15:08:12 +0000258
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000259 if (ProcessBlockEntrance(Blk, Pred, WList->getBlockCounter()))
Zhongxing Xue1190f72009-08-15 03:17:38 +0000260 GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()), Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000261}
262
Zhongxing Xu82003da2009-08-06 10:00:15 +0000263void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000264 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000266 // Increment the block counter.
267 GRBlockCounter Counter = WList->getBlockCounter();
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000268 Counter = BCounterFactory.IncrementCount(Counter,
269 Pred->getLocationContext()->getCurrentStackFrame(),
270 L.getBlock()->getBlockID());
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000271 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000272
273 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000274 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000275 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000276 SubEngine.getStateManager());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000277 ProcessStmt(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000278 }
Mike Stump11289f42009-09-09 15:08:12 +0000279 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000280 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000281}
282
Zhongxing Xu82003da2009-08-06 10:00:15 +0000283void GRCoreEngine::HandleBlockExit(CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000285 if (Stmt* Term = B->getTerminator()) {
286 switch (Term->getStmtClass()) {
287 default:
288 assert(false && "Analysis for this terminator not implemented.");
289 break;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Ted Kremenek822f7372008-02-12 21:51:20 +0000291 case Stmt::BinaryOperatorClass: // '&&' and '||'
292 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
293 return;
Mike Stump11289f42009-09-09 15:08:12 +0000294
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000295 case Stmt::ConditionalOperatorClass:
296 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000297 return;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Ted Kremenek822f7372008-02-12 21:51:20 +0000299 // FIXME: Use constant-folding in CFG construction to simplify this
300 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000302 case Stmt::ChooseExprClass:
303 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000304 return;
Mike Stump11289f42009-09-09 15:08:12 +0000305
Ted Kremenek822f7372008-02-12 21:51:20 +0000306 case Stmt::DoStmtClass:
307 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
308 return;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek822f7372008-02-12 21:51:20 +0000310 case Stmt::ForStmtClass:
311 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
312 return;
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek632bcb82008-02-13 16:56:51 +0000314 case Stmt::ContinueStmtClass:
315 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000316 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000317 break;
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000319 case Stmt::IfStmtClass:
320 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000321 return;
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek7022efb2008-02-13 00:24:44 +0000323 case Stmt::IndirectGotoStmtClass: {
324 // Only 1 successor: the indirect goto dispatch block.
325 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000326
Zhongxing Xu107f7592009-08-06 12:48:26 +0000327 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000328 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
329 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Ted Kremenek7022efb2008-02-13 00:24:44 +0000331 ProcessIndirectGoto(builder);
332 return;
333 }
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek17810802008-11-12 19:24:17 +0000335 case Stmt::ObjCForCollectionStmtClass: {
336 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
337 //
338 // (1) inside a basic block, which represents the binding of the
339 // 'element' variable to a value.
340 // (2) in a terminator, which represents the branch.
341 //
342 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
343 // whether or not collection contains any more elements. We cannot
344 // just test to see if the element is nil because a container can
345 // contain nil elements.
346 HandleBranch(Term, Term, B, Pred);
347 return;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000350 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000351 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
352 this);
Mike Stump11289f42009-09-09 15:08:12 +0000353
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000354 ProcessSwitch(builder);
355 return;
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000358 case Stmt::WhileStmtClass:
359 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000360 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000361 }
362 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000363
364 assert (B->succ_size() == 1 &&
365 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000366
367 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000368 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000369}
370
Zhongxing Xu82003da2009-08-06 10:00:15 +0000371void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
372 ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000373 assert (B->succ_size() == 2);
374
Zhongxing Xu107f7592009-08-06 12:48:26 +0000375 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
376 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000378 ProcessBranch(Cond, Term, Builder);
379}
380
Zhongxing Xu82003da2009-08-06 10:00:15 +0000381void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000382 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek3e743662008-01-14 23:24:37 +0000384 assert (!B->empty());
385
Ted Kremeneke5843592008-01-15 00:24:08 +0000386 if (StmtIdx == B->size())
387 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000388 else {
Mike Stump11289f42009-09-09 15:08:12 +0000389 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000390 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000391 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000392 }
393}
394
Ted Kremenek3e743662008-01-14 23:24:37 +0000395/// GenerateNode - Utility method to generate nodes, hook up successors,
396/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000397void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000398 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek3e743662008-01-14 23:24:37 +0000400 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000401 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000402
403 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000404 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000405 else {
406 assert (IsNew);
407 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ted Kremenek3e743662008-01-14 23:24:37 +0000410 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000411 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000412}
413
Zhongxing Xu107f7592009-08-06 12:48:26 +0000414GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
415 ExplodedNode* N, GRCoreEngine* e,
416 GRStateManager &mgr)
Zhongxing Xud041bc62010-02-26 02:38:09 +0000417 : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr), Auditor(0),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000418 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
419 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000420 Deferred.insert(N);
Zhongxing Xud041bc62010-02-26 02:38:09 +0000421 CleanedState = Pred->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000422}
423
Zhongxing Xu107f7592009-08-06 12:48:26 +0000424GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000425 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000426 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000427 GenerateAutoTransition(*I);
428}
429
Zhongxing Xu107f7592009-08-06 12:48:26 +0000430void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000431 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000432
Douglas Gregora2fbc942010-02-25 19:01:53 +0000433 // Check if this node entered a callee.
434 if (isa<CallEnter>(N->getLocation())) {
435 // Still use the index of the CallExpr. It's needed to create the callee
436 // StackFrameContext.
437 Eng.WList->Enqueue(N, B, Idx);
438 return;
439 }
440
Zhongxing Xue1190f72009-08-15 03:17:38 +0000441 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek3e743662008-01-14 23:24:37 +0000443 if (Loc == N->getLocation()) {
444 // Note: 'N' should be a fresh node because otherwise it shouldn't be
445 // a member of Deferred.
446 Eng.WList->Enqueue(N, B, Idx+1);
447 return;
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek3e743662008-01-14 23:24:37 +0000450 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000451 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000452 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000453
454 if (IsNew)
455 Eng.WList->Enqueue(Succ, B, Idx+1);
456}
457
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000458ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
459 ExplodedNode* Pred, const GRState* St,
460 ProgramPoint::Kind K) {
461 const GRState* PredState = GetState(Pred);
462
463 // If the state hasn't changed, don't generate a new node.
464 if (!BuildSinks && St == PredState && Auditor == 0) {
465 Dst.Add(Pred);
466 return NULL;
467 }
468
469 ExplodedNode* N = generateNode(S, St, Pred, K);
470
471 if (N) {
472 if (BuildSinks)
473 N->markAsSink();
474 else {
475 if (Auditor && Auditor->Audit(N, Mgr))
476 N->markAsSink();
477
478 Dst.Add(N);
479 }
480 }
481
482 return N;
483}
484
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000485static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
486 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000487 switch (K) {
488 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000489 assert(false && "Unhandled ProgramPoint kind");
490 case ProgramPoint::PreStmtKind:
491 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000492 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000493 return PostStmt(S, LC, tag);
494 case ProgramPoint::PreLoadKind:
495 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000496 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000497 return PostLoad(S, LC, tag);
498 case ProgramPoint::PreStoreKind:
499 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000500 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000501 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000502 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000503 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000504 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000505 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000506 }
507}
508
Zhongxing Xu20227f72009-08-06 01:32:16 +0000509ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000510GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000511 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000512 ProgramPoint::Kind K,
513 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000514
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000515 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000516 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000517}
518
Zhongxing Xu20227f72009-08-06 01:32:16 +0000519ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000520GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000521 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000522 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000523 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000524 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000525 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000526 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000527
Ted Kremenek3e743662008-01-14 23:24:37 +0000528 if (IsNew) {
529 Deferred.insert(N);
Ted Kremenek3e743662008-01-14 23:24:37 +0000530 return N;
531 }
Mike Stump11289f42009-09-09 15:08:12 +0000532
Mike Stump11289f42009-09-09 15:08:12 +0000533 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000534}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000535
Zhongxing Xu107f7592009-08-06 12:48:26 +0000536ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
537 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000538
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000539 // If the branch has been marked infeasible we should not generate a node.
540 if (!isFeasible(branch))
541 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000543 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000544
Zhongxing Xu20227f72009-08-06 01:32:16 +0000545 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000546 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
547 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Ted Kremenekc3661de2009-10-07 00:42:52 +0000549 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000551 if (branch)
552 GeneratedTrue = true;
553 else
Mike Stump11289f42009-09-09 15:08:12 +0000554 GeneratedFalse = true;
555
Ted Kremeneka50d9852008-01-30 23:03:39 +0000556 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000557 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000558 return Succ;
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Ted Kremeneka50d9852008-01-30 23:03:39 +0000561 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000562}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000563
Zhongxing Xu107f7592009-08-06 12:48:26 +0000564GRBranchNodeBuilder::~GRBranchNodeBuilder() {
565 if (!GeneratedTrue) generateNode(Pred->State, true);
566 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Ted Kremenek2531fce2008-01-30 23:24:39 +0000568 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000569 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000570}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000571
Ted Kremenek7022efb2008-02-13 00:24:44 +0000572
Zhongxing Xu20227f72009-08-06 01:32:16 +0000573ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000574GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
575 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000576 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000577
578 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000579 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Ted Kremenekc3661de2009-10-07 00:42:52 +0000581 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek7022efb2008-02-13 00:24:44 +0000583 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremenek7022efb2008-02-13 00:24:44 +0000585 if (isSink)
586 Succ->markAsSink();
587 else
588 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Ted Kremenek7022efb2008-02-13 00:24:44 +0000590 return Succ;
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek7022efb2008-02-13 00:24:44 +0000593 return NULL;
594}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000595
596
Zhongxing Xu20227f72009-08-06 01:32:16 +0000597ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000598GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000599
600 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Zhongxing Xue1190f72009-08-15 03:17:38 +0000602 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
603 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000604 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000605
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000606 if (IsNew) {
607 Eng.WList->Enqueue(Succ);
608 return Succ;
609 }
Mike Stump11289f42009-09-09 15:08:12 +0000610
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000611 return NULL;
612}
613
614
Zhongxing Xu20227f72009-08-06 01:32:16 +0000615ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000616GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000617
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000618 // Get the block for the default case.
619 assert (Src->succ_rbegin() != Src->succ_rend());
620 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000622 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000623
Zhongxing Xue1190f72009-08-15 03:17:38 +0000624 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
625 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000626 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000628 if (IsNew) {
629 if (isSink)
630 Succ->markAsSink();
631 else
632 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000633
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000634 return Succ;
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000637 return NULL;
638}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000639
Zhongxing Xu107f7592009-08-06 12:48:26 +0000640GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000641 // Auto-generate an EOP node if one has not been generated.
Douglas Gregora2fbc942010-02-25 19:01:53 +0000642 if (!HasGeneratedNode) {
643 // If we are in an inlined call, generate CallExit node.
644 if (Pred->getLocationContext()->getParent())
645 GenerateCallExitNode(Pred->State);
646 else
647 generateNode(Pred->State);
648 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000649}
650
Zhongxing Xu20227f72009-08-06 01:32:16 +0000651ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000652GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
653 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000654 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000655 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000656
657 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000658 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000659
Ted Kremenekc3661de2009-10-07 00:42:52 +0000660 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000661
Ted Kremenek811c2b42008-04-11 22:03:04 +0000662 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000663 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000664 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Ted Kremenekd004c412008-04-18 16:30:14 +0000667 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000668}
Douglas Gregora2fbc942010-02-25 19:01:53 +0000669
670void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
671 HasGeneratedNode = true;
672 // Create a CallExit node and enqueue it.
673 const StackFrameContext *LocCtx
674 = cast<StackFrameContext>(Pred->getLocationContext());
675 const Stmt *CE = LocCtx->getCallSite();
676
677 // Use the the callee location context.
678 CallExit Loc(CE, LocCtx);
679
680 bool isNew;
681 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
682 Node->addPredecessor(Pred, *Eng.G);
683
684 if (isNew)
685 Eng.WList->Enqueue(Node);
686}
687
688
689void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
690 const LocationContext *LocCtx) {
691 // Get the callee entry block.
692 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
693 assert(Entry->empty());
694 assert(Entry->succ_size() == 1);
695
696 // Get the solitary successor.
697 const CFGBlock *SuccB = *(Entry->succ_begin());
698
699 // Construct an edge representing the starting location in the callee.
700 BlockEdge Loc(Entry, SuccB, LocCtx);
701
702 bool isNew;
703 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
704 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
705
706 if (isNew)
707 Eng.WList->Enqueue(Node);
708}
709
710void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
711 // Get the callee's location context.
712 const StackFrameContext *LocCtx
713 = cast<StackFrameContext>(Pred->getLocationContext());
714
715 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
716 bool isNew;
717 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
718 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
719 if (isNew)
720 Eng.WList->Enqueue(Node, *const_cast<CFGBlock*>(LocCtx->getCallSiteBlock()),
721 LocCtx->getIndex() + 1);
722}