blob: 90b055c37020d66062d26b29f25e28ec96c1b2a5 [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 Kremenekd70b62e2008-02-08 20:29:23 +0000203 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000204
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000205 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
206 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000207 }
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.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000211 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000212 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 Kremenekd70b62e2008-02-08 20:29:23 +0000217 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000218 return StateMgr.GetValue(St, S);
219 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000220
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000221 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000222 return StateMgr.GetValue(St, S, &hasVal);
223 }
224
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000225 inline RValue GetValue(const StateTy& St, const Expr* S) {
226 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000227 }
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
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000235 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000236 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
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000293 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000294 NodeTy* Pred, NodeSet& Dst);
295
296 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
297 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000298
299 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
300 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
301 NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000302};
303} // end anonymous namespace
304
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000305
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000306GRConstants::StateTy
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000307GRConstants::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000308
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000309 if (!StateCleaned) {
310 St = RemoveDeadBindings(CurrentStmt, St);
311 StateCleaned = true;
312 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000313
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000314 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000315
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000316 if (S == CurrentStmt) {
317 isBlkExpr = getCFG().isBlkExpr(S);
318
319 if (!isBlkExpr)
320 return St;
321 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000322
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000323 return StateMgr.SetValue(St, S, isBlkExpr, V);
324}
325
Ted Kremenekcba2e432008-02-05 19:35:18 +0000326const GRConstants::StateTy::BufferTy&
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000327GRConstants::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000328 StateTy::BufferTy& RetBuf) {
329
330 assert (RetBuf.empty());
331
332 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
333 RetBuf.push_back(SetValue(St, S, *I));
334
335 return RetBuf;
336}
337
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000338GRConstants::StateTy
339GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
340
Ted Kremenek53c641a2008-02-08 03:02:48 +0000341 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000342 return St;
343
344 if (!StateCleaned) {
345 St = RemoveDeadBindings(CurrentStmt, St);
346 StateCleaned = true;
347 }
348
349 return StateMgr.SetValue(St, LV, V);
350}
351
Ted Kremenekf233d482008-02-05 00:26:40 +0000352void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000353 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000354
Ted Kremeneke7d22112008-02-11 19:21:59 +0000355 // Remove old bindings for subexpressions.
356 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000357
Ted Kremenekb38911f2008-01-30 23:03:39 +0000358 RValue V = GetValue(PrevState, Condition);
359
360 switch (V.getBaseKind()) {
361 default:
362 break;
363
Ted Kremenek53c641a2008-02-08 03:02:48 +0000364 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000365 builder.generateNode(PrevState, true);
366 builder.generateNode(PrevState, false);
367 return;
368
369 case RValue::UninitializedKind: {
370 NodeTy* N = builder.generateNode(PrevState, true);
371
372 if (N) {
373 N->markAsSink();
374 UninitBranches.insert(N);
375 }
376
377 builder.markInfeasible(false);
378 return;
379 }
380 }
381
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000382 // Get the current block counter.
383 GRBlockCounter BC = builder.getBlockCounter();
384
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000385 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
386 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000387
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000388 if (isa<nonlval::ConcreteInt>(V) ||
389 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
390
391 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000392
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000393 bool isFeasible = true;
394
395 StateTy St = Assume(PrevState, V, true, isFeasible);
396
397 if (isFeasible)
398 builder.generateNode(St, true);
399 else
400 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000401 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000402 else
403 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000404
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000405 BlockID = builder.getTargetBlock(false)->getBlockID();
406 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000407
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000408 if (isa<nonlval::ConcreteInt>(V) ||
409 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
410
411 // Process the false branch.
412
413 bool isFeasible = false;
414
415 StateTy St = Assume(PrevState, V, false, isFeasible);
416
417 if (isFeasible)
418 builder.generateNode(St, false);
419 else
420 builder.markInfeasible(false);
421 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000422 else
423 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000424}
425
Ted Kremenekf233d482008-02-05 00:26:40 +0000426
427void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
428 NodeSet& Dst) {
429
430 bool hasR2;
431 StateTy PrevState = Pred->getState();
432
433 RValue R1 = GetValue(PrevState, B->getLHS());
434 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
435
Ted Kremenek22031182008-02-08 02:57:34 +0000436 if (isa<UnknownVal>(R1) &&
437 (isa<UnknownVal>(R2) ||
438 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000439
440 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
441 return;
442 }
Ted Kremenek22031182008-02-08 02:57:34 +0000443 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000444 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
445 return;
446 }
447
448 // R1 is an expression that can evaluate to either 'true' or 'false'.
449 if (B->getOpcode() == BinaryOperator::LAnd) {
450 // hasR2 == 'false' means that LHS evaluated to 'false' and that
451 // we short-circuited, leading to a value of '0' for the '&&' expression.
452 if (hasR2 == false) {
453 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
454 return;
455 }
456 }
457 else {
458 assert (B->getOpcode() == BinaryOperator::LOr);
459 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
460 // we short-circuited, leading to a value of '1' for the '||' expression.
461 if (hasR2 == false) {
462 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
463 return;
464 }
465 }
466
467 // If we reach here we did not short-circuit. Assume R2 == true and
468 // R2 == false.
469
470 bool isFeasible;
471 StateTy St = Assume(PrevState, R2, true, isFeasible);
472
473 if (isFeasible)
474 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
475
476 St = Assume(PrevState, R2, false, isFeasible);
477
478 if (isFeasible)
479 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
480}
481
482
483
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000484void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000485 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000486
487 StmtEntryNode = builder.getLastNode();
488 CurrentStmt = S;
489 NodeSet Dst;
490 StateCleaned = false;
491
492 Visit(S, StmtEntryNode, Dst);
493
494 // If no nodes were generated, generate a new node that has all the
495 // dead mappings removed.
496 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
497 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
498 builder.generateNode(S, St, StmtEntryNode);
499 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000500
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000501 CurrentStmt = NULL;
502 StmtEntryNode = NULL;
503 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000504}
505
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000506GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000507GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000508
509 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000510 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000511 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000512
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000513 NodeTy* N = Builder->generateNode(S, St, Pred);
514 Dst.Add(N);
515 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000516}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000517
Ted Kremenekcba2e432008-02-05 19:35:18 +0000518void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
519 const StateTy::BufferTy& SB) {
520
521 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
522 Nodify(Dst, S, Pred, *I);
523}
524
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000525void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
526 if (D != CurrentStmt) {
527 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
528 return;
529 }
530
531 // If we are here, we are loading the value of the decl and binding
532 // it to the block-level expression.
533
534 StateTy St = Pred->getState();
535
536 Nodify(Dst, D, Pred,
537 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
538}
539
Ted Kremenekcba2e432008-02-05 19:35:18 +0000540void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000541
542 QualType T = CastE->getType();
543
544 // Check for redundant casts.
545 if (E->getType() == T) {
546 Dst.Add(Pred);
547 return;
548 }
549
550 NodeSet S1;
551 Visit(E, Pred, S1);
552
553 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
554 NodeTy* N = *I1;
555 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000556 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000557 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000558 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000559}
560
561void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
562 GRConstants::NodeSet& Dst) {
563
564 StateTy St = Pred->getState();
565
566 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000567 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
568 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000569 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000570 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000571 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000572
573 Nodify(Dst, DS, Pred, St);
574
575 if (Dst.empty())
576 Dst.Add(Pred);
577}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000578
Ted Kremenekf233d482008-02-05 00:26:40 +0000579
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000580void GRConstants::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000581 NodeTy* Pred, NodeSet& Dst) {
582
583 StateTy St = Pred->getState();
584
585 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000586 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000587
588 Nodify(Dst, S, Pred, SetValue(St, S, R));
589}
590
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000591/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
592void GRConstants::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
593 NodeTy* Pred,
594 NodeSet& Dst) {
595
596 // 6.5.3.4 sizeof: "The result type is an integer."
597
598 QualType T = S->getArgumentType();
599
600 // FIXME: Add support for VLAs.
601 if (isa<VariableArrayType>(T.getTypePtr()))
602 return;
603
604 SourceLocation L = S->getExprLoc();
605 uint64_t size = getContext().getTypeSize(T, L) / 8;
606
607 Nodify(Dst, S, Pred,
608 SetValue(Pred->getState(), S,
609 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
610
611}
612
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000613void GRConstants::VisitUnaryOperator(UnaryOperator* U,
614 GRConstants::NodeTy* Pred,
615 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000616
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000617 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000618 UnaryOperator::Opcode Op = U->getOpcode();
619
620 // FIXME: This is a hack so that for '*' and '&' we don't recurse
621 // on visiting the subexpression if it is a DeclRefExpr. We should
622 // probably just handle AddrOf and Deref in their own methods to make
623 // this cleaner.
624 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
625 isa<DeclRefExpr>(U->getSubExpr()))
626 S1.Add(Pred);
627 else
628 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000629
630 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
631 NodeTy* N1 = *I1;
632 StateTy St = N1->getState();
633
634 switch (U->getOpcode()) {
635 case UnaryOperator::PostInc: {
636 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000637 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000638
639 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
640 GetRValueConstant(1U, U));
641
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000642 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
643 break;
644 }
645
646 case UnaryOperator::PostDec: {
647 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000648 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000649
650 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
651 GetRValueConstant(1U, U));
652
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000653 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
654 break;
655 }
656
657 case UnaryOperator::PreInc: {
658 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000659 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000660
661 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
662 GetRValueConstant(1U, U));
663
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000664 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
665 break;
666 }
667
668 case UnaryOperator::PreDec: {
669 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000670 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000671
672 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
673 GetRValueConstant(1U, U));
674
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000675 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
676 break;
677 }
678
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000679 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000680 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000681 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000682 break;
683 }
684
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000685 case UnaryOperator::Not: {
686 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000687 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000688 break;
689 }
690
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000691 case UnaryOperator::LNot: {
692 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
693 //
694 // Note: technically we do "E == 0", but this is the same in the
695 // transfer functions as "0 == E".
696
697 RValue V1 = GetValue(St, U->getSubExpr());
698
699 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000700 const LValue& L1 = cast<LValue>(V1);
701 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
702 Nodify(Dst, U, N1,
703 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
704 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000705 }
706 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000707 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000708 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000709 Nodify(Dst, U, N1,
710 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
711 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000712 }
713
714 break;
715 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000716
717 case UnaryOperator::SizeOf: {
718 // 6.5.3.4 sizeof: "The result type is an integer."
719
720 QualType T = U->getSubExpr()->getType();
721
722 // FIXME: Add support for VLAs.
723 if (isa<VariableArrayType>(T.getTypePtr()))
724 return;
725
726 SourceLocation L = U->getExprLoc();
727 uint64_t size = getContext().getTypeSize(T, L) / 8;
728
729 Nodify(Dst, U, N1,
730 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
731 getContext().IntTy, L)));
732
733 break;
734 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000735
Ted Kremenek64924852008-01-31 02:35:41 +0000736 case UnaryOperator::AddrOf: {
737 const LValue& L1 = GetLValue(St, U->getSubExpr());
738 Nodify(Dst, U, N1, SetValue(St, U, L1));
739 break;
740 }
741
742 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000743 // FIXME: Stop when dereferencing an uninitialized value.
744 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
745
746 const RValue& V = GetValue(St, U->getSubExpr());
747 const LValue& L1 = cast<LValue>(V);
748
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000749 // After a dereference, one of two possible situations arise:
750 // (1) A crash, because the pointer was NULL.
751 // (2) The pointer is not NULL, and the dereference works.
752 //
753 // We add these assumptions.
754
Ted Kremenek63a4f692008-02-07 06:04:18 +0000755 bool isFeasibleNotNull;
756
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000757 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000758 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
759
760 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000761 QualType T = U->getType();
762 Nodify(Dst, U, N1, SetValue(StNotNull, U,
763 GetValue(StNotNull, L1, &T)));
764 }
765
Ted Kremenek63a4f692008-02-07 06:04:18 +0000766 bool isFeasibleNull;
767
768 // "Assume" that the pointer is NULL.
769 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
770
771 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000772 // We don't use "Nodify" here because the node will be a sink
773 // and we have no intention of processing it later.
774 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
775
Ted Kremenek63a4f692008-02-07 06:04:18 +0000776 if (NullNode) {
777 NullNode->markAsSink();
778
779 if (isFeasibleNotNull)
780 ImplicitNullDeref.insert(NullNode);
781 else
782 ExplicitNullDeref.insert(NullNode);
783 }
784 }
785
Ted Kremenek64924852008-01-31 02:35:41 +0000786 break;
787 }
788
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000789 default: ;
790 assert (false && "Not implemented.");
791 }
792 }
793}
794
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000795void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
796 GRConstants::NodeSet& Dst) {
797
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000798 if (isa<DeclRefExpr>(E)) {
799 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000800 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000801 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000802
803 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
804 if (U->getOpcode() == UnaryOperator::Deref) {
805 Visit(U->getSubExpr(), Pred, Dst);
806 return;
807 }
808 }
809
810 Visit(E, Pred, Dst);
811}
812
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000813void GRConstants::VisitBinaryOperator(BinaryOperator* B,
814 GRConstants::NodeTy* Pred,
815 GRConstants::NodeSet& Dst) {
816 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000817
818 if (B->isAssignmentOp())
819 VisitAssignmentLHS(B->getLHS(), Pred, S1);
820 else
821 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000822
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000823 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
824 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000825
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000826 // When getting the value for the LHS, check if we are in an assignment.
827 // In such cases, we want to (initially) treat the LHS as an LValue,
828 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000829 // evaluated to LValueDecl's instead of to an NonLValue.
830 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000831 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
832 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000833
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000834 NodeSet S2;
835 Visit(B->getRHS(), N1, S2);
836
837 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000838
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000839 NodeTy* N2 = *I2;
840 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000841 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000842
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000843 BinaryOperator::Opcode Op = B->getOpcode();
844
845 if (Op <= BinaryOperator::Or) {
846
Ted Kremenek22031182008-02-08 02:57:34 +0000847 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000848 Nodify(Dst, B, N2, SetValue(St, B, V1));
849 continue;
850 }
851
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000852 if (isa<LValue>(V1)) {
853 // FIXME: Add support for RHS being a non-lvalue.
854 const LValue& L1 = cast<LValue>(V1);
855 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000856
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000857 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
858 }
859 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000860 const NonLValue& R1 = cast<NonLValue>(V1);
861 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000862
863 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000864 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000865
866 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000867
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000868 }
869
870 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000871 case BinaryOperator::Assign: {
872 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000873 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000874 break;
875 }
876
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000877 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000878
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000879 assert (B->isCompoundAssignmentOp());
880
881 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000882 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000883
Ted Kremenekda9bd092008-02-08 07:05:39 +0000884 if (Op >= BinaryOperator::AndAssign)
885 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
886 else
887 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000888
889 if (isa<LValue>(V2)) {
890 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000891 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000892 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000893 }
894 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000895 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000896 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000897 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000898 }
899
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000900 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000901 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000902 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000903 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000904 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000905 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000906}
Ted Kremenekee985462008-01-16 18:18:48 +0000907
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000908
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000909void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
910 GRConstants::NodeSet& Dst) {
911
912 // FIXME: add metadata to the CFG so that we can disable
913 // this check when we KNOW that there is no block-level subexpression.
914 // The motivation is that this check requires a hashtable lookup.
915
916 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
917 Dst.Add(Pred);
918 return;
919 }
920
921 switch (S->getStmtClass()) {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000922 case Stmt::BinaryOperatorClass: {
923 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000924
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000925 if (B->isLogicalOp()) {
926 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000927 break;
928 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000929 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000930 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000931 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000932 break;
933 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000934
935 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
936 break;
937 }
938
939 case Stmt::CastExprClass: {
940 CastExpr* C = cast<CastExpr>(S);
941 VisitCast(C, C->getSubExpr(), Pred, Dst);
942 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000943 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000944
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000945 case Stmt::ChooseExprClass: { // __builtin_choose_expr
946 ChooseExpr* C = cast<ChooseExpr>(S);
947 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
948 break;
949 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000950
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000951 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000952 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
953 break;
954
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000955 case Stmt::ConditionalOperatorClass: { // '?' operator
956 ConditionalOperator* C = cast<ConditionalOperator>(S);
957 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
958 break;
959 }
960
961 case Stmt::DeclRefExprClass:
962 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
963 break;
964
965 case Stmt::DeclStmtClass:
966 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
967 break;
968
969 case Stmt::ImplicitCastExprClass: {
970 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
971 VisitCast(C, C->getSubExpr(), Pred, Dst);
972 break;
973 }
974
975 case Stmt::ParenExprClass:
976 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
977 break;
978
979 case Stmt::SizeOfAlignOfTypeExprClass:
980 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
981 break;
982
Ted Kremenekda9bd092008-02-08 07:05:39 +0000983 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000984 StmtExpr* SE = cast<StmtExpr>(S);
985
Ted Kremenekda9bd092008-02-08 07:05:39 +0000986 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000987 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
988 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000989 break;
990 }
991
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000992 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000993 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
994 Visit(R, Pred, Dst);
995 else
996 Dst.Add(Pred);
997
998 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000999 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001000
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001001 case Stmt::UnaryOperatorClass:
1002 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001003 break;
1004
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001005 default:
1006 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1007 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001008 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001009}
1010
Ted Kremenekee985462008-01-16 18:18:48 +00001011//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001012// "Assume" logic.
1013//===----------------------------------------------------------------------===//
1014
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001015GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1016 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001017 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001018
1019 switch (Cond.getSubKind()) {
1020 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001021 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001022 return St;
1023
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001024 case lval::SymbolValKind:
1025 if (Assumption)
1026 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1027 ValMgr.getZeroWithPtrWidth(), isFeasible);
1028 else
1029 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1030 ValMgr.getZeroWithPtrWidth(), isFeasible);
1031
Ted Kremenek08b66252008-02-06 04:31:33 +00001032
Ted Kremenek329f8542008-02-05 21:52:21 +00001033 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001034 isFeasible = Assumption;
1035 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001036
Ted Kremenek329f8542008-02-05 21:52:21 +00001037 case lval::ConcreteIntKind: {
1038 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001039 isFeasible = b ? Assumption : !Assumption;
1040 return St;
1041 }
1042 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001043}
1044
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001045GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1046 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001047 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001048
1049 switch (Cond.getSubKind()) {
1050 default:
1051 assert (false && "'Assume' not implemented for this NonLValue.");
1052 return St;
1053
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001054
1055 case nonlval::SymbolValKind: {
1056 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1057 SymbolID sym = SV.getSymbol();
1058
1059 if (Assumption)
1060 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1061 isFeasible);
1062 else
1063 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1064 isFeasible);
1065 }
1066
Ted Kremenek08b66252008-02-06 04:31:33 +00001067 case nonlval::SymIntConstraintValKind:
1068 return
1069 AssumeSymInt(St, Assumption,
1070 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1071 isFeasible);
1072
Ted Kremenek329f8542008-02-05 21:52:21 +00001073 case nonlval::ConcreteIntKind: {
1074 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001075 isFeasible = b ? Assumption : !Assumption;
1076 return St;
1077 }
1078 }
1079}
1080
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001081GRConstants::StateTy
1082GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1083 const llvm::APSInt& V, bool& isFeasible) {
1084
1085 // First, determine if sym == X, where X != V.
1086 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1087 isFeasible = *X != V;
1088 return St;
1089 }
1090
1091 // Second, determine if sym != V.
1092 if (St.isNotEqual(sym, V)) {
1093 isFeasible = true;
1094 return St;
1095 }
1096
1097 // If we reach here, sym is not a constant and we don't know if it is != V.
1098 // Make that assumption.
1099
1100 isFeasible = true;
1101 return StateMgr.AddNE(St, sym, V);
1102}
1103
1104GRConstants::StateTy
1105GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1106 const llvm::APSInt& V, bool& isFeasible) {
1107
1108 // First, determine if sym == X, where X != V.
1109 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1110 isFeasible = *X == V;
1111 return St;
1112 }
1113
1114 // Second, determine if sym != V.
1115 if (St.isNotEqual(sym, V)) {
1116 isFeasible = false;
1117 return St;
1118 }
1119
1120 // If we reach here, sym is not a constant and we don't know if it is == V.
1121 // Make that assumption.
1122
1123 isFeasible = true;
1124 return StateMgr.AddEQ(St, sym, V);
1125}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001126
Ted Kremenek08b66252008-02-06 04:31:33 +00001127GRConstants::StateTy
1128GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1129 const SymIntConstraint& C, bool& isFeasible) {
1130
1131 switch (C.getOpcode()) {
1132 default:
1133 // No logic yet for other operators.
1134 return St;
1135
1136 case BinaryOperator::EQ:
1137 if (Assumption)
1138 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1139 else
1140 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1141
1142 case BinaryOperator::NE:
1143 if (Assumption)
1144 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1145 else
1146 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1147 }
1148}
1149
Ted Kremenekb38911f2008-01-30 23:03:39 +00001150//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001151// Driver.
1152//===----------------------------------------------------------------------===//
1153
Ted Kremenekaa66a322008-01-16 21:46:15 +00001154#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001155static GRConstants* GraphPrintCheckerState;
1156
Ted Kremenekaa66a322008-01-16 21:46:15 +00001157namespace llvm {
1158template<>
1159struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1160 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001161
1162 static void PrintVarBindings(std::ostream& Out, GRConstants::StateTy St) {
1163
1164 Out << "Variables:\\l";
1165
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001166 bool isFirst = true;
1167
Ted Kremenek016f52f2008-02-08 21:10:02 +00001168 for (GRConstants::StateTy::vb_iterator I=St.vb_begin(),
1169 E=St.vb_end(); I!=E;++I) {
1170
1171 if (isFirst)
1172 isFirst = false;
1173 else
1174 Out << "\\l";
1175
1176 Out << ' ' << I.getKey()->getName() << " : ";
1177 I.getData().print(Out);
1178 }
1179
1180 }
1181
Ted Kremeneke7d22112008-02-11 19:21:59 +00001182
1183 static void PrintSubExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1184
1185 bool isFirst = true;
1186
1187 for (GRConstants::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
1188 I != E;++I) {
1189
1190 if (isFirst) {
1191 Out << "\\l\\lSub-Expressions:\\l";
1192 isFirst = false;
1193 }
1194 else
1195 Out << "\\l";
1196
1197 Out << " (" << (void*) I.getKey() << ") ";
1198 I.getKey()->printPretty(Out);
1199 Out << " : ";
1200 I.getData().print(Out);
1201 }
1202 }
1203
1204 static void PrintBlkExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1205
Ted Kremenek016f52f2008-02-08 21:10:02 +00001206 bool isFirst = true;
1207
Ted Kremeneke7d22112008-02-11 19:21:59 +00001208 for (GRConstants::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
1209 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001210 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001211 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001212 isFirst = false;
1213 }
1214 else
1215 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001216
Ted Kremeneke7d22112008-02-11 19:21:59 +00001217 Out << " (" << (void*) I.getKey() << ") ";
1218 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001219 Out << " : ";
1220 I.getData().print(Out);
1221 }
1222 }
1223
Ted Kremeneked4de312008-02-06 03:56:15 +00001224 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1225 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1226
1227 if (CE.isEmpty())
1228 return;
1229
1230 Out << "\\l\\|'==' constraints:";
1231
1232 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1233 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1234 }
1235
1236 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1237 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1238
1239 if (NE.isEmpty())
1240 return;
1241
1242 Out << "\\l\\|'!=' constraints:";
1243
1244 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1245 I != EI; ++I){
1246
1247 Out << "\\l $" << I.getKey() << " : ";
1248 bool isFirst = true;
1249
1250 ValueState::IntSetTy::iterator J=I.getData().begin(),
1251 EJ=I.getData().end();
1252 for ( ; J != EJ; ++J) {
1253 if (isFirst) isFirst = false;
1254 else Out << ", ";
1255
1256 Out << (*J)->toString();
1257 }
1258 }
1259 }
1260
Ted Kremenekaa66a322008-01-16 21:46:15 +00001261 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1262 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001263
1264 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001265 ProgramPoint Loc = N->getLocation();
1266
1267 switch (Loc.getKind()) {
1268 case ProgramPoint::BlockEntranceKind:
1269 Out << "Block Entrance: B"
1270 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1271 break;
1272
1273 case ProgramPoint::BlockExitKind:
1274 assert (false);
1275 break;
1276
1277 case ProgramPoint::PostStmtKind: {
1278 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001279 Out << L.getStmt()->getStmtClassName() << ':'
1280 << (void*) L.getStmt() << ' ';
1281
Ted Kremenekaa66a322008-01-16 21:46:15 +00001282 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001283
1284 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1285 Out << "\\|Implicit-Null Dereference.\\l";
1286 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001287 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1288 Out << "\\|Explicit-Null Dereference.\\l";
1289 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001290
Ted Kremenekaa66a322008-01-16 21:46:15 +00001291 break;
1292 }
1293
1294 default: {
1295 const BlockEdge& E = cast<BlockEdge>(Loc);
1296 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1297 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001298
1299 if (Stmt* T = E.getSrc()->getTerminator()) {
1300 Out << "\\|Terminator: ";
1301 E.getSrc()->printTerminator(Out);
1302
1303 if (isa<SwitchStmt>(T)) {
1304 // FIXME
1305 }
1306 else {
1307 Out << "\\lCondition: ";
1308 if (*E.getSrc()->succ_begin() == E.getDst())
1309 Out << "true";
1310 else
1311 Out << "false";
1312 }
1313
1314 Out << "\\l";
1315 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001316
1317 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1318 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1319 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001320 }
1321 }
1322
Ted Kremenek9153f732008-02-05 07:17:49 +00001323 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001324
Ted Kremeneke7d22112008-02-11 19:21:59 +00001325 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001326
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001327 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001328 return Out.str();
1329 }
1330};
1331} // end llvm namespace
1332#endif
1333
Ted Kremenekee985462008-01-16 18:18:48 +00001334namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001335void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1336 Diagnostic& Diag) {
1337
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001338 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001339 Engine.ExecuteWorkList();
1340
1341 // Look for explicit-Null dereferences and warn about them.
1342 GRConstants* CheckerState = &Engine.getCheckerState();
1343
1344 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1345 E=CheckerState->null_end(); I!=E; ++I) {
1346
1347 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1348 Expr* E = cast<Expr>(L.getStmt());
1349
1350 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1351 diag::chkr_null_deref_after_check);
1352 }
1353
1354
Ted Kremenekaa66a322008-01-16 21:46:15 +00001355#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001356 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001357 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001358 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001359#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001360}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001361} // end clang namespace