blob: c2395512c59e416a0b1f6e4227ae0996a9445543 [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 Kremenek754607e2008-02-13 00:24:44 +000070 typedef GRIndirectGotoNodeBuilder<GRConstants> IndirectGotoNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000071 typedef ExplodedGraph<GRConstants> GraphTy;
72 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000073
74 class NodeSet {
75 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
76 ImplTy Impl;
77 public:
78
79 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000080 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000081
Ted Kremenekb38911f2008-01-30 23:03:39 +000082 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000083
84 typedef ImplTy::iterator iterator;
85 typedef ImplTy::const_iterator const_iterator;
86
87 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000088 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000089
90 iterator begin() { return Impl.begin(); }
91 iterator end() { return Impl.end(); }
92
93 const_iterator begin() const { return Impl.begin(); }
94 const_iterator end() const { return Impl.end(); }
95 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000096
Ted Kremenekd27f8162008-01-15 23:55:06 +000097protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000098 /// G - the simulation graph.
99 GraphTy& G;
100
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000101 /// Liveness - live-variables information the ValueDecl* and block-level
102 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000103 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000104
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000105 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000106 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000107 StmtNodeBuilder* Builder;
108
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000109 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000110 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000111
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000112 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000113 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000114
Ted Kremenek68fd2572008-01-29 17:27:31 +0000115 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000116 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000117
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000118 /// StmtEntryNode - The immediate predecessor node.
119 NodeTy* StmtEntryNode;
120
121 /// CurrentStmt - The current block-level statement.
122 Stmt* CurrentStmt;
123
Ted Kremenekb38911f2008-01-30 23:03:39 +0000124 /// UninitBranches - Nodes in the ExplodedGraph that result from
125 /// taking a branch based on an uninitialized value.
126 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
127 UninitBranchesTy UninitBranches;
128
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000129 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
130 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000131 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
132 NullDerefTy ImplicitNullDeref;
133 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000134
135
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000136 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000137
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000139 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000140 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000141 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000142 ValMgr(StateMgr.getValueManager()),
143 SymMgr(StateMgr.getSymbolManager()),
144 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000145
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000146 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000147 Liveness.runOnCFG(G.getCFG());
148 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000149 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000150
Ted Kremenek19227e32008-02-07 06:33:19 +0000151 /// getContext - Return the ASTContext associated with this analysis.
152 ASTContext& getContext() const { return G.getContext(); }
153
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000154 /// getCFG - Returns the CFG associated with this analysis.
155 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000156
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000157 /// getInitialState - Return the initial state used for the root vertex
158 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000159 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000160 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000161
162 // Iterate the parameters.
163 FunctionDecl& F = G.getFunctionDecl();
164
165 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000166 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000167 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000168
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000169 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000170 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000171
172 bool isUninitControlFlow(const NodeTy* N) const {
173 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
174 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000175
176 bool isImplicitNullDeref(const NodeTy* N) const {
177 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
178 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000179
180 bool isExplicitNullDeref(const NodeTy* N) const {
181 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
182 }
183
Ted Kremenek19227e32008-02-07 06:33:19 +0000184 typedef NullDerefTy::iterator null_iterator;
185 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
186 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000187
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000188 /// ProcessStmt - Called by GREngine. Used to generate new successor
189 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000190 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
191
192 /// ProcessBranch - Called by GREngine. Used to generate successor
193 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000194 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000195
Ted Kremenek754607e2008-02-13 00:24:44 +0000196 /// ProcessIndirectGoto - Called by GREngine. Used to generate successor
197 /// nodes by processing the 'effects' of a computed goto jump.
198 void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder);
199
Ted Kremenekb87d9092008-02-08 19:17:19 +0000200 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000201 /// that all subexpression mappings are removed and that any
202 /// block-level expressions that are not live at 'S' also have their
203 /// mappings removed.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000204 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
205 return StateMgr.RemoveDeadBindings(St, S, Liveness);
206 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000207
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000208 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000209
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000210 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
211 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000212 }
213
Ted Kremenekcba2e432008-02-05 19:35:18 +0000214 /// SetValue - This version of SetValue is used to batch process a set
215 /// of different possible RValues and return a set of different states.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000216 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000217 const RValue::BufferTy& V,
218 StateTy::BufferTy& RetBuf);
219
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000220 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000221
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000222 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000223 return StateMgr.GetValue(St, S);
224 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000225
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000226 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000227 return StateMgr.GetValue(St, S, &hasVal);
228 }
229
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000230 inline RValue GetValue(const StateTy& St, const Expr* S) {
231 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000232 }
233
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000234 inline RValue GetValue(const StateTy& St, const LValue& LV,
235 QualType* T = NULL) {
236
237 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000238 }
239
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000240 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000241 return StateMgr.GetLValue(St, S);
242 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000243
244 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
245 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
246 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000247
248 /// Assume - Create new state by assuming that a given expression
249 /// is true or false.
250 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
251 bool& isFeasible) {
252 if (isa<LValue>(Cond))
253 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
254 else
255 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
256 }
257
258 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
259 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000260
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000261 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
262 bool& isFeasible);
263
264 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
265 bool& isFeasible);
266
Ted Kremenek08b66252008-02-06 04:31:33 +0000267 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
268 bool& isFeasible);
269
Ted Kremenek7e593362008-02-07 15:20:13 +0000270 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000271
Ted Kremenekcba2e432008-02-05 19:35:18 +0000272 /// Nodify - This version of Nodify is used to batch process a set of states.
273 /// The states are not guaranteed to be unique.
274 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
275
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000276 /// Visit - Transfer function logic for all statements. Dispatches to
277 /// other functions that handle specific kinds of statements.
278 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000279
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000280 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000281 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
282
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000283 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000284
Ted Kremenek230aaab2008-02-12 21:37:25 +0000285 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
286 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
287
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000288 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
289 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
290
Ted Kremenek9de04c42008-01-24 20:55:43 +0000291 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000292 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
293
294 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000295 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000296 NodeTy* Pred, NodeSet& Dst);
297
298 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
299 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000300
301 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
302 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
303 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000304
305 /// VisitUnaryOperator - Transfer function logic for unary operators.
306 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
307
Ted Kremenekd27f8162008-01-15 23:55:06 +0000308};
309} // end anonymous namespace
310
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000311
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000312GRConstants::StateTy
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000313GRConstants::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000314
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000315 if (!StateCleaned) {
316 St = RemoveDeadBindings(CurrentStmt, St);
317 StateCleaned = true;
318 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000319
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000320 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000321
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000322 if (S == CurrentStmt) {
323 isBlkExpr = getCFG().isBlkExpr(S);
324
325 if (!isBlkExpr)
326 return St;
327 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000328
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000329 return StateMgr.SetValue(St, S, isBlkExpr, V);
330}
331
Ted Kremenekcba2e432008-02-05 19:35:18 +0000332const GRConstants::StateTy::BufferTy&
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000333GRConstants::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000334 StateTy::BufferTy& RetBuf) {
335
336 assert (RetBuf.empty());
337
338 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
339 RetBuf.push_back(SetValue(St, S, *I));
340
341 return RetBuf;
342}
343
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000344GRConstants::StateTy
345GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
346
Ted Kremenek53c641a2008-02-08 03:02:48 +0000347 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000348 return St;
349
350 if (!StateCleaned) {
351 St = RemoveDeadBindings(CurrentStmt, St);
352 StateCleaned = true;
353 }
354
355 return StateMgr.SetValue(St, LV, V);
356}
357
Ted Kremenekf233d482008-02-05 00:26:40 +0000358void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000359 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000360
Ted Kremeneke7d22112008-02-11 19:21:59 +0000361 // Remove old bindings for subexpressions.
362 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000363
Ted Kremenekb38911f2008-01-30 23:03:39 +0000364 RValue V = GetValue(PrevState, Condition);
365
366 switch (V.getBaseKind()) {
367 default:
368 break;
369
Ted Kremenek53c641a2008-02-08 03:02:48 +0000370 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000371 builder.generateNode(PrevState, true);
372 builder.generateNode(PrevState, false);
373 return;
374
375 case RValue::UninitializedKind: {
376 NodeTy* N = builder.generateNode(PrevState, true);
377
378 if (N) {
379 N->markAsSink();
380 UninitBranches.insert(N);
381 }
382
383 builder.markInfeasible(false);
384 return;
385 }
386 }
387
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000388 // Get the current block counter.
389 GRBlockCounter BC = builder.getBlockCounter();
390
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000391 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
392 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000393
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000394 if (isa<nonlval::ConcreteInt>(V) ||
395 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
396
397 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000398
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000399 bool isFeasible = true;
400
401 StateTy St = Assume(PrevState, V, true, isFeasible);
402
403 if (isFeasible)
404 builder.generateNode(St, true);
405 else
406 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000407 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000408 else
409 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000410
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000411 BlockID = builder.getTargetBlock(false)->getBlockID();
412 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000413
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000414 if (isa<nonlval::ConcreteInt>(V) ||
415 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
416
417 // Process the false branch.
418
419 bool isFeasible = false;
420
421 StateTy St = Assume(PrevState, V, false, isFeasible);
422
423 if (isFeasible)
424 builder.generateNode(St, false);
425 else
426 builder.markInfeasible(false);
427 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000428 else
429 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000430}
431
Ted Kremenek754607e2008-02-13 00:24:44 +0000432/// ProcessIndirectGoto - Called by GREngine. Used to generate successor
433/// nodes by processing the 'effects' of a computed goto jump.
434void GRConstants::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
435
436 StateTy St = builder.getState();
437 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
438
439 // Three possibilities:
440 //
441 // (1) We know the computed label.
442 // (2) The label is NULL (or some other constant), or Uninitialized.
443 // (3) We have no clue about the label. Dispatch to all targets.
444 //
445
446 typedef IndirectGotoNodeBuilder::iterator iterator;
447
448 if (isa<lval::GotoLabel>(V)) {
449 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
450
451 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
452 IndirectGotoNodeBuilder::Destination D = *I;
453
454 if (D.getLabel() == L) {
455 builder.generateNode(D, St);
456 return;
457 }
458 }
459
460 assert (false && "No block with label.");
461 return;
462 }
463
464 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
465 // Dispatch to the first target and mark it as a sink.
466 NodeTy* N = builder.generateNode(*builder.begin(), St, true);
467 UninitBranches.insert(N);
468 return;
469 }
470
471 // This is really a catch-all. We don't support symbolics yet.
472
473 assert (isa<UnknownVal>(V));
474
475 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
476 builder.generateNode(*I, St);
477}
Ted Kremenekf233d482008-02-05 00:26:40 +0000478
479void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
480 NodeSet& Dst) {
481
482 bool hasR2;
483 StateTy PrevState = Pred->getState();
484
485 RValue R1 = GetValue(PrevState, B->getLHS());
486 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
487
Ted Kremenek22031182008-02-08 02:57:34 +0000488 if (isa<UnknownVal>(R1) &&
489 (isa<UnknownVal>(R2) ||
490 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000491
492 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
493 return;
494 }
Ted Kremenek22031182008-02-08 02:57:34 +0000495 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000496 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
497 return;
498 }
499
500 // R1 is an expression that can evaluate to either 'true' or 'false'.
501 if (B->getOpcode() == BinaryOperator::LAnd) {
502 // hasR2 == 'false' means that LHS evaluated to 'false' and that
503 // we short-circuited, leading to a value of '0' for the '&&' expression.
504 if (hasR2 == false) {
505 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
506 return;
507 }
508 }
509 else {
510 assert (B->getOpcode() == BinaryOperator::LOr);
511 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
512 // we short-circuited, leading to a value of '1' for the '||' expression.
513 if (hasR2 == false) {
514 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
515 return;
516 }
517 }
518
519 // If we reach here we did not short-circuit. Assume R2 == true and
520 // R2 == false.
521
522 bool isFeasible;
523 StateTy St = Assume(PrevState, R2, true, isFeasible);
524
525 if (isFeasible)
526 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
527
528 St = Assume(PrevState, R2, false, isFeasible);
529
530 if (isFeasible)
531 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
532}
533
534
535
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000536void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000537 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000538
539 StmtEntryNode = builder.getLastNode();
540 CurrentStmt = S;
541 NodeSet Dst;
542 StateCleaned = false;
543
544 Visit(S, StmtEntryNode, Dst);
545
546 // If no nodes were generated, generate a new node that has all the
547 // dead mappings removed.
548 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
549 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
550 builder.generateNode(S, St, StmtEntryNode);
551 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000552
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000553 CurrentStmt = NULL;
554 StmtEntryNode = NULL;
555 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000556}
557
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000558GRConstants::NodeTy*
Ted Kremenek7e593362008-02-07 15:20:13 +0000559GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000560
561 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000562 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000563 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000564
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000565 NodeTy* N = Builder->generateNode(S, St, Pred);
566 Dst.Add(N);
567 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000568}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000569
Ted Kremenekcba2e432008-02-05 19:35:18 +0000570void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
571 const StateTy::BufferTy& SB) {
572
573 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
574 Nodify(Dst, S, Pred, *I);
575}
576
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000577void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
578 if (D != CurrentStmt) {
579 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
580 return;
581 }
582
583 // If we are here, we are loading the value of the decl and binding
584 // it to the block-level expression.
585
586 StateTy St = Pred->getState();
587
588 Nodify(Dst, D, Pred,
589 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
590}
591
Ted Kremenekcba2e432008-02-05 19:35:18 +0000592void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000593
594 QualType T = CastE->getType();
595
596 // Check for redundant casts.
597 if (E->getType() == T) {
598 Dst.Add(Pred);
599 return;
600 }
601
602 NodeSet S1;
603 Visit(E, Pred, S1);
604
605 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
606 NodeTy* N = *I1;
607 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000608 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000609 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000610 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000611}
612
613void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
614 GRConstants::NodeSet& Dst) {
615
616 StateTy St = Pred->getState();
617
618 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000619 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
620 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000621 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000622 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000623 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000624
625 Nodify(Dst, DS, Pred, St);
626
627 if (Dst.empty())
628 Dst.Add(Pred);
629}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000630
Ted Kremenekf233d482008-02-05 00:26:40 +0000631
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000632void GRConstants::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000633 NodeTy* Pred, NodeSet& Dst) {
634
635 StateTy St = Pred->getState();
636
637 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000638 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000639
640 Nodify(Dst, S, Pred, SetValue(St, S, R));
641}
642
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000643/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
644void GRConstants::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
645 NodeTy* Pred,
646 NodeSet& Dst) {
647
648 // 6.5.3.4 sizeof: "The result type is an integer."
649
650 QualType T = S->getArgumentType();
651
652 // FIXME: Add support for VLAs.
653 if (isa<VariableArrayType>(T.getTypePtr()))
654 return;
655
656 SourceLocation L = S->getExprLoc();
657 uint64_t size = getContext().getTypeSize(T, L) / 8;
658
659 Nodify(Dst, S, Pred,
660 SetValue(Pred->getState(), S,
661 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
662
663}
664
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000665void GRConstants::VisitUnaryOperator(UnaryOperator* U,
666 GRConstants::NodeTy* Pred,
667 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000668
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000669 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000670 UnaryOperator::Opcode Op = U->getOpcode();
671
672 // FIXME: This is a hack so that for '*' and '&' we don't recurse
673 // on visiting the subexpression if it is a DeclRefExpr. We should
674 // probably just handle AddrOf and Deref in their own methods to make
675 // this cleaner.
676 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
677 isa<DeclRefExpr>(U->getSubExpr()))
678 S1.Add(Pred);
679 else
680 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000681
682 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
683 NodeTy* N1 = *I1;
684 StateTy St = N1->getState();
685
686 switch (U->getOpcode()) {
687 case UnaryOperator::PostInc: {
688 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000689 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000690
691 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
692 GetRValueConstant(1U, U));
693
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000694 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
695 break;
696 }
697
698 case UnaryOperator::PostDec: {
699 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000700 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000701
702 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
703 GetRValueConstant(1U, U));
704
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000705 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
706 break;
707 }
708
709 case UnaryOperator::PreInc: {
710 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000711 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000712
713 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
714 GetRValueConstant(1U, U));
715
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000716 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
717 break;
718 }
719
720 case UnaryOperator::PreDec: {
721 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000722 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000723
724 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
725 GetRValueConstant(1U, U));
726
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000727 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
728 break;
729 }
730
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000731 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000732 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000733 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000734 break;
735 }
736
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000737 case UnaryOperator::Not: {
738 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000739 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000740 break;
741 }
742
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000743 case UnaryOperator::LNot: {
744 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
745 //
746 // Note: technically we do "E == 0", but this is the same in the
747 // transfer functions as "0 == E".
748
749 RValue V1 = GetValue(St, U->getSubExpr());
750
751 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000752 const LValue& L1 = cast<LValue>(V1);
753 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
754 Nodify(Dst, U, N1,
755 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
756 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000757 }
758 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000759 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000760 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000761 Nodify(Dst, U, N1,
762 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
763 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000764 }
765
766 break;
767 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000768
769 case UnaryOperator::SizeOf: {
770 // 6.5.3.4 sizeof: "The result type is an integer."
771
772 QualType T = U->getSubExpr()->getType();
773
774 // FIXME: Add support for VLAs.
775 if (isa<VariableArrayType>(T.getTypePtr()))
776 return;
777
778 SourceLocation L = U->getExprLoc();
779 uint64_t size = getContext().getTypeSize(T, L) / 8;
780
781 Nodify(Dst, U, N1,
782 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
783 getContext().IntTy, L)));
784
785 break;
786 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000787
Ted Kremenek64924852008-01-31 02:35:41 +0000788 case UnaryOperator::AddrOf: {
789 const LValue& L1 = GetLValue(St, U->getSubExpr());
790 Nodify(Dst, U, N1, SetValue(St, U, L1));
791 break;
792 }
793
794 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000795 // FIXME: Stop when dereferencing an uninitialized value.
796 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
797
798 const RValue& V = GetValue(St, U->getSubExpr());
799 const LValue& L1 = cast<LValue>(V);
800
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000801 // After a dereference, one of two possible situations arise:
802 // (1) A crash, because the pointer was NULL.
803 // (2) The pointer is not NULL, and the dereference works.
804 //
805 // We add these assumptions.
806
Ted Kremenek63a4f692008-02-07 06:04:18 +0000807 bool isFeasibleNotNull;
808
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000809 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000810 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
811
812 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000813 QualType T = U->getType();
814 Nodify(Dst, U, N1, SetValue(StNotNull, U,
815 GetValue(StNotNull, L1, &T)));
816 }
817
Ted Kremenek63a4f692008-02-07 06:04:18 +0000818 bool isFeasibleNull;
819
820 // "Assume" that the pointer is NULL.
821 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
822
823 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000824 // We don't use "Nodify" here because the node will be a sink
825 // and we have no intention of processing it later.
826 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
827
Ted Kremenek63a4f692008-02-07 06:04:18 +0000828 if (NullNode) {
829 NullNode->markAsSink();
830
831 if (isFeasibleNotNull)
832 ImplicitNullDeref.insert(NullNode);
833 else
834 ExplicitNullDeref.insert(NullNode);
835 }
836 }
837
Ted Kremenek64924852008-01-31 02:35:41 +0000838 break;
839 }
840
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000841 default: ;
842 assert (false && "Not implemented.");
843 }
844 }
845}
846
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000847void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
848 GRConstants::NodeSet& Dst) {
849
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000850 if (isa<DeclRefExpr>(E)) {
851 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000852 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000853 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000854
855 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
856 if (U->getOpcode() == UnaryOperator::Deref) {
857 Visit(U->getSubExpr(), Pred, Dst);
858 return;
859 }
860 }
861
862 Visit(E, Pred, Dst);
863}
864
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000865void GRConstants::VisitBinaryOperator(BinaryOperator* B,
866 GRConstants::NodeTy* Pred,
867 GRConstants::NodeSet& Dst) {
868 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000869
870 if (B->isAssignmentOp())
871 VisitAssignmentLHS(B->getLHS(), Pred, S1);
872 else
873 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000874
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000875 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
876 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000877
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000878 // When getting the value for the LHS, check if we are in an assignment.
879 // In such cases, we want to (initially) treat the LHS as an LValue,
880 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000881 // evaluated to LValueDecl's instead of to an NonLValue.
882 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000883 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
884 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000885
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000886 NodeSet S2;
887 Visit(B->getRHS(), N1, S2);
888
889 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000890
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000891 NodeTy* N2 = *I2;
892 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000893 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000894
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000895 BinaryOperator::Opcode Op = B->getOpcode();
896
897 if (Op <= BinaryOperator::Or) {
898
Ted Kremenek22031182008-02-08 02:57:34 +0000899 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000900 Nodify(Dst, B, N2, SetValue(St, B, V1));
901 continue;
902 }
903
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000904 if (isa<LValue>(V1)) {
905 // FIXME: Add support for RHS being a non-lvalue.
906 const LValue& L1 = cast<LValue>(V1);
907 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000908
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000909 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
910 }
911 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000912 const NonLValue& R1 = cast<NonLValue>(V1);
913 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000914
915 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000916 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000917
918 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000919
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000920 }
921
922 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000923 case BinaryOperator::Assign: {
924 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000925 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000926 break;
927 }
928
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000929 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000930
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000931 assert (B->isCompoundAssignmentOp());
932
933 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000934 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000935
Ted Kremenekda9bd092008-02-08 07:05:39 +0000936 if (Op >= BinaryOperator::AndAssign)
937 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
938 else
939 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000940
941 if (isa<LValue>(V2)) {
942 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000943 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000944 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000945 }
946 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000947 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000948 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000949 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000950 }
951
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000952 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000953 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000954 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000955 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000956 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000957 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000958}
Ted Kremenekee985462008-01-16 18:18:48 +0000959
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000960
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000961void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
962 GRConstants::NodeSet& Dst) {
963
964 // FIXME: add metadata to the CFG so that we can disable
965 // this check when we KNOW that there is no block-level subexpression.
966 // The motivation is that this check requires a hashtable lookup.
967
968 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
969 Dst.Add(Pred);
970 return;
971 }
972
973 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000974
975 default:
976 // Cases we intentionally have "default" handle:
977 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
978
979 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
980 break;
981
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000982 case Stmt::BinaryOperatorClass: {
983 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000984
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000985 if (B->isLogicalOp()) {
986 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000987 break;
988 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000989 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000990 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000991 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000992 break;
993 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000994
995 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
996 break;
997 }
998
999 case Stmt::CastExprClass: {
1000 CastExpr* C = cast<CastExpr>(S);
1001 VisitCast(C, C->getSubExpr(), Pred, Dst);
1002 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001003 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001004
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001005 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1006 ChooseExpr* C = cast<ChooseExpr>(S);
1007 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1008 break;
1009 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001010
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001011 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001012 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1013 break;
1014
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001015 case Stmt::ConditionalOperatorClass: { // '?' operator
1016 ConditionalOperator* C = cast<ConditionalOperator>(S);
1017 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1018 break;
1019 }
1020
1021 case Stmt::DeclRefExprClass:
1022 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1023 break;
1024
1025 case Stmt::DeclStmtClass:
1026 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1027 break;
1028
1029 case Stmt::ImplicitCastExprClass: {
1030 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1031 VisitCast(C, C->getSubExpr(), Pred, Dst);
1032 break;
1033 }
1034
1035 case Stmt::ParenExprClass:
1036 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1037 break;
1038
1039 case Stmt::SizeOfAlignOfTypeExprClass:
1040 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1041 break;
1042
Ted Kremenekda9bd092008-02-08 07:05:39 +00001043 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001044 StmtExpr* SE = cast<StmtExpr>(S);
1045
Ted Kremenekda9bd092008-02-08 07:05:39 +00001046 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001047 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1048 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001049 break;
1050 }
1051
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001052 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001053 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1054 Visit(R, Pred, Dst);
1055 else
1056 Dst.Add(Pred);
1057
1058 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001059 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001060
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001061 case Stmt::UnaryOperatorClass:
1062 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001063 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001064 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001065}
1066
Ted Kremenekee985462008-01-16 18:18:48 +00001067//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001068// "Assume" logic.
1069//===----------------------------------------------------------------------===//
1070
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001071GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1072 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001073 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001074
1075 switch (Cond.getSubKind()) {
1076 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001077 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001078 return St;
1079
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001080 case lval::SymbolValKind:
1081 if (Assumption)
1082 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1083 ValMgr.getZeroWithPtrWidth(), isFeasible);
1084 else
1085 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1086 ValMgr.getZeroWithPtrWidth(), isFeasible);
1087
Ted Kremenek08b66252008-02-06 04:31:33 +00001088
Ted Kremenek329f8542008-02-05 21:52:21 +00001089 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001090 isFeasible = Assumption;
1091 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001092
Ted Kremenek329f8542008-02-05 21:52:21 +00001093 case lval::ConcreteIntKind: {
1094 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001095 isFeasible = b ? Assumption : !Assumption;
1096 return St;
1097 }
1098 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001099}
1100
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001101GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1102 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001103 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001104
1105 switch (Cond.getSubKind()) {
1106 default:
1107 assert (false && "'Assume' not implemented for this NonLValue.");
1108 return St;
1109
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001110
1111 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001112 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001113 SymbolID sym = SV.getSymbol();
1114
1115 if (Assumption)
1116 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1117 isFeasible);
1118 else
1119 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1120 isFeasible);
1121 }
1122
Ted Kremenek08b66252008-02-06 04:31:33 +00001123 case nonlval::SymIntConstraintValKind:
1124 return
1125 AssumeSymInt(St, Assumption,
1126 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1127 isFeasible);
1128
Ted Kremenek329f8542008-02-05 21:52:21 +00001129 case nonlval::ConcreteIntKind: {
1130 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001131 isFeasible = b ? Assumption : !Assumption;
1132 return St;
1133 }
1134 }
1135}
1136
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001137GRConstants::StateTy
1138GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1139 const llvm::APSInt& V, bool& isFeasible) {
1140
1141 // First, determine if sym == X, where X != V.
1142 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1143 isFeasible = *X != V;
1144 return St;
1145 }
1146
1147 // Second, determine if sym != V.
1148 if (St.isNotEqual(sym, V)) {
1149 isFeasible = true;
1150 return St;
1151 }
1152
1153 // If we reach here, sym is not a constant and we don't know if it is != V.
1154 // Make that assumption.
1155
1156 isFeasible = true;
1157 return StateMgr.AddNE(St, sym, V);
1158}
1159
1160GRConstants::StateTy
1161GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1162 const llvm::APSInt& V, bool& isFeasible) {
1163
1164 // First, determine if sym == X, where X != V.
1165 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1166 isFeasible = *X == V;
1167 return St;
1168 }
1169
1170 // Second, determine if sym != V.
1171 if (St.isNotEqual(sym, V)) {
1172 isFeasible = false;
1173 return St;
1174 }
1175
1176 // If we reach here, sym is not a constant and we don't know if it is == V.
1177 // Make that assumption.
1178
1179 isFeasible = true;
1180 return StateMgr.AddEQ(St, sym, V);
1181}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001182
Ted Kremenek08b66252008-02-06 04:31:33 +00001183GRConstants::StateTy
1184GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1185 const SymIntConstraint& C, bool& isFeasible) {
1186
1187 switch (C.getOpcode()) {
1188 default:
1189 // No logic yet for other operators.
1190 return St;
1191
1192 case BinaryOperator::EQ:
1193 if (Assumption)
1194 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1195 else
1196 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1197
1198 case BinaryOperator::NE:
1199 if (Assumption)
1200 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1201 else
1202 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1203 }
1204}
1205
Ted Kremenekb38911f2008-01-30 23:03:39 +00001206//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001207// Driver.
1208//===----------------------------------------------------------------------===//
1209
Ted Kremenekaa66a322008-01-16 21:46:15 +00001210#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001211static GRConstants* GraphPrintCheckerState;
1212
Ted Kremenekaa66a322008-01-16 21:46:15 +00001213namespace llvm {
1214template<>
1215struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1216 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001217
1218 static void PrintVarBindings(std::ostream& Out, GRConstants::StateTy St) {
1219
1220 Out << "Variables:\\l";
1221
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001222 bool isFirst = true;
1223
Ted Kremenek016f52f2008-02-08 21:10:02 +00001224 for (GRConstants::StateTy::vb_iterator I=St.vb_begin(),
1225 E=St.vb_end(); I!=E;++I) {
1226
1227 if (isFirst)
1228 isFirst = false;
1229 else
1230 Out << "\\l";
1231
1232 Out << ' ' << I.getKey()->getName() << " : ";
1233 I.getData().print(Out);
1234 }
1235
1236 }
1237
Ted Kremeneke7d22112008-02-11 19:21:59 +00001238
1239 static void PrintSubExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1240
1241 bool isFirst = true;
1242
1243 for (GRConstants::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
1244 I != E;++I) {
1245
1246 if (isFirst) {
1247 Out << "\\l\\lSub-Expressions:\\l";
1248 isFirst = false;
1249 }
1250 else
1251 Out << "\\l";
1252
1253 Out << " (" << (void*) I.getKey() << ") ";
1254 I.getKey()->printPretty(Out);
1255 Out << " : ";
1256 I.getData().print(Out);
1257 }
1258 }
1259
1260 static void PrintBlkExprBindings(std::ostream& Out, GRConstants::StateTy St) {
1261
Ted Kremenek016f52f2008-02-08 21:10:02 +00001262 bool isFirst = true;
1263
Ted Kremeneke7d22112008-02-11 19:21:59 +00001264 for (GRConstants::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
1265 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001266 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001267 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001268 isFirst = false;
1269 }
1270 else
1271 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001272
Ted Kremeneke7d22112008-02-11 19:21:59 +00001273 Out << " (" << (void*) I.getKey() << ") ";
1274 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001275 Out << " : ";
1276 I.getData().print(Out);
1277 }
1278 }
1279
Ted Kremeneked4de312008-02-06 03:56:15 +00001280 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1281 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1282
1283 if (CE.isEmpty())
1284 return;
1285
1286 Out << "\\l\\|'==' constraints:";
1287
1288 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1289 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1290 }
1291
1292 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1293 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1294
1295 if (NE.isEmpty())
1296 return;
1297
1298 Out << "\\l\\|'!=' constraints:";
1299
1300 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1301 I != EI; ++I){
1302
1303 Out << "\\l $" << I.getKey() << " : ";
1304 bool isFirst = true;
1305
1306 ValueState::IntSetTy::iterator J=I.getData().begin(),
1307 EJ=I.getData().end();
1308 for ( ; J != EJ; ++J) {
1309 if (isFirst) isFirst = false;
1310 else Out << ", ";
1311
1312 Out << (*J)->toString();
1313 }
1314 }
1315 }
1316
Ted Kremenekaa66a322008-01-16 21:46:15 +00001317 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1318 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001319
1320 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001321 ProgramPoint Loc = N->getLocation();
1322
1323 switch (Loc.getKind()) {
1324 case ProgramPoint::BlockEntranceKind:
1325 Out << "Block Entrance: B"
1326 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1327 break;
1328
1329 case ProgramPoint::BlockExitKind:
1330 assert (false);
1331 break;
1332
1333 case ProgramPoint::PostStmtKind: {
1334 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001335 Out << L.getStmt()->getStmtClassName() << ':'
1336 << (void*) L.getStmt() << ' ';
1337
Ted Kremenekaa66a322008-01-16 21:46:15 +00001338 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001339
1340 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1341 Out << "\\|Implicit-Null Dereference.\\l";
1342 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001343 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1344 Out << "\\|Explicit-Null Dereference.\\l";
1345 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001346
Ted Kremenekaa66a322008-01-16 21:46:15 +00001347 break;
1348 }
1349
1350 default: {
1351 const BlockEdge& E = cast<BlockEdge>(Loc);
1352 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1353 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001354
1355 if (Stmt* T = E.getSrc()->getTerminator()) {
1356 Out << "\\|Terminator: ";
1357 E.getSrc()->printTerminator(Out);
1358
Ted Kremenek754607e2008-02-13 00:24:44 +00001359 if (isa<SwitchStmt>(T) || isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001360 // FIXME
1361 }
1362 else {
1363 Out << "\\lCondition: ";
1364 if (*E.getSrc()->succ_begin() == E.getDst())
1365 Out << "true";
1366 else
1367 Out << "false";
1368 }
1369
1370 Out << "\\l";
1371 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001372
1373 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1374 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1375 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001376 }
1377 }
1378
Ted Kremenek9153f732008-02-05 07:17:49 +00001379 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001380
Ted Kremeneke7d22112008-02-11 19:21:59 +00001381 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001382
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001383 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001384 return Out.str();
1385 }
1386};
1387} // end llvm namespace
1388#endif
1389
Ted Kremenekee985462008-01-16 18:18:48 +00001390namespace clang {
Ted Kremenek19227e32008-02-07 06:33:19 +00001391void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
1392 Diagnostic& Diag) {
1393
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001394 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001395 Engine.ExecuteWorkList();
1396
1397 // Look for explicit-Null dereferences and warn about them.
1398 GRConstants* CheckerState = &Engine.getCheckerState();
1399
1400 for (GRConstants::null_iterator I=CheckerState->null_begin(),
1401 E=CheckerState->null_end(); I!=E; ++I) {
1402
1403 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1404 Expr* E = cast<Expr>(L.getStmt());
1405
1406 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1407 diag::chkr_null_deref_after_check);
1408 }
1409
1410
Ted Kremenekaa66a322008-01-16 21:46:15 +00001411#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001412 GraphPrintCheckerState = CheckerState;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001413 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001414 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001415#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001416}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001417} // end clang namespace