blob: 39a50a55ce2d27e3ffd41172140c362ca97390a9 [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 Kremenekcba2e432008-02-05 19:35:18 +000094
Ted Kremenekd27f8162008-01-15 23:55:06 +000095protected:
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)
Ted Kremenek329f8542008-02-05 21:52:21 +0000157 St = SetValue(St, lval::DeclVal(*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 Kremenekcba2e432008-02-05 19:35:18 +0000186 /// SetValue - This version of SetValue is used to batch process a set
187 /// of different possible RValues and return a set of different states.
188 const StateTy::BufferTy& SetValue(StateTy St, Stmt* S,
189 const RValue::BufferTy& V,
190 StateTy::BufferTy& RetBuf);
191
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000192 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000193
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194 inline RValue GetValue(const StateTy& St, Stmt* S) {
195 return StateMgr.GetValue(St, S);
196 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000197
198 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
199 return StateMgr.GetValue(St, S, &hasVal);
200 }
201
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000202 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000203 return GetValue(St, const_cast<Stmt*>(S));
204 }
205
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000206 inline RValue GetValue(const StateTy& St, const LValue& LV) {
207 return StateMgr.GetValue(St, LV);
208 }
209
210 inline LValue GetLValue(const StateTy& St, Stmt* S) {
211 return StateMgr.GetLValue(St, S);
212 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000213
214 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
215 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
216 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000217
218 /// Assume - Create new state by assuming that a given expression
219 /// is true or false.
220 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
221 bool& isFeasible) {
222 if (isa<LValue>(Cond))
223 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
224 else
225 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
226 }
227
228 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
229 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000230
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000231 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
232 bool& isFeasible);
233
234 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
235 bool& isFeasible);
236
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000237 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000238
Ted Kremenekcba2e432008-02-05 19:35:18 +0000239 /// Nodify - This version of Nodify is used to batch process a set of states.
240 /// The states are not guaranteed to be unique.
241 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
242
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000243 /// Visit - Transfer function logic for all statements. Dispatches to
244 /// other functions that handle specific kinds of statements.
245 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000246
247 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
248 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000249
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000250 /// VisitUnaryOperator - Transfer function logic for unary operators.
251 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
252
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000253 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000254 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
255
256 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000257 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
258
259 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
260 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
261 NodeTy* Pred, NodeSet& Dst);
262
263 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
264 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000265};
266} // end anonymous namespace
267
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000268
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000269GRConstants::StateTy
270GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
271
272 if (!StateCleaned) {
273 St = RemoveDeadBindings(CurrentStmt, St);
274 StateCleaned = true;
275 }
276
277 bool isBlkExpr = false;
278
279 if (S == CurrentStmt) {
280 isBlkExpr = getCFG().isBlkExpr(S);
281
282 if (!isBlkExpr)
283 return St;
284 }
285
286 return StateMgr.SetValue(St, S, isBlkExpr, V);
287}
288
Ted Kremenekcba2e432008-02-05 19:35:18 +0000289const GRConstants::StateTy::BufferTy&
290GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
291 StateTy::BufferTy& RetBuf) {
292
293 assert (RetBuf.empty());
294
295 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
296 RetBuf.push_back(SetValue(St, S, *I));
297
298 return RetBuf;
299}
300
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000301GRConstants::StateTy
302GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
303
304 if (!LV.isValid())
305 return St;
306
307 if (!StateCleaned) {
308 St = RemoveDeadBindings(CurrentStmt, St);
309 StateCleaned = true;
310 }
311
312 return StateMgr.SetValue(St, LV, V);
313}
314
Ted Kremenekf233d482008-02-05 00:26:40 +0000315void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000316 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000317
318 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000319
Ted Kremenekb38911f2008-01-30 23:03:39 +0000320 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000321 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000322 if (I.getKey().isSubExpr())
323 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000324
Ted Kremenekf233d482008-02-05 00:26:40 +0000325 // Remove terminator-specific bindings.
326 switch (Term->getStmtClass()) {
327 default: break;
328
329 case Stmt::BinaryOperatorClass: { // '&&', '||'
330 BinaryOperator* B = cast<BinaryOperator>(Term);
331 // FIXME: Liveness analysis should probably remove these automatically.
332 // Verify later when we converge to an 'optimization' stage.
333 PrevState = StateMgr.Remove(PrevState, B->getRHS());
334 break;
335 }
336
337 case Stmt::ConditionalOperatorClass: { // '?' operator
338 ConditionalOperator* C = cast<ConditionalOperator>(Term);
339 // FIXME: Liveness analysis should probably remove these automatically.
340 // Verify later when we converge to an 'optimization' stage.
341 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
342 PrevState = StateMgr.Remove(PrevState, C->getRHS());
343 break;
344 }
345
346 case Stmt::ChooseExprClass: { // __builtin_choose_expr
347 ChooseExpr* C = cast<ChooseExpr>(Term);
348 // FIXME: Liveness analysis should probably remove these automatically.
349 // Verify later when we converge to an 'optimization' stage.
350 PrevState = StateMgr.Remove(PrevState, C->getRHS());
351 PrevState = StateMgr.Remove(PrevState, C->getRHS());
352 break;
353 }
354 }
355
Ted Kremenekb38911f2008-01-30 23:03:39 +0000356 RValue V = GetValue(PrevState, Condition);
357
358 switch (V.getBaseKind()) {
359 default:
360 break;
361
362 case RValue::InvalidKind:
363 builder.generateNode(PrevState, true);
364 builder.generateNode(PrevState, false);
365 return;
366
367 case RValue::UninitializedKind: {
368 NodeTy* N = builder.generateNode(PrevState, true);
369
370 if (N) {
371 N->markAsSink();
372 UninitBranches.insert(N);
373 }
374
375 builder.markInfeasible(false);
376 return;
377 }
378 }
379
380 // Process the true branch.
381 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000382
Ted Kremenekb38911f2008-01-30 23:03:39 +0000383 StateTy St = Assume(PrevState, V, true, isFeasible);
384
Ted Kremenekf233d482008-02-05 00:26:40 +0000385 if (isFeasible)
386 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000387 else {
388 builder.markInfeasible(true);
389 isFeasible = true;
390 }
391
392 // Process the false branch.
393 St = Assume(PrevState, V, false, isFeasible);
394
Ted Kremenekf233d482008-02-05 00:26:40 +0000395 if (isFeasible)
396 builder.generateNode(St, false);
397 else
398 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000399}
400
Ted Kremenekf233d482008-02-05 00:26:40 +0000401
402void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
403 NodeSet& Dst) {
404
405 bool hasR2;
406 StateTy PrevState = Pred->getState();
407
408 RValue R1 = GetValue(PrevState, B->getLHS());
409 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
410
411 if (isa<InvalidValue>(R1) &&
412 (isa<InvalidValue>(R2) ||
413 isa<UninitializedValue>(R2))) {
414
415 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
416 return;
417 }
418 else if (isa<UninitializedValue>(R1)) {
419 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
420 return;
421 }
422
423 // R1 is an expression that can evaluate to either 'true' or 'false'.
424 if (B->getOpcode() == BinaryOperator::LAnd) {
425 // hasR2 == 'false' means that LHS evaluated to 'false' and that
426 // we short-circuited, leading to a value of '0' for the '&&' expression.
427 if (hasR2 == false) {
428 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
429 return;
430 }
431 }
432 else {
433 assert (B->getOpcode() == BinaryOperator::LOr);
434 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
435 // we short-circuited, leading to a value of '1' for the '||' expression.
436 if (hasR2 == false) {
437 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
438 return;
439 }
440 }
441
442 // If we reach here we did not short-circuit. Assume R2 == true and
443 // R2 == false.
444
445 bool isFeasible;
446 StateTy St = Assume(PrevState, R2, true, isFeasible);
447
448 if (isFeasible)
449 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
450
451 St = Assume(PrevState, R2, false, isFeasible);
452
453 if (isFeasible)
454 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
455}
456
457
458
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000459void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000460 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000461
462 StmtEntryNode = builder.getLastNode();
463 CurrentStmt = S;
464 NodeSet Dst;
465 StateCleaned = false;
466
467 Visit(S, StmtEntryNode, Dst);
468
469 // If no nodes were generated, generate a new node that has all the
470 // dead mappings removed.
471 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
472 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
473 builder.generateNode(S, St, StmtEntryNode);
474 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000475
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000476 CurrentStmt = NULL;
477 StmtEntryNode = NULL;
478 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000479}
480
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000481GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000482 // Note: in the code below, we can assign a new map to M since the
483 // iterators are iterating over the tree of the *original* map.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000484 StateTy::vb_iterator I = M.begin(), E = M.end();
Ted Kremenekf84469b2008-01-18 00:41:32 +0000485
Ted Kremenekf84469b2008-01-18 00:41:32 +0000486
Ted Kremenek65cac132008-01-29 05:25:31 +0000487 for (; I!=E && !I.getKey().isSymbol(); ++I) {
488 // Remove old bindings for subexpressions and "dead"
489 // block-level expressions.
490 if (I.getKey().isSubExpr() ||
491 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
492 M = StateMgr.Remove(M, I.getKey());
493 }
494 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
495 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
496 if (!Liveness.isLive(Loc, V))
497 M = StateMgr.Remove(M, I.getKey());
498 }
499 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000500
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000501 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000502}
503
Ted Kremenekcba2e432008-02-05 19:35:18 +0000504void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000505
506 // If the state hasn't changed, don't generate a new node.
507 if (St == Pred->getState())
508 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000509
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000510 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000511}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000512
Ted Kremenekcba2e432008-02-05 19:35:18 +0000513void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
514 const StateTy::BufferTy& SB) {
515
516 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
517 Nodify(Dst, S, Pred, *I);
518}
519
520void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000521
522 QualType T = CastE->getType();
523
524 // Check for redundant casts.
525 if (E->getType() == T) {
526 Dst.Add(Pred);
527 return;
528 }
529
530 NodeSet S1;
531 Visit(E, Pred, S1);
532
533 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
534 NodeTy* N = *I1;
535 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000536 const RValue& V = GetValue(St, E);
537 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000538 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000539}
540
541void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
542 GRConstants::NodeSet& Dst) {
543
544 StateTy St = Pred->getState();
545
546 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000547 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
548 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000549 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek403c1812008-01-28 22:51:57 +0000550 E ? GetValue(St, E) : UninitializedValue());
551 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000552
553 Nodify(Dst, DS, Pred, St);
554
555 if (Dst.empty())
556 Dst.Add(Pred);
557}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000558
Ted Kremenekf233d482008-02-05 00:26:40 +0000559
560void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
561 NodeTy* Pred, NodeSet& Dst) {
562
563 StateTy St = Pred->getState();
564
565 RValue R = GetValue(St, LHS);
566 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
567
568 Nodify(Dst, S, Pred, SetValue(St, S, R));
569}
570
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000571void GRConstants::VisitUnaryOperator(UnaryOperator* U,
572 GRConstants::NodeTy* Pred,
573 GRConstants::NodeSet& Dst) {
574 NodeSet S1;
575 Visit(U->getSubExpr(), Pred, S1);
576
577 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
578 NodeTy* N1 = *I1;
579 StateTy St = N1->getState();
580
581 switch (U->getOpcode()) {
582 case UnaryOperator::PostInc: {
583 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000584 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000585 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000586 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
587 break;
588 }
589
590 case UnaryOperator::PostDec: {
591 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000592 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000593 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000594 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
595 break;
596 }
597
598 case UnaryOperator::PreInc: {
599 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000600 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000601 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000602 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
603 break;
604 }
605
606 case UnaryOperator::PreDec: {
607 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000608 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000609 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000610 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
611 break;
612 }
613
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000614 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000615 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
616 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000617 break;
618 }
619
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000620 case UnaryOperator::Not: {
621 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
622 Nodify(Dst, U, N1, SetValue(St, U, R1.BitwiseComplement(ValMgr)));
623 break;
624 }
625
Ted Kremenek64924852008-01-31 02:35:41 +0000626 case UnaryOperator::AddrOf: {
627 const LValue& L1 = GetLValue(St, U->getSubExpr());
628 Nodify(Dst, U, N1, SetValue(St, U, L1));
629 break;
630 }
631
632 case UnaryOperator::Deref: {
633 const LValue& L1 = GetLValue(St, U->getSubExpr());
634 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
635 break;
636 }
637
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000638 default: ;
639 assert (false && "Not implemented.");
640 }
641 }
642}
643
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000644void GRConstants::VisitBinaryOperator(BinaryOperator* B,
645 GRConstants::NodeTy* Pred,
646 GRConstants::NodeSet& Dst) {
647 NodeSet S1;
648 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000649
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000650 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
651 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000652
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000653 // When getting the value for the LHS, check if we are in an assignment.
654 // In such cases, we want to (initially) treat the LHS as an LValue,
655 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000656 // evaluated to LValueDecl's instead of to an NonLValue.
657 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000658 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
659 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000660
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000661 NodeSet S2;
662 Visit(B->getRHS(), N1, S2);
663
664 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
665 NodeTy* N2 = *I2;
666 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000667 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000668
669 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +0000670 default:
671 Dst.Add(N2);
672 break;
673
Ted Kremenekf233d482008-02-05 00:26:40 +0000674 // Arithmetic operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000675
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000676 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000677 const NonLValue& R1 = cast<NonLValue>(V1);
678 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000679
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000680 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000681 break;
682 }
683
684 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000685 const NonLValue& R1 = cast<NonLValue>(V1);
686 const NonLValue& R2 = cast<NonLValue>(V2);
687 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000688 break;
689 }
690
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000691 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000692 const NonLValue& R1 = cast<NonLValue>(V1);
693 const NonLValue& R2 = cast<NonLValue>(V2);
694 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000695 break;
696 }
697
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000698 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000699 const NonLValue& R1 = cast<NonLValue>(V1);
700 const NonLValue& R2 = cast<NonLValue>(V2);
701 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000702 break;
703 }
704
Ted Kremenekcce207d2008-01-28 22:26:15 +0000705 case BinaryOperator::Rem: {
706 const NonLValue& R1 = cast<NonLValue>(V1);
707 const NonLValue& R2 = cast<NonLValue>(V2);
708 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
709 break;
710 }
711
Ted Kremenek687af802008-01-29 19:43:15 +0000712 // Assignment operators.
713
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000714 case BinaryOperator::Assign: {
715 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000716 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000717 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
718 break;
719 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000720
721 case BinaryOperator::AddAssign: {
722 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000723 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
724 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000725 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
726 break;
727 }
728
729 case BinaryOperator::SubAssign: {
730 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000731 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
732 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000733 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
734 break;
735 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000736
737 case BinaryOperator::MulAssign: {
738 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000739 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
740 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000741 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
742 break;
743 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000744
745 case BinaryOperator::DivAssign: {
746 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000747 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
748 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000749 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
750 break;
751 }
Ted Kremenek10099a62008-01-28 22:28:54 +0000752
753 case BinaryOperator::RemAssign: {
754 const LValue& L1 = cast<LValue>(V1);
755 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
756 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
757 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
758 break;
759 }
Ted Kremenek687af802008-01-29 19:43:15 +0000760
761 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000762
Ted Kremenek687af802008-01-29 19:43:15 +0000763 case BinaryOperator::EQ:
764 // FIXME: should we allow XX.EQ() to return a set of values,
765 // allowing state bifurcation? In such cases, they will also
766 // modify the state (meaning that a new state will be returned
767 // as well).
768 assert (B->getType() == getContext().IntTy);
769
770 if (isa<LValue>(V1)) {
771 const LValue& L1 = cast<LValue>(V1);
772 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcba2e432008-02-05 19:35:18 +0000773 Nodify(Dst, B, N2, SetValue(St, B, L1.EQ(ValMgr, L2)));
Ted Kremenek687af802008-01-29 19:43:15 +0000774 }
775 else {
776 const NonLValue& R1 = cast<NonLValue>(V1);
777 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcba2e432008-02-05 19:35:18 +0000778 Nodify(Dst, B, N2, SetValue(St, B, R1.EQ(ValMgr, R2)));
Ted Kremenek687af802008-01-29 19:43:15 +0000779 }
780
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000781 break;
782 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000783 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000784 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000785}
Ted Kremenekee985462008-01-16 18:18:48 +0000786
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000787
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000788void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
789 GRConstants::NodeSet& Dst) {
790
791 // FIXME: add metadata to the CFG so that we can disable
792 // this check when we KNOW that there is no block-level subexpression.
793 // The motivation is that this check requires a hashtable lookup.
794
795 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
796 Dst.Add(Pred);
797 return;
798 }
799
800 switch (S->getStmtClass()) {
801 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000802
803 if (cast<BinaryOperator>(S)->isLogicalOp()) {
804 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
805 break;
806 }
807
808 // Fall-through.
809
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000810 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000811 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
812 break;
813
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000814 case Stmt::UnaryOperatorClass:
815 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
816 break;
817
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 case Stmt::ParenExprClass:
819 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
820 break;
821
Ted Kremenek874d63f2008-01-24 02:02:54 +0000822 case Stmt::ImplicitCastExprClass: {
823 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
824 VisitCast(C, C->getSubExpr(), Pred, Dst);
825 break;
826 }
827
828 case Stmt::CastExprClass: {
829 CastExpr* C = cast<CastExpr>(S);
830 VisitCast(C, C->getSubExpr(), Pred, Dst);
831 break;
832 }
833
Ted Kremenekf233d482008-02-05 00:26:40 +0000834 case Stmt::ConditionalOperatorClass: { // '?' operator
835 ConditionalOperator* C = cast<ConditionalOperator>(S);
836 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
837 break;
838 }
839
840 case Stmt::ChooseExprClass: { // __builtin_choose_expr
841 ChooseExpr* C = cast<ChooseExpr>(S);
842 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
843 break;
844 }
845
Ted Kremenek9de04c42008-01-24 20:55:43 +0000846 case Stmt::DeclStmtClass:
847 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
848 break;
849
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000850 default:
851 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
852 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000853 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000854}
855
Ted Kremenekee985462008-01-16 18:18:48 +0000856//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000857// "Assume" logic.
858//===----------------------------------------------------------------------===//
859
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000860GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
861 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000862 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000863
864 switch (Cond.getSubKind()) {
865 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000866 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000867 return St;
868
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000869 case lval::SymbolValKind:
870 if (Assumption)
871 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
872 ValMgr.getZeroWithPtrWidth(), isFeasible);
873 else
874 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
875 ValMgr.getZeroWithPtrWidth(), isFeasible);
876
Ted Kremenek329f8542008-02-05 21:52:21 +0000877 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000878 isFeasible = Assumption;
879 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000880
Ted Kremenek329f8542008-02-05 21:52:21 +0000881 case lval::ConcreteIntKind: {
882 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000883 isFeasible = b ? Assumption : !Assumption;
884 return St;
885 }
886 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000887}
888
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000889GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
890 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000891 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000892
893 switch (Cond.getSubKind()) {
894 default:
895 assert (false && "'Assume' not implemented for this NonLValue.");
896 return St;
897
Ted Kremenek329f8542008-02-05 21:52:21 +0000898 case nonlval::ConcreteIntKind: {
899 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000900 isFeasible = b ? Assumption : !Assumption;
901 return St;
902 }
903 }
904}
905
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000906GRConstants::StateTy
907GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
908 const llvm::APSInt& V, bool& isFeasible) {
909
910 // First, determine if sym == X, where X != V.
911 if (const llvm::APSInt* X = St.getSymVal(sym)) {
912 isFeasible = *X != V;
913 return St;
914 }
915
916 // Second, determine if sym != V.
917 if (St.isNotEqual(sym, V)) {
918 isFeasible = true;
919 return St;
920 }
921
922 // If we reach here, sym is not a constant and we don't know if it is != V.
923 // Make that assumption.
924
925 isFeasible = true;
926 return StateMgr.AddNE(St, sym, V);
927}
928
929GRConstants::StateTy
930GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
931 const llvm::APSInt& V, bool& isFeasible) {
932
933 // First, determine if sym == X, where X != V.
934 if (const llvm::APSInt* X = St.getSymVal(sym)) {
935 isFeasible = *X == V;
936 return St;
937 }
938
939 // Second, determine if sym != V.
940 if (St.isNotEqual(sym, V)) {
941 isFeasible = false;
942 return St;
943 }
944
945 // If we reach here, sym is not a constant and we don't know if it is == V.
946 // Make that assumption.
947
948 isFeasible = true;
949 return StateMgr.AddEQ(St, sym, V);
950}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000951
952//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000953// Driver.
954//===----------------------------------------------------------------------===//
955
Ted Kremenekaa66a322008-01-16 21:46:15 +0000956#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000957static GRConstants* GraphPrintCheckerState;
958
Ted Kremenekaa66a322008-01-16 21:46:15 +0000959namespace llvm {
960template<>
961struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
962 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000963
Ted Kremenek9153f732008-02-05 07:17:49 +0000964 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000965 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000966 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
967 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
968 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
969 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000970 }
971 }
972
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000973 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +0000974 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000975 bool isFirst = true;
976
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000977 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000978 if (I.getKey().getKind() != kind)
979 continue;
980
981 if (isFirst) {
982 if (!isFirstGroup) Out << "\\l\\l";
983 PrintKindLabel(Out, kind);
984 isFirst = false;
985 }
986 else
987 Out << "\\l";
988
989 Out << ' ';
990
991 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
992 Out << V->getName();
993 else {
994 Stmt* E = cast<Stmt>(I.getKey());
995 Out << " (" << (void*) E << ") ";
996 E->printPretty(Out);
997 }
998
999 Out << " : ";
1000 I.getData().print(Out);
1001 }
1002 }
1003
Ted Kremeneked4de312008-02-06 03:56:15 +00001004 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1005 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1006
1007 if (CE.isEmpty())
1008 return;
1009
1010 Out << "\\l\\|'==' constraints:";
1011
1012 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1013 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1014 }
1015
1016 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1017 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1018
1019 if (NE.isEmpty())
1020 return;
1021
1022 Out << "\\l\\|'!=' constraints:";
1023
1024 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1025 I != EI; ++I){
1026
1027 Out << "\\l $" << I.getKey() << " : ";
1028 bool isFirst = true;
1029
1030 ValueState::IntSetTy::iterator J=I.getData().begin(),
1031 EJ=I.getData().end();
1032 for ( ; J != EJ; ++J) {
1033 if (isFirst) isFirst = false;
1034 else Out << ", ";
1035
1036 Out << (*J)->toString();
1037 }
1038 }
1039 }
1040
Ted Kremenekaa66a322008-01-16 21:46:15 +00001041 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1042 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001043
1044 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001045 ProgramPoint Loc = N->getLocation();
1046
1047 switch (Loc.getKind()) {
1048 case ProgramPoint::BlockEntranceKind:
1049 Out << "Block Entrance: B"
1050 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1051 break;
1052
1053 case ProgramPoint::BlockExitKind:
1054 assert (false);
1055 break;
1056
1057 case ProgramPoint::PostStmtKind: {
1058 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001059 Out << L.getStmt()->getStmtClassName() << ':'
1060 << (void*) L.getStmt() << ' ';
1061
Ted Kremenekaa66a322008-01-16 21:46:15 +00001062 L.getStmt()->printPretty(Out);
1063 break;
1064 }
1065
1066 default: {
1067 const BlockEdge& E = cast<BlockEdge>(Loc);
1068 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1069 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001070
1071 if (Stmt* T = E.getSrc()->getTerminator()) {
1072 Out << "\\|Terminator: ";
1073 E.getSrc()->printTerminator(Out);
1074
1075 if (isa<SwitchStmt>(T)) {
1076 // FIXME
1077 }
1078 else {
1079 Out << "\\lCondition: ";
1080 if (*E.getSrc()->succ_begin() == E.getDst())
1081 Out << "true";
1082 else
1083 Out << "false";
1084 }
1085
1086 Out << "\\l";
1087 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001088
1089 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1090 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1091 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001092 }
1093 }
1094
Ted Kremenek9153f732008-02-05 07:17:49 +00001095 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001096
Ted Kremenek9153f732008-02-05 07:17:49 +00001097 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1098 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1099 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001100
1101 PrintEQ(Out, N->getState());
1102 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001103
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001104 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001105 return Out.str();
1106 }
1107};
1108} // end llvm namespace
1109#endif
1110
Ted Kremenekee985462008-01-16 18:18:48 +00001111namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001112void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1113 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001114 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001115#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001116 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001117 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001118 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001119#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001120}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001121} // end clang namespace