blob: a156cdb261ee9777cb3c7d366d3af0afc2e86e17 [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Constant Propagation via Graph Reachability
11//
12// This files defines a simple analysis that performs path-sensitive
13// constant propagation within a function. An example use of this analysis
14// is to perform simple checks for NULL dereferences.
15//
16//===----------------------------------------------------------------------===//
17
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000018#include "RValues.h"
19#include "ValueState.h"
20
Ted Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/Analysis/PathSensitive/GREngine.h"
22#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000023#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000024#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000025
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/DataTypes.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000031#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000032#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000034#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000035#include "llvm/Support/Streams.h"
36
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000037#include <functional>
38
Ted Kremenekaa66a322008-01-16 21:46:15 +000039#ifndef NDEBUG
40#include "llvm/Support/GraphWriter.h"
41#include <sstream>
42#endif
43
Ted Kremenekd27f8162008-01-15 23:55:06 +000044using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000045using llvm::dyn_cast;
46using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000047using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000048
49//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000050// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000051//
52// FIXME: This checker logic should be eventually broken into two components.
53// The first is the "meta"-level checking logic; the code that
54// does the Stmt visitation, fetching values from the map, etc.
55// The second part does the actual state manipulation. This way we
56// get more of a separate of concerns of these two pieces, with the
57// latter potentially being refactored back into the main checking
58// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000059//===----------------------------------------------------------------------===//
60
61namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000062
Ted Kremenekab2b8c52008-01-23 19:59:44 +000063class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +000064
65public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000066 typedef ValueStateManager::StateTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +000067 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
68 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000069 typedef ExplodedGraph<GRConstants> GraphTy;
70 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000071
72 class NodeSet {
73 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
74 ImplTy Impl;
75 public:
76
77 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000078 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000079
Ted Kremenekb38911f2008-01-30 23:03:39 +000080 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000081
82 typedef ImplTy::iterator iterator;
83 typedef ImplTy::const_iterator const_iterator;
84
85 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000086 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000087
88 iterator begin() { return Impl.begin(); }
89 iterator end() { return Impl.end(); }
90
91 const_iterator begin() const { return Impl.begin(); }
92 const_iterator end() const { return Impl.end(); }
93 };
Ted Kremenekd27f8162008-01-15 23:55:06 +000094
95protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000096 /// G - the simulation graph.
97 GraphTy& G;
98
Ted Kremenekab2b8c52008-01-23 19:59:44 +000099 /// Liveness - live-variables information the ValueDecl* and block-level
100 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000101 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000102
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000103 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000104 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000105 StmtNodeBuilder* Builder;
106
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000107 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000108 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000109
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000110 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000111 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000112
Ted Kremenek68fd2572008-01-29 17:27:31 +0000113 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000114 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000115
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000116 /// StmtEntryNode - The immediate predecessor node.
117 NodeTy* StmtEntryNode;
118
119 /// CurrentStmt - The current block-level statement.
120 Stmt* CurrentStmt;
121
Ted Kremenekb38911f2008-01-30 23:03:39 +0000122 /// UninitBranches - Nodes in the ExplodedGraph that result from
123 /// taking a branch based on an uninitialized value.
124 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
125 UninitBranchesTy UninitBranches;
126
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000127 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000128
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000129 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000130
Ted Kremenekd27f8162008-01-15 23:55:06 +0000131public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000132 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000133 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000134 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000135 ValMgr(StateMgr.getValueManager()),
136 SymMgr(StateMgr.getSymbolManager()),
137 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000139 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000140 Liveness.runOnCFG(G.getCFG());
141 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000142 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000143
144 /// getCFG - Returns the CFG associated with this analysis.
145 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000147 /// getInitialState - Return the initial state used for the root vertex
148 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000149 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000150 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000151
152 // Iterate the parameters.
153 FunctionDecl& F = G.getFunctionDecl();
154
155 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000156 I!=E; ++I)
157 St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000158
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000159 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000160 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000161
162 bool isUninitControlFlow(const NodeTy* N) const {
163 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
164 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000165
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000166 /// ProcessStmt - Called by GREngine. Used to generate new successor
167 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000168 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
169
170 /// ProcessBranch - Called by GREngine. Used to generate successor
171 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000172 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000173
174 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
175 /// that all subexpression mappings are removed and that any
176 /// block-level expressions that are not live at 'S' also have their
177 /// mappings removed.
178 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
179
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000180 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000181
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000182 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000183 return SetValue(St, const_cast<Stmt*>(S), V);
184 }
185
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000186 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000187
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000188 inline RValue GetValue(const StateTy& St, Stmt* S) {
189 return StateMgr.GetValue(St, S);
190 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000191
192 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
193 return StateMgr.GetValue(St, S, &hasVal);
194 }
195
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000196 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000197 return GetValue(St, const_cast<Stmt*>(S));
198 }
199
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000200 inline RValue GetValue(const StateTy& St, const LValue& LV) {
201 return StateMgr.GetValue(St, LV);
202 }
203
204 inline LValue GetLValue(const StateTy& St, Stmt* S) {
205 return StateMgr.GetLValue(St, S);
206 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000207
208 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
209 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
210 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000211
212 /// Assume - Create new state by assuming that a given expression
213 /// is true or false.
214 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
215 bool& isFeasible) {
216 if (isa<LValue>(Cond))
217 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
218 else
219 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
220 }
221
222 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
223 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000224
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000225 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000226
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000227 /// Visit - Transfer function logic for all statements. Dispatches to
228 /// other functions that handle specific kinds of statements.
229 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000230
231 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
232 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000233
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000234 /// VisitUnaryOperator - Transfer function logic for unary operators.
235 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
236
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000237 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000238 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
239
240 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000241 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
242
243 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
244 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
245 NodeTy* Pred, NodeSet& Dst);
246
247 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
248 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000249};
250} // end anonymous namespace
251
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000252
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000253GRConstants::StateTy
254GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
255
256 if (!StateCleaned) {
257 St = RemoveDeadBindings(CurrentStmt, St);
258 StateCleaned = true;
259 }
260
261 bool isBlkExpr = false;
262
263 if (S == CurrentStmt) {
264 isBlkExpr = getCFG().isBlkExpr(S);
265
266 if (!isBlkExpr)
267 return St;
268 }
269
270 return StateMgr.SetValue(St, S, isBlkExpr, V);
271}
272
273GRConstants::StateTy
274GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
275
276 if (!LV.isValid())
277 return St;
278
279 if (!StateCleaned) {
280 St = RemoveDeadBindings(CurrentStmt, St);
281 StateCleaned = true;
282 }
283
284 return StateMgr.SetValue(St, LV, V);
285}
286
Ted Kremenekf233d482008-02-05 00:26:40 +0000287void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000288 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000289
290 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000291
Ted Kremenekb38911f2008-01-30 23:03:39 +0000292 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000293 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000294 if (I.getKey().isSubExpr())
295 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000296
Ted Kremenekf233d482008-02-05 00:26:40 +0000297 // Remove terminator-specific bindings.
298 switch (Term->getStmtClass()) {
299 default: break;
300
301 case Stmt::BinaryOperatorClass: { // '&&', '||'
302 BinaryOperator* B = cast<BinaryOperator>(Term);
303 // FIXME: Liveness analysis should probably remove these automatically.
304 // Verify later when we converge to an 'optimization' stage.
305 PrevState = StateMgr.Remove(PrevState, B->getRHS());
306 break;
307 }
308
309 case Stmt::ConditionalOperatorClass: { // '?' operator
310 ConditionalOperator* C = cast<ConditionalOperator>(Term);
311 // FIXME: Liveness analysis should probably remove these automatically.
312 // Verify later when we converge to an 'optimization' stage.
313 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
314 PrevState = StateMgr.Remove(PrevState, C->getRHS());
315 break;
316 }
317
318 case Stmt::ChooseExprClass: { // __builtin_choose_expr
319 ChooseExpr* C = cast<ChooseExpr>(Term);
320 // FIXME: Liveness analysis should probably remove these automatically.
321 // Verify later when we converge to an 'optimization' stage.
322 PrevState = StateMgr.Remove(PrevState, C->getRHS());
323 PrevState = StateMgr.Remove(PrevState, C->getRHS());
324 break;
325 }
326 }
327
Ted Kremenekb38911f2008-01-30 23:03:39 +0000328 RValue V = GetValue(PrevState, Condition);
329
330 switch (V.getBaseKind()) {
331 default:
332 break;
333
334 case RValue::InvalidKind:
335 builder.generateNode(PrevState, true);
336 builder.generateNode(PrevState, false);
337 return;
338
339 case RValue::UninitializedKind: {
340 NodeTy* N = builder.generateNode(PrevState, true);
341
342 if (N) {
343 N->markAsSink();
344 UninitBranches.insert(N);
345 }
346
347 builder.markInfeasible(false);
348 return;
349 }
350 }
351
352 // Process the true branch.
353 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000354
Ted Kremenekb38911f2008-01-30 23:03:39 +0000355 StateTy St = Assume(PrevState, V, true, isFeasible);
356
Ted Kremenekf233d482008-02-05 00:26:40 +0000357 if (isFeasible)
358 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000359 else {
360 builder.markInfeasible(true);
361 isFeasible = true;
362 }
363
364 // Process the false branch.
365 St = Assume(PrevState, V, false, isFeasible);
366
Ted Kremenekf233d482008-02-05 00:26:40 +0000367 if (isFeasible)
368 builder.generateNode(St, false);
369 else
370 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000371}
372
Ted Kremenekf233d482008-02-05 00:26:40 +0000373
374void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
375 NodeSet& Dst) {
376
377 bool hasR2;
378 StateTy PrevState = Pred->getState();
379
380 RValue R1 = GetValue(PrevState, B->getLHS());
381 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
382
383 if (isa<InvalidValue>(R1) &&
384 (isa<InvalidValue>(R2) ||
385 isa<UninitializedValue>(R2))) {
386
387 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
388 return;
389 }
390 else if (isa<UninitializedValue>(R1)) {
391 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
392 return;
393 }
394
395 // R1 is an expression that can evaluate to either 'true' or 'false'.
396 if (B->getOpcode() == BinaryOperator::LAnd) {
397 // hasR2 == 'false' means that LHS evaluated to 'false' and that
398 // we short-circuited, leading to a value of '0' for the '&&' expression.
399 if (hasR2 == false) {
400 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
401 return;
402 }
403 }
404 else {
405 assert (B->getOpcode() == BinaryOperator::LOr);
406 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
407 // we short-circuited, leading to a value of '1' for the '||' expression.
408 if (hasR2 == false) {
409 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
410 return;
411 }
412 }
413
414 // If we reach here we did not short-circuit. Assume R2 == true and
415 // R2 == false.
416
417 bool isFeasible;
418 StateTy St = Assume(PrevState, R2, true, isFeasible);
419
420 if (isFeasible)
421 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
422
423 St = Assume(PrevState, R2, false, isFeasible);
424
425 if (isFeasible)
426 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
427}
428
429
430
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000431void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000432 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000433
434 StmtEntryNode = builder.getLastNode();
435 CurrentStmt = S;
436 NodeSet Dst;
437 StateCleaned = false;
438
439 Visit(S, StmtEntryNode, Dst);
440
441 // If no nodes were generated, generate a new node that has all the
442 // dead mappings removed.
443 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
444 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
445 builder.generateNode(S, St, StmtEntryNode);
446 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000447
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000448 CurrentStmt = NULL;
449 StmtEntryNode = NULL;
450 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000451}
452
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000453GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000454 // Note: in the code below, we can assign a new map to M since the
455 // iterators are iterating over the tree of the *original* map.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000456 StateTy::vb_iterator I = M.begin(), E = M.end();
Ted Kremenekf84469b2008-01-18 00:41:32 +0000457
Ted Kremenekf84469b2008-01-18 00:41:32 +0000458
Ted Kremenek65cac132008-01-29 05:25:31 +0000459 for (; I!=E && !I.getKey().isSymbol(); ++I) {
460 // Remove old bindings for subexpressions and "dead"
461 // block-level expressions.
462 if (I.getKey().isSubExpr() ||
463 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
464 M = StateMgr.Remove(M, I.getKey());
465 }
466 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
467 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
468 if (!Liveness.isLive(Loc, V))
469 M = StateMgr.Remove(M, I.getKey());
470 }
471 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000472
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000473 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000474}
475
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000476void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
477 GRConstants::StateTy St) {
478
479 // If the state hasn't changed, don't generate a new node.
480 if (St == Pred->getState())
481 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000482
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000483 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000484}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000485
Ted Kremenek874d63f2008-01-24 02:02:54 +0000486void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
487 GRConstants::NodeSet& Dst) {
488
489 QualType T = CastE->getType();
490
491 // Check for redundant casts.
492 if (E->getType() == T) {
493 Dst.Add(Pred);
494 return;
495 }
496
497 NodeSet S1;
498 Visit(E, Pred, S1);
499
500 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
501 NodeTy* N = *I1;
502 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000503 const RValue& V = GetValue(St, E);
504 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000505 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000506}
507
508void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
509 GRConstants::NodeSet& Dst) {
510
511 StateTy St = Pred->getState();
512
513 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000514 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
515 const Expr* E = VD->getInit();
516 St = SetValue(St, LValueDecl(VD),
517 E ? GetValue(St, E) : UninitializedValue());
518 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000519
520 Nodify(Dst, DS, Pred, St);
521
522 if (Dst.empty())
523 Dst.Add(Pred);
524}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000525
Ted Kremenekf233d482008-02-05 00:26:40 +0000526
527void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
528 NodeTy* Pred, NodeSet& Dst) {
529
530 StateTy St = Pred->getState();
531
532 RValue R = GetValue(St, LHS);
533 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
534
535 Nodify(Dst, S, Pred, SetValue(St, S, R));
536}
537
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000538void GRConstants::VisitUnaryOperator(UnaryOperator* U,
539 GRConstants::NodeTy* Pred,
540 GRConstants::NodeSet& Dst) {
541 NodeSet S1;
542 Visit(U->getSubExpr(), Pred, S1);
543
544 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
545 NodeTy* N1 = *I1;
546 StateTy St = N1->getState();
547
548 switch (U->getOpcode()) {
549 case UnaryOperator::PostInc: {
550 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000551 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000552 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000553 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
554 break;
555 }
556
557 case UnaryOperator::PostDec: {
558 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000559 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000560 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000561 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
562 break;
563 }
564
565 case UnaryOperator::PreInc: {
566 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000567 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000568 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000569 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
570 break;
571 }
572
573 case UnaryOperator::PreDec: {
574 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000575 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000576 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000577 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
578 break;
579 }
580
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000581 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000582 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
583 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000584 break;
585 }
586
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000587 case UnaryOperator::Not: {
588 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
589 Nodify(Dst, U, N1, SetValue(St, U, R1.BitwiseComplement(ValMgr)));
590 break;
591 }
592
Ted Kremenek64924852008-01-31 02:35:41 +0000593 case UnaryOperator::AddrOf: {
594 const LValue& L1 = GetLValue(St, U->getSubExpr());
595 Nodify(Dst, U, N1, SetValue(St, U, L1));
596 break;
597 }
598
599 case UnaryOperator::Deref: {
600 const LValue& L1 = GetLValue(St, U->getSubExpr());
601 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
602 break;
603 }
604
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000605 default: ;
606 assert (false && "Not implemented.");
607 }
608 }
609}
610
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000611void GRConstants::VisitBinaryOperator(BinaryOperator* B,
612 GRConstants::NodeTy* Pred,
613 GRConstants::NodeSet& Dst) {
614 NodeSet S1;
615 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000616
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000617 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
618 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000619
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000620 // When getting the value for the LHS, check if we are in an assignment.
621 // In such cases, we want to (initially) treat the LHS as an LValue,
622 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000623 // evaluated to LValueDecl's instead of to an NonLValue.
624 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000625 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
626 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000627
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000628 NodeSet S2;
629 Visit(B->getRHS(), N1, S2);
630
631 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
632 NodeTy* N2 = *I2;
633 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000634 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000635
636 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +0000637 default:
638 Dst.Add(N2);
639 break;
640
Ted Kremenekf233d482008-02-05 00:26:40 +0000641 // Arithmetic operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000642
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000643 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000644 const NonLValue& R1 = cast<NonLValue>(V1);
645 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000646
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000647 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000648 break;
649 }
650
651 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000652 const NonLValue& R1 = cast<NonLValue>(V1);
653 const NonLValue& R2 = cast<NonLValue>(V2);
654 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000655 break;
656 }
657
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000658 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000659 const NonLValue& R1 = cast<NonLValue>(V1);
660 const NonLValue& R2 = cast<NonLValue>(V2);
661 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000662 break;
663 }
664
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000665 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000666 const NonLValue& R1 = cast<NonLValue>(V1);
667 const NonLValue& R2 = cast<NonLValue>(V2);
668 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000669 break;
670 }
671
Ted Kremenekcce207d2008-01-28 22:26:15 +0000672 case BinaryOperator::Rem: {
673 const NonLValue& R1 = cast<NonLValue>(V1);
674 const NonLValue& R2 = cast<NonLValue>(V2);
675 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
676 break;
677 }
678
Ted Kremenek687af802008-01-29 19:43:15 +0000679 // Assignment operators.
680
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000681 case BinaryOperator::Assign: {
682 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000683 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000684 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
685 break;
686 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000687
688 case BinaryOperator::AddAssign: {
689 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000690 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
691 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000692 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
693 break;
694 }
695
696 case BinaryOperator::SubAssign: {
697 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000698 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
699 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000700 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
701 break;
702 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000703
704 case BinaryOperator::MulAssign: {
705 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000706 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
707 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000708 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
709 break;
710 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000711
712 case BinaryOperator::DivAssign: {
713 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000714 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
715 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000716 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
717 break;
718 }
Ted Kremenek10099a62008-01-28 22:28:54 +0000719
720 case BinaryOperator::RemAssign: {
721 const LValue& L1 = cast<LValue>(V1);
722 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
723 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
724 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
725 break;
726 }
Ted Kremenek687af802008-01-29 19:43:15 +0000727
728 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000729
Ted Kremenek687af802008-01-29 19:43:15 +0000730 case BinaryOperator::EQ:
731 // FIXME: should we allow XX.EQ() to return a set of values,
732 // allowing state bifurcation? In such cases, they will also
733 // modify the state (meaning that a new state will be returned
734 // as well).
735 assert (B->getType() == getContext().IntTy);
736
737 if (isa<LValue>(V1)) {
738 const LValue& L1 = cast<LValue>(V1);
739 const LValue& L2 = cast<LValue>(V2);
740 St = SetValue(St, B, L1.EQ(ValMgr, L2));
741 }
742 else {
743 const NonLValue& R1 = cast<NonLValue>(V1);
744 const NonLValue& R2 = cast<NonLValue>(V2);
745 St = SetValue(St, B, R1.EQ(ValMgr, R2));
746 }
747
748 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000749 break;
750 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000751 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000752 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000753}
Ted Kremenekee985462008-01-16 18:18:48 +0000754
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000755
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000756void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
757 GRConstants::NodeSet& Dst) {
758
759 // FIXME: add metadata to the CFG so that we can disable
760 // this check when we KNOW that there is no block-level subexpression.
761 // The motivation is that this check requires a hashtable lookup.
762
763 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
764 Dst.Add(Pred);
765 return;
766 }
767
768 switch (S->getStmtClass()) {
769 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000770
771 if (cast<BinaryOperator>(S)->isLogicalOp()) {
772 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
773 break;
774 }
775
776 // Fall-through.
777
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000778 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000779 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
780 break;
781
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000782 case Stmt::UnaryOperatorClass:
783 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
784 break;
785
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000786 case Stmt::ParenExprClass:
787 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
788 break;
789
Ted Kremenek874d63f2008-01-24 02:02:54 +0000790 case Stmt::ImplicitCastExprClass: {
791 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
792 VisitCast(C, C->getSubExpr(), Pred, Dst);
793 break;
794 }
795
796 case Stmt::CastExprClass: {
797 CastExpr* C = cast<CastExpr>(S);
798 VisitCast(C, C->getSubExpr(), Pred, Dst);
799 break;
800 }
801
Ted Kremenekf233d482008-02-05 00:26:40 +0000802 case Stmt::ConditionalOperatorClass: { // '?' operator
803 ConditionalOperator* C = cast<ConditionalOperator>(S);
804 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
805 break;
806 }
807
808 case Stmt::ChooseExprClass: { // __builtin_choose_expr
809 ChooseExpr* C = cast<ChooseExpr>(S);
810 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
811 break;
812 }
813
Ted Kremenek9de04c42008-01-24 20:55:43 +0000814 case Stmt::DeclStmtClass:
815 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
816 break;
817
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 default:
819 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
820 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000821 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000822}
823
Ted Kremenekee985462008-01-16 18:18:48 +0000824//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000825// "Assume" logic.
826//===----------------------------------------------------------------------===//
827
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000828GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
829 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000830
831 switch (Cond.getSubKind()) {
832 default:
833 assert (false && "'Assume' not implemented for this NonLValue.");
834 return St;
835
836 case LValueDeclKind:
837 isFeasible = Assumption;
838 return St;
839
840 case ConcreteIntLValueKind: {
841 bool b = cast<ConcreteIntLValue>(Cond).getValue() != 0;
842 isFeasible = b ? Assumption : !Assumption;
843 return St;
844 }
845 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000846}
847
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000848GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
849 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000850
851 switch (Cond.getSubKind()) {
852 default:
853 assert (false && "'Assume' not implemented for this NonLValue.");
854 return St;
855
856 case ConcreteIntKind: {
857 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
858 isFeasible = b ? Assumption : !Assumption;
859 return St;
860 }
861 }
862}
863
864
865//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000866// Driver.
867//===----------------------------------------------------------------------===//
868
Ted Kremenekaa66a322008-01-16 21:46:15 +0000869#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000870static GRConstants* GraphPrintCheckerState;
871
Ted Kremenekaa66a322008-01-16 21:46:15 +0000872namespace llvm {
873template<>
874struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
875 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000876
Ted Kremenek9153f732008-02-05 07:17:49 +0000877 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000878 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000879 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
880 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
881 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
882 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000883 }
884 }
885
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000886 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +0000887 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000888 bool isFirst = true;
889
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000890 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000891 if (I.getKey().getKind() != kind)
892 continue;
893
894 if (isFirst) {
895 if (!isFirstGroup) Out << "\\l\\l";
896 PrintKindLabel(Out, kind);
897 isFirst = false;
898 }
899 else
900 Out << "\\l";
901
902 Out << ' ';
903
904 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
905 Out << V->getName();
906 else {
907 Stmt* E = cast<Stmt>(I.getKey());
908 Out << " (" << (void*) E << ") ";
909 E->printPretty(Out);
910 }
911
912 Out << " : ";
913 I.getData().print(Out);
914 }
915 }
916
Ted Kremenekaa66a322008-01-16 21:46:15 +0000917 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
918 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000919
920 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +0000921 ProgramPoint Loc = N->getLocation();
922
923 switch (Loc.getKind()) {
924 case ProgramPoint::BlockEntranceKind:
925 Out << "Block Entrance: B"
926 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
927 break;
928
929 case ProgramPoint::BlockExitKind:
930 assert (false);
931 break;
932
933 case ProgramPoint::PostStmtKind: {
934 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000935 Out << L.getStmt()->getStmtClassName() << ':'
936 << (void*) L.getStmt() << ' ';
937
Ted Kremenekaa66a322008-01-16 21:46:15 +0000938 L.getStmt()->printPretty(Out);
939 break;
940 }
941
942 default: {
943 const BlockEdge& E = cast<BlockEdge>(Loc);
944 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
945 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +0000946
947 if (Stmt* T = E.getSrc()->getTerminator()) {
948 Out << "\\|Terminator: ";
949 E.getSrc()->printTerminator(Out);
950
951 if (isa<SwitchStmt>(T)) {
952 // FIXME
953 }
954 else {
955 Out << "\\lCondition: ";
956 if (*E.getSrc()->succ_begin() == E.getDst())
957 Out << "true";
958 else
959 Out << "false";
960 }
961
962 Out << "\\l";
963 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000964
965 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
966 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
967 }
Ted Kremenekaa66a322008-01-16 21:46:15 +0000968 }
969 }
970
Ted Kremenek9153f732008-02-05 07:17:49 +0000971 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +0000972
Ted Kremenek9153f732008-02-05 07:17:49 +0000973 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
974 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
975 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000976
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000977 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +0000978 return Out.str();
979 }
980};
981} // end llvm namespace
982#endif
983
Ted Kremenekee985462008-01-16 18:18:48 +0000984namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000985void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
986 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +0000987 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +0000988#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000989 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +0000990 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000991 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +0000992#endif
Ted Kremenekee985462008-01-16 18:18:48 +0000993}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000994} // end clang namespace