blob: 8a06b725b9c132461b36de594fb2715d7878862f [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
Ted Kremenekb87d9092008-02-08 19:17:19 +0000195 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000196 /// 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.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000199 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
200 return StateMgr.RemoveDeadBindings(St, S, Liveness);
201 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000202
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000203 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000204
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000205 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000206 return SetValue(St, const_cast<Stmt*>(S), V);
207 }
208
Ted Kremenekcba2e432008-02-05 19:35:18 +0000209 /// SetValue - This version of SetValue is used to batch process a set
210 /// of different possible RValues and return a set of different states.
211 const StateTy::BufferTy& SetValue(StateTy St, Stmt* S,
212 const RValue::BufferTy& V,
213 StateTy::BufferTy& RetBuf);
214
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000215 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000216
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000217 inline RValue GetValue(const StateTy& St, Stmt* S) {
218 return StateMgr.GetValue(St, S);
219 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000220
221 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
222 return StateMgr.GetValue(St, S, &hasVal);
223 }
224
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000225 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000226 return GetValue(St, const_cast<Stmt*>(S));
227 }
228
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000229 inline RValue GetValue(const StateTy& St, const LValue& LV,
230 QualType* T = NULL) {
231
232 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000233 }
234
235 inline LValue GetLValue(const StateTy& St, Stmt* S) {
236 return StateMgr.GetLValue(St, S);
237 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000238
239 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
240 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
241 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000242
243 /// Assume - Create new state by assuming that a given expression
244 /// is true or false.
245 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
246 bool& isFeasible) {
247 if (isa<LValue>(Cond))
248 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
249 else
250 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
251 }
252
253 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
254 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000255
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000256 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
257 bool& isFeasible);
258
259 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
260 bool& isFeasible);
261
Ted Kremenek08b66252008-02-06 04:31:33 +0000262 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
263 bool& isFeasible);
264
Ted Kremenek7e593362008-02-07 15:20:13 +0000265 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000266
Ted Kremenekcba2e432008-02-05 19:35:18 +0000267 /// Nodify - This version of Nodify is used to batch process a set of states.
268 /// The states are not guaranteed to be unique.
269 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
270
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000271 /// Visit - Transfer function logic for all statements. Dispatches to
272 /// other functions that handle specific kinds of statements.
273 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000274
275 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
276 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000277
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000278 /// VisitUnaryOperator - Transfer function logic for unary operators.
279 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
280
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000281 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000282 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
283
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000284 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000285
286 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
287 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
288
Ted Kremenek9de04c42008-01-24 20:55:43 +0000289 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000290 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
291
292 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
293 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
294 NodeTy* Pred, NodeSet& Dst);
295
296 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
297 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000298};
299} // end anonymous namespace
300
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000301
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000302GRConstants::StateTy
303GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000304
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000305 if (!StateCleaned) {
306 St = RemoveDeadBindings(CurrentStmt, St);
307 StateCleaned = true;
308 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000309
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000310 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000311
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000312 if (S == CurrentStmt) {
313 isBlkExpr = getCFG().isBlkExpr(S);
314
315 if (!isBlkExpr)
316 return St;
317 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000318
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000319 return StateMgr.SetValue(St, S, isBlkExpr, V);
320}
321
Ted Kremenekcba2e432008-02-05 19:35:18 +0000322const GRConstants::StateTy::BufferTy&
323GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
324 StateTy::BufferTy& RetBuf) {
325
326 assert (RetBuf.empty());
327
328 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
329 RetBuf.push_back(SetValue(St, S, *I));
330
331 return RetBuf;
332}
333
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000334GRConstants::StateTy
335GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
336
Ted Kremenek53c641a2008-02-08 03:02:48 +0000337 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000338 return St;
339
340 if (!StateCleaned) {
341 St = RemoveDeadBindings(CurrentStmt, St);
342 StateCleaned = true;
343 }
344
345 return StateMgr.SetValue(St, LV, V);
346}
347
Ted Kremenekf233d482008-02-05 00:26:40 +0000348void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000349 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000350
351 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000352
Ted Kremenekb38911f2008-01-30 23:03:39 +0000353 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000354 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000355 if (I.getKey().isSubExpr())
356 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000357
Ted Kremenekf233d482008-02-05 00:26:40 +0000358 // Remove terminator-specific bindings.
359 switch (Term->getStmtClass()) {
360 default: break;
361
362 case Stmt::BinaryOperatorClass: { // '&&', '||'
363 BinaryOperator* B = cast<BinaryOperator>(Term);
364 // FIXME: Liveness analysis should probably remove these automatically.
365 // Verify later when we converge to an 'optimization' stage.
366 PrevState = StateMgr.Remove(PrevState, B->getRHS());
367 break;
368 }
369
370 case Stmt::ConditionalOperatorClass: { // '?' operator
371 ConditionalOperator* C = cast<ConditionalOperator>(Term);
372 // FIXME: Liveness analysis should probably remove these automatically.
373 // Verify later when we converge to an 'optimization' stage.
374 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
375 PrevState = StateMgr.Remove(PrevState, C->getRHS());
376 break;
377 }
378
379 case Stmt::ChooseExprClass: { // __builtin_choose_expr
380 ChooseExpr* C = cast<ChooseExpr>(Term);
381 // FIXME: Liveness analysis should probably remove these automatically.
382 // Verify later when we converge to an 'optimization' stage.
383 PrevState = StateMgr.Remove(PrevState, C->getRHS());
384 PrevState = StateMgr.Remove(PrevState, C->getRHS());
385 break;
386 }
387 }
388
Ted Kremenekb38911f2008-01-30 23:03:39 +0000389 RValue V = GetValue(PrevState, Condition);
390
391 switch (V.getBaseKind()) {
392 default:
393 break;
394
Ted Kremenek53c641a2008-02-08 03:02:48 +0000395 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000396 builder.generateNode(PrevState, true);
397 builder.generateNode(PrevState, false);
398 return;
399
400 case RValue::UninitializedKind: {
401 NodeTy* N = builder.generateNode(PrevState, true);
402
403 if (N) {
404 N->markAsSink();
405 UninitBranches.insert(N);
406 }
407
408 builder.markInfeasible(false);
409 return;
410 }
411 }
412
413 // Process the true branch.
414 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000415
Ted Kremenekb38911f2008-01-30 23:03:39 +0000416 StateTy St = Assume(PrevState, V, true, isFeasible);
417
Ted Kremenekf233d482008-02-05 00:26:40 +0000418 if (isFeasible)
419 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000420 else {
421 builder.markInfeasible(true);
422 isFeasible = true;
423 }
424
425 // Process the false branch.
426 St = Assume(PrevState, V, false, isFeasible);
427
Ted Kremenekf233d482008-02-05 00:26:40 +0000428 if (isFeasible)
429 builder.generateNode(St, false);
430 else
431 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000432}
433
Ted Kremenekf233d482008-02-05 00:26:40 +0000434
435void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
436 NodeSet& Dst) {
437
438 bool hasR2;
439 StateTy PrevState = Pred->getState();
440
441 RValue R1 = GetValue(PrevState, B->getLHS());
442 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
443
Ted Kremenek22031182008-02-08 02:57:34 +0000444 if (isa<UnknownVal>(R1) &&
445 (isa<UnknownVal>(R2) ||
446 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000447
448 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
449 return;
450 }
Ted Kremenek22031182008-02-08 02:57:34 +0000451 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000452 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
453 return;
454 }
455
456 // R1 is an expression that can evaluate to either 'true' or 'false'.
457 if (B->getOpcode() == BinaryOperator::LAnd) {
458 // hasR2 == 'false' means that LHS evaluated to 'false' and that
459 // we short-circuited, leading to a value of '0' for the '&&' expression.
460 if (hasR2 == false) {
461 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
462 return;
463 }
464 }
465 else {
466 assert (B->getOpcode() == BinaryOperator::LOr);
467 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
468 // we short-circuited, leading to a value of '1' for the '||' expression.
469 if (hasR2 == false) {
470 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
471 return;
472 }
473 }
474
475 // If we reach here we did not short-circuit. Assume R2 == true and
476 // R2 == false.
477
478 bool isFeasible;
479 StateTy St = Assume(PrevState, R2, true, isFeasible);
480
481 if (isFeasible)
482 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
483
484 St = Assume(PrevState, R2, false, isFeasible);
485
486 if (isFeasible)
487 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
488}
489
490
491
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000492void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000493 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000494
495 StmtEntryNode = builder.getLastNode();
496 CurrentStmt = S;
497 NodeSet Dst;
498 StateCleaned = false;
499
500 Visit(S, StmtEntryNode, Dst);
501
502 // If no nodes were generated, generate a new node that has all the
503 // dead mappings removed.
504 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
505 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
506 builder.generateNode(S, St, StmtEntryNode);
507 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000508
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000509 CurrentStmt = NULL;
510 StmtEntryNode = NULL;
511 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000512}
513
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000514GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000515GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000516
517 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000518 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000519 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000520
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000521 NodeTy* N = Builder->generateNode(S, St, Pred);
522 Dst.Add(N);
523 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000524}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000525
Ted Kremenekcba2e432008-02-05 19:35:18 +0000526void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
527 const StateTy::BufferTy& SB) {
528
529 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
530 Nodify(Dst, S, Pred, *I);
531}
532
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000533void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
534 if (D != CurrentStmt) {
535 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
536 return;
537 }
538
539 // If we are here, we are loading the value of the decl and binding
540 // it to the block-level expression.
541
542 StateTy St = Pred->getState();
543
544 Nodify(Dst, D, Pred,
545 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
546}
547
Ted Kremenekcba2e432008-02-05 19:35:18 +0000548void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000549
550 QualType T = CastE->getType();
551
552 // Check for redundant casts.
553 if (E->getType() == T) {
554 Dst.Add(Pred);
555 return;
556 }
557
558 NodeSet S1;
559 Visit(E, Pred, S1);
560
561 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
562 NodeTy* N = *I1;
563 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000564 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000565 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000566 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000567}
568
569void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
570 GRConstants::NodeSet& Dst) {
571
572 StateTy St = Pred->getState();
573
574 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000575 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
576 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000577 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000578 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000579 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000580
581 Nodify(Dst, DS, Pred, St);
582
583 if (Dst.empty())
584 Dst.Add(Pred);
585}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000586
Ted Kremenekf233d482008-02-05 00:26:40 +0000587
588void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
589 NodeTy* Pred, NodeSet& Dst) {
590
591 StateTy St = Pred->getState();
592
593 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000594 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000595
596 Nodify(Dst, S, Pred, SetValue(St, S, R));
597}
598
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000599void GRConstants::VisitUnaryOperator(UnaryOperator* U,
600 GRConstants::NodeTy* Pred,
601 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000602
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000603 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000604 UnaryOperator::Opcode Op = U->getOpcode();
605
606 // FIXME: This is a hack so that for '*' and '&' we don't recurse
607 // on visiting the subexpression if it is a DeclRefExpr. We should
608 // probably just handle AddrOf and Deref in their own methods to make
609 // this cleaner.
610 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
611 isa<DeclRefExpr>(U->getSubExpr()))
612 S1.Add(Pred);
613 else
614 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000615
616 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
617 NodeTy* N1 = *I1;
618 StateTy St = N1->getState();
619
620 switch (U->getOpcode()) {
621 case UnaryOperator::PostInc: {
622 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000623 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000624
625 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
626 GetRValueConstant(1U, U));
627
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000628 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
629 break;
630 }
631
632 case UnaryOperator::PostDec: {
633 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000634 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000635
636 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
637 GetRValueConstant(1U, U));
638
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000639 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
640 break;
641 }
642
643 case UnaryOperator::PreInc: {
644 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000645 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000646
647 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
648 GetRValueConstant(1U, U));
649
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000650 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
651 break;
652 }
653
654 case UnaryOperator::PreDec: {
655 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000656 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000657
658 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
659 GetRValueConstant(1U, U));
660
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000661 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
662 break;
663 }
664
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000665 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000666 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000667 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000668 break;
669 }
670
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000671 case UnaryOperator::Not: {
672 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000673 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000674 break;
675 }
676
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000677 case UnaryOperator::LNot: {
678 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
679 //
680 // Note: technically we do "E == 0", but this is the same in the
681 // transfer functions as "0 == E".
682
683 RValue V1 = GetValue(St, U->getSubExpr());
684
685 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000686 const LValue& L1 = cast<LValue>(V1);
687 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
688 Nodify(Dst, U, N1,
689 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
690 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000691 }
692 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000693 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000694 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000695 Nodify(Dst, U, N1,
696 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
697 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000698 }
699
700 break;
701 }
702
Ted Kremenek64924852008-01-31 02:35:41 +0000703 case UnaryOperator::AddrOf: {
704 const LValue& L1 = GetLValue(St, U->getSubExpr());
705 Nodify(Dst, U, N1, SetValue(St, U, L1));
706 break;
707 }
708
709 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000710 // FIXME: Stop when dereferencing an uninitialized value.
711 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
712
713 const RValue& V = GetValue(St, U->getSubExpr());
714 const LValue& L1 = cast<LValue>(V);
715
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000716 // After a dereference, one of two possible situations arise:
717 // (1) A crash, because the pointer was NULL.
718 // (2) The pointer is not NULL, and the dereference works.
719 //
720 // We add these assumptions.
721
Ted Kremenek63a4f692008-02-07 06:04:18 +0000722 bool isFeasibleNotNull;
723
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000724 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000725 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
726
727 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000728 QualType T = U->getType();
729 Nodify(Dst, U, N1, SetValue(StNotNull, U,
730 GetValue(StNotNull, L1, &T)));
731 }
732
Ted Kremenek63a4f692008-02-07 06:04:18 +0000733 bool isFeasibleNull;
734
735 // "Assume" that the pointer is NULL.
736 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
737
738 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000739 // We don't use "Nodify" here because the node will be a sink
740 // and we have no intention of processing it later.
741 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
742
Ted Kremenek63a4f692008-02-07 06:04:18 +0000743 if (NullNode) {
744 NullNode->markAsSink();
745
746 if (isFeasibleNotNull)
747 ImplicitNullDeref.insert(NullNode);
748 else
749 ExplicitNullDeref.insert(NullNode);
750 }
751 }
752
Ted Kremenek64924852008-01-31 02:35:41 +0000753 break;
754 }
755
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000756 default: ;
757 assert (false && "Not implemented.");
758 }
759 }
760}
761
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000762void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
763 GRConstants::NodeSet& Dst) {
764
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000765 if (isa<DeclRefExpr>(E)) {
766 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000767 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000768 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000769
770 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
771 if (U->getOpcode() == UnaryOperator::Deref) {
772 Visit(U->getSubExpr(), Pred, Dst);
773 return;
774 }
775 }
776
777 Visit(E, Pred, Dst);
778}
779
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000780void GRConstants::VisitBinaryOperator(BinaryOperator* B,
781 GRConstants::NodeTy* Pred,
782 GRConstants::NodeSet& Dst) {
783 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000784
785 if (B->isAssignmentOp())
786 VisitAssignmentLHS(B->getLHS(), Pred, S1);
787 else
788 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000789
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000790 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
791 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000792
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000793 // When getting the value for the LHS, check if we are in an assignment.
794 // In such cases, we want to (initially) treat the LHS as an LValue,
795 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000796 // evaluated to LValueDecl's instead of to an NonLValue.
797 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000798 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
799 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000800
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000801 NodeSet S2;
802 Visit(B->getRHS(), N1, S2);
803
804 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000805
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000806 NodeTy* N2 = *I2;
807 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000808 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000809
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000810 BinaryOperator::Opcode Op = B->getOpcode();
811
812 if (Op <= BinaryOperator::Or) {
813
Ted Kremenek22031182008-02-08 02:57:34 +0000814 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000815 Nodify(Dst, B, N2, SetValue(St, B, V1));
816 continue;
817 }
818
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000819 if (isa<LValue>(V1)) {
820 // FIXME: Add support for RHS being a non-lvalue.
821 const LValue& L1 = cast<LValue>(V1);
822 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000823
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000824 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
825 }
826 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000827 const NonLValue& R1 = cast<NonLValue>(V1);
828 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000829
830 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000831 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000832
833 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000834
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000835 }
836
837 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000838 case BinaryOperator::Assign: {
839 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000840 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000841 break;
842 }
843
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000844 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000845
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000846 assert (B->isCompoundAssignmentOp());
847
848 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000849 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000850
Ted Kremenekda9bd092008-02-08 07:05:39 +0000851 if (Op >= BinaryOperator::AndAssign)
852 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
853 else
854 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000855
856 if (isa<LValue>(V2)) {
857 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000858 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000859 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000860 }
861 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000862 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000863 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000864 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000865 }
866
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000867 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000868 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000869 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000870 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000871 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000872 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000873}
Ted Kremenekee985462008-01-16 18:18:48 +0000874
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000875
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000876void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
877 GRConstants::NodeSet& Dst) {
878
879 // FIXME: add metadata to the CFG so that we can disable
880 // this check when we KNOW that there is no block-level subexpression.
881 // The motivation is that this check requires a hashtable lookup.
882
883 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
884 Dst.Add(Pred);
885 return;
886 }
887
888 switch (S->getStmtClass()) {
889 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000890
891 if (cast<BinaryOperator>(S)->isLogicalOp()) {
892 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
893 break;
894 }
Ted Kremenekda9bd092008-02-08 07:05:39 +0000895 else if (cast<BinaryOperator>(S)->getOpcode() == BinaryOperator::Comma) {
896 StateTy St = Pred->getState();
897 Stmt* LastStmt = cast<BinaryOperator>(S)->getRHS();
898 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
899 break;
900 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000901
902 // Fall-through.
903
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000904 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000905 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
906 break;
907
Ted Kremenekda9bd092008-02-08 07:05:39 +0000908 case Stmt::StmtExprClass: {
909 StateTy St = Pred->getState();
910 Stmt* LastStmt = *(cast<StmtExpr>(S)->getSubStmt()->body_rbegin());
911 Nodify(Dst, S, Pred, SetValue(St, S, GetValue(St, LastStmt)));
912 break;
913 }
914
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000915 case Stmt::UnaryOperatorClass:
916 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
917 break;
918
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000919 case Stmt::ParenExprClass:
920 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
921 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000922
923 case Stmt::DeclRefExprClass:
924 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
925 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000926
Ted Kremenek874d63f2008-01-24 02:02:54 +0000927 case Stmt::ImplicitCastExprClass: {
928 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
929 VisitCast(C, C->getSubExpr(), Pred, Dst);
930 break;
931 }
932
933 case Stmt::CastExprClass: {
934 CastExpr* C = cast<CastExpr>(S);
935 VisitCast(C, C->getSubExpr(), Pred, Dst);
936 break;
937 }
938
Ted Kremenekf233d482008-02-05 00:26:40 +0000939 case Stmt::ConditionalOperatorClass: { // '?' operator
940 ConditionalOperator* C = cast<ConditionalOperator>(S);
941 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
942 break;
943 }
944
945 case Stmt::ChooseExprClass: { // __builtin_choose_expr
946 ChooseExpr* C = cast<ChooseExpr>(S);
947 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
948 break;
949 }
950
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000951 case Stmt::ReturnStmtClass:
952 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
953 Visit(R, Pred, Dst);
954 else
955 Dst.Add(Pred);
956
957 break;
958
Ted Kremenek9de04c42008-01-24 20:55:43 +0000959 case Stmt::DeclStmtClass:
960 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
961 break;
962
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000963 default:
964 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
965 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000966 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000967}
968
Ted Kremenekee985462008-01-16 18:18:48 +0000969//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000970// "Assume" logic.
971//===----------------------------------------------------------------------===//
972
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000973GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
974 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000975 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000976
977 switch (Cond.getSubKind()) {
978 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000979 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000980 return St;
981
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000982 case lval::SymbolValKind:
983 if (Assumption)
984 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
985 ValMgr.getZeroWithPtrWidth(), isFeasible);
986 else
987 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
988 ValMgr.getZeroWithPtrWidth(), isFeasible);
989
Ted Kremenek08b66252008-02-06 04:31:33 +0000990
Ted Kremenek329f8542008-02-05 21:52:21 +0000991 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000992 isFeasible = Assumption;
993 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000994
Ted Kremenek329f8542008-02-05 21:52:21 +0000995 case lval::ConcreteIntKind: {
996 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000997 isFeasible = b ? Assumption : !Assumption;
998 return St;
999 }
1000 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001001}
1002
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001003GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1004 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001005 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001006
1007 switch (Cond.getSubKind()) {
1008 default:
1009 assert (false && "'Assume' not implemented for this NonLValue.");
1010 return St;
1011
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001012
1013 case nonlval::SymbolValKind: {
1014 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1015 SymbolID sym = SV.getSymbol();
1016
1017 if (Assumption)
1018 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1019 isFeasible);
1020 else
1021 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1022 isFeasible);
1023 }
1024
Ted Kremenek08b66252008-02-06 04:31:33 +00001025 case nonlval::SymIntConstraintValKind:
1026 return
1027 AssumeSymInt(St, Assumption,
1028 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1029 isFeasible);
1030
Ted Kremenek329f8542008-02-05 21:52:21 +00001031 case nonlval::ConcreteIntKind: {
1032 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001033 isFeasible = b ? Assumption : !Assumption;
1034 return St;
1035 }
1036 }
1037}
1038
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001039GRConstants::StateTy
1040GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1041 const llvm::APSInt& V, bool& isFeasible) {
1042
1043 // First, determine if sym == X, where X != V.
1044 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1045 isFeasible = *X != V;
1046 return St;
1047 }
1048
1049 // Second, determine if sym != V.
1050 if (St.isNotEqual(sym, V)) {
1051 isFeasible = true;
1052 return St;
1053 }
1054
1055 // If we reach here, sym is not a constant and we don't know if it is != V.
1056 // Make that assumption.
1057
1058 isFeasible = true;
1059 return StateMgr.AddNE(St, sym, V);
1060}
1061
1062GRConstants::StateTy
1063GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1064 const llvm::APSInt& V, bool& isFeasible) {
1065
1066 // First, determine if sym == X, where X != V.
1067 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1068 isFeasible = *X == V;
1069 return St;
1070 }
1071
1072 // Second, determine if sym != V.
1073 if (St.isNotEqual(sym, V)) {
1074 isFeasible = false;
1075 return St;
1076 }
1077
1078 // If we reach here, sym is not a constant and we don't know if it is == V.
1079 // Make that assumption.
1080
1081 isFeasible = true;
1082 return StateMgr.AddEQ(St, sym, V);
1083}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001084
Ted Kremenek08b66252008-02-06 04:31:33 +00001085GRConstants::StateTy
1086GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1087 const SymIntConstraint& C, bool& isFeasible) {
1088
1089 switch (C.getOpcode()) {
1090 default:
1091 // No logic yet for other operators.
1092 return St;
1093
1094 case BinaryOperator::EQ:
1095 if (Assumption)
1096 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1097 else
1098 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1099
1100 case BinaryOperator::NE:
1101 if (Assumption)
1102 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1103 else
1104 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1105 }
1106}
1107
Ted Kremenekb38911f2008-01-30 23:03:39 +00001108//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001109// Driver.
1110//===----------------------------------------------------------------------===//
1111
Ted Kremenekaa66a322008-01-16 21:46:15 +00001112#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001113static GRConstants* GraphPrintCheckerState;
1114
Ted Kremenekaa66a322008-01-16 21:46:15 +00001115namespace llvm {
1116template<>
1117struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1118 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001119
Ted Kremenek9153f732008-02-05 07:17:49 +00001120 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001121 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001122 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1123 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1124 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1125 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001126 }
1127 }
1128
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001129 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001130 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001131 bool isFirst = true;
1132
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001133 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001134 if (I.getKey().getKind() != kind)
1135 continue;
1136
1137 if (isFirst) {
1138 if (!isFirstGroup) Out << "\\l\\l";
1139 PrintKindLabel(Out, kind);
1140 isFirst = false;
1141 }
1142 else
1143 Out << "\\l";
1144
1145 Out << ' ';
1146
1147 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1148 Out << V->getName();
1149 else {
1150 Stmt* E = cast<Stmt>(I.getKey());
1151 Out << " (" << (void*) E << ") ";
1152 E->printPretty(Out);
1153 }
1154
1155 Out << " : ";
1156 I.getData().print(Out);
1157 }
1158 }
1159
Ted Kremeneked4de312008-02-06 03:56:15 +00001160 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1161 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1162
1163 if (CE.isEmpty())
1164 return;
1165
1166 Out << "\\l\\|'==' constraints:";
1167
1168 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1169 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1170 }
1171
1172 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1173 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1174
1175 if (NE.isEmpty())
1176 return;
1177
1178 Out << "\\l\\|'!=' constraints:";
1179
1180 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1181 I != EI; ++I){
1182
1183 Out << "\\l $" << I.getKey() << " : ";
1184 bool isFirst = true;
1185
1186 ValueState::IntSetTy::iterator J=I.getData().begin(),
1187 EJ=I.getData().end();
1188 for ( ; J != EJ; ++J) {
1189 if (isFirst) isFirst = false;
1190 else Out << ", ";
1191
1192 Out << (*J)->toString();
1193 }
1194 }
1195 }
1196
Ted Kremenekaa66a322008-01-16 21:46:15 +00001197 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1198 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001199
1200 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001201 ProgramPoint Loc = N->getLocation();
1202
1203 switch (Loc.getKind()) {
1204 case ProgramPoint::BlockEntranceKind:
1205 Out << "Block Entrance: B"
1206 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1207 break;
1208
1209 case ProgramPoint::BlockExitKind:
1210 assert (false);
1211 break;
1212
1213 case ProgramPoint::PostStmtKind: {
1214 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001215 Out << L.getStmt()->getStmtClassName() << ':'
1216 << (void*) L.getStmt() << ' ';
1217
Ted Kremenekaa66a322008-01-16 21:46:15 +00001218 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001219
1220 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1221 Out << "\\|Implicit-Null Dereference.\\l";
1222 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001223 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1224 Out << "\\|Explicit-Null Dereference.\\l";
1225 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001226
Ted Kremenekaa66a322008-01-16 21:46:15 +00001227 break;
1228 }
1229
1230 default: {
1231 const BlockEdge& E = cast<BlockEdge>(Loc);
1232 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1233 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001234
1235 if (Stmt* T = E.getSrc()->getTerminator()) {
1236 Out << "\\|Terminator: ";
1237 E.getSrc()->printTerminator(Out);
1238
1239 if (isa<SwitchStmt>(T)) {
1240 // FIXME
1241 }
1242 else {
1243 Out << "\\lCondition: ";
1244 if (*E.getSrc()->succ_begin() == E.getDst())
1245 Out << "true";
1246 else
1247 Out << "false";
1248 }
1249
1250 Out << "\\l";
1251 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001252
1253 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1254 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1255 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001256 }
1257 }
1258
Ted Kremenek9153f732008-02-05 07:17:49 +00001259 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001260
Ted Kremenek9153f732008-02-05 07:17:49 +00001261 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1262 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1263 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001264
1265 PrintEQ(Out, N->getState());
1266 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001267
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001268 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001269 return Out.str();
1270 }
1271};
1272} // end llvm namespace
1273#endif
1274
Ted Kremenekee985462008-01-16 18:18:48 +00001275namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001276void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1277 Diagnostic& Diag) {
1278
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001279 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001280 Engine.ExecuteWorkList();
1281
1282 // Look for explicit-Null dereferences and warn about them.
1283 GRConstants* CheckerState = &Engine.getCheckerState();
1284
1285 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1286 E=CheckerState->null_end(); I!=E; ++I) {
1287
1288 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1289 Expr* E = cast<Expr>(L.getStmt());
1290
1291 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1292 diag::chkr_null_deref_after_check);
1293 }
1294
1295
Ted Kremenekaa66a322008-01-16 21:46:15 +00001296#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001297 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001298 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001299 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001300#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001301}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001302} // end clang namespace