blob: 66a320b16d3d2c3542d78250b464a64ebb143489 [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 Kremenek19227e32008-02-07 06:33:19 +000025#include "clang/Basic/Diagnostic.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000026
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/DataTypes.h"
29#include "llvm/ADT/APSInt.h"
30#include "llvm/ADT/FoldingSet.h"
31#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000032#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000033#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000034#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000035#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036#include "llvm/Support/Streams.h"
37
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000038#include <functional>
39
Ted Kremenekaa66a322008-01-16 21:46:15 +000040#ifndef NDEBUG
41#include "llvm/Support/GraphWriter.h"
42#include <sstream>
43#endif
44
Ted Kremenekd27f8162008-01-15 23:55:06 +000045using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000046using llvm::dyn_cast;
47using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000048using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000049
50//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000051// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000052//
53// FIXME: This checker logic should be eventually broken into two components.
54// The first is the "meta"-level checking logic; the code that
55// does the Stmt visitation, fetching values from the map, etc.
56// The second part does the actual state manipulation. This way we
57// get more of a separate of concerns of these two pieces, with the
58// latter potentially being refactored back into the main checking
59// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000060//===----------------------------------------------------------------------===//
61
62namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000063
Ted Kremenekab2b8c52008-01-23 19:59:44 +000064class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +000065
66public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000067 typedef ValueStateManager::StateTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +000068 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
69 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000070 typedef ExplodedGraph<GRConstants> GraphTy;
71 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000072
73 class NodeSet {
74 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
75 ImplTy Impl;
76 public:
77
78 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000079 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000080
Ted Kremenekb38911f2008-01-30 23:03:39 +000081 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000082
83 typedef ImplTy::iterator iterator;
84 typedef ImplTy::const_iterator const_iterator;
85
86 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000087 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000088
89 iterator begin() { return Impl.begin(); }
90 iterator end() { return Impl.end(); }
91
92 const_iterator begin() const { return Impl.begin(); }
93 const_iterator end() const { return Impl.end(); }
94 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000095
Ted Kremenekd27f8162008-01-15 23:55:06 +000096protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000097 /// G - the simulation graph.
98 GraphTy& G;
99
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000100 /// Liveness - live-variables information the ValueDecl* and block-level
101 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000102 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000103
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000104 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000105 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000106 StmtNodeBuilder* Builder;
107
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000108 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000109 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000110
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000111 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000112 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000113
Ted Kremenek68fd2572008-01-29 17:27:31 +0000114 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000115 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000116
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000117 /// StmtEntryNode - The immediate predecessor node.
118 NodeTy* StmtEntryNode;
119
120 /// CurrentStmt - The current block-level statement.
121 Stmt* CurrentStmt;
122
Ted Kremenekb38911f2008-01-30 23:03:39 +0000123 /// UninitBranches - Nodes in the ExplodedGraph that result from
124 /// taking a branch based on an uninitialized value.
125 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
126 UninitBranchesTy UninitBranches;
127
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000128 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
129 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000130 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
131 NullDerefTy ImplicitNullDeref;
132 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000133
134
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000135 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000136
Ted Kremenekd27f8162008-01-15 23:55:06 +0000137public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000138 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000139 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000140 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000141 ValMgr(StateMgr.getValueManager()),
142 SymMgr(StateMgr.getSymbolManager()),
143 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000144
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000145 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000146 Liveness.runOnCFG(G.getCFG());
147 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000148 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000149
Ted Kremenek19227e32008-02-07 06:33:19 +0000150 /// getContext - Return the ASTContext associated with this analysis.
151 ASTContext& getContext() const { return G.getContext(); }
152
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000153 /// getCFG - Returns the CFG associated with this analysis.
154 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000155
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000156 /// getInitialState - Return the initial state used for the root vertex
157 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000158 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000159 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000160
161 // Iterate the parameters.
162 FunctionDecl& F = G.getFunctionDecl();
163
164 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000165 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000166 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000167
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000168 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000169 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000170
171 bool isUninitControlFlow(const NodeTy* N) const {
172 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
173 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000174
175 bool isImplicitNullDeref(const NodeTy* N) const {
176 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
177 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000178
179 bool isExplicitNullDeref(const NodeTy* N) const {
180 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
181 }
182
Ted Kremenek19227e32008-02-07 06:33:19 +0000183 typedef NullDerefTy::iterator null_iterator;
184 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
185 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000186
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000187 /// ProcessStmt - Called by GREngine. Used to generate new successor
188 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000189 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
190
191 /// ProcessBranch - Called by GREngine. Used to generate successor
192 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000193 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000194
195 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
196 /// that all subexpression mappings are removed and that any
197 /// block-level expressions that are not live at 'S' also have their
198 /// mappings removed.
199 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
200
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000201 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000202
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000203 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000204 return SetValue(St, const_cast<Stmt*>(S), V);
205 }
206
Ted Kremenekcba2e432008-02-05 19:35:18 +0000207 /// SetValue - This version of SetValue is used to batch process a set
208 /// of different possible RValues and return a set of different states.
209 const StateTy::BufferTy& SetValue(StateTy St, Stmt* S,
210 const RValue::BufferTy& V,
211 StateTy::BufferTy& RetBuf);
212
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000213 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000214
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000215 inline RValue GetValue(const StateTy& St, Stmt* S) {
216 return StateMgr.GetValue(St, S);
217 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000218
219 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
220 return StateMgr.GetValue(St, S, &hasVal);
221 }
222
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000223 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000224 return GetValue(St, const_cast<Stmt*>(S));
225 }
226
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000227 inline RValue GetValue(const StateTy& St, const LValue& LV,
228 QualType* T = NULL) {
229
230 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000231 }
232
233 inline LValue GetLValue(const StateTy& St, Stmt* S) {
234 return StateMgr.GetLValue(St, S);
235 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000236
237 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
238 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
239 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000240
241 /// Assume - Create new state by assuming that a given expression
242 /// is true or false.
243 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
244 bool& isFeasible) {
245 if (isa<LValue>(Cond))
246 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
247 else
248 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
249 }
250
251 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
252 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000253
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000254 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
255 bool& isFeasible);
256
257 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
258 bool& isFeasible);
259
Ted Kremenek08b66252008-02-06 04:31:33 +0000260 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
261 bool& isFeasible);
262
Ted Kremenek7e593362008-02-07 15:20:13 +0000263 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000264
Ted Kremenekcba2e432008-02-05 19:35:18 +0000265 /// Nodify - This version of Nodify is used to batch process a set of states.
266 /// The states are not guaranteed to be unique.
267 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
268
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000269 /// Visit - Transfer function logic for all statements. Dispatches to
270 /// other functions that handle specific kinds of statements.
271 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000272
273 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
274 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000275
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000276 /// VisitUnaryOperator - Transfer function logic for unary operators.
277 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
278
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000279 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000280 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
281
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000282 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000283
284 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
285 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
286
Ted Kremenek9de04c42008-01-24 20:55:43 +0000287 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000288 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
289
290 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
291 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
292 NodeTy* Pred, NodeSet& Dst);
293
294 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
295 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000296};
297} // end anonymous namespace
298
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000299
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000300GRConstants::StateTy
301GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000302
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000303 if (!StateCleaned) {
304 St = RemoveDeadBindings(CurrentStmt, St);
305 StateCleaned = true;
306 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000307
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000308 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000309
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000310 if (S == CurrentStmt) {
311 isBlkExpr = getCFG().isBlkExpr(S);
312
313 if (!isBlkExpr)
314 return St;
315 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000316
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000317 return StateMgr.SetValue(St, S, isBlkExpr, V);
318}
319
Ted Kremenekcba2e432008-02-05 19:35:18 +0000320const GRConstants::StateTy::BufferTy&
321GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
322 StateTy::BufferTy& RetBuf) {
323
324 assert (RetBuf.empty());
325
326 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
327 RetBuf.push_back(SetValue(St, S, *I));
328
329 return RetBuf;
330}
331
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000332GRConstants::StateTy
333GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
334
Ted Kremenek53c641a2008-02-08 03:02:48 +0000335 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000336 return St;
337
338 if (!StateCleaned) {
339 St = RemoveDeadBindings(CurrentStmt, St);
340 StateCleaned = true;
341 }
342
343 return StateMgr.SetValue(St, LV, V);
344}
345
Ted Kremenekf233d482008-02-05 00:26:40 +0000346void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000347 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000348
349 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000350
Ted Kremenekb38911f2008-01-30 23:03:39 +0000351 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000352 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000353 if (I.getKey().isSubExpr())
354 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000355
Ted Kremenekf233d482008-02-05 00:26:40 +0000356 // Remove terminator-specific bindings.
357 switch (Term->getStmtClass()) {
358 default: break;
359
360 case Stmt::BinaryOperatorClass: { // '&&', '||'
361 BinaryOperator* B = cast<BinaryOperator>(Term);
362 // FIXME: Liveness analysis should probably remove these automatically.
363 // Verify later when we converge to an 'optimization' stage.
364 PrevState = StateMgr.Remove(PrevState, B->getRHS());
365 break;
366 }
367
368 case Stmt::ConditionalOperatorClass: { // '?' operator
369 ConditionalOperator* C = cast<ConditionalOperator>(Term);
370 // FIXME: Liveness analysis should probably remove these automatically.
371 // Verify later when we converge to an 'optimization' stage.
372 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
373 PrevState = StateMgr.Remove(PrevState, C->getRHS());
374 break;
375 }
376
377 case Stmt::ChooseExprClass: { // __builtin_choose_expr
378 ChooseExpr* C = cast<ChooseExpr>(Term);
379 // FIXME: Liveness analysis should probably remove these automatically.
380 // Verify later when we converge to an 'optimization' stage.
381 PrevState = StateMgr.Remove(PrevState, C->getRHS());
382 PrevState = StateMgr.Remove(PrevState, C->getRHS());
383 break;
384 }
385 }
386
Ted Kremenekb38911f2008-01-30 23:03:39 +0000387 RValue V = GetValue(PrevState, Condition);
388
389 switch (V.getBaseKind()) {
390 default:
391 break;
392
Ted Kremenek53c641a2008-02-08 03:02:48 +0000393 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000394 builder.generateNode(PrevState, true);
395 builder.generateNode(PrevState, false);
396 return;
397
398 case RValue::UninitializedKind: {
399 NodeTy* N = builder.generateNode(PrevState, true);
400
401 if (N) {
402 N->markAsSink();
403 UninitBranches.insert(N);
404 }
405
406 builder.markInfeasible(false);
407 return;
408 }
409 }
410
411 // Process the true branch.
412 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000413
Ted Kremenekb38911f2008-01-30 23:03:39 +0000414 StateTy St = Assume(PrevState, V, true, isFeasible);
415
Ted Kremenekf233d482008-02-05 00:26:40 +0000416 if (isFeasible)
417 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000418 else {
419 builder.markInfeasible(true);
420 isFeasible = true;
421 }
422
423 // Process the false branch.
424 St = Assume(PrevState, V, false, isFeasible);
425
Ted Kremenekf233d482008-02-05 00:26:40 +0000426 if (isFeasible)
427 builder.generateNode(St, false);
428 else
429 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000430}
431
Ted Kremenekf233d482008-02-05 00:26:40 +0000432
433void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
434 NodeSet& Dst) {
435
436 bool hasR2;
437 StateTy PrevState = Pred->getState();
438
439 RValue R1 = GetValue(PrevState, B->getLHS());
440 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
441
Ted Kremenek22031182008-02-08 02:57:34 +0000442 if (isa<UnknownVal>(R1) &&
443 (isa<UnknownVal>(R2) ||
444 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000445
446 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
447 return;
448 }
Ted Kremenek22031182008-02-08 02:57:34 +0000449 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000450 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
451 return;
452 }
453
454 // R1 is an expression that can evaluate to either 'true' or 'false'.
455 if (B->getOpcode() == BinaryOperator::LAnd) {
456 // hasR2 == 'false' means that LHS evaluated to 'false' and that
457 // we short-circuited, leading to a value of '0' for the '&&' expression.
458 if (hasR2 == false) {
459 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
460 return;
461 }
462 }
463 else {
464 assert (B->getOpcode() == BinaryOperator::LOr);
465 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
466 // we short-circuited, leading to a value of '1' for the '||' expression.
467 if (hasR2 == false) {
468 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
469 return;
470 }
471 }
472
473 // If we reach here we did not short-circuit. Assume R2 == true and
474 // R2 == false.
475
476 bool isFeasible;
477 StateTy St = Assume(PrevState, R2, true, isFeasible);
478
479 if (isFeasible)
480 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
481
482 St = Assume(PrevState, R2, false, isFeasible);
483
484 if (isFeasible)
485 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
486}
487
488
489
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000490void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000491 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000492
493 StmtEntryNode = builder.getLastNode();
494 CurrentStmt = S;
495 NodeSet Dst;
496 StateCleaned = false;
497
498 Visit(S, StmtEntryNode, Dst);
499
500 // If no nodes were generated, generate a new node that has all the
501 // dead mappings removed.
502 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
503 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
504 builder.generateNode(S, St, StmtEntryNode);
505 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000506
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000507 CurrentStmt = NULL;
508 StmtEntryNode = NULL;
509 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000510}
511
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000512GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000513
514 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
515 // The roots are any Block-level exprs and Decls that our liveness algorithm
516 // tells us are live. We then see what Decls they may reference, and keep
517 // those around. This code more than likely can be made faster, and the
518 // frequency of which this method is called should be experimented with
519 // for optimum performance.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000520
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000521 llvm::SmallVector<ValueDecl*, 10> WList;
Ted Kremenekf84469b2008-01-18 00:41:32 +0000522
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000523 for (StateTy::vb_iterator I = M.begin(), E = M.end();
524 I!=E && !I.getKey().isSymbol(); ++I) {
525
526 // Remove old bindings for subexpressions.
527 if (I.getKey().isSubExpr()) {
Ted Kremenek65cac132008-01-29 05:25:31 +0000528 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000529 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000530 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000531
532 if (I.getKey().isBlkExpr()) {
533 if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) {
534 if (isa<lval::DeclVal>(I.getData())) {
535 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
536 WList.push_back(LV.getDecl());
537 }
538 }
539 else
540 M = StateMgr.Remove(M, I.getKey());
541
542 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000543 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000544
545 assert (I.getKey().isDecl());
546
547 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
548 if (Liveness.isLive(Loc, V))
549 WList.push_back(V);
Ted Kremenek65cac132008-01-29 05:25:31 +0000550 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000551
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000552 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
553
554 while (!WList.empty()) {
555 ValueDecl* V = WList.back();
556 WList.pop_back();
557
558 if (Marked.count(V))
559 continue;
560
561 Marked.insert(V);
562
563 if (V->getType()->isPointerType()) {
564 const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V)));
565
566 if (!isa<lval::DeclVal>(LV))
567 continue;
568
569 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
570 WList.push_back(LVD.getDecl());
571 }
572 }
573
574 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I)
575 if (I.getKey().isDecl())
576 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
577 if (!Marked.count(V))
578 M = StateMgr.Remove(M, V);
579
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000580 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000581}
582
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000583GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000584GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000585
586 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000587 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000588 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000589
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000590 NodeTy* N = Builder->generateNode(S, St, Pred);
591 Dst.Add(N);
592 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000593}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000594
Ted Kremenekcba2e432008-02-05 19:35:18 +0000595void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
596 const StateTy::BufferTy& SB) {
597
598 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
599 Nodify(Dst, S, Pred, *I);
600}
601
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000602void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
603 if (D != CurrentStmt) {
604 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
605 return;
606 }
607
608 // If we are here, we are loading the value of the decl and binding
609 // it to the block-level expression.
610
611 StateTy St = Pred->getState();
612
613 Nodify(Dst, D, Pred,
614 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
615}
616
Ted Kremenekcba2e432008-02-05 19:35:18 +0000617void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000618
619 QualType T = CastE->getType();
620
621 // Check for redundant casts.
622 if (E->getType() == T) {
623 Dst.Add(Pred);
624 return;
625 }
626
627 NodeSet S1;
628 Visit(E, Pred, S1);
629
630 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
631 NodeTy* N = *I1;
632 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000633 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000634 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000635 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000636}
637
638void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
639 GRConstants::NodeSet& Dst) {
640
641 StateTy St = Pred->getState();
642
643 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000644 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
645 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000646 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000647 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000648 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000649
650 Nodify(Dst, DS, Pred, St);
651
652 if (Dst.empty())
653 Dst.Add(Pred);
654}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000655
Ted Kremenekf233d482008-02-05 00:26:40 +0000656
657void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
658 NodeTy* Pred, NodeSet& Dst) {
659
660 StateTy St = Pred->getState();
661
662 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000663 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000664
665 Nodify(Dst, S, Pred, SetValue(St, S, R));
666}
667
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000668void GRConstants::VisitUnaryOperator(UnaryOperator* U,
669 GRConstants::NodeTy* Pred,
670 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000671
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000672 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000673 UnaryOperator::Opcode Op = U->getOpcode();
674
675 // FIXME: This is a hack so that for '*' and '&' we don't recurse
676 // on visiting the subexpression if it is a DeclRefExpr. We should
677 // probably just handle AddrOf and Deref in their own methods to make
678 // this cleaner.
679 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
680 isa<DeclRefExpr>(U->getSubExpr()))
681 S1.Add(Pred);
682 else
683 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000684
685 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
686 NodeTy* N1 = *I1;
687 StateTy St = N1->getState();
688
689 switch (U->getOpcode()) {
690 case UnaryOperator::PostInc: {
691 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000692 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000693
694 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
695 GetRValueConstant(1U, U));
696
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000697 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
698 break;
699 }
700
701 case UnaryOperator::PostDec: {
702 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000703 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000704
705 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
706 GetRValueConstant(1U, U));
707
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000708 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
709 break;
710 }
711
712 case UnaryOperator::PreInc: {
713 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000714 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000715
716 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
717 GetRValueConstant(1U, U));
718
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000719 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
720 break;
721 }
722
723 case UnaryOperator::PreDec: {
724 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000725 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000726
727 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
728 GetRValueConstant(1U, U));
729
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000730 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
731 break;
732 }
733
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000734 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000735 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000736 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000737 break;
738 }
739
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000740 case UnaryOperator::Not: {
741 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000742 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000743 break;
744 }
745
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000746 case UnaryOperator::LNot: {
747 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
748 //
749 // Note: technically we do "E == 0", but this is the same in the
750 // transfer functions as "0 == E".
751
752 RValue V1 = GetValue(St, U->getSubExpr());
753
754 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000755 const LValue& L1 = cast<LValue>(V1);
756 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
757 Nodify(Dst, U, N1,
758 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
759 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000760 }
761 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000762 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000763 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000764 Nodify(Dst, U, N1,
765 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
766 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000767 }
768
769 break;
770 }
771
Ted Kremenek64924852008-01-31 02:35:41 +0000772 case UnaryOperator::AddrOf: {
773 const LValue& L1 = GetLValue(St, U->getSubExpr());
774 Nodify(Dst, U, N1, SetValue(St, U, L1));
775 break;
776 }
777
778 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000779 // FIXME: Stop when dereferencing an uninitialized value.
780 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
781
782 const RValue& V = GetValue(St, U->getSubExpr());
783 const LValue& L1 = cast<LValue>(V);
784
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000785 // After a dereference, one of two possible situations arise:
786 // (1) A crash, because the pointer was NULL.
787 // (2) The pointer is not NULL, and the dereference works.
788 //
789 // We add these assumptions.
790
Ted Kremenek63a4f692008-02-07 06:04:18 +0000791 bool isFeasibleNotNull;
792
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000793 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000794 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
795
796 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000797 QualType T = U->getType();
798 Nodify(Dst, U, N1, SetValue(StNotNull, U,
799 GetValue(StNotNull, L1, &T)));
800 }
801
Ted Kremenek63a4f692008-02-07 06:04:18 +0000802 bool isFeasibleNull;
803
804 // "Assume" that the pointer is NULL.
805 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
806
807 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000808 // We don't use "Nodify" here because the node will be a sink
809 // and we have no intention of processing it later.
810 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
811
Ted Kremenek63a4f692008-02-07 06:04:18 +0000812 if (NullNode) {
813 NullNode->markAsSink();
814
815 if (isFeasibleNotNull)
816 ImplicitNullDeref.insert(NullNode);
817 else
818 ExplicitNullDeref.insert(NullNode);
819 }
820 }
821
Ted Kremenek64924852008-01-31 02:35:41 +0000822 break;
823 }
824
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000825 default: ;
826 assert (false && "Not implemented.");
827 }
828 }
829}
830
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000831void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
832 GRConstants::NodeSet& Dst) {
833
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000834 if (isa<DeclRefExpr>(E)) {
835 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000836 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000837 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000838
839 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
840 if (U->getOpcode() == UnaryOperator::Deref) {
841 Visit(U->getSubExpr(), Pred, Dst);
842 return;
843 }
844 }
845
846 Visit(E, Pred, Dst);
847}
848
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000849void GRConstants::VisitBinaryOperator(BinaryOperator* B,
850 GRConstants::NodeTy* Pred,
851 GRConstants::NodeSet& Dst) {
852 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000853
854 if (B->isAssignmentOp())
855 VisitAssignmentLHS(B->getLHS(), Pred, S1);
856 else
857 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000858
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000859 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
860 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000861
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000862 // When getting the value for the LHS, check if we are in an assignment.
863 // In such cases, we want to (initially) treat the LHS as an LValue,
864 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000865 // evaluated to LValueDecl's instead of to an NonLValue.
866 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000867 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
868 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000869
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000870 NodeSet S2;
871 Visit(B->getRHS(), N1, S2);
872
873 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000874
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000875 NodeTy* N2 = *I2;
876 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000877 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000878
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000879 BinaryOperator::Opcode Op = B->getOpcode();
880
881 if (Op <= BinaryOperator::Or) {
882
Ted Kremenek22031182008-02-08 02:57:34 +0000883 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000884 Nodify(Dst, B, N2, SetValue(St, B, V1));
885 continue;
886 }
887
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000888 if (isa<LValue>(V1)) {
889 // FIXME: Add support for RHS being a non-lvalue.
890 const LValue& L1 = cast<LValue>(V1);
891 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000892
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000893 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
894 }
895 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000896 const NonLValue& R1 = cast<NonLValue>(V1);
897 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000898
899 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000900 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000901
902 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000903
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000904 }
905
906 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000907 case BinaryOperator::Assign: {
908 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000909 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000910 break;
911 }
912
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000913 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000914
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000915 assert (B->isCompoundAssignmentOp());
916
917 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000918 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000919
Ted Kremenekda9bd092008-02-08 07:05:39 +0000920 if (Op >= BinaryOperator::AndAssign)
921 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
922 else
923 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000924
925 if (isa<LValue>(V2)) {
926 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000927 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000928 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000929 }
930 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000931 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000932 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000933 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000934 }
935
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000936 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000937 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000938 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000939 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000940 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000941 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000942}
Ted Kremenekee985462008-01-16 18:18:48 +0000943
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000944
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000945void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
946 GRConstants::NodeSet& Dst) {
947
948 // FIXME: add metadata to the CFG so that we can disable
949 // this check when we KNOW that there is no block-level subexpression.
950 // The motivation is that this check requires a hashtable lookup.
951
952 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
953 Dst.Add(Pred);
954 return;
955 }
956
957 switch (S->getStmtClass()) {
958 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000959
960 if (cast<BinaryOperator>(S)->isLogicalOp()) {
961 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
962 break;
963 }
Ted Kremenekda9bd092008-02-08 07:05:39 +0000964 else if (cast<BinaryOperator>(S)->getOpcode() == BinaryOperator::Comma) {
965 StateTy St = Pred->getState();
966 Stmt* LastStmt = cast<BinaryOperator>(S)->getRHS();
967 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
968 break;
969 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000970
971 // Fall-through.
972
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000973 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000974 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
975 break;
976
Ted Kremenekda9bd092008-02-08 07:05:39 +0000977 case Stmt::StmtExprClass: {
978 StateTy St = Pred->getState();
979 Stmt* LastStmt = *(cast<StmtExpr>(S)->getSubStmt()->body_rbegin());
980 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
981 break;
982 }
983
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000984 case Stmt::UnaryOperatorClass:
985 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
986 break;
987
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000988 case Stmt::ParenExprClass:
989 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
990 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000991
992 case Stmt::DeclRefExprClass:
993 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
994 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000995
Ted Kremenek874d63f2008-01-24 02:02:54 +0000996 case Stmt::ImplicitCastExprClass: {
997 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
998 VisitCast(C, C->getSubExpr(), Pred, Dst);
999 break;
1000 }
1001
1002 case Stmt::CastExprClass: {
1003 CastExpr* C = cast<CastExpr>(S);
1004 VisitCast(C, C->getSubExpr(), Pred, Dst);
1005 break;
1006 }
1007
Ted Kremenekf233d482008-02-05 00:26:40 +00001008 case Stmt::ConditionalOperatorClass: { // '?' operator
1009 ConditionalOperator* C = cast<ConditionalOperator>(S);
1010 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1011 break;
1012 }
1013
1014 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1015 ChooseExpr* C = cast<ChooseExpr>(S);
1016 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1017 break;
1018 }
1019
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001020 case Stmt::ReturnStmtClass:
1021 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1022 Visit(R, Pred, Dst);
1023 else
1024 Dst.Add(Pred);
1025
1026 break;
1027
Ted Kremenek9de04c42008-01-24 20:55:43 +00001028 case Stmt::DeclStmtClass:
1029 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1030 break;
1031
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001032 default:
1033 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1034 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001035 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001036}
1037
Ted Kremenekee985462008-01-16 18:18:48 +00001038//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001039// "Assume" logic.
1040//===----------------------------------------------------------------------===//
1041
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001042GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1043 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001044 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001045
1046 switch (Cond.getSubKind()) {
1047 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001048 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001049 return St;
1050
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001051 case lval::SymbolValKind:
1052 if (Assumption)
1053 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1054 ValMgr.getZeroWithPtrWidth(), isFeasible);
1055 else
1056 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1057 ValMgr.getZeroWithPtrWidth(), isFeasible);
1058
Ted Kremenek08b66252008-02-06 04:31:33 +00001059
Ted Kremenek329f8542008-02-05 21:52:21 +00001060 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001061 isFeasible = Assumption;
1062 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001063
Ted Kremenek329f8542008-02-05 21:52:21 +00001064 case lval::ConcreteIntKind: {
1065 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001066 isFeasible = b ? Assumption : !Assumption;
1067 return St;
1068 }
1069 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001070}
1071
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001072GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1073 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001074 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001075
1076 switch (Cond.getSubKind()) {
1077 default:
1078 assert (false && "'Assume' not implemented for this NonLValue.");
1079 return St;
1080
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001081
1082 case nonlval::SymbolValKind: {
1083 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1084 SymbolID sym = SV.getSymbol();
1085
1086 if (Assumption)
1087 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1088 isFeasible);
1089 else
1090 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1091 isFeasible);
1092 }
1093
Ted Kremenek08b66252008-02-06 04:31:33 +00001094 case nonlval::SymIntConstraintValKind:
1095 return
1096 AssumeSymInt(St, Assumption,
1097 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1098 isFeasible);
1099
Ted Kremenek329f8542008-02-05 21:52:21 +00001100 case nonlval::ConcreteIntKind: {
1101 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001102 isFeasible = b ? Assumption : !Assumption;
1103 return St;
1104 }
1105 }
1106}
1107
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001108GRConstants::StateTy
1109GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1110 const llvm::APSInt& V, bool& isFeasible) {
1111
1112 // First, determine if sym == X, where X != V.
1113 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1114 isFeasible = *X != V;
1115 return St;
1116 }
1117
1118 // Second, determine if sym != V.
1119 if (St.isNotEqual(sym, V)) {
1120 isFeasible = true;
1121 return St;
1122 }
1123
1124 // If we reach here, sym is not a constant and we don't know if it is != V.
1125 // Make that assumption.
1126
1127 isFeasible = true;
1128 return StateMgr.AddNE(St, sym, V);
1129}
1130
1131GRConstants::StateTy
1132GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1133 const llvm::APSInt& V, bool& isFeasible) {
1134
1135 // First, determine if sym == X, where X != V.
1136 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1137 isFeasible = *X == V;
1138 return St;
1139 }
1140
1141 // Second, determine if sym != V.
1142 if (St.isNotEqual(sym, V)) {
1143 isFeasible = false;
1144 return St;
1145 }
1146
1147 // If we reach here, sym is not a constant and we don't know if it is == V.
1148 // Make that assumption.
1149
1150 isFeasible = true;
1151 return StateMgr.AddEQ(St, sym, V);
1152}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001153
Ted Kremenek08b66252008-02-06 04:31:33 +00001154GRConstants::StateTy
1155GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1156 const SymIntConstraint& C, bool& isFeasible) {
1157
1158 switch (C.getOpcode()) {
1159 default:
1160 // No logic yet for other operators.
1161 return St;
1162
1163 case BinaryOperator::EQ:
1164 if (Assumption)
1165 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1166 else
1167 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1168
1169 case BinaryOperator::NE:
1170 if (Assumption)
1171 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1172 else
1173 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1174 }
1175}
1176
Ted Kremenekb38911f2008-01-30 23:03:39 +00001177//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001178// Driver.
1179//===----------------------------------------------------------------------===//
1180
Ted Kremenekaa66a322008-01-16 21:46:15 +00001181#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001182static GRConstants* GraphPrintCheckerState;
1183
Ted Kremenekaa66a322008-01-16 21:46:15 +00001184namespace llvm {
1185template<>
1186struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1187 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001188
Ted Kremenek9153f732008-02-05 07:17:49 +00001189 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001190 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001191 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1192 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1193 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1194 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001195 }
1196 }
1197
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001198 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001199 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001200 bool isFirst = true;
1201
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001202 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001203 if (I.getKey().getKind() != kind)
1204 continue;
1205
1206 if (isFirst) {
1207 if (!isFirstGroup) Out << "\\l\\l";
1208 PrintKindLabel(Out, kind);
1209 isFirst = false;
1210 }
1211 else
1212 Out << "\\l";
1213
1214 Out << ' ';
1215
1216 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1217 Out << V->getName();
1218 else {
1219 Stmt* E = cast<Stmt>(I.getKey());
1220 Out << " (" << (void*) E << ") ";
1221 E->printPretty(Out);
1222 }
1223
1224 Out << " : ";
1225 I.getData().print(Out);
1226 }
1227 }
1228
Ted Kremeneked4de312008-02-06 03:56:15 +00001229 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1230 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1231
1232 if (CE.isEmpty())
1233 return;
1234
1235 Out << "\\l\\|'==' constraints:";
1236
1237 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1238 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1239 }
1240
1241 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1242 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1243
1244 if (NE.isEmpty())
1245 return;
1246
1247 Out << "\\l\\|'!=' constraints:";
1248
1249 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1250 I != EI; ++I){
1251
1252 Out << "\\l $" << I.getKey() << " : ";
1253 bool isFirst = true;
1254
1255 ValueState::IntSetTy::iterator J=I.getData().begin(),
1256 EJ=I.getData().end();
1257 for ( ; J != EJ; ++J) {
1258 if (isFirst) isFirst = false;
1259 else Out << ", ";
1260
1261 Out << (*J)->toString();
1262 }
1263 }
1264 }
1265
Ted Kremenekaa66a322008-01-16 21:46:15 +00001266 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1267 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001268
1269 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001270 ProgramPoint Loc = N->getLocation();
1271
1272 switch (Loc.getKind()) {
1273 case ProgramPoint::BlockEntranceKind:
1274 Out << "Block Entrance: B"
1275 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1276 break;
1277
1278 case ProgramPoint::BlockExitKind:
1279 assert (false);
1280 break;
1281
1282 case ProgramPoint::PostStmtKind: {
1283 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001284 Out << L.getStmt()->getStmtClassName() << ':'
1285 << (void*) L.getStmt() << ' ';
1286
Ted Kremenekaa66a322008-01-16 21:46:15 +00001287 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001288
1289 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1290 Out << "\\|Implicit-Null Dereference.\\l";
1291 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001292 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1293 Out << "\\|Explicit-Null Dereference.\\l";
1294 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001295
Ted Kremenekaa66a322008-01-16 21:46:15 +00001296 break;
1297 }
1298
1299 default: {
1300 const BlockEdge& E = cast<BlockEdge>(Loc);
1301 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1302 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001303
1304 if (Stmt* T = E.getSrc()->getTerminator()) {
1305 Out << "\\|Terminator: ";
1306 E.getSrc()->printTerminator(Out);
1307
1308 if (isa<SwitchStmt>(T)) {
1309 // FIXME
1310 }
1311 else {
1312 Out << "\\lCondition: ";
1313 if (*E.getSrc()->succ_begin() == E.getDst())
1314 Out << "true";
1315 else
1316 Out << "false";
1317 }
1318
1319 Out << "\\l";
1320 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001321
1322 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1323 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1324 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001325 }
1326 }
1327
Ted Kremenek9153f732008-02-05 07:17:49 +00001328 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001329
Ted Kremenek9153f732008-02-05 07:17:49 +00001330 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1331 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1332 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001333
1334 PrintEQ(Out, N->getState());
1335 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001336
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001337 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001338 return Out.str();
1339 }
1340};
1341} // end llvm namespace
1342#endif
1343
Ted Kremenekee985462008-01-16 18:18:48 +00001344namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001345void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1346 Diagnostic& Diag) {
1347
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001348 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001349 Engine.ExecuteWorkList();
1350
1351 // Look for explicit-Null dereferences and warn about them.
1352 GRConstants* CheckerState = &Engine.getCheckerState();
1353
1354 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1355 E=CheckerState->null_end(); I!=E; ++I) {
1356
1357 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1358 Expr* E = cast<Expr>(L.getStmt());
1359
1360 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1361 diag::chkr_null_deref_after_check);
1362 }
1363
1364
Ted Kremenekaa66a322008-01-16 21:46:15 +00001365#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001366 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001367 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001368 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001369#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001370}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001371} // end clang namespace