blob: 858ed9360a50f0e74b21dbb0613e370ec4634360 [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
335 if (!LV.isValid())
336 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
393 case RValue::InvalidKind:
394 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
442 if (isa<InvalidValue>(R1) &&
443 (isa<InvalidValue>(R2) ||
444 isa<UninitializedValue>(R2))) {
445
446 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
447 return;
448 }
449 else if (isa<UninitializedValue>(R1)) {
450 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 Kremenek403c1812008-01-28 22:51:57 +0000647 E ? GetValue(St, E) : UninitializedValue());
648 }
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);
663 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
664
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 Kremenek5b6dc2d2008-02-07 01:08:27 +0000883 if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) {
884 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);
918 RValue Result = cast<NonLValue>(InvalidValue());
919
920 Op = (BinaryOperator::Opcode)
921 (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign));
922
923 if (isa<LValue>(V2)) {
924 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000925 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000926 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000927 }
928 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000929 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000930 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000931 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000932 }
933
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000934 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000935 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000936 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000937 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000938 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000939 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000940}
Ted Kremenekee985462008-01-16 18:18:48 +0000941
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000942
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000943void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
944 GRConstants::NodeSet& Dst) {
945
946 // FIXME: add metadata to the CFG so that we can disable
947 // this check when we KNOW that there is no block-level subexpression.
948 // The motivation is that this check requires a hashtable lookup.
949
950 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
951 Dst.Add(Pred);
952 return;
953 }
954
955 switch (S->getStmtClass()) {
956 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000957
958 if (cast<BinaryOperator>(S)->isLogicalOp()) {
959 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
960 break;
961 }
962
963 // Fall-through.
964
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000965 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000966 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
967 break;
968
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000969 case Stmt::UnaryOperatorClass:
970 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
971 break;
972
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000973 case Stmt::ParenExprClass:
974 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
975 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000976
977 case Stmt::DeclRefExprClass:
978 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
979 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000980
Ted Kremenek874d63f2008-01-24 02:02:54 +0000981 case Stmt::ImplicitCastExprClass: {
982 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
983 VisitCast(C, C->getSubExpr(), Pred, Dst);
984 break;
985 }
986
987 case Stmt::CastExprClass: {
988 CastExpr* C = cast<CastExpr>(S);
989 VisitCast(C, C->getSubExpr(), Pred, Dst);
990 break;
991 }
992
Ted Kremenekf233d482008-02-05 00:26:40 +0000993 case Stmt::ConditionalOperatorClass: { // '?' operator
994 ConditionalOperator* C = cast<ConditionalOperator>(S);
995 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
996 break;
997 }
998
999 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1000 ChooseExpr* C = cast<ChooseExpr>(S);
1001 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1002 break;
1003 }
1004
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001005 case Stmt::ReturnStmtClass:
1006 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1007 Visit(R, Pred, Dst);
1008 else
1009 Dst.Add(Pred);
1010
1011 break;
1012
Ted Kremenek9de04c42008-01-24 20:55:43 +00001013 case Stmt::DeclStmtClass:
1014 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1015 break;
1016
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001017 default:
1018 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1019 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001020 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001021}
1022
Ted Kremenekee985462008-01-16 18:18:48 +00001023//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001024// "Assume" logic.
1025//===----------------------------------------------------------------------===//
1026
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001027GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1028 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001029 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001030
1031 switch (Cond.getSubKind()) {
1032 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001033 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001034 return St;
1035
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001036 case lval::SymbolValKind:
1037 if (Assumption)
1038 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1039 ValMgr.getZeroWithPtrWidth(), isFeasible);
1040 else
1041 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1042 ValMgr.getZeroWithPtrWidth(), isFeasible);
1043
Ted Kremenek08b66252008-02-06 04:31:33 +00001044
Ted Kremenek329f8542008-02-05 21:52:21 +00001045 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001046 isFeasible = Assumption;
1047 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001048
Ted Kremenek329f8542008-02-05 21:52:21 +00001049 case lval::ConcreteIntKind: {
1050 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001051 isFeasible = b ? Assumption : !Assumption;
1052 return St;
1053 }
1054 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001055}
1056
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001057GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1058 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001059 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001060
1061 switch (Cond.getSubKind()) {
1062 default:
1063 assert (false && "'Assume' not implemented for this NonLValue.");
1064 return St;
1065
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001066
1067 case nonlval::SymbolValKind: {
1068 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1069 SymbolID sym = SV.getSymbol();
1070
1071 if (Assumption)
1072 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1073 isFeasible);
1074 else
1075 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1076 isFeasible);
1077 }
1078
Ted Kremenek08b66252008-02-06 04:31:33 +00001079 case nonlval::SymIntConstraintValKind:
1080 return
1081 AssumeSymInt(St, Assumption,
1082 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1083 isFeasible);
1084
Ted Kremenek329f8542008-02-05 21:52:21 +00001085 case nonlval::ConcreteIntKind: {
1086 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001087 isFeasible = b ? Assumption : !Assumption;
1088 return St;
1089 }
1090 }
1091}
1092
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001093GRConstants::StateTy
1094GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1095 const llvm::APSInt& V, bool& isFeasible) {
1096
1097 // First, determine if sym == X, where X != V.
1098 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1099 isFeasible = *X != V;
1100 return St;
1101 }
1102
1103 // Second, determine if sym != V.
1104 if (St.isNotEqual(sym, V)) {
1105 isFeasible = true;
1106 return St;
1107 }
1108
1109 // If we reach here, sym is not a constant and we don't know if it is != V.
1110 // Make that assumption.
1111
1112 isFeasible = true;
1113 return StateMgr.AddNE(St, sym, V);
1114}
1115
1116GRConstants::StateTy
1117GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1118 const llvm::APSInt& V, bool& isFeasible) {
1119
1120 // First, determine if sym == X, where X != V.
1121 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1122 isFeasible = *X == V;
1123 return St;
1124 }
1125
1126 // Second, determine if sym != V.
1127 if (St.isNotEqual(sym, V)) {
1128 isFeasible = false;
1129 return St;
1130 }
1131
1132 // If we reach here, sym is not a constant and we don't know if it is == V.
1133 // Make that assumption.
1134
1135 isFeasible = true;
1136 return StateMgr.AddEQ(St, sym, V);
1137}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001138
Ted Kremenek08b66252008-02-06 04:31:33 +00001139GRConstants::StateTy
1140GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1141 const SymIntConstraint& C, bool& isFeasible) {
1142
1143 switch (C.getOpcode()) {
1144 default:
1145 // No logic yet for other operators.
1146 return St;
1147
1148 case BinaryOperator::EQ:
1149 if (Assumption)
1150 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1151 else
1152 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1153
1154 case BinaryOperator::NE:
1155 if (Assumption)
1156 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1157 else
1158 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1159 }
1160}
1161
Ted Kremenekb38911f2008-01-30 23:03:39 +00001162//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001163// Driver.
1164//===----------------------------------------------------------------------===//
1165
Ted Kremenekaa66a322008-01-16 21:46:15 +00001166#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001167static GRConstants* GraphPrintCheckerState;
1168
Ted Kremenekaa66a322008-01-16 21:46:15 +00001169namespace llvm {
1170template<>
1171struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1172 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001173
Ted Kremenek9153f732008-02-05 07:17:49 +00001174 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001175 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001176 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1177 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1178 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1179 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001180 }
1181 }
1182
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001183 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001184 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001185 bool isFirst = true;
1186
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001187 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001188 if (I.getKey().getKind() != kind)
1189 continue;
1190
1191 if (isFirst) {
1192 if (!isFirstGroup) Out << "\\l\\l";
1193 PrintKindLabel(Out, kind);
1194 isFirst = false;
1195 }
1196 else
1197 Out << "\\l";
1198
1199 Out << ' ';
1200
1201 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1202 Out << V->getName();
1203 else {
1204 Stmt* E = cast<Stmt>(I.getKey());
1205 Out << " (" << (void*) E << ") ";
1206 E->printPretty(Out);
1207 }
1208
1209 Out << " : ";
1210 I.getData().print(Out);
1211 }
1212 }
1213
Ted Kremeneked4de312008-02-06 03:56:15 +00001214 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1215 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1216
1217 if (CE.isEmpty())
1218 return;
1219
1220 Out << "\\l\\|'==' constraints:";
1221
1222 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1223 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1224 }
1225
1226 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1227 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1228
1229 if (NE.isEmpty())
1230 return;
1231
1232 Out << "\\l\\|'!=' constraints:";
1233
1234 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1235 I != EI; ++I){
1236
1237 Out << "\\l $" << I.getKey() << " : ";
1238 bool isFirst = true;
1239
1240 ValueState::IntSetTy::iterator J=I.getData().begin(),
1241 EJ=I.getData().end();
1242 for ( ; J != EJ; ++J) {
1243 if (isFirst) isFirst = false;
1244 else Out << ", ";
1245
1246 Out << (*J)->toString();
1247 }
1248 }
1249 }
1250
Ted Kremenekaa66a322008-01-16 21:46:15 +00001251 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1252 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001253
1254 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001255 ProgramPoint Loc = N->getLocation();
1256
1257 switch (Loc.getKind()) {
1258 case ProgramPoint::BlockEntranceKind:
1259 Out << "Block Entrance: B"
1260 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1261 break;
1262
1263 case ProgramPoint::BlockExitKind:
1264 assert (false);
1265 break;
1266
1267 case ProgramPoint::PostStmtKind: {
1268 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001269 Out << L.getStmt()->getStmtClassName() << ':'
1270 << (void*) L.getStmt() << ' ';
1271
Ted Kremenekaa66a322008-01-16 21:46:15 +00001272 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001273
1274 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1275 Out << "\\|Implicit-Null Dereference.\\l";
1276 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001277 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1278 Out << "\\|Explicit-Null Dereference.\\l";
1279 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001280
Ted Kremenekaa66a322008-01-16 21:46:15 +00001281 break;
1282 }
1283
1284 default: {
1285 const BlockEdge& E = cast<BlockEdge>(Loc);
1286 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1287 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001288
1289 if (Stmt* T = E.getSrc()->getTerminator()) {
1290 Out << "\\|Terminator: ";
1291 E.getSrc()->printTerminator(Out);
1292
1293 if (isa<SwitchStmt>(T)) {
1294 // FIXME
1295 }
1296 else {
1297 Out << "\\lCondition: ";
1298 if (*E.getSrc()->succ_begin() == E.getDst())
1299 Out << "true";
1300 else
1301 Out << "false";
1302 }
1303
1304 Out << "\\l";
1305 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001306
1307 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1308 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1309 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001310 }
1311 }
1312
Ted Kremenek9153f732008-02-05 07:17:49 +00001313 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001314
Ted Kremenek9153f732008-02-05 07:17:49 +00001315 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1316 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1317 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001318
1319 PrintEQ(Out, N->getState());
1320 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001321
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001322 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001323 return Out.str();
1324 }
1325};
1326} // end llvm namespace
1327#endif
1328
Ted Kremenekee985462008-01-16 18:18:48 +00001329namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001330void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1331 Diagnostic& Diag) {
1332
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001333 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001334 Engine.ExecuteWorkList();
1335
1336 // Look for explicit-Null dereferences and warn about them.
1337 GRConstants* CheckerState = &Engine.getCheckerState();
1338
1339 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1340 E=CheckerState->null_end(); I!=E; ++I) {
1341
1342 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1343 Expr* E = cast<Expr>(L.getStmt());
1344
1345 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1346 diag::chkr_null_deref_after_check);
1347 }
1348
1349
Ted Kremenekaa66a322008-01-16 21:46:15 +00001350#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001351 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001352 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001353 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001354#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001355}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001356} // end clang namespace