blob: 86ac5ef1aab87d23fd0f41b3b47fe7adb34e7042 [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
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000275 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000276 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
277
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000278 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000279
Ted Kremenek230aaab2008-02-12 21:37:25 +0000280 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
281 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
282
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000283 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
284 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
285
Ted Kremenek9de04c42008-01-24 20:55:43 +0000286 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000287 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
288
289 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000290 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000291 NodeTy* Pred, NodeSet& Dst);
292
293 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
294 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000295
296 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
297 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
298 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000299
300 /// VisitUnaryOperator - Transfer function logic for unary operators.
301 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
302
Ted Kremenekd27f8162008-01-15 23:55:06 +0000303};
304} // end anonymous namespace
305
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000306
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000307GRConstants::StateTy
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000308GRConstants::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000309
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000310 if (!StateCleaned) {
311 St = RemoveDeadBindings(CurrentStmt, St);
312 StateCleaned = true;
313 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000314
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000315 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000316
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000317 if (S == CurrentStmt) {
318 isBlkExpr = getCFG().isBlkExpr(S);
319
320 if (!isBlkExpr)
321 return St;
322 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000323
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000324 return StateMgr.SetValue(St, S, isBlkExpr, V);
325}
326
Ted Kremenekcba2e432008-02-05 19:35:18 +0000327const GRConstants::StateTy::BufferTy&
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000328GRConstants::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000329 StateTy::BufferTy& RetBuf) {
330
331 assert (RetBuf.empty());
332
333 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
334 RetBuf.push_back(SetValue(St, S, *I));
335
336 return RetBuf;
337}
338
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000339GRConstants::StateTy
340GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
341
Ted Kremenek53c641a2008-02-08 03:02:48 +0000342 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000343 return St;
344
345 if (!StateCleaned) {
346 St = RemoveDeadBindings(CurrentStmt, St);
347 StateCleaned = true;
348 }
349
350 return StateMgr.SetValue(St, LV, V);
351}
352
Ted Kremenekf233d482008-02-05 00:26:40 +0000353void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000354 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000355
Ted Kremeneke7d22112008-02-11 19:21:59 +0000356 // Remove old bindings for subexpressions.
357 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000358
Ted Kremenekb38911f2008-01-30 23:03:39 +0000359 RValue V = GetValue(PrevState, Condition);
360
361 switch (V.getBaseKind()) {
362 default:
363 break;
364
Ted Kremenek53c641a2008-02-08 03:02:48 +0000365 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000366 builder.generateNode(PrevState, true);
367 builder.generateNode(PrevState, false);
368 return;
369
370 case RValue::UninitializedKind: {
371 NodeTy* N = builder.generateNode(PrevState, true);
372
373 if (N) {
374 N->markAsSink();
375 UninitBranches.insert(N);
376 }
377
378 builder.markInfeasible(false);
379 return;
380 }
381 }
382
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000383 // Get the current block counter.
384 GRBlockCounter BC = builder.getBlockCounter();
385
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000386 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
387 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000388
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000389 if (isa<nonlval::ConcreteInt>(V) ||
390 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
391
392 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000393
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000394 bool isFeasible = true;
395
396 StateTy St = Assume(PrevState, V, true, isFeasible);
397
398 if (isFeasible)
399 builder.generateNode(St, true);
400 else
401 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000402 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000403 else
404 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000405
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000406 BlockID = builder.getTargetBlock(false)->getBlockID();
407 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000408
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000409 if (isa<nonlval::ConcreteInt>(V) ||
410 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
411
412 // Process the false branch.
413
414 bool isFeasible = false;
415
416 StateTy St = Assume(PrevState, V, false, isFeasible);
417
418 if (isFeasible)
419 builder.generateNode(St, false);
420 else
421 builder.markInfeasible(false);
422 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000423 else
424 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000425}
426
Ted Kremenekf233d482008-02-05 00:26:40 +0000427
428void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
429 NodeSet& Dst) {
430
431 bool hasR2;
432 StateTy PrevState = Pred->getState();
433
434 RValue R1 = GetValue(PrevState, B->getLHS());
435 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
436
Ted Kremenek22031182008-02-08 02:57:34 +0000437 if (isa<UnknownVal>(R1) &&
438 (isa<UnknownVal>(R2) ||
439 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000440
441 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
442 return;
443 }
Ted Kremenek22031182008-02-08 02:57:34 +0000444 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000445 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
446 return;
447 }
448
449 // R1 is an expression that can evaluate to either 'true' or 'false'.
450 if (B->getOpcode() == BinaryOperator::LAnd) {
451 // hasR2 == 'false' means that LHS evaluated to 'false' and that
452 // we short-circuited, leading to a value of '0' for the '&&' expression.
453 if (hasR2 == false) {
454 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
455 return;
456 }
457 }
458 else {
459 assert (B->getOpcode() == BinaryOperator::LOr);
460 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
461 // we short-circuited, leading to a value of '1' for the '||' expression.
462 if (hasR2 == false) {
463 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
464 return;
465 }
466 }
467
468 // If we reach here we did not short-circuit. Assume R2 == true and
469 // R2 == false.
470
471 bool isFeasible;
472 StateTy St = Assume(PrevState, R2, true, isFeasible);
473
474 if (isFeasible)
475 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
476
477 St = Assume(PrevState, R2, false, isFeasible);
478
479 if (isFeasible)
480 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
481}
482
483
484
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000485void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000486 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000487
488 StmtEntryNode = builder.getLastNode();
489 CurrentStmt = S;
490 NodeSet Dst;
491 StateCleaned = false;
492
493 Visit(S, StmtEntryNode, Dst);
494
495 // If no nodes were generated, generate a new node that has all the
496 // dead mappings removed.
497 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
498 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
499 builder.generateNode(S, St, StmtEntryNode);
500 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000501
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000502 CurrentStmt = NULL;
503 StmtEntryNode = NULL;
504 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000505}
506
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000507GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000508GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000509
510 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000511 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000512 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000513
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000514 NodeTy* N = Builder->generateNode(S, St, Pred);
515 Dst.Add(N);
516 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000517}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000518
Ted Kremenekcba2e432008-02-05 19:35:18 +0000519void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
520 const StateTy::BufferTy& SB) {
521
522 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
523 Nodify(Dst, S, Pred, *I);
524}
525
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000526void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
527 if (D != CurrentStmt) {
528 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
529 return;
530 }
531
532 // If we are here, we are loading the value of the decl and binding
533 // it to the block-level expression.
534
535 StateTy St = Pred->getState();
536
537 Nodify(Dst, D, Pred,
538 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
539}
540
Ted Kremenekcba2e432008-02-05 19:35:18 +0000541void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000542
543 QualType T = CastE->getType();
544
545 // Check for redundant casts.
546 if (E->getType() == T) {
547 Dst.Add(Pred);
548 return;
549 }
550
551 NodeSet S1;
552 Visit(E, Pred, S1);
553
554 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
555 NodeTy* N = *I1;
556 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000557 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000558 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000559 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000560}
561
562void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
563 GRConstants::NodeSet& Dst) {
564
565 StateTy St = Pred->getState();
566
567 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000568 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
569 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000570 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000571 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000572 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000573
574 Nodify(Dst, DS, Pred, St);
575
576 if (Dst.empty())
577 Dst.Add(Pred);
578}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000579
Ted Kremenekf233d482008-02-05 00:26:40 +0000580
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000581void GRConstants::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000582 NodeTy* Pred, NodeSet& Dst) {
583
584 StateTy St = Pred->getState();
585
586 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000587 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000588
589 Nodify(Dst, S, Pred, SetValue(St, S, R));
590}
591
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000592/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
593void GRConstants::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
594 NodeTy* Pred,
595 NodeSet& Dst) {
596
597 // 6.5.3.4 sizeof: "The result type is an integer."
598
599 QualType T = S->getArgumentType();
600
601 // FIXME: Add support for VLAs.
602 if (isa<VariableArrayType>(T.getTypePtr()))
603 return;
604
605 SourceLocation L = S->getExprLoc();
606 uint64_t size = getContext().getTypeSize(T, L) / 8;
607
608 Nodify(Dst, S, Pred,
609 SetValue(Pred->getState(), S,
610 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
611
612}
613
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000614void GRConstants::VisitUnaryOperator(UnaryOperator* U,
615 GRConstants::NodeTy* Pred,
616 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000617
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000618 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000619 UnaryOperator::Opcode Op = U->getOpcode();
620
621 // FIXME: This is a hack so that for '*' and '&' we don't recurse
622 // on visiting the subexpression if it is a DeclRefExpr. We should
623 // probably just handle AddrOf and Deref in their own methods to make
624 // this cleaner.
625 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
626 isa<DeclRefExpr>(U->getSubExpr()))
627 S1.Add(Pred);
628 else
629 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000630
631 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
632 NodeTy* N1 = *I1;
633 StateTy St = N1->getState();
634
635 switch (U->getOpcode()) {
636 case UnaryOperator::PostInc: {
637 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000638 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000639
640 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
641 GetRValueConstant(1U, U));
642
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000643 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
644 break;
645 }
646
647 case UnaryOperator::PostDec: {
648 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000649 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000650
651 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
652 GetRValueConstant(1U, U));
653
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000654 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
655 break;
656 }
657
658 case UnaryOperator::PreInc: {
659 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000660 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000661
662 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
663 GetRValueConstant(1U, U));
664
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000665 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
666 break;
667 }
668
669 case UnaryOperator::PreDec: {
670 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000671 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000672
673 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
674 GetRValueConstant(1U, U));
675
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000676 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
677 break;
678 }
679
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000680 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000681 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000682 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000683 break;
684 }
685
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000686 case UnaryOperator::Not: {
687 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000688 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000689 break;
690 }
691
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000692 case UnaryOperator::LNot: {
693 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
694 //
695 // Note: technically we do "E == 0", but this is the same in the
696 // transfer functions as "0 == E".
697
698 RValue V1 = GetValue(St, U->getSubExpr());
699
700 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000701 const LValue& L1 = cast<LValue>(V1);
702 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
703 Nodify(Dst, U, N1,
704 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
705 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000706 }
707 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000708 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000709 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000710 Nodify(Dst, U, N1,
711 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
712 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000713 }
714
715 break;
716 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000717
718 case UnaryOperator::SizeOf: {
719 // 6.5.3.4 sizeof: "The result type is an integer."
720
721 QualType T = U->getSubExpr()->getType();
722
723 // FIXME: Add support for VLAs.
724 if (isa<VariableArrayType>(T.getTypePtr()))
725 return;
726
727 SourceLocation L = U->getExprLoc();
728 uint64_t size = getContext().getTypeSize(T, L) / 8;
729
730 Nodify(Dst, U, N1,
731 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
732 getContext().IntTy, L)));
733
734 break;
735 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000736
Ted Kremenek64924852008-01-31 02:35:41 +0000737 case UnaryOperator::AddrOf: {
738 const LValue& L1 = GetLValue(St, U->getSubExpr());
739 Nodify(Dst, U, N1, SetValue(St, U, L1));
740 break;
741 }
742
743 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000744 // FIXME: Stop when dereferencing an uninitialized value.
745 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
746
747 const RValue& V = GetValue(St, U->getSubExpr());
748 const LValue& L1 = cast<LValue>(V);
749
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000750 // After a dereference, one of two possible situations arise:
751 // (1) A crash, because the pointer was NULL.
752 // (2) The pointer is not NULL, and the dereference works.
753 //
754 // We add these assumptions.
755
Ted Kremenek63a4f692008-02-07 06:04:18 +0000756 bool isFeasibleNotNull;
757
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000758 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000759 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
760
761 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000762 QualType T = U->getType();
763 Nodify(Dst, U, N1, SetValue(StNotNull, U,
764 GetValue(StNotNull, L1, &T)));
765 }
766
Ted Kremenek63a4f692008-02-07 06:04:18 +0000767 bool isFeasibleNull;
768
769 // "Assume" that the pointer is NULL.
770 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
771
772 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000773 // We don't use "Nodify" here because the node will be a sink
774 // and we have no intention of processing it later.
775 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
776
Ted Kremenek63a4f692008-02-07 06:04:18 +0000777 if (NullNode) {
778 NullNode->markAsSink();
779
780 if (isFeasibleNotNull)
781 ImplicitNullDeref.insert(NullNode);
782 else
783 ExplicitNullDeref.insert(NullNode);
784 }
785 }
786
Ted Kremenek64924852008-01-31 02:35:41 +0000787 break;
788 }
789
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000790 default: ;
791 assert (false && "Not implemented.");
792 }
793 }
794}
795
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000796void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
797 GRConstants::NodeSet& Dst) {
798
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000799 if (isa<DeclRefExpr>(E)) {
800 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000801 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000802 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000803
804 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
805 if (U->getOpcode() == UnaryOperator::Deref) {
806 Visit(U->getSubExpr(), Pred, Dst);
807 return;
808 }
809 }
810
811 Visit(E, Pred, Dst);
812}
813
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000814void GRConstants::VisitBinaryOperator(BinaryOperator* B,
815 GRConstants::NodeTy* Pred,
816 GRConstants::NodeSet& Dst) {
817 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000818
819 if (B->isAssignmentOp())
820 VisitAssignmentLHS(B->getLHS(), Pred, S1);
821 else
822 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000823
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000824 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
825 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000826
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000827 // When getting the value for the LHS, check if we are in an assignment.
828 // In such cases, we want to (initially) treat the LHS as an LValue,
829 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000830 // evaluated to LValueDecl's instead of to an NonLValue.
831 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000832 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
833 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000834
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000835 NodeSet S2;
836 Visit(B->getRHS(), N1, S2);
837
838 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000839
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000840 NodeTy* N2 = *I2;
841 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000842 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000843
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000844 BinaryOperator::Opcode Op = B->getOpcode();
845
846 if (Op <= BinaryOperator::Or) {
847
Ted Kremenek22031182008-02-08 02:57:34 +0000848 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000849 Nodify(Dst, B, N2, SetValue(St, B, V1));
850 continue;
851 }
852
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000853 if (isa<LValue>(V1)) {
854 // FIXME: Add support for RHS being a non-lvalue.
855 const LValue& L1 = cast<LValue>(V1);
856 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000857
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000858 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
859 }
860 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000861 const NonLValue& R1 = cast<NonLValue>(V1);
862 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000863
864 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000865 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000866
867 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000868
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000869 }
870
871 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000872 case BinaryOperator::Assign: {
873 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000874 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000875 break;
876 }
877
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000878 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000879
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000880 assert (B->isCompoundAssignmentOp());
881
882 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000883 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000884
Ted Kremenekda9bd092008-02-08 07:05:39 +0000885 if (Op >= BinaryOperator::AndAssign)
886 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
887 else
888 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000889
890 if (isa<LValue>(V2)) {
891 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000892 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000893 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000894 }
895 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000896 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000897 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000898 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000899 }
900
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000901 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000902 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000903 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000904 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000905 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000906 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000907}
Ted Kremenekee985462008-01-16 18:18:48 +0000908
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000909
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000910void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
911 GRConstants::NodeSet& Dst) {
912
913 // FIXME: add metadata to the CFG so that we can disable
914 // this check when we KNOW that there is no block-level subexpression.
915 // The motivation is that this check requires a hashtable lookup.
916
917 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
918 Dst.Add(Pred);
919 return;
920 }
921
922 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000923
924 default:
925 // Cases we intentionally have "default" handle:
926 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
927
928 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
929 break;
930
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000931 case Stmt::BinaryOperatorClass: {
932 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000933
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000934 if (B->isLogicalOp()) {
935 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000936 break;
937 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000938 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000939 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000940 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000941 break;
942 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000943
944 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
945 break;
946 }
947
948 case Stmt::CastExprClass: {
949 CastExpr* C = cast<CastExpr>(S);
950 VisitCast(C, C->getSubExpr(), Pred, Dst);
951 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000952 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000953
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000954 case Stmt::ChooseExprClass: { // __builtin_choose_expr
955 ChooseExpr* C = cast<ChooseExpr>(S);
956 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
957 break;
958 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000959
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000960 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000961 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
962 break;
963
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000964 case Stmt::ConditionalOperatorClass: { // '?' operator
965 ConditionalOperator* C = cast<ConditionalOperator>(S);
966 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
967 break;
968 }
969
970 case Stmt::DeclRefExprClass:
971 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
972 break;
973
974 case Stmt::DeclStmtClass:
975 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
976 break;
977
978 case Stmt::ImplicitCastExprClass: {
979 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
980 VisitCast(C, C->getSubExpr(), Pred, Dst);
981 break;
982 }
983
984 case Stmt::ParenExprClass:
985 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
986 break;
987
988 case Stmt::SizeOfAlignOfTypeExprClass:
989 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
990 break;
991
Ted Kremenekda9bd092008-02-08 07:05:39 +0000992 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000993 StmtExpr* SE = cast<StmtExpr>(S);
994
Ted Kremenekda9bd092008-02-08 07:05:39 +0000995 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000996 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
997 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000998 break;
999 }
1000
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001001 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001002 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1003 Visit(R, Pred, Dst);
1004 else
1005 Dst.Add(Pred);
1006
1007 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001008 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001009
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001010 case Stmt::UnaryOperatorClass:
1011 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001012 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001013 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001014}
1015
Ted Kremenekee985462008-01-16 18:18:48 +00001016//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001017// "Assume" logic.
1018//===----------------------------------------------------------------------===//
1019
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001020GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1021 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001022 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001023
1024 switch (Cond.getSubKind()) {
1025 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001026 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001027 return St;
1028
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001029 case lval::SymbolValKind:
1030 if (Assumption)
1031 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1032 ValMgr.getZeroWithPtrWidth(), isFeasible);
1033 else
1034 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1035 ValMgr.getZeroWithPtrWidth(), isFeasible);
1036
Ted Kremenek08b66252008-02-06 04:31:33 +00001037
Ted Kremenek329f8542008-02-05 21:52:21 +00001038 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001039 isFeasible = Assumption;
1040 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001041
Ted Kremenek329f8542008-02-05 21:52:21 +00001042 case lval::ConcreteIntKind: {
1043 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001044 isFeasible = b ? Assumption : !Assumption;
1045 return St;
1046 }
1047 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001048}
1049
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001050GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1051 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001052 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001053
1054 switch (Cond.getSubKind()) {
1055 default:
1056 assert (false && "'Assume' not implemented for this NonLValue.");
1057 return St;
1058
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001059
1060 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001061 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001062 SymbolID sym = SV.getSymbol();
1063
1064 if (Assumption)
1065 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1066 isFeasible);
1067 else
1068 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1069 isFeasible);
1070 }
1071
Ted Kremenek08b66252008-02-06 04:31:33 +00001072 case nonlval::SymIntConstraintValKind:
1073 return
1074 AssumeSymInt(St, Assumption,
1075 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1076 isFeasible);
1077
Ted Kremenek329f8542008-02-05 21:52:21 +00001078 case nonlval::ConcreteIntKind: {
1079 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001080 isFeasible = b ? Assumption : !Assumption;
1081 return St;
1082 }
1083 }
1084}
1085
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001086GRConstants::StateTy
1087GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1088 const llvm::APSInt& V, bool& isFeasible) {
1089
1090 // First, determine if sym == X, where X != V.
1091 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1092 isFeasible = *X != V;
1093 return St;
1094 }
1095
1096 // Second, determine if sym != V.
1097 if (St.isNotEqual(sym, V)) {
1098 isFeasible = true;
1099 return St;
1100 }
1101
1102 // If we reach here, sym is not a constant and we don't know if it is != V.
1103 // Make that assumption.
1104
1105 isFeasible = true;
1106 return StateMgr.AddNE(St, sym, V);
1107}
1108
1109GRConstants::StateTy
1110GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1111 const llvm::APSInt& V, bool& isFeasible) {
1112
1113 // First, determine if sym == X, where X != V.
1114 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1115 isFeasible = *X == V;
1116 return St;
1117 }
1118
1119 // Second, determine if sym != V.
1120 if (St.isNotEqual(sym, V)) {
1121 isFeasible = false;
1122 return St;
1123 }
1124
1125 // If we reach here, sym is not a constant and we don't know if it is == V.
1126 // Make that assumption.
1127
1128 isFeasible = true;
1129 return StateMgr.AddEQ(St, sym, V);
1130}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001131
Ted Kremenek08b66252008-02-06 04:31:33 +00001132GRConstants::StateTy
1133GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1134 const SymIntConstraint& C, bool& isFeasible) {
1135
1136 switch (C.getOpcode()) {
1137 default:
1138 // No logic yet for other operators.
1139 return St;
1140
1141 case BinaryOperator::EQ:
1142 if (Assumption)
1143 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1144 else
1145 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1146
1147 case BinaryOperator::NE:
1148 if (Assumption)
1149 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1150 else
1151 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1152 }
1153}
1154
Ted Kremenekb38911f2008-01-30 23:03:39 +00001155//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001156// Driver.
1157//===----------------------------------------------------------------------===//
1158
Ted Kremenekaa66a322008-01-16 21:46:15 +00001159#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001160static GRConstants* GraphPrintCheckerState;
1161
Ted Kremenekaa66a322008-01-16 21:46:15 +00001162namespace llvm {
1163template<>
1164struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1165 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001166
1167 static void PrintVarBindings(std::ostream& Out, GRConstants::StateTy St) {
1168
1169 Out << "Variables:\\l";
1170
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001171 bool isFirst = true;
1172
Ted Kremenek016f52f2008-02-08 21:10:02 +00001173 for (GRConstants::StateTy::vb_iterator I=St.vb_begin(),
1174 E=St.vb_end(); I!=E;++I) {
1175
1176 if (isFirst)
1177 isFirst = false;
1178 else
1179 Out << "\\l";
1180
1181 Out << ' ' << I.getKey()->getName() << " : ";
1182 I.getData().print(Out);
1183 }
1184
1185 }
1186
Ted Kremeneke7d22112008-02-11 19:21:59 +00001187
1188 static void PrintSubExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1189
1190 bool isFirst = true;
1191
1192 for (GRConstants::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
1193 I != E;++I) {
1194
1195 if (isFirst) {
1196 Out << "\\l\\lSub-Expressions:\\l";
1197 isFirst = false;
1198 }
1199 else
1200 Out << "\\l";
1201
1202 Out << " (" << (void*) I.getKey() << ") ";
1203 I.getKey()->printPretty(Out);
1204 Out << " : ";
1205 I.getData().print(Out);
1206 }
1207 }
1208
1209 static void PrintBlkExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1210
Ted Kremenek016f52f2008-02-08 21:10:02 +00001211 bool isFirst = true;
1212
Ted Kremeneke7d22112008-02-11 19:21:59 +00001213 for (GRConstants::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
1214 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001215 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001216 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001217 isFirst = false;
1218 }
1219 else
1220 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001221
Ted Kremeneke7d22112008-02-11 19:21:59 +00001222 Out << " (" << (void*) I.getKey() << ") ";
1223 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001224 Out << " : ";
1225 I.getData().print(Out);
1226 }
1227 }
1228
Ted Kremeneked4de312008-02-06 03:56:15 +00001229 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1230 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1231
1232 if (CE.isEmpty())
1233 return;
1234
1235 Out << "\\l\\|'==' constraints:";
1236
1237 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1238 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1239 }
1240
1241 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1242 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1243
1244 if (NE.isEmpty())
1245 return;
1246
1247 Out << "\\l\\|'!=' constraints:";
1248
1249 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1250 I != EI; ++I){
1251
1252 Out << "\\l $" << I.getKey() << " : ";
1253 bool isFirst = true;
1254
1255 ValueState::IntSetTy::iterator J=I.getData().begin(),
1256 EJ=I.getData().end();
1257 for ( ; J != EJ; ++J) {
1258 if (isFirst) isFirst = false;
1259 else Out << ", ";
1260
1261 Out << (*J)->toString();
1262 }
1263 }
1264 }
1265
Ted Kremenekaa66a322008-01-16 21:46:15 +00001266 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1267 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001268
1269 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001270 ProgramPoint Loc = N->getLocation();
1271
1272 switch (Loc.getKind()) {
1273 case ProgramPoint::BlockEntranceKind:
1274 Out << "Block Entrance: B"
1275 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1276 break;
1277
1278 case ProgramPoint::BlockExitKind:
1279 assert (false);
1280 break;
1281
1282 case ProgramPoint::PostStmtKind: {
1283 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001284 Out << L.getStmt()->getStmtClassName() << ':'
1285 << (void*) L.getStmt() << ' ';
1286
Ted Kremenekaa66a322008-01-16 21:46:15 +00001287 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001288
1289 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1290 Out << "\\|Implicit-Null Dereference.\\l";
1291 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001292 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1293 Out << "\\|Explicit-Null Dereference.\\l";
1294 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001295
Ted Kremenekaa66a322008-01-16 21:46:15 +00001296 break;
1297 }
1298
1299 default: {
1300 const BlockEdge& E = cast<BlockEdge>(Loc);
1301 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1302 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001303
1304 if (Stmt* T = E.getSrc()->getTerminator()) {
1305 Out << "\\|Terminator: ";
1306 E.getSrc()->printTerminator(Out);
1307
1308 if (isa<SwitchStmt>(T)) {
1309 // FIXME
1310 }
1311 else {
1312 Out << "\\lCondition: ";
1313 if (*E.getSrc()->succ_begin() == E.getDst())
1314 Out << "true";
1315 else
1316 Out << "false";
1317 }
1318
1319 Out << "\\l";
1320 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001321
1322 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1323 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1324 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001325 }
1326 }
1327
Ted Kremenek9153f732008-02-05 07:17:49 +00001328 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001329
Ted Kremeneke7d22112008-02-11 19:21:59 +00001330 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001331
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001332 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001333 return Out.str();
1334 }
1335};
1336} // end llvm namespace
1337#endif
1338
Ted Kremenekee985462008-01-16 18:18:48 +00001339namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001340void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1341 Diagnostic& Diag) {
1342
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001343 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001344 Engine.ExecuteWorkList();
1345
1346 // Look for explicit-Null dereferences and warn about them.
1347 GRConstants* CheckerState = &Engine.getCheckerState();
1348
1349 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1350 E=CheckerState->null_end(); I!=E; ++I) {
1351
1352 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1353 Expr* E = cast<Expr>(L.getStmt());
1354
1355 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1356 diag::chkr_null_deref_after_check);
1357 }
1358
1359
Ted Kremenekaa66a322008-01-16 21:46:15 +00001360#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001361 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001362 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001363 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001364#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001365}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001366} // end clang namespace