blob: cc8abc870afb3b18a8b8f2e47bad2aff94fe2939 [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
129bool GRCoreEngine::ProcessBlockEntrance(CFGBlock* Blk, const GRState* State,
Mike Stump11289f42009-09-09 15:08:12 +0000130 GRBlockCounter BC) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000131 return SubEngine.ProcessBlockEntrance(Blk, State, BC);
132}
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
Zhongxing Xu14863612010-02-25 06:46:30 +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
Zhongxing Xu14863612010-02-25 06:46:30 +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
Zhongxing Xu14863612010-02-25 06:46:30 +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
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000259 if (ProcessBlockEntrance(Blk, Pred->State, 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();
268 Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID());
269 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000270
271 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000272 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000273 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000274 SubEngine.getStateManager());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000275 ProcessStmt(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000276 }
Mike Stump11289f42009-09-09 15:08:12 +0000277 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000278 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000279}
280
Zhongxing Xu82003da2009-08-06 10:00:15 +0000281void GRCoreEngine::HandleBlockExit(CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000282
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000283 if (Stmt* Term = B->getTerminator()) {
284 switch (Term->getStmtClass()) {
285 default:
286 assert(false && "Analysis for this terminator not implemented.");
287 break;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek822f7372008-02-12 21:51:20 +0000289 case Stmt::BinaryOperatorClass: // '&&' and '||'
290 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
291 return;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000293 case Stmt::ConditionalOperatorClass:
294 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000295 return;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek822f7372008-02-12 21:51:20 +0000297 // FIXME: Use constant-folding in CFG construction to simplify this
298 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000300 case Stmt::ChooseExprClass:
301 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000302 return;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremenek822f7372008-02-12 21:51:20 +0000304 case Stmt::DoStmtClass:
305 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
306 return;
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek822f7372008-02-12 21:51:20 +0000308 case Stmt::ForStmtClass:
309 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
310 return;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenek632bcb82008-02-13 16:56:51 +0000312 case Stmt::ContinueStmtClass:
313 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000314 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000315 break;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000317 case Stmt::IfStmtClass:
318 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000319 return;
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenek7022efb2008-02-13 00:24:44 +0000321 case Stmt::IndirectGotoStmtClass: {
322 // Only 1 successor: the indirect goto dispatch block.
323 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000324
Zhongxing Xu107f7592009-08-06 12:48:26 +0000325 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000326 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
327 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek7022efb2008-02-13 00:24:44 +0000329 ProcessIndirectGoto(builder);
330 return;
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Ted Kremenek17810802008-11-12 19:24:17 +0000333 case Stmt::ObjCForCollectionStmtClass: {
334 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
335 //
336 // (1) inside a basic block, which represents the binding of the
337 // 'element' variable to a value.
338 // (2) in a terminator, which represents the branch.
339 //
340 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
341 // whether or not collection contains any more elements. We cannot
342 // just test to see if the element is nil because a container can
343 // contain nil elements.
344 HandleBranch(Term, Term, B, Pred);
345 return;
346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000348 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000349 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
350 this);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000352 ProcessSwitch(builder);
353 return;
354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000356 case Stmt::WhileStmtClass:
357 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000358 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000359 }
360 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000361
362 assert (B->succ_size() == 1 &&
363 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000364
365 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000366 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000367}
368
Zhongxing Xu82003da2009-08-06 10:00:15 +0000369void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
370 ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000371 assert (B->succ_size() == 2);
372
Zhongxing Xu107f7592009-08-06 12:48:26 +0000373 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
374 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000376 ProcessBranch(Cond, Term, Builder);
377}
378
Zhongxing Xu82003da2009-08-06 10:00:15 +0000379void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000380 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenek3e743662008-01-14 23:24:37 +0000382 assert (!B->empty());
383
Ted Kremeneke5843592008-01-15 00:24:08 +0000384 if (StmtIdx == B->size())
385 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000386 else {
Mike Stump11289f42009-09-09 15:08:12 +0000387 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000388 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000389 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000390 }
391}
392
Ted Kremenek3e743662008-01-14 23:24:37 +0000393/// GenerateNode - Utility method to generate nodes, hook up successors,
394/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000395void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000396 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek3e743662008-01-14 23:24:37 +0000398 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000399 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000400
401 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000402 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000403 else {
404 assert (IsNew);
405 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenek3e743662008-01-14 23:24:37 +0000408 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000409 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000410}
411
Zhongxing Xu107f7592009-08-06 12:48:26 +0000412GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
413 ExplodedNode* N, GRCoreEngine* e,
414 GRStateManager &mgr)
Mike Stump11289f42009-09-09 15:08:12 +0000415 : Eng(*e), B(*b), Idx(idx), Pred(N), LastNode(N), Mgr(mgr), Auditor(0),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000416 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
417 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000418 Deferred.insert(N);
Zhongxing Xu107f7592009-08-06 12:48:26 +0000419 CleanedState = getLastNode()->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000420}
421
Zhongxing Xu107f7592009-08-06 12:48:26 +0000422GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000423 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000424 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000425 GenerateAutoTransition(*I);
426}
427
Zhongxing Xu107f7592009-08-06 12:48:26 +0000428void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000429 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000430
Zhongxing Xu14863612010-02-25 06:46:30 +0000431 // Check if this node entered a callee.
432 if (isa<CallEnter>(N->getLocation())) {
Zhongxing Xu2fa52b02010-02-25 07:03:08 +0000433 // Still use the index of the CallExpr. It's needed to create the callee
434 // StackFrameContext.
435 Eng.WList->Enqueue(N, B, Idx);
Zhongxing Xu14863612010-02-25 06:46:30 +0000436 return;
437 }
438
Zhongxing Xue1190f72009-08-15 03:17:38 +0000439 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenek3e743662008-01-14 23:24:37 +0000441 if (Loc == N->getLocation()) {
442 // Note: 'N' should be a fresh node because otherwise it shouldn't be
443 // a member of Deferred.
444 Eng.WList->Enqueue(N, B, Idx+1);
445 return;
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek3e743662008-01-14 23:24:37 +0000448 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000449 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000450 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000451
452 if (IsNew)
453 Eng.WList->Enqueue(Succ, B, Idx+1);
454}
455
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000456static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
457 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000458 switch (K) {
459 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000460 assert(false && "Unhandled ProgramPoint kind");
461 case ProgramPoint::PreStmtKind:
462 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000463 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000464 return PostStmt(S, LC, tag);
465 case ProgramPoint::PreLoadKind:
466 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000467 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000468 return PostLoad(S, LC, tag);
469 case ProgramPoint::PreStoreKind:
470 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000471 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000472 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000473 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000474 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000475 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000476 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000477 }
478}
479
Zhongxing Xu20227f72009-08-06 01:32:16 +0000480ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000481GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000482 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000483 ProgramPoint::Kind K,
484 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000485
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000486 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000487 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000488}
489
Zhongxing Xu20227f72009-08-06 01:32:16 +0000490ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000491GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000492 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000493 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000494 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000495 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000496 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000497 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek3e743662008-01-14 23:24:37 +0000499 if (IsNew) {
500 Deferred.insert(N);
501 LastNode = N;
502 return N;
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek3e743662008-01-14 23:24:37 +0000505 LastNode = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000506 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000507}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000508
Zhongxing Xu107f7592009-08-06 12:48:26 +0000509ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
510 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000511
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000512 // If the branch has been marked infeasible we should not generate a node.
513 if (!isFeasible(branch))
514 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000515
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000516 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Zhongxing Xu20227f72009-08-06 01:32:16 +0000518 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000519 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
520 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000521
Ted Kremenekc3661de2009-10-07 00:42:52 +0000522 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000524 if (branch)
525 GeneratedTrue = true;
526 else
Mike Stump11289f42009-09-09 15:08:12 +0000527 GeneratedFalse = true;
528
Ted Kremeneka50d9852008-01-30 23:03:39 +0000529 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000530 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000531 return Succ;
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Ted Kremeneka50d9852008-01-30 23:03:39 +0000534 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000535}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000536
Zhongxing Xu107f7592009-08-06 12:48:26 +0000537GRBranchNodeBuilder::~GRBranchNodeBuilder() {
538 if (!GeneratedTrue) generateNode(Pred->State, true);
539 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek2531fce2008-01-30 23:24:39 +0000541 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000542 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000543}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000544
Ted Kremenek7022efb2008-02-13 00:24:44 +0000545
Zhongxing Xu20227f72009-08-06 01:32:16 +0000546ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000547GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
548 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000549 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000550
551 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000552 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremenekc3661de2009-10-07 00:42:52 +0000554 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremenek7022efb2008-02-13 00:24:44 +0000556 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000557
Ted Kremenek7022efb2008-02-13 00:24:44 +0000558 if (isSink)
559 Succ->markAsSink();
560 else
561 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenek7022efb2008-02-13 00:24:44 +0000563 return Succ;
564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Ted Kremenek7022efb2008-02-13 00:24:44 +0000566 return NULL;
567}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000568
569
Zhongxing Xu20227f72009-08-06 01:32:16 +0000570ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000571GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000572
573 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Zhongxing Xue1190f72009-08-15 03:17:38 +0000575 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
576 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000577 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000579 if (IsNew) {
580 Eng.WList->Enqueue(Succ);
581 return Succ;
582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000584 return NULL;
585}
586
587
Zhongxing Xu20227f72009-08-06 01:32:16 +0000588ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000589GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000590
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000591 // Get the block for the default case.
592 assert (Src->succ_rbegin() != Src->succ_rend());
593 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000594
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000595 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, DefaultBlock,
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 if (isSink)
603 Succ->markAsSink();
604 else
605 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000607 return Succ;
608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000610 return NULL;
611}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000612
Zhongxing Xu107f7592009-08-06 12:48:26 +0000613GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000614 // Auto-generate an EOP node if one has not been generated.
Zhongxing Xuee29cc32010-02-25 07:57:35 +0000615 if (!HasGeneratedNode) {
616 // If we are in an inlined call, generate CallExit node.
617 if (Pred->getLocationContext()->getParent())
618 GenerateCallExitNode(Pred->State);
619 else
620 generateNode(Pred->State);
621 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000622}
623
Zhongxing Xu20227f72009-08-06 01:32:16 +0000624ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000625GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
626 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000627 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000628 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000629
630 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000631 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenekc3661de2009-10-07 00:42:52 +0000633 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000634
Ted Kremenek811c2b42008-04-11 22:03:04 +0000635 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000636 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000637 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Ted Kremenekd004c412008-04-18 16:30:14 +0000640 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000641}
Zhongxing Xu14863612010-02-25 06:46:30 +0000642
643void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
644 HasGeneratedNode = true;
645 // Create a CallExit node and enqueue it.
646 const StackFrameContext *LocCtx
647 = cast<StackFrameContext>(Pred->getLocationContext());
648 const Stmt *CE = LocCtx->getCallSite();
649
650 // Use the the callee location context.
651 CallExit Loc(CE, LocCtx);
652
653 bool isNew;
654 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
655 Node->addPredecessor(Pred, *Eng.G);
656
657 if (isNew)
658 Eng.WList->Enqueue(Node);
659}
660
661
662void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
663 const LocationContext *LocCtx) {
664 // Get the callee entry block.
665 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
666 assert(Entry->empty());
667 assert(Entry->succ_size() == 1);
668
669 // Get the solitary successor.
670 const CFGBlock *SuccB = *(Entry->succ_begin());
671
672 // Construct an edge representing the starting location in the callee.
673 BlockEdge Loc(Entry, SuccB, LocCtx);
674
675 bool isNew;
676 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
677 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
678
679 if (isNew)
680 Eng.WList->Enqueue(Node);
681}
682
Zhongxing Xu9516fea2010-02-25 07:36:34 +0000683void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
Zhongxing Xu14863612010-02-25 06:46:30 +0000684 // Get the callee's location context.
685 const StackFrameContext *LocCtx
686 = cast<StackFrameContext>(Pred->getLocationContext());
687
688 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
689 bool isNew;
Zhongxing Xu9516fea2010-02-25 07:36:34 +0000690 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
Zhongxing Xu14863612010-02-25 06:46:30 +0000691 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
692 if (isNew)
693 Eng.WList->Enqueue(Node, *const_cast<CFGBlock*>(LocCtx->getCallSiteBlock()),
694 LocCtx->getIndex() + 1);
695}