blob: 75edb911e637174c30a3b46f1680ece48ace8b3d [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 Kremenek071679d2008-02-08 19:08:13 +0000523 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000524
525 // Remove old bindings for subexpressions.
526 if (I.getKey().isSubExpr()) {
Ted Kremenek65cac132008-01-29 05:25:31 +0000527 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000528 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000529 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000530
531 if (I.getKey().isBlkExpr()) {
532 if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) {
533 if (isa<lval::DeclVal>(I.getData())) {
534 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
535 WList.push_back(LV.getDecl());
536 }
537 }
538 else
539 M = StateMgr.Remove(M, I.getKey());
540
541 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000542 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000543
544 assert (I.getKey().isDecl());
545
546 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
547 if (Liveness.isLive(Loc, V))
548 WList.push_back(V);
Ted Kremenek65cac132008-01-29 05:25:31 +0000549 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000550
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000551 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
552
553 while (!WList.empty()) {
554 ValueDecl* V = WList.back();
555 WList.pop_back();
556
557 if (Marked.count(V))
558 continue;
559
560 Marked.insert(V);
561
562 if (V->getType()->isPointerType()) {
563 const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V)));
564
565 if (!isa<lval::DeclVal>(LV))
566 continue;
567
568 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
569 WList.push_back(LVD.getDecl());
570 }
571 }
572
573 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I)
574 if (I.getKey().isDecl())
575 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
576 if (!Marked.count(V))
577 M = StateMgr.Remove(M, V);
578
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000579 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000580}
581
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000582GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000583GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000584
585 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000586 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000587 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000588
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000589 NodeTy* N = Builder->generateNode(S, St, Pred);
590 Dst.Add(N);
591 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000592}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000593
Ted Kremenekcba2e432008-02-05 19:35:18 +0000594void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
595 const StateTy::BufferTy& SB) {
596
597 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
598 Nodify(Dst, S, Pred, *I);
599}
600
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000601void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
602 if (D != CurrentStmt) {
603 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
604 return;
605 }
606
607 // If we are here, we are loading the value of the decl and binding
608 // it to the block-level expression.
609
610 StateTy St = Pred->getState();
611
612 Nodify(Dst, D, Pred,
613 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
614}
615
Ted Kremenekcba2e432008-02-05 19:35:18 +0000616void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000617
618 QualType T = CastE->getType();
619
620 // Check for redundant casts.
621 if (E->getType() == T) {
622 Dst.Add(Pred);
623 return;
624 }
625
626 NodeSet S1;
627 Visit(E, Pred, S1);
628
629 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
630 NodeTy* N = *I1;
631 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000632 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000633 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000634 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000635}
636
637void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
638 GRConstants::NodeSet& Dst) {
639
640 StateTy St = Pred->getState();
641
642 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000643 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
644 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000645 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000646 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000647 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000648
649 Nodify(Dst, DS, Pred, St);
650
651 if (Dst.empty())
652 Dst.Add(Pred);
653}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000654
Ted Kremenekf233d482008-02-05 00:26:40 +0000655
656void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
657 NodeTy* Pred, NodeSet& Dst) {
658
659 StateTy St = Pred->getState();
660
661 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000662 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000663
664 Nodify(Dst, S, Pred, SetValue(St, S, R));
665}
666
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000667void GRConstants::VisitUnaryOperator(UnaryOperator* U,
668 GRConstants::NodeTy* Pred,
669 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000670
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000671 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000672 UnaryOperator::Opcode Op = U->getOpcode();
673
674 // FIXME: This is a hack so that for '*' and '&' we don't recurse
675 // on visiting the subexpression if it is a DeclRefExpr. We should
676 // probably just handle AddrOf and Deref in their own methods to make
677 // this cleaner.
678 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
679 isa<DeclRefExpr>(U->getSubExpr()))
680 S1.Add(Pred);
681 else
682 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000683
684 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
685 NodeTy* N1 = *I1;
686 StateTy St = N1->getState();
687
688 switch (U->getOpcode()) {
689 case UnaryOperator::PostInc: {
690 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000691 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000692
693 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
694 GetRValueConstant(1U, U));
695
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000696 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
697 break;
698 }
699
700 case UnaryOperator::PostDec: {
701 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000702 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000703
704 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
705 GetRValueConstant(1U, U));
706
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000707 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
708 break;
709 }
710
711 case UnaryOperator::PreInc: {
712 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000713 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000714
715 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
716 GetRValueConstant(1U, U));
717
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000718 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
719 break;
720 }
721
722 case UnaryOperator::PreDec: {
723 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000724 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000725
726 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
727 GetRValueConstant(1U, U));
728
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000729 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
730 break;
731 }
732
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000733 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000734 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000735 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000736 break;
737 }
738
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000739 case UnaryOperator::Not: {
740 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000741 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000742 break;
743 }
744
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000745 case UnaryOperator::LNot: {
746 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
747 //
748 // Note: technically we do "E == 0", but this is the same in the
749 // transfer functions as "0 == E".
750
751 RValue V1 = GetValue(St, U->getSubExpr());
752
753 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000754 const LValue& L1 = cast<LValue>(V1);
755 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
756 Nodify(Dst, U, N1,
757 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
758 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000759 }
760 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000761 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000762 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000763 Nodify(Dst, U, N1,
764 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
765 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000766 }
767
768 break;
769 }
770
Ted Kremenek64924852008-01-31 02:35:41 +0000771 case UnaryOperator::AddrOf: {
772 const LValue& L1 = GetLValue(St, U->getSubExpr());
773 Nodify(Dst, U, N1, SetValue(St, U, L1));
774 break;
775 }
776
777 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000778 // FIXME: Stop when dereferencing an uninitialized value.
779 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
780
781 const RValue& V = GetValue(St, U->getSubExpr());
782 const LValue& L1 = cast<LValue>(V);
783
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000784 // After a dereference, one of two possible situations arise:
785 // (1) A crash, because the pointer was NULL.
786 // (2) The pointer is not NULL, and the dereference works.
787 //
788 // We add these assumptions.
789
Ted Kremenek63a4f692008-02-07 06:04:18 +0000790 bool isFeasibleNotNull;
791
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000792 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000793 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
794
795 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000796 QualType T = U->getType();
797 Nodify(Dst, U, N1, SetValue(StNotNull, U,
798 GetValue(StNotNull, L1, &T)));
799 }
800
Ted Kremenek63a4f692008-02-07 06:04:18 +0000801 bool isFeasibleNull;
802
803 // "Assume" that the pointer is NULL.
804 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
805
806 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000807 // We don't use "Nodify" here because the node will be a sink
808 // and we have no intention of processing it later.
809 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
810
Ted Kremenek63a4f692008-02-07 06:04:18 +0000811 if (NullNode) {
812 NullNode->markAsSink();
813
814 if (isFeasibleNotNull)
815 ImplicitNullDeref.insert(NullNode);
816 else
817 ExplicitNullDeref.insert(NullNode);
818 }
819 }
820
Ted Kremenek64924852008-01-31 02:35:41 +0000821 break;
822 }
823
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000824 default: ;
825 assert (false && "Not implemented.");
826 }
827 }
828}
829
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000830void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
831 GRConstants::NodeSet& Dst) {
832
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000833 if (isa<DeclRefExpr>(E)) {
834 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000835 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000836 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000837
838 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
839 if (U->getOpcode() == UnaryOperator::Deref) {
840 Visit(U->getSubExpr(), Pred, Dst);
841 return;
842 }
843 }
844
845 Visit(E, Pred, Dst);
846}
847
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000848void GRConstants::VisitBinaryOperator(BinaryOperator* B,
849 GRConstants::NodeTy* Pred,
850 GRConstants::NodeSet& Dst) {
851 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000852
853 if (B->isAssignmentOp())
854 VisitAssignmentLHS(B->getLHS(), Pred, S1);
855 else
856 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000857
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000858 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
859 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000860
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861 // When getting the value for the LHS, check if we are in an assignment.
862 // In such cases, we want to (initially) treat the LHS as an LValue,
863 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000864 // evaluated to LValueDecl's instead of to an NonLValue.
865 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000866 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
867 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000868
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000869 NodeSet S2;
870 Visit(B->getRHS(), N1, S2);
871
872 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000873
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000874 NodeTy* N2 = *I2;
875 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000876 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000877
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000878 BinaryOperator::Opcode Op = B->getOpcode();
879
880 if (Op <= BinaryOperator::Or) {
881
Ted Kremenek22031182008-02-08 02:57:34 +0000882 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000883 Nodify(Dst, B, N2, SetValue(St, B, V1));
884 continue;
885 }
886
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000887 if (isa<LValue>(V1)) {
888 // FIXME: Add support for RHS being a non-lvalue.
889 const LValue& L1 = cast<LValue>(V1);
890 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000891
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000892 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
893 }
894 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000895 const NonLValue& R1 = cast<NonLValue>(V1);
896 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000897
898 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000899 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000900
901 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000902
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000903 }
904
905 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000906 case BinaryOperator::Assign: {
907 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000908 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000909 break;
910 }
911
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000912 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000913
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000914 assert (B->isCompoundAssignmentOp());
915
916 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000917 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000918
Ted Kremenekda9bd092008-02-08 07:05:39 +0000919 if (Op >= BinaryOperator::AndAssign)
920 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
921 else
922 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000923
924 if (isa<LValue>(V2)) {
925 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000926 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000927 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000928 }
929 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000930 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000931 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000932 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000933 }
934
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000935 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000936 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000937 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000938 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000939 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000940 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000941}
Ted Kremenekee985462008-01-16 18:18:48 +0000942
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000943
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000944void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
945 GRConstants::NodeSet& Dst) {
946
947 // FIXME: add metadata to the CFG so that we can disable
948 // this check when we KNOW that there is no block-level subexpression.
949 // The motivation is that this check requires a hashtable lookup.
950
951 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
952 Dst.Add(Pred);
953 return;
954 }
955
956 switch (S->getStmtClass()) {
957 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000958
959 if (cast<BinaryOperator>(S)->isLogicalOp()) {
960 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
961 break;
962 }
Ted Kremenekda9bd092008-02-08 07:05:39 +0000963 else if (cast<BinaryOperator>(S)->getOpcode() == BinaryOperator::Comma) {
964 StateTy St = Pred->getState();
965 Stmt* LastStmt = cast<BinaryOperator>(S)->getRHS();
966 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
967 break;
968 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000969
970 // Fall-through.
971
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000972 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000973 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
974 break;
975
Ted Kremenekda9bd092008-02-08 07:05:39 +0000976 case Stmt::StmtExprClass: {
977 StateTy St = Pred->getState();
978 Stmt* LastStmt = *(cast<StmtExpr>(S)->getSubStmt()->body_rbegin());
979 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
980 break;
981 }
982
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000983 case Stmt::UnaryOperatorClass:
984 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
985 break;
986
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000987 case Stmt::ParenExprClass:
988 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
989 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000990
991 case Stmt::DeclRefExprClass:
992 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
993 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000994
Ted Kremenek874d63f2008-01-24 02:02:54 +0000995 case Stmt::ImplicitCastExprClass: {
996 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
997 VisitCast(C, C->getSubExpr(), Pred, Dst);
998 break;
999 }
1000
1001 case Stmt::CastExprClass: {
1002 CastExpr* C = cast<CastExpr>(S);
1003 VisitCast(C, C->getSubExpr(), Pred, Dst);
1004 break;
1005 }
1006
Ted Kremenekf233d482008-02-05 00:26:40 +00001007 case Stmt::ConditionalOperatorClass: { // '?' operator
1008 ConditionalOperator* C = cast<ConditionalOperator>(S);
1009 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1010 break;
1011 }
1012
1013 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1014 ChooseExpr* C = cast<ChooseExpr>(S);
1015 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1016 break;
1017 }
1018
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001019 case Stmt::ReturnStmtClass:
1020 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1021 Visit(R, Pred, Dst);
1022 else
1023 Dst.Add(Pred);
1024
1025 break;
1026
Ted Kremenek9de04c42008-01-24 20:55:43 +00001027 case Stmt::DeclStmtClass:
1028 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1029 break;
1030
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001031 default:
1032 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1033 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001034 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001035}
1036
Ted Kremenekee985462008-01-16 18:18:48 +00001037//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001038// "Assume" logic.
1039//===----------------------------------------------------------------------===//
1040
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001041GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1042 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001043 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001044
1045 switch (Cond.getSubKind()) {
1046 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001047 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001048 return St;
1049
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001050 case lval::SymbolValKind:
1051 if (Assumption)
1052 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1053 ValMgr.getZeroWithPtrWidth(), isFeasible);
1054 else
1055 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1056 ValMgr.getZeroWithPtrWidth(), isFeasible);
1057
Ted Kremenek08b66252008-02-06 04:31:33 +00001058
Ted Kremenek329f8542008-02-05 21:52:21 +00001059 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001060 isFeasible = Assumption;
1061 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001062
Ted Kremenek329f8542008-02-05 21:52:21 +00001063 case lval::ConcreteIntKind: {
1064 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001065 isFeasible = b ? Assumption : !Assumption;
1066 return St;
1067 }
1068 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001069}
1070
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001071GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1072 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001073 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001074
1075 switch (Cond.getSubKind()) {
1076 default:
1077 assert (false && "'Assume' not implemented for this NonLValue.");
1078 return St;
1079
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001080
1081 case nonlval::SymbolValKind: {
1082 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1083 SymbolID sym = SV.getSymbol();
1084
1085 if (Assumption)
1086 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1087 isFeasible);
1088 else
1089 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1090 isFeasible);
1091 }
1092
Ted Kremenek08b66252008-02-06 04:31:33 +00001093 case nonlval::SymIntConstraintValKind:
1094 return
1095 AssumeSymInt(St, Assumption,
1096 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1097 isFeasible);
1098
Ted Kremenek329f8542008-02-05 21:52:21 +00001099 case nonlval::ConcreteIntKind: {
1100 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001101 isFeasible = b ? Assumption : !Assumption;
1102 return St;
1103 }
1104 }
1105}
1106
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001107GRConstants::StateTy
1108GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1109 const llvm::APSInt& V, bool& isFeasible) {
1110
1111 // First, determine if sym == X, where X != V.
1112 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1113 isFeasible = *X != V;
1114 return St;
1115 }
1116
1117 // Second, determine if sym != V.
1118 if (St.isNotEqual(sym, V)) {
1119 isFeasible = true;
1120 return St;
1121 }
1122
1123 // If we reach here, sym is not a constant and we don't know if it is != V.
1124 // Make that assumption.
1125
1126 isFeasible = true;
1127 return StateMgr.AddNE(St, sym, V);
1128}
1129
1130GRConstants::StateTy
1131GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1132 const llvm::APSInt& V, bool& isFeasible) {
1133
1134 // First, determine if sym == X, where X != V.
1135 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1136 isFeasible = *X == V;
1137 return St;
1138 }
1139
1140 // Second, determine if sym != V.
1141 if (St.isNotEqual(sym, V)) {
1142 isFeasible = false;
1143 return St;
1144 }
1145
1146 // If we reach here, sym is not a constant and we don't know if it is == V.
1147 // Make that assumption.
1148
1149 isFeasible = true;
1150 return StateMgr.AddEQ(St, sym, V);
1151}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001152
Ted Kremenek08b66252008-02-06 04:31:33 +00001153GRConstants::StateTy
1154GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1155 const SymIntConstraint& C, bool& isFeasible) {
1156
1157 switch (C.getOpcode()) {
1158 default:
1159 // No logic yet for other operators.
1160 return St;
1161
1162 case BinaryOperator::EQ:
1163 if (Assumption)
1164 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1165 else
1166 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1167
1168 case BinaryOperator::NE:
1169 if (Assumption)
1170 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1171 else
1172 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1173 }
1174}
1175
Ted Kremenekb38911f2008-01-30 23:03:39 +00001176//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001177// Driver.
1178//===----------------------------------------------------------------------===//
1179
Ted Kremenekaa66a322008-01-16 21:46:15 +00001180#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001181static GRConstants* GraphPrintCheckerState;
1182
Ted Kremenekaa66a322008-01-16 21:46:15 +00001183namespace llvm {
1184template<>
1185struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1186 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001187
Ted Kremenek9153f732008-02-05 07:17:49 +00001188 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001189 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001190 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1191 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1192 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1193 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001194 }
1195 }
1196
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001197 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001198 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001199 bool isFirst = true;
1200
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001201 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001202 if (I.getKey().getKind() != kind)
1203 continue;
1204
1205 if (isFirst) {
1206 if (!isFirstGroup) Out << "\\l\\l";
1207 PrintKindLabel(Out, kind);
1208 isFirst = false;
1209 }
1210 else
1211 Out << "\\l";
1212
1213 Out << ' ';
1214
1215 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1216 Out << V->getName();
1217 else {
1218 Stmt* E = cast<Stmt>(I.getKey());
1219 Out << " (" << (void*) E << ") ";
1220 E->printPretty(Out);
1221 }
1222
1223 Out << " : ";
1224 I.getData().print(Out);
1225 }
1226 }
1227
Ted Kremeneked4de312008-02-06 03:56:15 +00001228 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1229 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1230
1231 if (CE.isEmpty())
1232 return;
1233
1234 Out << "\\l\\|'==' constraints:";
1235
1236 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1237 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1238 }
1239
1240 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1241 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1242
1243 if (NE.isEmpty())
1244 return;
1245
1246 Out << "\\l\\|'!=' constraints:";
1247
1248 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1249 I != EI; ++I){
1250
1251 Out << "\\l $" << I.getKey() << " : ";
1252 bool isFirst = true;
1253
1254 ValueState::IntSetTy::iterator J=I.getData().begin(),
1255 EJ=I.getData().end();
1256 for ( ; J != EJ; ++J) {
1257 if (isFirst) isFirst = false;
1258 else Out << ", ";
1259
1260 Out << (*J)->toString();
1261 }
1262 }
1263 }
1264
Ted Kremenekaa66a322008-01-16 21:46:15 +00001265 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1266 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001267
1268 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001269 ProgramPoint Loc = N->getLocation();
1270
1271 switch (Loc.getKind()) {
1272 case ProgramPoint::BlockEntranceKind:
1273 Out << "Block Entrance: B"
1274 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1275 break;
1276
1277 case ProgramPoint::BlockExitKind:
1278 assert (false);
1279 break;
1280
1281 case ProgramPoint::PostStmtKind: {
1282 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001283 Out << L.getStmt()->getStmtClassName() << ':'
1284 << (void*) L.getStmt() << ' ';
1285
Ted Kremenekaa66a322008-01-16 21:46:15 +00001286 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001287
1288 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1289 Out << "\\|Implicit-Null Dereference.\\l";
1290 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001291 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1292 Out << "\\|Explicit-Null Dereference.\\l";
1293 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001294
Ted Kremenekaa66a322008-01-16 21:46:15 +00001295 break;
1296 }
1297
1298 default: {
1299 const BlockEdge& E = cast<BlockEdge>(Loc);
1300 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1301 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001302
1303 if (Stmt* T = E.getSrc()->getTerminator()) {
1304 Out << "\\|Terminator: ";
1305 E.getSrc()->printTerminator(Out);
1306
1307 if (isa<SwitchStmt>(T)) {
1308 // FIXME
1309 }
1310 else {
1311 Out << "\\lCondition: ";
1312 if (*E.getSrc()->succ_begin() == E.getDst())
1313 Out << "true";
1314 else
1315 Out << "false";
1316 }
1317
1318 Out << "\\l";
1319 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001320
1321 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1322 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1323 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001324 }
1325 }
1326
Ted Kremenek9153f732008-02-05 07:17:49 +00001327 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001328
Ted Kremenek9153f732008-02-05 07:17:49 +00001329 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1330 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1331 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001332
1333 PrintEQ(Out, N->getState());
1334 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001335
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001336 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001337 return Out.str();
1338 }
1339};
1340} // end llvm namespace
1341#endif
1342
Ted Kremenekee985462008-01-16 18:18:48 +00001343namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001344void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1345 Diagnostic& Diag) {
1346
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001347 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001348 Engine.ExecuteWorkList();
1349
1350 // Look for explicit-Null dereferences and warn about them.
1351 GRConstants* CheckerState = &Engine.getCheckerState();
1352
1353 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1354 E=CheckerState->null_end(); I!=E; ++I) {
1355
1356 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1357 Expr* E = cast<Expr>(L.getStmt());
1358
1359 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1360 diag::chkr_null_deref_after_check);
1361 }
1362
1363
Ted Kremenekaa66a322008-01-16 21:46:15 +00001364#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001365 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001366 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001367 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001368#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001369}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001370} // end clang namespace