blob: 8b94aaa70b8a223dbd16d8f43cb5d8ab1a3289c2 [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 Kremenekd27f8162008-01-15 23:55:06 +0000298};
299} // end anonymous namespace
300
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000301
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000302GRConstants::StateTy
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000303GRConstants::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000304
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000305 if (!StateCleaned) {
306 St = RemoveDeadBindings(CurrentStmt, St);
307 StateCleaned = true;
308 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000309
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000310 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000311
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000312 if (S == CurrentStmt) {
313 isBlkExpr = getCFG().isBlkExpr(S);
314
315 if (!isBlkExpr)
316 return St;
317 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000318
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000319 return StateMgr.SetValue(St, S, isBlkExpr, V);
320}
321
Ted Kremenekcba2e432008-02-05 19:35:18 +0000322const GRConstants::StateTy::BufferTy&
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000323GRConstants::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000324 StateTy::BufferTy& RetBuf) {
325
326 assert (RetBuf.empty());
327
328 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
329 RetBuf.push_back(SetValue(St, S, *I));
330
331 return RetBuf;
332}
333
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000334GRConstants::StateTy
335GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
336
Ted Kremenek53c641a2008-02-08 03:02:48 +0000337 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000338 return St;
339
340 if (!StateCleaned) {
341 St = RemoveDeadBindings(CurrentStmt, St);
342 StateCleaned = true;
343 }
344
345 return StateMgr.SetValue(St, LV, V);
346}
347
Ted Kremenekf233d482008-02-05 00:26:40 +0000348void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000349 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000350
Ted Kremeneke7d22112008-02-11 19:21:59 +0000351 // Remove old bindings for subexpressions.
352 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000353
Ted Kremenekb38911f2008-01-30 23:03:39 +0000354 RValue V = GetValue(PrevState, Condition);
355
356 switch (V.getBaseKind()) {
357 default:
358 break;
359
Ted Kremenek53c641a2008-02-08 03:02:48 +0000360 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000361 builder.generateNode(PrevState, true);
362 builder.generateNode(PrevState, false);
363 return;
364
365 case RValue::UninitializedKind: {
366 NodeTy* N = builder.generateNode(PrevState, true);
367
368 if (N) {
369 N->markAsSink();
370 UninitBranches.insert(N);
371 }
372
373 builder.markInfeasible(false);
374 return;
375 }
376 }
377
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000378 // Get the current block counter.
379 GRBlockCounter BC = builder.getBlockCounter();
380
381 unsigned NumVisited = BC.getNumVisited(builder.getTargetBlock(true)->getBlockID());
Ted Kremenekf233d482008-02-05 00:26:40 +0000382
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000383 if (isa<nonlval::ConcreteInt>(V) ||
384 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
385
386 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000387
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000388 bool isFeasible = true;
389
390 StateTy St = Assume(PrevState, V, true, isFeasible);
391
392 if (isFeasible)
393 builder.generateNode(St, true);
394 else
395 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000396 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000397 else
398 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000399
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000400 NumVisited = BC.getNumVisited(builder.getTargetBlock(true)->getBlockID());
401
Ted Kremenekb38911f2008-01-30 23:03:39 +0000402
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000403 if (isa<nonlval::ConcreteInt>(V) ||
404 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
405
406 // Process the false branch.
407
408 bool isFeasible = false;
409
410 StateTy St = Assume(PrevState, V, false, isFeasible);
411
412 if (isFeasible)
413 builder.generateNode(St, false);
414 else
415 builder.markInfeasible(false);
416 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000417 else
418 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000419}
420
Ted Kremenekf233d482008-02-05 00:26:40 +0000421
422void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
423 NodeSet& Dst) {
424
425 bool hasR2;
426 StateTy PrevState = Pred->getState();
427
428 RValue R1 = GetValue(PrevState, B->getLHS());
429 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
430
Ted Kremenek22031182008-02-08 02:57:34 +0000431 if (isa<UnknownVal>(R1) &&
432 (isa<UnknownVal>(R2) ||
433 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000434
435 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
436 return;
437 }
Ted Kremenek22031182008-02-08 02:57:34 +0000438 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000439 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
440 return;
441 }
442
443 // R1 is an expression that can evaluate to either 'true' or 'false'.
444 if (B->getOpcode() == BinaryOperator::LAnd) {
445 // hasR2 == 'false' means that LHS evaluated to 'false' and that
446 // we short-circuited, leading to a value of '0' for the '&&' expression.
447 if (hasR2 == false) {
448 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
449 return;
450 }
451 }
452 else {
453 assert (B->getOpcode() == BinaryOperator::LOr);
454 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
455 // we short-circuited, leading to a value of '1' for the '||' expression.
456 if (hasR2 == false) {
457 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
458 return;
459 }
460 }
461
462 // If we reach here we did not short-circuit. Assume R2 == true and
463 // R2 == false.
464
465 bool isFeasible;
466 StateTy St = Assume(PrevState, R2, true, isFeasible);
467
468 if (isFeasible)
469 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
470
471 St = Assume(PrevState, R2, false, isFeasible);
472
473 if (isFeasible)
474 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
475}
476
477
478
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000479void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000480 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000481
482 StmtEntryNode = builder.getLastNode();
483 CurrentStmt = S;
484 NodeSet Dst;
485 StateCleaned = false;
486
487 Visit(S, StmtEntryNode, Dst);
488
489 // If no nodes were generated, generate a new node that has all the
490 // dead mappings removed.
491 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
492 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
493 builder.generateNode(S, St, StmtEntryNode);
494 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000495
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000496 CurrentStmt = NULL;
497 StmtEntryNode = NULL;
498 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000499}
500
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000501GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000502GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000503
504 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000505 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000506 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000507
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000508 NodeTy* N = Builder->generateNode(S, St, Pred);
509 Dst.Add(N);
510 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000511}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000512
Ted Kremenekcba2e432008-02-05 19:35:18 +0000513void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
514 const StateTy::BufferTy& SB) {
515
516 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
517 Nodify(Dst, S, Pred, *I);
518}
519
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000520void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
521 if (D != CurrentStmt) {
522 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
523 return;
524 }
525
526 // If we are here, we are loading the value of the decl and binding
527 // it to the block-level expression.
528
529 StateTy St = Pred->getState();
530
531 Nodify(Dst, D, Pred,
532 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
533}
534
Ted Kremenekcba2e432008-02-05 19:35:18 +0000535void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000536
537 QualType T = CastE->getType();
538
539 // Check for redundant casts.
540 if (E->getType() == T) {
541 Dst.Add(Pred);
542 return;
543 }
544
545 NodeSet S1;
546 Visit(E, Pred, S1);
547
548 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
549 NodeTy* N = *I1;
550 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000551 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000552 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000553 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000554}
555
556void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
557 GRConstants::NodeSet& Dst) {
558
559 StateTy St = Pred->getState();
560
561 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000562 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
563 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000564 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000565 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000566 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000567
568 Nodify(Dst, DS, Pred, St);
569
570 if (Dst.empty())
571 Dst.Add(Pred);
572}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000573
Ted Kremenekf233d482008-02-05 00:26:40 +0000574
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000575void GRConstants::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000576 NodeTy* Pred, NodeSet& Dst) {
577
578 StateTy St = Pred->getState();
579
580 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000581 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000582
583 Nodify(Dst, S, Pred, SetValue(St, S, R));
584}
585
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000586void GRConstants::VisitUnaryOperator(UnaryOperator* U,
587 GRConstants::NodeTy* Pred,
588 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000589
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000590 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000591 UnaryOperator::Opcode Op = U->getOpcode();
592
593 // FIXME: This is a hack so that for '*' and '&' we don't recurse
594 // on visiting the subexpression if it is a DeclRefExpr. We should
595 // probably just handle AddrOf and Deref in their own methods to make
596 // this cleaner.
597 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
598 isa<DeclRefExpr>(U->getSubExpr()))
599 S1.Add(Pred);
600 else
601 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000602
603 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
604 NodeTy* N1 = *I1;
605 StateTy St = N1->getState();
606
607 switch (U->getOpcode()) {
608 case UnaryOperator::PostInc: {
609 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000610 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000611
612 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
613 GetRValueConstant(1U, U));
614
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000615 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
616 break;
617 }
618
619 case UnaryOperator::PostDec: {
620 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000621 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000622
623 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
624 GetRValueConstant(1U, U));
625
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000626 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
627 break;
628 }
629
630 case UnaryOperator::PreInc: {
631 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000632 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000633
634 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
635 GetRValueConstant(1U, U));
636
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000637 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
638 break;
639 }
640
641 case UnaryOperator::PreDec: {
642 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000643 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000644
645 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
646 GetRValueConstant(1U, U));
647
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000648 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
649 break;
650 }
651
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000652 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000653 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000654 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000655 break;
656 }
657
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000658 case UnaryOperator::Not: {
659 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000660 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000661 break;
662 }
663
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000664 case UnaryOperator::LNot: {
665 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
666 //
667 // Note: technically we do "E == 0", but this is the same in the
668 // transfer functions as "0 == E".
669
670 RValue V1 = GetValue(St, U->getSubExpr());
671
672 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000673 const LValue& L1 = cast<LValue>(V1);
674 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
675 Nodify(Dst, U, N1,
676 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
677 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000678 }
679 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000680 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000681 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000682 Nodify(Dst, U, N1,
683 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
684 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000685 }
686
687 break;
688 }
689
Ted Kremenek64924852008-01-31 02:35:41 +0000690 case UnaryOperator::AddrOf: {
691 const LValue& L1 = GetLValue(St, U->getSubExpr());
692 Nodify(Dst, U, N1, SetValue(St, U, L1));
693 break;
694 }
695
696 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000697 // FIXME: Stop when dereferencing an uninitialized value.
698 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
699
700 const RValue& V = GetValue(St, U->getSubExpr());
701 const LValue& L1 = cast<LValue>(V);
702
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000703 // After a dereference, one of two possible situations arise:
704 // (1) A crash, because the pointer was NULL.
705 // (2) The pointer is not NULL, and the dereference works.
706 //
707 // We add these assumptions.
708
Ted Kremenek63a4f692008-02-07 06:04:18 +0000709 bool isFeasibleNotNull;
710
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000711 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000712 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
713
714 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000715 QualType T = U->getType();
716 Nodify(Dst, U, N1, SetValue(StNotNull, U,
717 GetValue(StNotNull, L1, &T)));
718 }
719
Ted Kremenek63a4f692008-02-07 06:04:18 +0000720 bool isFeasibleNull;
721
722 // "Assume" that the pointer is NULL.
723 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
724
725 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000726 // We don't use "Nodify" here because the node will be a sink
727 // and we have no intention of processing it later.
728 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
729
Ted Kremenek63a4f692008-02-07 06:04:18 +0000730 if (NullNode) {
731 NullNode->markAsSink();
732
733 if (isFeasibleNotNull)
734 ImplicitNullDeref.insert(NullNode);
735 else
736 ExplicitNullDeref.insert(NullNode);
737 }
738 }
739
Ted Kremenek64924852008-01-31 02:35:41 +0000740 break;
741 }
742
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000743 default: ;
744 assert (false && "Not implemented.");
745 }
746 }
747}
748
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000749void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
750 GRConstants::NodeSet& Dst) {
751
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000752 if (isa<DeclRefExpr>(E)) {
753 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000754 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000755 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000756
757 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
758 if (U->getOpcode() == UnaryOperator::Deref) {
759 Visit(U->getSubExpr(), Pred, Dst);
760 return;
761 }
762 }
763
764 Visit(E, Pred, Dst);
765}
766
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000767void GRConstants::VisitBinaryOperator(BinaryOperator* B,
768 GRConstants::NodeTy* Pred,
769 GRConstants::NodeSet& Dst) {
770 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000771
772 if (B->isAssignmentOp())
773 VisitAssignmentLHS(B->getLHS(), Pred, S1);
774 else
775 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000776
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000777 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
778 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000779
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000780 // When getting the value for the LHS, check if we are in an assignment.
781 // In such cases, we want to (initially) treat the LHS as an LValue,
782 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000783 // evaluated to LValueDecl's instead of to an NonLValue.
784 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000785 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
786 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000787
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000788 NodeSet S2;
789 Visit(B->getRHS(), N1, S2);
790
791 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000792
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000793 NodeTy* N2 = *I2;
794 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000795 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000796
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000797 BinaryOperator::Opcode Op = B->getOpcode();
798
799 if (Op <= BinaryOperator::Or) {
800
Ted Kremenek22031182008-02-08 02:57:34 +0000801 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000802 Nodify(Dst, B, N2, SetValue(St, B, V1));
803 continue;
804 }
805
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000806 if (isa<LValue>(V1)) {
807 // FIXME: Add support for RHS being a non-lvalue.
808 const LValue& L1 = cast<LValue>(V1);
809 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000810
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000811 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
812 }
813 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000814 const NonLValue& R1 = cast<NonLValue>(V1);
815 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000816
817 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000819
820 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000821
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000822 }
823
824 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000825 case BinaryOperator::Assign: {
826 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000827 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000828 break;
829 }
830
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000831 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000832
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000833 assert (B->isCompoundAssignmentOp());
834
835 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000836 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000837
Ted Kremenekda9bd092008-02-08 07:05:39 +0000838 if (Op >= BinaryOperator::AndAssign)
839 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
840 else
841 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000842
843 if (isa<LValue>(V2)) {
844 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000845 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000846 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000847 }
848 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000849 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000850 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000851 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000852 }
853
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000854 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000855 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000856 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000857 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000858 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000859 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000860}
Ted Kremenekee985462008-01-16 18:18:48 +0000861
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000862
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000863void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
864 GRConstants::NodeSet& Dst) {
865
866 // FIXME: add metadata to the CFG so that we can disable
867 // this check when we KNOW that there is no block-level subexpression.
868 // The motivation is that this check requires a hashtable lookup.
869
870 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
871 Dst.Add(Pred);
872 return;
873 }
874
875 switch (S->getStmtClass()) {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000876 case Stmt::BinaryOperatorClass: {
877 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000878
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000879 if (B->isLogicalOp()) {
880 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000881 break;
882 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000883 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000884 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000885 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000886 break;
887 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000888 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000889
890 // Fall-through.
891
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000892 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000893 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
894 break;
895
Ted Kremenekda9bd092008-02-08 07:05:39 +0000896 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000897 StmtExpr* SE = cast<StmtExpr>(S);
898
Ted Kremenekda9bd092008-02-08 07:05:39 +0000899 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000900 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
901 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000902 break;
903 }
904
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000905 case Stmt::UnaryOperatorClass:
906 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
907 break;
908
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000909 case Stmt::ParenExprClass:
910 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
911 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000912
913 case Stmt::DeclRefExprClass:
914 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
915 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000916
Ted Kremenek874d63f2008-01-24 02:02:54 +0000917 case Stmt::ImplicitCastExprClass: {
918 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
919 VisitCast(C, C->getSubExpr(), Pred, Dst);
920 break;
921 }
922
923 case Stmt::CastExprClass: {
924 CastExpr* C = cast<CastExpr>(S);
925 VisitCast(C, C->getSubExpr(), Pred, Dst);
926 break;
927 }
928
Ted Kremenekf233d482008-02-05 00:26:40 +0000929 case Stmt::ConditionalOperatorClass: { // '?' operator
930 ConditionalOperator* C = cast<ConditionalOperator>(S);
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000931 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000932 break;
933 }
934
935 case Stmt::ChooseExprClass: { // __builtin_choose_expr
936 ChooseExpr* C = cast<ChooseExpr>(S);
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000937 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000938 break;
939 }
940
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000941 case Stmt::ReturnStmtClass:
942 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
943 Visit(R, Pred, Dst);
944 else
945 Dst.Add(Pred);
946
947 break;
948
Ted Kremenek9de04c42008-01-24 20:55:43 +0000949 case Stmt::DeclStmtClass:
950 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
951 break;
952
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000953 default:
954 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
955 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000956 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000957}
958
Ted Kremenekee985462008-01-16 18:18:48 +0000959//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000960// "Assume" logic.
961//===----------------------------------------------------------------------===//
962
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000963GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
964 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000965 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000966
967 switch (Cond.getSubKind()) {
968 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000969 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000970 return St;
971
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000972 case lval::SymbolValKind:
973 if (Assumption)
974 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
975 ValMgr.getZeroWithPtrWidth(), isFeasible);
976 else
977 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
978 ValMgr.getZeroWithPtrWidth(), isFeasible);
979
Ted Kremenek08b66252008-02-06 04:31:33 +0000980
Ted Kremenek329f8542008-02-05 21:52:21 +0000981 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000982 isFeasible = Assumption;
983 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000984
Ted Kremenek329f8542008-02-05 21:52:21 +0000985 case lval::ConcreteIntKind: {
986 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000987 isFeasible = b ? Assumption : !Assumption;
988 return St;
989 }
990 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000991}
992
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000993GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
994 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000995 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000996
997 switch (Cond.getSubKind()) {
998 default:
999 assert (false && "'Assume' not implemented for this NonLValue.");
1000 return St;
1001
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001002
1003 case nonlval::SymbolValKind: {
1004 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1005 SymbolID sym = SV.getSymbol();
1006
1007 if (Assumption)
1008 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1009 isFeasible);
1010 else
1011 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1012 isFeasible);
1013 }
1014
Ted Kremenek08b66252008-02-06 04:31:33 +00001015 case nonlval::SymIntConstraintValKind:
1016 return
1017 AssumeSymInt(St, Assumption,
1018 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1019 isFeasible);
1020
Ted Kremenek329f8542008-02-05 21:52:21 +00001021 case nonlval::ConcreteIntKind: {
1022 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001023 isFeasible = b ? Assumption : !Assumption;
1024 return St;
1025 }
1026 }
1027}
1028
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001029GRConstants::StateTy
1030GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1031 const llvm::APSInt& V, bool& isFeasible) {
1032
1033 // First, determine if sym == X, where X != V.
1034 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1035 isFeasible = *X != V;
1036 return St;
1037 }
1038
1039 // Second, determine if sym != V.
1040 if (St.isNotEqual(sym, V)) {
1041 isFeasible = true;
1042 return St;
1043 }
1044
1045 // If we reach here, sym is not a constant and we don't know if it is != V.
1046 // Make that assumption.
1047
1048 isFeasible = true;
1049 return StateMgr.AddNE(St, sym, V);
1050}
1051
1052GRConstants::StateTy
1053GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1054 const llvm::APSInt& V, bool& isFeasible) {
1055
1056 // First, determine if sym == X, where X != V.
1057 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1058 isFeasible = *X == V;
1059 return St;
1060 }
1061
1062 // Second, determine if sym != V.
1063 if (St.isNotEqual(sym, V)) {
1064 isFeasible = false;
1065 return St;
1066 }
1067
1068 // If we reach here, sym is not a constant and we don't know if it is == V.
1069 // Make that assumption.
1070
1071 isFeasible = true;
1072 return StateMgr.AddEQ(St, sym, V);
1073}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001074
Ted Kremenek08b66252008-02-06 04:31:33 +00001075GRConstants::StateTy
1076GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1077 const SymIntConstraint& C, bool& isFeasible) {
1078
1079 switch (C.getOpcode()) {
1080 default:
1081 // No logic yet for other operators.
1082 return St;
1083
1084 case BinaryOperator::EQ:
1085 if (Assumption)
1086 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1087 else
1088 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1089
1090 case BinaryOperator::NE:
1091 if (Assumption)
1092 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1093 else
1094 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1095 }
1096}
1097
Ted Kremenekb38911f2008-01-30 23:03:39 +00001098//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001099// Driver.
1100//===----------------------------------------------------------------------===//
1101
Ted Kremenekaa66a322008-01-16 21:46:15 +00001102#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001103static GRConstants* GraphPrintCheckerState;
1104
Ted Kremenekaa66a322008-01-16 21:46:15 +00001105namespace llvm {
1106template<>
1107struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1108 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001109
1110 static void PrintVarBindings(std::ostream& Out, GRConstants::StateTy St) {
1111
1112 Out << "Variables:\\l";
1113
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001114 bool isFirst = true;
1115
Ted Kremenek016f52f2008-02-08 21:10:02 +00001116 for (GRConstants::StateTy::vb_iterator I=St.vb_begin(),
1117 E=St.vb_end(); I!=E;++I) {
1118
1119 if (isFirst)
1120 isFirst = false;
1121 else
1122 Out << "\\l";
1123
1124 Out << ' ' << I.getKey()->getName() << " : ";
1125 I.getData().print(Out);
1126 }
1127
1128 }
1129
Ted Kremeneke7d22112008-02-11 19:21:59 +00001130
1131 static void PrintSubExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1132
1133 bool isFirst = true;
1134
1135 for (GRConstants::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
1136 I != E;++I) {
1137
1138 if (isFirst) {
1139 Out << "\\l\\lSub-Expressions:\\l";
1140 isFirst = false;
1141 }
1142 else
1143 Out << "\\l";
1144
1145 Out << " (" << (void*) I.getKey() << ") ";
1146 I.getKey()->printPretty(Out);
1147 Out << " : ";
1148 I.getData().print(Out);
1149 }
1150 }
1151
1152 static void PrintBlkExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1153
Ted Kremenek016f52f2008-02-08 21:10:02 +00001154 bool isFirst = true;
1155
Ted Kremeneke7d22112008-02-11 19:21:59 +00001156 for (GRConstants::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
1157 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001158 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001159 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001160 isFirst = false;
1161 }
1162 else
1163 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001164
Ted Kremeneke7d22112008-02-11 19:21:59 +00001165 Out << " (" << (void*) I.getKey() << ") ";
1166 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001167 Out << " : ";
1168 I.getData().print(Out);
1169 }
1170 }
1171
Ted Kremeneked4de312008-02-06 03:56:15 +00001172 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1173 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1174
1175 if (CE.isEmpty())
1176 return;
1177
1178 Out << "\\l\\|'==' constraints:";
1179
1180 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1181 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1182 }
1183
1184 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1185 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1186
1187 if (NE.isEmpty())
1188 return;
1189
1190 Out << "\\l\\|'!=' constraints:";
1191
1192 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1193 I != EI; ++I){
1194
1195 Out << "\\l $" << I.getKey() << " : ";
1196 bool isFirst = true;
1197
1198 ValueState::IntSetTy::iterator J=I.getData().begin(),
1199 EJ=I.getData().end();
1200 for ( ; J != EJ; ++J) {
1201 if (isFirst) isFirst = false;
1202 else Out << ", ";
1203
1204 Out << (*J)->toString();
1205 }
1206 }
1207 }
1208
Ted Kremenekaa66a322008-01-16 21:46:15 +00001209 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1210 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001211
1212 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001213 ProgramPoint Loc = N->getLocation();
1214
1215 switch (Loc.getKind()) {
1216 case ProgramPoint::BlockEntranceKind:
1217 Out << "Block Entrance: B"
1218 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1219 break;
1220
1221 case ProgramPoint::BlockExitKind:
1222 assert (false);
1223 break;
1224
1225 case ProgramPoint::PostStmtKind: {
1226 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001227 Out << L.getStmt()->getStmtClassName() << ':'
1228 << (void*) L.getStmt() << ' ';
1229
Ted Kremenekaa66a322008-01-16 21:46:15 +00001230 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001231
1232 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1233 Out << "\\|Implicit-Null Dereference.\\l";
1234 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001235 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1236 Out << "\\|Explicit-Null Dereference.\\l";
1237 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001238
Ted Kremenekaa66a322008-01-16 21:46:15 +00001239 break;
1240 }
1241
1242 default: {
1243 const BlockEdge& E = cast<BlockEdge>(Loc);
1244 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1245 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001246
1247 if (Stmt* T = E.getSrc()->getTerminator()) {
1248 Out << "\\|Terminator: ";
1249 E.getSrc()->printTerminator(Out);
1250
1251 if (isa<SwitchStmt>(T)) {
1252 // FIXME
1253 }
1254 else {
1255 Out << "\\lCondition: ";
1256 if (*E.getSrc()->succ_begin() == E.getDst())
1257 Out << "true";
1258 else
1259 Out << "false";
1260 }
1261
1262 Out << "\\l";
1263 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001264
1265 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1266 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1267 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001268 }
1269 }
1270
Ted Kremenek9153f732008-02-05 07:17:49 +00001271 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001272
Ted Kremeneke7d22112008-02-11 19:21:59 +00001273 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001274
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001275 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001276 return Out.str();
1277 }
1278};
1279} // end llvm namespace
1280#endif
1281
Ted Kremenekee985462008-01-16 18:18:48 +00001282namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001283void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1284 Diagnostic& Diag) {
1285
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001286 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001287 Engine.ExecuteWorkList();
1288
1289 // Look for explicit-Null dereferences and warn about them.
1290 GRConstants* CheckerState = &Engine.getCheckerState();
1291
1292 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1293 E=CheckerState->null_end(); I!=E; ++I) {
1294
1295 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1296 Expr* E = cast<Expr>(L.getStmt());
1297
1298 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1299 diag::chkr_null_deref_after_check);
1300 }
1301
1302
Ted Kremenekaa66a322008-01-16 21:46:15 +00001303#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001304 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001305 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001306 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001307#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001308}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001309} // end clang namespace