blob: 8656d776c86ec9b578891b8a9b8a2dd4ea904e7e [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 Kremenek08b66252008-02-06 04:31:33 +0000237 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
238 bool& isFeasible);
239
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000240 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000241
Ted Kremenekcba2e432008-02-05 19:35:18 +0000242 /// Nodify - This version of Nodify is used to batch process a set of states.
243 /// The states are not guaranteed to be unique.
244 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
245
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000246 /// Visit - Transfer function logic for all statements. Dispatches to
247 /// other functions that handle specific kinds of statements.
248 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000249
250 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
251 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000252
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000253 /// VisitUnaryOperator - Transfer function logic for unary operators.
254 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
255
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000256 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000257 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
258
259 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000260 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
261
262 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
263 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
264 NodeTy* Pred, NodeSet& Dst);
265
266 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
267 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000268};
269} // end anonymous namespace
270
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000271
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000272GRConstants::StateTy
273GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
274
275 if (!StateCleaned) {
276 St = RemoveDeadBindings(CurrentStmt, St);
277 StateCleaned = true;
278 }
279
280 bool isBlkExpr = false;
281
282 if (S == CurrentStmt) {
283 isBlkExpr = getCFG().isBlkExpr(S);
284
285 if (!isBlkExpr)
286 return St;
287 }
288
289 return StateMgr.SetValue(St, S, isBlkExpr, V);
290}
291
Ted Kremenekcba2e432008-02-05 19:35:18 +0000292const GRConstants::StateTy::BufferTy&
293GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
294 StateTy::BufferTy& RetBuf) {
295
296 assert (RetBuf.empty());
297
298 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
299 RetBuf.push_back(SetValue(St, S, *I));
300
301 return RetBuf;
302}
303
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000304GRConstants::StateTy
305GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
306
307 if (!LV.isValid())
308 return St;
309
310 if (!StateCleaned) {
311 St = RemoveDeadBindings(CurrentStmt, St);
312 StateCleaned = true;
313 }
314
315 return StateMgr.SetValue(St, LV, V);
316}
317
Ted Kremenekf233d482008-02-05 00:26:40 +0000318void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000319 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000320
321 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000322
Ted Kremenekb38911f2008-01-30 23:03:39 +0000323 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000324 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000325 if (I.getKey().isSubExpr())
326 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000327
Ted Kremenekf233d482008-02-05 00:26:40 +0000328 // Remove terminator-specific bindings.
329 switch (Term->getStmtClass()) {
330 default: break;
331
332 case Stmt::BinaryOperatorClass: { // '&&', '||'
333 BinaryOperator* B = cast<BinaryOperator>(Term);
334 // FIXME: Liveness analysis should probably remove these automatically.
335 // Verify later when we converge to an 'optimization' stage.
336 PrevState = StateMgr.Remove(PrevState, B->getRHS());
337 break;
338 }
339
340 case Stmt::ConditionalOperatorClass: { // '?' operator
341 ConditionalOperator* C = cast<ConditionalOperator>(Term);
342 // FIXME: Liveness analysis should probably remove these automatically.
343 // Verify later when we converge to an 'optimization' stage.
344 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
345 PrevState = StateMgr.Remove(PrevState, C->getRHS());
346 break;
347 }
348
349 case Stmt::ChooseExprClass: { // __builtin_choose_expr
350 ChooseExpr* C = cast<ChooseExpr>(Term);
351 // FIXME: Liveness analysis should probably remove these automatically.
352 // Verify later when we converge to an 'optimization' stage.
353 PrevState = StateMgr.Remove(PrevState, C->getRHS());
354 PrevState = StateMgr.Remove(PrevState, C->getRHS());
355 break;
356 }
357 }
358
Ted Kremenekb38911f2008-01-30 23:03:39 +0000359 RValue V = GetValue(PrevState, Condition);
360
361 switch (V.getBaseKind()) {
362 default:
363 break;
364
365 case RValue::InvalidKind:
366 builder.generateNode(PrevState, true);
367 builder.generateNode(PrevState, false);
368 return;
369
370 case RValue::UninitializedKind: {
371 NodeTy* N = builder.generateNode(PrevState, true);
372
373 if (N) {
374 N->markAsSink();
375 UninitBranches.insert(N);
376 }
377
378 builder.markInfeasible(false);
379 return;
380 }
381 }
382
383 // Process the true branch.
384 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000385
Ted Kremenekb38911f2008-01-30 23:03:39 +0000386 StateTy St = Assume(PrevState, V, true, isFeasible);
387
Ted Kremenekf233d482008-02-05 00:26:40 +0000388 if (isFeasible)
389 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000390 else {
391 builder.markInfeasible(true);
392 isFeasible = true;
393 }
394
395 // Process the false branch.
396 St = Assume(PrevState, V, false, isFeasible);
397
Ted Kremenekf233d482008-02-05 00:26:40 +0000398 if (isFeasible)
399 builder.generateNode(St, false);
400 else
401 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000402}
403
Ted Kremenekf233d482008-02-05 00:26:40 +0000404
405void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
406 NodeSet& Dst) {
407
408 bool hasR2;
409 StateTy PrevState = Pred->getState();
410
411 RValue R1 = GetValue(PrevState, B->getLHS());
412 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
413
414 if (isa<InvalidValue>(R1) &&
415 (isa<InvalidValue>(R2) ||
416 isa<UninitializedValue>(R2))) {
417
418 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
419 return;
420 }
421 else if (isa<UninitializedValue>(R1)) {
422 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
423 return;
424 }
425
426 // R1 is an expression that can evaluate to either 'true' or 'false'.
427 if (B->getOpcode() == BinaryOperator::LAnd) {
428 // hasR2 == 'false' means that LHS evaluated to 'false' and that
429 // we short-circuited, leading to a value of '0' for the '&&' expression.
430 if (hasR2 == false) {
431 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
432 return;
433 }
434 }
435 else {
436 assert (B->getOpcode() == BinaryOperator::LOr);
437 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
438 // we short-circuited, leading to a value of '1' for the '||' expression.
439 if (hasR2 == false) {
440 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
441 return;
442 }
443 }
444
445 // If we reach here we did not short-circuit. Assume R2 == true and
446 // R2 == false.
447
448 bool isFeasible;
449 StateTy St = Assume(PrevState, R2, true, isFeasible);
450
451 if (isFeasible)
452 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
453
454 St = Assume(PrevState, R2, false, isFeasible);
455
456 if (isFeasible)
457 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
458}
459
460
461
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000462void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000463 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000464
465 StmtEntryNode = builder.getLastNode();
466 CurrentStmt = S;
467 NodeSet Dst;
468 StateCleaned = false;
469
470 Visit(S, StmtEntryNode, Dst);
471
472 // If no nodes were generated, generate a new node that has all the
473 // dead mappings removed.
474 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
475 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
476 builder.generateNode(S, St, StmtEntryNode);
477 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000478
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000479 CurrentStmt = NULL;
480 StmtEntryNode = NULL;
481 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000482}
483
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000484GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000485 // Note: in the code below, we can assign a new map to M since the
486 // iterators are iterating over the tree of the *original* map.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000487 StateTy::vb_iterator I = M.begin(), E = M.end();
Ted Kremenekf84469b2008-01-18 00:41:32 +0000488
Ted Kremenekf84469b2008-01-18 00:41:32 +0000489
Ted Kremenek65cac132008-01-29 05:25:31 +0000490 for (; I!=E && !I.getKey().isSymbol(); ++I) {
491 // Remove old bindings for subexpressions and "dead"
492 // block-level expressions.
493 if (I.getKey().isSubExpr() ||
494 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
495 M = StateMgr.Remove(M, I.getKey());
496 }
497 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
498 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
499 if (!Liveness.isLive(Loc, V))
500 M = StateMgr.Remove(M, I.getKey());
501 }
502 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000503
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000504 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000505}
506
Ted Kremenekcba2e432008-02-05 19:35:18 +0000507void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000508
509 // If the state hasn't changed, don't generate a new node.
510 if (St == Pred->getState())
511 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000512
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000513 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000514}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000515
Ted Kremenekcba2e432008-02-05 19:35:18 +0000516void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
517 const StateTy::BufferTy& SB) {
518
519 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
520 Nodify(Dst, S, Pred, *I);
521}
522
523void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000524
525 QualType T = CastE->getType();
526
527 // Check for redundant casts.
528 if (E->getType() == T) {
529 Dst.Add(Pred);
530 return;
531 }
532
533 NodeSet S1;
534 Visit(E, Pred, S1);
535
536 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
537 NodeTy* N = *I1;
538 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000539 const RValue& V = GetValue(St, E);
540 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000541 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000542}
543
544void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
545 GRConstants::NodeSet& Dst) {
546
547 StateTy St = Pred->getState();
548
549 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000550 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
551 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000552 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek403c1812008-01-28 22:51:57 +0000553 E ? GetValue(St, E) : UninitializedValue());
554 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000555
556 Nodify(Dst, DS, Pred, St);
557
558 if (Dst.empty())
559 Dst.Add(Pred);
560}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000561
Ted Kremenekf233d482008-02-05 00:26:40 +0000562
563void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
564 NodeTy* Pred, NodeSet& Dst) {
565
566 StateTy St = Pred->getState();
567
568 RValue R = GetValue(St, LHS);
569 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
570
571 Nodify(Dst, S, Pred, SetValue(St, S, R));
572}
573
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000574void GRConstants::VisitUnaryOperator(UnaryOperator* U,
575 GRConstants::NodeTy* Pred,
576 GRConstants::NodeSet& Dst) {
577 NodeSet S1;
578 Visit(U->getSubExpr(), Pred, S1);
579
580 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
581 NodeTy* N1 = *I1;
582 StateTy St = N1->getState();
583
584 switch (U->getOpcode()) {
585 case UnaryOperator::PostInc: {
586 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000587 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000588 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000589 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
590 break;
591 }
592
593 case UnaryOperator::PostDec: {
594 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000595 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000596 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000597 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
598 break;
599 }
600
601 case UnaryOperator::PreInc: {
602 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000603 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000604 NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000605 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
606 break;
607 }
608
609 case UnaryOperator::PreDec: {
610 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000611 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek0ff9a4d2008-02-05 00:43:43 +0000612 NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000613 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
614 break;
615 }
616
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000617 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000618 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
619 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000620 break;
621 }
622
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000623 case UnaryOperator::Not: {
624 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
625 Nodify(Dst, U, N1, SetValue(St, U, R1.BitwiseComplement(ValMgr)));
626 break;
627 }
628
Ted Kremenek64924852008-01-31 02:35:41 +0000629 case UnaryOperator::AddrOf: {
630 const LValue& L1 = GetLValue(St, U->getSubExpr());
631 Nodify(Dst, U, N1, SetValue(St, U, L1));
632 break;
633 }
634
635 case UnaryOperator::Deref: {
636 const LValue& L1 = GetLValue(St, U->getSubExpr());
637 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
638 break;
639 }
640
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000641 default: ;
642 assert (false && "Not implemented.");
643 }
644 }
645}
646
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000647void GRConstants::VisitBinaryOperator(BinaryOperator* B,
648 GRConstants::NodeTy* Pred,
649 GRConstants::NodeSet& Dst) {
650 NodeSet S1;
651 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000652
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000653 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
654 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000655
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000656 // When getting the value for the LHS, check if we are in an assignment.
657 // In such cases, we want to (initially) treat the LHS as an LValue,
658 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000659 // evaluated to LValueDecl's instead of to an NonLValue.
660 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000661 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
662 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000663
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000664 NodeSet S2;
665 Visit(B->getRHS(), N1, S2);
666
667 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
668 NodeTy* N2 = *I2;
669 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000670 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000671
672 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +0000673 default:
674 Dst.Add(N2);
675 break;
676
Ted Kremenekf233d482008-02-05 00:26:40 +0000677 // Arithmetic operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000678
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000679 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000680 const NonLValue& R1 = cast<NonLValue>(V1);
681 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000682
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000683 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000684 break;
685 }
686
687 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000688 const NonLValue& R1 = cast<NonLValue>(V1);
689 const NonLValue& R2 = cast<NonLValue>(V2);
690 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000691 break;
692 }
693
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000694 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000695 const NonLValue& R1 = cast<NonLValue>(V1);
696 const NonLValue& R2 = cast<NonLValue>(V2);
697 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000698 break;
699 }
700
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000701 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000702 const NonLValue& R1 = cast<NonLValue>(V1);
703 const NonLValue& R2 = cast<NonLValue>(V2);
704 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000705 break;
706 }
707
Ted Kremenekcce207d2008-01-28 22:26:15 +0000708 case BinaryOperator::Rem: {
709 const NonLValue& R1 = cast<NonLValue>(V1);
710 const NonLValue& R2 = cast<NonLValue>(V2);
711 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
712 break;
713 }
714
Ted Kremenek687af802008-01-29 19:43:15 +0000715 // Assignment operators.
716
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000717 case BinaryOperator::Assign: {
718 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000719 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000720 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
721 break;
722 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000723
724 case BinaryOperator::AddAssign: {
725 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000726 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
727 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000728 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
729 break;
730 }
731
732 case BinaryOperator::SubAssign: {
733 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000734 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
735 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000736 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
737 break;
738 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000739
740 case BinaryOperator::MulAssign: {
741 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000742 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
743 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000744 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
745 break;
746 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000747
748 case BinaryOperator::DivAssign: {
749 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000750 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
751 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000752 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
753 break;
754 }
Ted Kremenek10099a62008-01-28 22:28:54 +0000755
756 case BinaryOperator::RemAssign: {
757 const LValue& L1 = cast<LValue>(V1);
758 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
759 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
760 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
761 break;
762 }
Ted Kremenek687af802008-01-29 19:43:15 +0000763
764 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000765
Ted Kremenek687af802008-01-29 19:43:15 +0000766 case BinaryOperator::EQ:
767 // FIXME: should we allow XX.EQ() to return a set of values,
768 // allowing state bifurcation? In such cases, they will also
769 // modify the state (meaning that a new state will be returned
770 // as well).
771 assert (B->getType() == getContext().IntTy);
772
773 if (isa<LValue>(V1)) {
774 const LValue& L1 = cast<LValue>(V1);
775 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcba2e432008-02-05 19:35:18 +0000776 Nodify(Dst, B, N2, SetValue(St, B, L1.EQ(ValMgr, L2)));
Ted Kremenek687af802008-01-29 19:43:15 +0000777 }
778 else {
779 const NonLValue& R1 = cast<NonLValue>(V1);
780 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcba2e432008-02-05 19:35:18 +0000781 Nodify(Dst, B, N2, SetValue(St, B, R1.EQ(ValMgr, R2)));
Ted Kremenek687af802008-01-29 19:43:15 +0000782 }
783
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000784 break;
785 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000786 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000787 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000788}
Ted Kremenekee985462008-01-16 18:18:48 +0000789
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000790
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000791void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
792 GRConstants::NodeSet& Dst) {
793
794 // FIXME: add metadata to the CFG so that we can disable
795 // this check when we KNOW that there is no block-level subexpression.
796 // The motivation is that this check requires a hashtable lookup.
797
798 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
799 Dst.Add(Pred);
800 return;
801 }
802
803 switch (S->getStmtClass()) {
804 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000805
806 if (cast<BinaryOperator>(S)->isLogicalOp()) {
807 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
808 break;
809 }
810
811 // Fall-through.
812
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000813 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000814 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
815 break;
816
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000817 case Stmt::UnaryOperatorClass:
818 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
819 break;
820
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000821 case Stmt::ParenExprClass:
822 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
823 break;
824
Ted Kremenek874d63f2008-01-24 02:02:54 +0000825 case Stmt::ImplicitCastExprClass: {
826 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
827 VisitCast(C, C->getSubExpr(), Pred, Dst);
828 break;
829 }
830
831 case Stmt::CastExprClass: {
832 CastExpr* C = cast<CastExpr>(S);
833 VisitCast(C, C->getSubExpr(), Pred, Dst);
834 break;
835 }
836
Ted Kremenekf233d482008-02-05 00:26:40 +0000837 case Stmt::ConditionalOperatorClass: { // '?' operator
838 ConditionalOperator* C = cast<ConditionalOperator>(S);
839 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
840 break;
841 }
842
843 case Stmt::ChooseExprClass: { // __builtin_choose_expr
844 ChooseExpr* C = cast<ChooseExpr>(S);
845 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
846 break;
847 }
848
Ted Kremenek9de04c42008-01-24 20:55:43 +0000849 case Stmt::DeclStmtClass:
850 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
851 break;
852
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000853 default:
854 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
855 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000856 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000857}
858
Ted Kremenekee985462008-01-16 18:18:48 +0000859//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000860// "Assume" logic.
861//===----------------------------------------------------------------------===//
862
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000863GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
864 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000865 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000866
867 switch (Cond.getSubKind()) {
868 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000869 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000870 return St;
871
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000872 case lval::SymbolValKind:
873 if (Assumption)
874 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
875 ValMgr.getZeroWithPtrWidth(), isFeasible);
876 else
877 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
878 ValMgr.getZeroWithPtrWidth(), isFeasible);
879
Ted Kremenek08b66252008-02-06 04:31:33 +0000880
Ted Kremenek329f8542008-02-05 21:52:21 +0000881 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000882 isFeasible = Assumption;
883 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000884
Ted Kremenek329f8542008-02-05 21:52:21 +0000885 case lval::ConcreteIntKind: {
886 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000887 isFeasible = b ? Assumption : !Assumption;
888 return St;
889 }
890 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000891}
892
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000893GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
894 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000895 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000896
897 switch (Cond.getSubKind()) {
898 default:
899 assert (false && "'Assume' not implemented for this NonLValue.");
900 return St;
901
Ted Kremenek08b66252008-02-06 04:31:33 +0000902 case nonlval::SymIntConstraintValKind:
903 return
904 AssumeSymInt(St, Assumption,
905 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
906 isFeasible);
907
Ted Kremenek329f8542008-02-05 21:52:21 +0000908 case nonlval::ConcreteIntKind: {
909 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000910 isFeasible = b ? Assumption : !Assumption;
911 return St;
912 }
913 }
914}
915
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000916GRConstants::StateTy
917GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
918 const llvm::APSInt& V, bool& isFeasible) {
919
920 // First, determine if sym == X, where X != V.
921 if (const llvm::APSInt* X = St.getSymVal(sym)) {
922 isFeasible = *X != V;
923 return St;
924 }
925
926 // Second, determine if sym != V.
927 if (St.isNotEqual(sym, V)) {
928 isFeasible = true;
929 return St;
930 }
931
932 // If we reach here, sym is not a constant and we don't know if it is != V.
933 // Make that assumption.
934
935 isFeasible = true;
936 return StateMgr.AddNE(St, sym, V);
937}
938
939GRConstants::StateTy
940GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
941 const llvm::APSInt& V, bool& isFeasible) {
942
943 // First, determine if sym == X, where X != V.
944 if (const llvm::APSInt* X = St.getSymVal(sym)) {
945 isFeasible = *X == V;
946 return St;
947 }
948
949 // Second, determine if sym != V.
950 if (St.isNotEqual(sym, V)) {
951 isFeasible = false;
952 return St;
953 }
954
955 // If we reach here, sym is not a constant and we don't know if it is == V.
956 // Make that assumption.
957
958 isFeasible = true;
959 return StateMgr.AddEQ(St, sym, V);
960}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000961
Ted Kremenek08b66252008-02-06 04:31:33 +0000962GRConstants::StateTy
963GRConstants::AssumeSymInt(StateTy St, bool Assumption,
964 const SymIntConstraint& C, bool& isFeasible) {
965
966 switch (C.getOpcode()) {
967 default:
968 // No logic yet for other operators.
969 return St;
970
971 case BinaryOperator::EQ:
972 if (Assumption)
973 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
974 else
975 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
976
977 case BinaryOperator::NE:
978 if (Assumption)
979 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
980 else
981 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
982 }
983}
984
Ted Kremenekb38911f2008-01-30 23:03:39 +0000985//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000986// Driver.
987//===----------------------------------------------------------------------===//
988
Ted Kremenekaa66a322008-01-16 21:46:15 +0000989#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000990static GRConstants* GraphPrintCheckerState;
991
Ted Kremenekaa66a322008-01-16 21:46:15 +0000992namespace llvm {
993template<>
994struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
995 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000996
Ted Kremenek9153f732008-02-05 07:17:49 +0000997 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000998 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000999 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1000 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1001 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1002 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001003 }
1004 }
1005
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001006 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001007 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001008 bool isFirst = true;
1009
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001010 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001011 if (I.getKey().getKind() != kind)
1012 continue;
1013
1014 if (isFirst) {
1015 if (!isFirstGroup) Out << "\\l\\l";
1016 PrintKindLabel(Out, kind);
1017 isFirst = false;
1018 }
1019 else
1020 Out << "\\l";
1021
1022 Out << ' ';
1023
1024 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1025 Out << V->getName();
1026 else {
1027 Stmt* E = cast<Stmt>(I.getKey());
1028 Out << " (" << (void*) E << ") ";
1029 E->printPretty(Out);
1030 }
1031
1032 Out << " : ";
1033 I.getData().print(Out);
1034 }
1035 }
1036
Ted Kremeneked4de312008-02-06 03:56:15 +00001037 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1038 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1039
1040 if (CE.isEmpty())
1041 return;
1042
1043 Out << "\\l\\|'==' constraints:";
1044
1045 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1046 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1047 }
1048
1049 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1050 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1051
1052 if (NE.isEmpty())
1053 return;
1054
1055 Out << "\\l\\|'!=' constraints:";
1056
1057 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1058 I != EI; ++I){
1059
1060 Out << "\\l $" << I.getKey() << " : ";
1061 bool isFirst = true;
1062
1063 ValueState::IntSetTy::iterator J=I.getData().begin(),
1064 EJ=I.getData().end();
1065 for ( ; J != EJ; ++J) {
1066 if (isFirst) isFirst = false;
1067 else Out << ", ";
1068
1069 Out << (*J)->toString();
1070 }
1071 }
1072 }
1073
Ted Kremenekaa66a322008-01-16 21:46:15 +00001074 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1075 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001076
1077 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001078 ProgramPoint Loc = N->getLocation();
1079
1080 switch (Loc.getKind()) {
1081 case ProgramPoint::BlockEntranceKind:
1082 Out << "Block Entrance: B"
1083 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1084 break;
1085
1086 case ProgramPoint::BlockExitKind:
1087 assert (false);
1088 break;
1089
1090 case ProgramPoint::PostStmtKind: {
1091 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001092 Out << L.getStmt()->getStmtClassName() << ':'
1093 << (void*) L.getStmt() << ' ';
1094
Ted Kremenekaa66a322008-01-16 21:46:15 +00001095 L.getStmt()->printPretty(Out);
1096 break;
1097 }
1098
1099 default: {
1100 const BlockEdge& E = cast<BlockEdge>(Loc);
1101 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1102 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001103
1104 if (Stmt* T = E.getSrc()->getTerminator()) {
1105 Out << "\\|Terminator: ";
1106 E.getSrc()->printTerminator(Out);
1107
1108 if (isa<SwitchStmt>(T)) {
1109 // FIXME
1110 }
1111 else {
1112 Out << "\\lCondition: ";
1113 if (*E.getSrc()->succ_begin() == E.getDst())
1114 Out << "true";
1115 else
1116 Out << "false";
1117 }
1118
1119 Out << "\\l";
1120 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001121
1122 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1123 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1124 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001125 }
1126 }
1127
Ted Kremenek9153f732008-02-05 07:17:49 +00001128 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001129
Ted Kremenek9153f732008-02-05 07:17:49 +00001130 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1131 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1132 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001133
1134 PrintEQ(Out, N->getState());
1135 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001136
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001137 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001138 return Out.str();
1139 }
1140};
1141} // end llvm namespace
1142#endif
1143
Ted Kremenekee985462008-01-16 18:18:48 +00001144namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001145void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1146 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001147 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001148#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001149 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001150 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001151 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001152#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001153}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001154} // end clang namespace