blob: 84b0debb61fc089ef95594f3dc4a9a60e3daf66e [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 Kremenek63a4f692008-02-07 06:04:18 +0000263 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St,
264 bool AlwaysMakeNode = false);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000265
Ted Kremenekcba2e432008-02-05 19:35:18 +0000266 /// Nodify - This version of Nodify is used to batch process a set of states.
267 /// The states are not guaranteed to be unique.
268 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
269
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000270 /// Visit - Transfer function logic for all statements. Dispatches to
271 /// other functions that handle specific kinds of statements.
272 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000273
274 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
275 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000276
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000277 /// VisitUnaryOperator - Transfer function logic for unary operators.
278 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
279
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000280 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000281 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
282
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000283 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000284
285 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
286 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
287
Ted Kremenek9de04c42008-01-24 20:55:43 +0000288 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000289 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
290
291 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
292 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
293 NodeTy* Pred, NodeSet& Dst);
294
295 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
296 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000297};
298} // end anonymous namespace
299
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000300
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000301GRConstants::StateTy
302GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000303
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000304 if (!StateCleaned) {
305 St = RemoveDeadBindings(CurrentStmt, St);
306 StateCleaned = true;
307 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000308
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000309 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000310
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000311 if (S == CurrentStmt) {
312 isBlkExpr = getCFG().isBlkExpr(S);
313
314 if (!isBlkExpr)
315 return St;
316 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000317
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000318 return StateMgr.SetValue(St, S, isBlkExpr, V);
319}
320
Ted Kremenekcba2e432008-02-05 19:35:18 +0000321const GRConstants::StateTy::BufferTy&
322GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
323 StateTy::BufferTy& RetBuf) {
324
325 assert (RetBuf.empty());
326
327 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
328 RetBuf.push_back(SetValue(St, S, *I));
329
330 return RetBuf;
331}
332
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000333GRConstants::StateTy
334GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
335
336 if (!LV.isValid())
337 return St;
338
339 if (!StateCleaned) {
340 St = RemoveDeadBindings(CurrentStmt, St);
341 StateCleaned = true;
342 }
343
344 return StateMgr.SetValue(St, LV, V);
345}
346
Ted Kremenekf233d482008-02-05 00:26:40 +0000347void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000348 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000349
350 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000351
Ted Kremenekb38911f2008-01-30 23:03:39 +0000352 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000353 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000354 if (I.getKey().isSubExpr())
355 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000356
Ted Kremenekf233d482008-02-05 00:26:40 +0000357 // Remove terminator-specific bindings.
358 switch (Term->getStmtClass()) {
359 default: break;
360
361 case Stmt::BinaryOperatorClass: { // '&&', '||'
362 BinaryOperator* B = cast<BinaryOperator>(Term);
363 // FIXME: Liveness analysis should probably remove these automatically.
364 // Verify later when we converge to an 'optimization' stage.
365 PrevState = StateMgr.Remove(PrevState, B->getRHS());
366 break;
367 }
368
369 case Stmt::ConditionalOperatorClass: { // '?' operator
370 ConditionalOperator* C = cast<ConditionalOperator>(Term);
371 // FIXME: Liveness analysis should probably remove these automatically.
372 // Verify later when we converge to an 'optimization' stage.
373 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
374 PrevState = StateMgr.Remove(PrevState, C->getRHS());
375 break;
376 }
377
378 case Stmt::ChooseExprClass: { // __builtin_choose_expr
379 ChooseExpr* C = cast<ChooseExpr>(Term);
380 // FIXME: Liveness analysis should probably remove these automatically.
381 // Verify later when we converge to an 'optimization' stage.
382 PrevState = StateMgr.Remove(PrevState, C->getRHS());
383 PrevState = StateMgr.Remove(PrevState, C->getRHS());
384 break;
385 }
386 }
387
Ted Kremenekb38911f2008-01-30 23:03:39 +0000388 RValue V = GetValue(PrevState, Condition);
389
390 switch (V.getBaseKind()) {
391 default:
392 break;
393
394 case RValue::InvalidKind:
395 builder.generateNode(PrevState, true);
396 builder.generateNode(PrevState, false);
397 return;
398
399 case RValue::UninitializedKind: {
400 NodeTy* N = builder.generateNode(PrevState, true);
401
402 if (N) {
403 N->markAsSink();
404 UninitBranches.insert(N);
405 }
406
407 builder.markInfeasible(false);
408 return;
409 }
410 }
411
412 // Process the true branch.
413 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000414
Ted Kremenekb38911f2008-01-30 23:03:39 +0000415 StateTy St = Assume(PrevState, V, true, isFeasible);
416
Ted Kremenekf233d482008-02-05 00:26:40 +0000417 if (isFeasible)
418 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000419 else {
420 builder.markInfeasible(true);
421 isFeasible = true;
422 }
423
424 // Process the false branch.
425 St = Assume(PrevState, V, false, isFeasible);
426
Ted Kremenekf233d482008-02-05 00:26:40 +0000427 if (isFeasible)
428 builder.generateNode(St, false);
429 else
430 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000431}
432
Ted Kremenekf233d482008-02-05 00:26:40 +0000433
434void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
435 NodeSet& Dst) {
436
437 bool hasR2;
438 StateTy PrevState = Pred->getState();
439
440 RValue R1 = GetValue(PrevState, B->getLHS());
441 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
442
443 if (isa<InvalidValue>(R1) &&
444 (isa<InvalidValue>(R2) ||
445 isa<UninitializedValue>(R2))) {
446
447 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
448 return;
449 }
450 else if (isa<UninitializedValue>(R1)) {
451 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
452 return;
453 }
454
455 // R1 is an expression that can evaluate to either 'true' or 'false'.
456 if (B->getOpcode() == BinaryOperator::LAnd) {
457 // hasR2 == 'false' means that LHS evaluated to 'false' and that
458 // we short-circuited, leading to a value of '0' for the '&&' expression.
459 if (hasR2 == false) {
460 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
461 return;
462 }
463 }
464 else {
465 assert (B->getOpcode() == BinaryOperator::LOr);
466 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
467 // we short-circuited, leading to a value of '1' for the '||' expression.
468 if (hasR2 == false) {
469 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
470 return;
471 }
472 }
473
474 // If we reach here we did not short-circuit. Assume R2 == true and
475 // R2 == false.
476
477 bool isFeasible;
478 StateTy St = Assume(PrevState, R2, true, isFeasible);
479
480 if (isFeasible)
481 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
482
483 St = Assume(PrevState, R2, false, isFeasible);
484
485 if (isFeasible)
486 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
487}
488
489
490
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000491void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000492 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000493
494 StmtEntryNode = builder.getLastNode();
495 CurrentStmt = S;
496 NodeSet Dst;
497 StateCleaned = false;
498
499 Visit(S, StmtEntryNode, Dst);
500
501 // If no nodes were generated, generate a new node that has all the
502 // dead mappings removed.
503 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
504 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
505 builder.generateNode(S, St, StmtEntryNode);
506 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000507
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000508 CurrentStmt = NULL;
509 StmtEntryNode = NULL;
510 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000511}
512
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000513GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000514
515 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
516 // The roots are any Block-level exprs and Decls that our liveness algorithm
517 // tells us are live. We then see what Decls they may reference, and keep
518 // those around. This code more than likely can be made faster, and the
519 // frequency of which this method is called should be experimented with
520 // for optimum performance.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000521
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000522 llvm::SmallVector<ValueDecl*, 10> WList;
Ted Kremenekf84469b2008-01-18 00:41:32 +0000523
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000524 for (StateTy::vb_iterator I = M.begin(), E = M.end();
525 I!=E && !I.getKey().isSymbol(); ++I) {
526
527 // Remove old bindings for subexpressions.
528 if (I.getKey().isSubExpr()) {
Ted Kremenek65cac132008-01-29 05:25:31 +0000529 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000530 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000531 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000532
533 if (I.getKey().isBlkExpr()) {
534 if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) {
535 if (isa<lval::DeclVal>(I.getData())) {
536 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
537 WList.push_back(LV.getDecl());
538 }
539 }
540 else
541 M = StateMgr.Remove(M, I.getKey());
542
543 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000544 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000545
546 assert (I.getKey().isDecl());
547
548 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
549 if (Liveness.isLive(Loc, V))
550 WList.push_back(V);
Ted Kremenek65cac132008-01-29 05:25:31 +0000551 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000552
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000553 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
554
555 while (!WList.empty()) {
556 ValueDecl* V = WList.back();
557 WList.pop_back();
558
559 if (Marked.count(V))
560 continue;
561
562 Marked.insert(V);
563
564 if (V->getType()->isPointerType()) {
565 const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V)));
566
567 if (!isa<lval::DeclVal>(LV))
568 continue;
569
570 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
571 WList.push_back(LVD.getDecl());
572 }
573 }
574
575 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I)
576 if (I.getKey().isDecl())
577 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
578 if (!Marked.count(V))
579 M = StateMgr.Remove(M, V);
580
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000581 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000582}
583
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000584GRConstants::NodeTy*
Ted Kremenek63a4f692008-02-07 06:04:18 +0000585GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St,
586 bool AlwaysMakeNode) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000587
588 // If the state hasn't changed, don't generate a new node.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000589 if (!AlwaysMakeNode && St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000590 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000591
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000592 NodeTy* N = Builder->generateNode(S, St, Pred);
593 Dst.Add(N);
594 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000595}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000596
Ted Kremenekcba2e432008-02-05 19:35:18 +0000597void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
598 const StateTy::BufferTy& SB) {
599
600 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
601 Nodify(Dst, S, Pred, *I);
602}
603
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000604void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
605 if (D != CurrentStmt) {
606 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
607 return;
608 }
609
610 // If we are here, we are loading the value of the decl and binding
611 // it to the block-level expression.
612
613 StateTy St = Pred->getState();
614
615 Nodify(Dst, D, Pred,
616 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
617}
618
Ted Kremenekcba2e432008-02-05 19:35:18 +0000619void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000620
621 QualType T = CastE->getType();
622
623 // Check for redundant casts.
624 if (E->getType() == T) {
625 Dst.Add(Pred);
626 return;
627 }
628
629 NodeSet S1;
630 Visit(E, Pred, S1);
631
632 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
633 NodeTy* N = *I1;
634 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000635 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000636 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000637 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000638}
639
640void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
641 GRConstants::NodeSet& Dst) {
642
643 StateTy St = Pred->getState();
644
645 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000646 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
647 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000648 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek403c1812008-01-28 22:51:57 +0000649 E ? GetValue(St, E) : UninitializedValue());
650 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000651
652 Nodify(Dst, DS, Pred, St);
653
654 if (Dst.empty())
655 Dst.Add(Pred);
656}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000657
Ted Kremenekf233d482008-02-05 00:26:40 +0000658
659void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
660 NodeTy* Pred, NodeSet& Dst) {
661
662 StateTy St = Pred->getState();
663
664 RValue R = GetValue(St, LHS);
665 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
666
667 Nodify(Dst, S, Pred, SetValue(St, S, R));
668}
669
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000670void GRConstants::VisitUnaryOperator(UnaryOperator* U,
671 GRConstants::NodeTy* Pred,
672 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000673
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000674 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000675 UnaryOperator::Opcode Op = U->getOpcode();
676
677 // FIXME: This is a hack so that for '*' and '&' we don't recurse
678 // on visiting the subexpression if it is a DeclRefExpr. We should
679 // probably just handle AddrOf and Deref in their own methods to make
680 // this cleaner.
681 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
682 isa<DeclRefExpr>(U->getSubExpr()))
683 S1.Add(Pred);
684 else
685 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000686
687 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
688 NodeTy* N1 = *I1;
689 StateTy St = N1->getState();
690
691 switch (U->getOpcode()) {
692 case UnaryOperator::PostInc: {
693 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000694 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000695
696 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
697 GetRValueConstant(1U, U));
698
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000699 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
700 break;
701 }
702
703 case UnaryOperator::PostDec: {
704 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000705 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000706
707 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
708 GetRValueConstant(1U, U));
709
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000710 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
711 break;
712 }
713
714 case UnaryOperator::PreInc: {
715 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000716 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000717
718 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
719 GetRValueConstant(1U, U));
720
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000721 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
722 break;
723 }
724
725 case UnaryOperator::PreDec: {
726 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000727 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000728
729 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
730 GetRValueConstant(1U, U));
731
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000732 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
733 break;
734 }
735
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000736 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000737 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000738 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000739 break;
740 }
741
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000742 case UnaryOperator::Not: {
743 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000744 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000745 break;
746 }
747
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000748 case UnaryOperator::LNot: {
749 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
750 //
751 // Note: technically we do "E == 0", but this is the same in the
752 // transfer functions as "0 == E".
753
754 RValue V1 = GetValue(St, U->getSubExpr());
755
756 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000757 const LValue& L1 = cast<LValue>(V1);
758 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
759 Nodify(Dst, U, N1,
760 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
761 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000762 }
763 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000764 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000765 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000766 Nodify(Dst, U, N1,
767 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
768 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000769 }
770
771 break;
772 }
773
Ted Kremenek64924852008-01-31 02:35:41 +0000774 case UnaryOperator::AddrOf: {
775 const LValue& L1 = GetLValue(St, U->getSubExpr());
776 Nodify(Dst, U, N1, SetValue(St, U, L1));
777 break;
778 }
779
780 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000781 // FIXME: Stop when dereferencing an uninitialized value.
782 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
783
784 const RValue& V = GetValue(St, U->getSubExpr());
785 const LValue& L1 = cast<LValue>(V);
786
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000787 // After a dereference, one of two possible situations arise:
788 // (1) A crash, because the pointer was NULL.
789 // (2) The pointer is not NULL, and the dereference works.
790 //
791 // We add these assumptions.
792
Ted Kremenek63a4f692008-02-07 06:04:18 +0000793 bool isFeasibleNotNull;
794
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000795 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000796 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
797
798 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000799 QualType T = U->getType();
800 Nodify(Dst, U, N1, SetValue(StNotNull, U,
801 GetValue(StNotNull, L1, &T)));
802 }
803
Ted Kremenek63a4f692008-02-07 06:04:18 +0000804 bool isFeasibleNull;
805
806 // "Assume" that the pointer is NULL.
807 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
808
809 if (isFeasibleNull) {
810 NodeTy* NullNode = Nodify(Dst, U, N1, StNull, true);
811 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 Kremenek5b6dc2d2008-02-07 01:08:27 +0000882 if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) {
883 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);
917 RValue Result = cast<NonLValue>(InvalidValue());
918
919 Op = (BinaryOperator::Opcode)
920 (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign));
921
922 if (isa<LValue>(V2)) {
923 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000924 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000925 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000926 }
927 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000928 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000929 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000930 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000931 }
932
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000933 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000934 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000935 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000936 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000937 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000938 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000939}
Ted Kremenekee985462008-01-16 18:18:48 +0000940
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000941
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000942void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
943 GRConstants::NodeSet& Dst) {
944
945 // FIXME: add metadata to the CFG so that we can disable
946 // this check when we KNOW that there is no block-level subexpression.
947 // The motivation is that this check requires a hashtable lookup.
948
949 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
950 Dst.Add(Pred);
951 return;
952 }
953
954 switch (S->getStmtClass()) {
955 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000956
957 if (cast<BinaryOperator>(S)->isLogicalOp()) {
958 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
959 break;
960 }
961
962 // Fall-through.
963
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000964 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000965 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
966 break;
967
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000968 case Stmt::UnaryOperatorClass:
969 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
970 break;
971
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000972 case Stmt::ParenExprClass:
973 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
974 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000975
976 case Stmt::DeclRefExprClass:
977 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
978 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000979
Ted Kremenek874d63f2008-01-24 02:02:54 +0000980 case Stmt::ImplicitCastExprClass: {
981 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
982 VisitCast(C, C->getSubExpr(), Pred, Dst);
983 break;
984 }
985
986 case Stmt::CastExprClass: {
987 CastExpr* C = cast<CastExpr>(S);
988 VisitCast(C, C->getSubExpr(), Pred, Dst);
989 break;
990 }
991
Ted Kremenekf233d482008-02-05 00:26:40 +0000992 case Stmt::ConditionalOperatorClass: { // '?' operator
993 ConditionalOperator* C = cast<ConditionalOperator>(S);
994 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
995 break;
996 }
997
998 case Stmt::ChooseExprClass: { // __builtin_choose_expr
999 ChooseExpr* C = cast<ChooseExpr>(S);
1000 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
1001 break;
1002 }
1003
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001004 case Stmt::ReturnStmtClass:
1005 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1006 Visit(R, Pred, Dst);
1007 else
1008 Dst.Add(Pred);
1009
1010 break;
1011
Ted Kremenek9de04c42008-01-24 20:55:43 +00001012 case Stmt::DeclStmtClass:
1013 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1014 break;
1015
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001016 default:
1017 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1018 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001019 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001020}
1021
Ted Kremenekee985462008-01-16 18:18:48 +00001022//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001023// "Assume" logic.
1024//===----------------------------------------------------------------------===//
1025
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001026GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1027 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001028 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001029
1030 switch (Cond.getSubKind()) {
1031 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001032 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001033 return St;
1034
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001035 case lval::SymbolValKind:
1036 if (Assumption)
1037 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1038 ValMgr.getZeroWithPtrWidth(), isFeasible);
1039 else
1040 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1041 ValMgr.getZeroWithPtrWidth(), isFeasible);
1042
Ted Kremenek08b66252008-02-06 04:31:33 +00001043
Ted Kremenek329f8542008-02-05 21:52:21 +00001044 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001045 isFeasible = Assumption;
1046 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001047
Ted Kremenek329f8542008-02-05 21:52:21 +00001048 case lval::ConcreteIntKind: {
1049 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001050 isFeasible = b ? Assumption : !Assumption;
1051 return St;
1052 }
1053 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001054}
1055
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001056GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1057 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001058 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001059
1060 switch (Cond.getSubKind()) {
1061 default:
1062 assert (false && "'Assume' not implemented for this NonLValue.");
1063 return St;
1064
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001065
1066 case nonlval::SymbolValKind: {
1067 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1068 SymbolID sym = SV.getSymbol();
1069
1070 if (Assumption)
1071 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1072 isFeasible);
1073 else
1074 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1075 isFeasible);
1076 }
1077
Ted Kremenek08b66252008-02-06 04:31:33 +00001078 case nonlval::SymIntConstraintValKind:
1079 return
1080 AssumeSymInt(St, Assumption,
1081 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1082 isFeasible);
1083
Ted Kremenek329f8542008-02-05 21:52:21 +00001084 case nonlval::ConcreteIntKind: {
1085 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001086 isFeasible = b ? Assumption : !Assumption;
1087 return St;
1088 }
1089 }
1090}
1091
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001092GRConstants::StateTy
1093GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1094 const llvm::APSInt& V, bool& isFeasible) {
1095
1096 // First, determine if sym == X, where X != V.
1097 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1098 isFeasible = *X != V;
1099 return St;
1100 }
1101
1102 // Second, determine if sym != V.
1103 if (St.isNotEqual(sym, V)) {
1104 isFeasible = true;
1105 return St;
1106 }
1107
1108 // If we reach here, sym is not a constant and we don't know if it is != V.
1109 // Make that assumption.
1110
1111 isFeasible = true;
1112 return StateMgr.AddNE(St, sym, V);
1113}
1114
1115GRConstants::StateTy
1116GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1117 const llvm::APSInt& V, bool& isFeasible) {
1118
1119 // First, determine if sym == X, where X != V.
1120 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1121 isFeasible = *X == V;
1122 return St;
1123 }
1124
1125 // Second, determine if sym != V.
1126 if (St.isNotEqual(sym, V)) {
1127 isFeasible = false;
1128 return St;
1129 }
1130
1131 // If we reach here, sym is not a constant and we don't know if it is == V.
1132 // Make that assumption.
1133
1134 isFeasible = true;
1135 return StateMgr.AddEQ(St, sym, V);
1136}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001137
Ted Kremenek08b66252008-02-06 04:31:33 +00001138GRConstants::StateTy
1139GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1140 const SymIntConstraint& C, bool& isFeasible) {
1141
1142 switch (C.getOpcode()) {
1143 default:
1144 // No logic yet for other operators.
1145 return St;
1146
1147 case BinaryOperator::EQ:
1148 if (Assumption)
1149 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1150 else
1151 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1152
1153 case BinaryOperator::NE:
1154 if (Assumption)
1155 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1156 else
1157 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1158 }
1159}
1160
Ted Kremenekb38911f2008-01-30 23:03:39 +00001161//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001162// Driver.
1163//===----------------------------------------------------------------------===//
1164
Ted Kremenekaa66a322008-01-16 21:46:15 +00001165#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001166static GRConstants* GraphPrintCheckerState;
1167
Ted Kremenekaa66a322008-01-16 21:46:15 +00001168namespace llvm {
1169template<>
1170struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1171 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001172
Ted Kremenek9153f732008-02-05 07:17:49 +00001173 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001174 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001175 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1176 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1177 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1178 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001179 }
1180 }
1181
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001182 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001183 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001184 bool isFirst = true;
1185
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001186 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001187 if (I.getKey().getKind() != kind)
1188 continue;
1189
1190 if (isFirst) {
1191 if (!isFirstGroup) Out << "\\l\\l";
1192 PrintKindLabel(Out, kind);
1193 isFirst = false;
1194 }
1195 else
1196 Out << "\\l";
1197
1198 Out << ' ';
1199
1200 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1201 Out << V->getName();
1202 else {
1203 Stmt* E = cast<Stmt>(I.getKey());
1204 Out << " (" << (void*) E << ") ";
1205 E->printPretty(Out);
1206 }
1207
1208 Out << " : ";
1209 I.getData().print(Out);
1210 }
1211 }
1212
Ted Kremeneked4de312008-02-06 03:56:15 +00001213 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1214 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1215
1216 if (CE.isEmpty())
1217 return;
1218
1219 Out << "\\l\\|'==' constraints:";
1220
1221 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1222 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1223 }
1224
1225 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1226 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1227
1228 if (NE.isEmpty())
1229 return;
1230
1231 Out << "\\l\\|'!=' constraints:";
1232
1233 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1234 I != EI; ++I){
1235
1236 Out << "\\l $" << I.getKey() << " : ";
1237 bool isFirst = true;
1238
1239 ValueState::IntSetTy::iterator J=I.getData().begin(),
1240 EJ=I.getData().end();
1241 for ( ; J != EJ; ++J) {
1242 if (isFirst) isFirst = false;
1243 else Out << ", ";
1244
1245 Out << (*J)->toString();
1246 }
1247 }
1248 }
1249
Ted Kremenekaa66a322008-01-16 21:46:15 +00001250 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1251 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001252
1253 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001254 ProgramPoint Loc = N->getLocation();
1255
1256 switch (Loc.getKind()) {
1257 case ProgramPoint::BlockEntranceKind:
1258 Out << "Block Entrance: B"
1259 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1260 break;
1261
1262 case ProgramPoint::BlockExitKind:
1263 assert (false);
1264 break;
1265
1266 case ProgramPoint::PostStmtKind: {
1267 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001268 Out << L.getStmt()->getStmtClassName() << ':'
1269 << (void*) L.getStmt() << ' ';
1270
Ted Kremenekaa66a322008-01-16 21:46:15 +00001271 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001272
1273 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1274 Out << "\\|Implicit-Null Dereference.\\l";
1275 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001276 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1277 Out << "\\|Explicit-Null Dereference.\\l";
1278 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001279
Ted Kremenekaa66a322008-01-16 21:46:15 +00001280 break;
1281 }
1282
1283 default: {
1284 const BlockEdge& E = cast<BlockEdge>(Loc);
1285 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1286 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001287
1288 if (Stmt* T = E.getSrc()->getTerminator()) {
1289 Out << "\\|Terminator: ";
1290 E.getSrc()->printTerminator(Out);
1291
1292 if (isa<SwitchStmt>(T)) {
1293 // FIXME
1294 }
1295 else {
1296 Out << "\\lCondition: ";
1297 if (*E.getSrc()->succ_begin() == E.getDst())
1298 Out << "true";
1299 else
1300 Out << "false";
1301 }
1302
1303 Out << "\\l";
1304 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001305
1306 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1307 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1308 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001309 }
1310 }
1311
Ted Kremenek9153f732008-02-05 07:17:49 +00001312 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001313
Ted Kremenek9153f732008-02-05 07:17:49 +00001314 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1315 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1316 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001317
1318 PrintEQ(Out, N->getState());
1319 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001320
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001321 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001322 return Out.str();
1323 }
1324};
1325} // end llvm namespace
1326#endif
1327
Ted Kremenekee985462008-01-16 18:18:48 +00001328namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001329void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1330 Diagnostic& Diag) {
1331
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001332 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001333 Engine.ExecuteWorkList();
1334
1335 // Look for explicit-Null dereferences and warn about them.
1336 GRConstants* CheckerState = &Engine.getCheckerState();
1337
1338 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1339 E=CheckerState->null_end(); I!=E; ++I) {
1340
1341 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1342 Expr* E = cast<Expr>(L.getStmt());
1343
1344 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1345 diag::chkr_null_deref_after_check);
1346 }
1347
1348
Ted Kremenekaa66a322008-01-16 21:46:15 +00001349#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001350 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001351 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001352 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001353#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001354}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001355} // end clang namespace