blob: 3ff4c4bdfe0deda09ad6d85c4cf6e64c9af75aa8 [file] [log] [blame]
Ted Kremenek44842c22008-02-13 18:06:44 +00001//===-- GRExprEngine.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 "ValueState.h"
19
Ted Kremenek4d4dd852008-02-13 17:41:41 +000020#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
Ted Kremenekd59cccc2008-02-14 18:28:23 +000021#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
22#include "GRSimpleVals.h"
23
Ted Kremenekd27f8162008-01-15 23:55:06 +000024#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000025#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000026#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek19227e32008-02-07 06:33:19 +000027#include "clang/Basic/Diagnostic.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000028
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/DataTypes.h"
31#include "llvm/ADT/APSInt.h"
32#include "llvm/ADT/FoldingSet.h"
33#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000034#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000035#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000037#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000038#include "llvm/Support/Streams.h"
39
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000040#include <functional>
41
Ted Kremenekaa66a322008-01-16 21:46:15 +000042#ifndef NDEBUG
43#include "llvm/Support/GraphWriter.h"
44#include <sstream>
45#endif
46
Ted Kremenekd27f8162008-01-15 23:55:06 +000047using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000048using llvm::dyn_cast;
49using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000050using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000051
52//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000053// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000054//
55// FIXME: This checker logic should be eventually broken into two components.
56// The first is the "meta"-level checking logic; the code that
57// does the Stmt visitation, fetching values from the map, etc.
58// The second part does the actual state manipulation. This way we
59// get more of a separate of concerns of these two pieces, with the
60// latter potentially being refactored back into the main checking
61// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000062//===----------------------------------------------------------------------===//
63
64namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000065
Ted Kremenek4d4dd852008-02-13 17:41:41 +000066class VISIBILITY_HIDDEN GRExprEngine {
Ted Kremenekd27f8162008-01-15 23:55:06 +000067
68public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000069 typedef ValueStateManager::StateTy StateTy;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000070 typedef ExplodedGraph<GRExprEngine> GraphTy;
71 typedef GraphTy::NodeTy NodeTy;
72
73 // Builders.
Ted Kremenek4d4dd852008-02-13 17:41:41 +000074 typedef GRStmtNodeBuilder<GRExprEngine> StmtNodeBuilder;
75 typedef GRBranchNodeBuilder<GRExprEngine> BranchNodeBuilder;
76 typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000077 typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder;
78
Ted Kremenekab2b8c52008-01-23 19:59:44 +000079 class NodeSet {
80 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
81 ImplTy Impl;
82 public:
83
84 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000085 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000086
Ted Kremenekb38911f2008-01-30 23:03:39 +000087 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000088
89 typedef ImplTy::iterator iterator;
90 typedef ImplTy::const_iterator const_iterator;
91
92 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000093 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000094
95 iterator begin() { return Impl.begin(); }
96 iterator end() { return Impl.end(); }
97
98 const_iterator begin() const { return Impl.begin(); }
99 const_iterator end() const { return Impl.end(); }
100 };
Ted Kremenekcba2e432008-02-05 19:35:18 +0000101
Ted Kremenekd27f8162008-01-15 23:55:06 +0000102protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000103 /// G - the simulation graph.
104 GraphTy& G;
105
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000106 /// Liveness - live-variables information the ValueDecl* and block-level
107 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000108 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000109
Ted Kremenek44842c22008-02-13 18:06:44 +0000110 /// Builder - The current GRStmtNodeBuilder which is used when building the
111 /// nodes for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000112 StmtNodeBuilder* Builder;
113
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000114 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000115 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000116
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000117 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000118 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000119
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000120 /// TF - Object that represents a bundle of transfer functions
121 /// for manipulating and creating RValues.
122 GRTransferFuncs& TF;
123
Ted Kremenek68fd2572008-01-29 17:27:31 +0000124 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000125 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000126
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000127 /// StmtEntryNode - The immediate predecessor node.
128 NodeTy* StmtEntryNode;
129
130 /// CurrentStmt - The current block-level statement.
131 Stmt* CurrentStmt;
132
Ted Kremenekb38911f2008-01-30 23:03:39 +0000133 /// UninitBranches - Nodes in the ExplodedGraph that result from
134 /// taking a branch based on an uninitialized value.
135 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
136 UninitBranchesTy UninitBranches;
137
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000138 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
139 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000140 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
141 NullDerefTy ImplicitNullDeref;
142 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000143
144
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000145 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146
Ted Kremenekd27f8162008-01-15 23:55:06 +0000147public:
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000148 GRExprEngine(GraphTy& g) :
149 G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000150 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000151 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000152 ValMgr(StateMgr.getValueManager()),
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000153 TF(*(new GRSimpleVals())), // FIXME.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000154 SymMgr(StateMgr.getSymbolManager()),
155 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000156
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000157 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000158 Liveness.runOnCFG(G.getCFG());
159 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000160 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000161
Ted Kremenek19227e32008-02-07 06:33:19 +0000162 /// getContext - Return the ASTContext associated with this analysis.
163 ASTContext& getContext() const { return G.getContext(); }
164
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000165 /// getCFG - Returns the CFG associated with this analysis.
166 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000167
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000168 /// getInitialState - Return the initial state used for the root vertex
169 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000170 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000171 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000172
173 // Iterate the parameters.
174 FunctionDecl& F = G.getFunctionDecl();
175
176 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000177 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000178 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000179
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000180 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000181 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000182
183 bool isUninitControlFlow(const NodeTy* N) const {
184 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
185 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000186
187 bool isImplicitNullDeref(const NodeTy* N) const {
188 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
189 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000190
191 bool isExplicitNullDeref(const NodeTy* N) const {
192 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
193 }
194
Ted Kremenek19227e32008-02-07 06:33:19 +0000195 typedef NullDerefTy::iterator null_iterator;
196 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
197 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000198
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000199 /// ProcessStmt - Called by GRCoreEngine. Used to generate new successor
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000200 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000201 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
202
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000203 /// ProcessBranch - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000204 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000205 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000206
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000207 /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000208 /// nodes by processing the 'effects' of a computed goto jump.
209 void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder);
210
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000211 /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
212 /// nodes by processing the 'effects' of a switch statement.
213 void ProcessSwitch(SwitchNodeBuilder& builder);
214
Ted Kremenekb87d9092008-02-08 19:17:19 +0000215 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000216 /// that all subexpression mappings are removed and that any
217 /// block-level expressions that are not live at 'S' also have their
218 /// mappings removed.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000219 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
220 return StateMgr.RemoveDeadBindings(St, S, Liveness);
221 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000222
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000223 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000224
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000225 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
226 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000227 }
228
Ted Kremenekcba2e432008-02-05 19:35:18 +0000229 /// SetValue - This version of SetValue is used to batch process a set
230 /// of different possible RValues and return a set of different states.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000231 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000232 const RValue::BufferTy& V,
233 StateTy::BufferTy& RetBuf);
234
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000235 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000236
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000237 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000238 return StateMgr.GetValue(St, S);
239 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000240
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000241 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000242 return StateMgr.GetValue(St, S, &hasVal);
243 }
244
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000245 inline RValue GetValue(const StateTy& St, const Expr* S) {
246 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000247 }
248
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000249 inline RValue GetValue(const StateTy& St, const LValue& LV,
250 QualType* T = NULL) {
251
252 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000253 }
254
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000255 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000256 return StateMgr.GetLValue(St, S);
257 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000258
259 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
260 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
261 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000262
263 /// Assume - Create new state by assuming that a given expression
264 /// is true or false.
265 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
266 bool& isFeasible) {
267 if (isa<LValue>(Cond))
268 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
269 else
270 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
271 }
272
273 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
274 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000275
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000276 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
277 bool& isFeasible);
278
279 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
280 bool& isFeasible);
281
Ted Kremenek08b66252008-02-06 04:31:33 +0000282 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
283 bool& isFeasible);
284
Ted Kremenek7e593362008-02-07 15:20:13 +0000285 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000286
Ted Kremenekcba2e432008-02-05 19:35:18 +0000287 /// Nodify - This version of Nodify is used to batch process a set of states.
288 /// The states are not guaranteed to be unique.
289 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
290
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000291 /// Visit - Transfer function logic for all statements. Dispatches to
292 /// other functions that handle specific kinds of statements.
293 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000294
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000295 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000296 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
297
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000298 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000299
Ted Kremenek230aaab2008-02-12 21:37:25 +0000300 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
301 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
302
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000303 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
304 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
305
Ted Kremenek9de04c42008-01-24 20:55:43 +0000306 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000307 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
308
309 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000310 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000311 NodeTy* Pred, NodeSet& Dst);
312
313 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
314 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000315
316 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
317 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
318 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000319
320 /// VisitUnaryOperator - Transfer function logic for unary operators.
321 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
322
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000323
324 inline RValue EvalCast(ValueManager& ValMgr, RValue R, Expr* CastExpr) {
325 return TF.EvalCast(ValMgr, R, CastExpr);
326 }
327
Ted Kremenekd27f8162008-01-15 23:55:06 +0000328};
329} // end anonymous namespace
330
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000331
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000332GRExprEngine::StateTy
333GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000334
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000335 if (!StateCleaned) {
336 St = RemoveDeadBindings(CurrentStmt, St);
337 StateCleaned = true;
338 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000339
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000340 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000341
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000342 if (S == CurrentStmt) {
343 isBlkExpr = getCFG().isBlkExpr(S);
344
345 if (!isBlkExpr)
346 return St;
347 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000348
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000349 return StateMgr.SetValue(St, S, isBlkExpr, V);
350}
351
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000352const GRExprEngine::StateTy::BufferTy&
353GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000354 StateTy::BufferTy& RetBuf) {
355
356 assert (RetBuf.empty());
357
358 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
359 RetBuf.push_back(SetValue(St, S, *I));
360
361 return RetBuf;
362}
363
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000364GRExprEngine::StateTy
365GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000366
Ted Kremenek53c641a2008-02-08 03:02:48 +0000367 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000368 return St;
369
370 if (!StateCleaned) {
371 St = RemoveDeadBindings(CurrentStmt, St);
372 StateCleaned = true;
373 }
374
375 return StateMgr.SetValue(St, LV, V);
376}
377
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000378void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000379 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000380
Ted Kremeneke7d22112008-02-11 19:21:59 +0000381 // Remove old bindings for subexpressions.
382 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000383
Ted Kremenekb38911f2008-01-30 23:03:39 +0000384 RValue V = GetValue(PrevState, Condition);
385
386 switch (V.getBaseKind()) {
387 default:
388 break;
389
Ted Kremenek53c641a2008-02-08 03:02:48 +0000390 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000391 builder.generateNode(PrevState, true);
392 builder.generateNode(PrevState, false);
393 return;
394
395 case RValue::UninitializedKind: {
396 NodeTy* N = builder.generateNode(PrevState, true);
397
398 if (N) {
399 N->markAsSink();
400 UninitBranches.insert(N);
401 }
402
403 builder.markInfeasible(false);
404 return;
405 }
406 }
407
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000408 // Get the current block counter.
409 GRBlockCounter BC = builder.getBlockCounter();
410
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000411 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
412 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000413
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000414 if (isa<nonlval::ConcreteInt>(V) ||
415 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
416
417 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000418
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000419 bool isFeasible = true;
420
421 StateTy St = Assume(PrevState, V, true, isFeasible);
422
423 if (isFeasible)
424 builder.generateNode(St, true);
425 else
426 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000427 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000428 else
429 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000430
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000431 BlockID = builder.getTargetBlock(false)->getBlockID();
432 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000433
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000434 if (isa<nonlval::ConcreteInt>(V) ||
435 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
436
437 // Process the false branch.
438
439 bool isFeasible = false;
440
441 StateTy St = Assume(PrevState, V, false, isFeasible);
442
443 if (isFeasible)
444 builder.generateNode(St, false);
445 else
446 builder.markInfeasible(false);
447 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000448 else
449 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000450}
451
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000452/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000453/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000454void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000455
456 StateTy St = builder.getState();
457 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
458
459 // Three possibilities:
460 //
461 // (1) We know the computed label.
462 // (2) The label is NULL (or some other constant), or Uninitialized.
463 // (3) We have no clue about the label. Dispatch to all targets.
464 //
465
466 typedef IndirectGotoNodeBuilder::iterator iterator;
467
468 if (isa<lval::GotoLabel>(V)) {
469 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
470
471 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000472 if (I.getLabel() == L) {
473 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000474 return;
475 }
476 }
477
478 assert (false && "No block with label.");
479 return;
480 }
481
482 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
483 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000484 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000485 UninitBranches.insert(N);
486 return;
487 }
488
489 // This is really a catch-all. We don't support symbolics yet.
490
491 assert (isa<UnknownVal>(V));
492
493 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000494 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000495}
Ted Kremenekf233d482008-02-05 00:26:40 +0000496
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000497/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
498/// nodes by processing the 'effects' of a switch statement.
499void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
500
501 typedef SwitchNodeBuilder::iterator iterator;
502
503 StateTy St = builder.getState();
504 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
505
506 if (isa<UninitializedVal>(CondV)) {
507 NodeTy* N = builder.generateDefaultCaseNode(St, true);
508 UninitBranches.insert(N);
509 return;
510 }
511
512 StateTy DefaultSt = St;
513
514 // While most of this can be assumed (such as the signedness), having it
515 // just computed makes sure everything makes the same assumptions end-to-end.
516 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
517 APSInt V1(bits, false);
518 APSInt V2 = V1;
519
520 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
521
522 CaseStmt* Case = cast<CaseStmt>(I.getCase());
523
524 // Evaluate the case.
525 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
526 assert (false && "Case condition must evaluate to an integer constant.");
527 return;
528 }
529
530 // Get the RHS of the case, if it exists.
531
532 if (Expr* E = Case->getRHS()) {
533 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
534 assert (false &&
535 "Case condition (RHS) must evaluate to an integer constant.");
536 return ;
537 }
538
539 assert (V1 <= V2);
540 }
541 else V2 = V1;
542
543 // FIXME: Eventually we should replace the logic below with a range
544 // comparison, rather than concretize the values within the range.
545 // This should be easy once we have "ranges" for NonLValues.
546
547 do {
548 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
549
550 NonLValue Result =
551 CondV.EvalBinaryOp(ValMgr, BinaryOperator::EQ, CaseVal);
552
553 // Now "assume" that the case matches.
554 bool isFeasible;
555
556 StateTy StNew = Assume(St, Result, true, isFeasible);
557
558 if (isFeasible) {
559 builder.generateCaseStmtNode(I, StNew);
560
561 // If CondV evaluates to a constant, then we know that this
562 // is the *only* case that we can take, so stop evaluating the
563 // others.
564 if (isa<nonlval::ConcreteInt>(CondV))
565 return;
566 }
567
568 // Now "assume" that the case doesn't match. Add this state
569 // to the default state (if it is feasible).
570
571 StNew = Assume(DefaultSt, Result, false, isFeasible);
572
573 if (isFeasible)
574 DefaultSt = StNew;
575
576 // Concretize the next value in the range.
577 ++V1;
578
579 } while (V1 < V2);
580 }
581
582 // If we reach here, than we know that the default branch is
583 // possible.
584 builder.generateDefaultCaseNode(DefaultSt);
585}
586
587
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000588void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000589 NodeSet& Dst) {
590
591 bool hasR2;
592 StateTy PrevState = Pred->getState();
593
594 RValue R1 = GetValue(PrevState, B->getLHS());
595 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
596
Ted Kremenek22031182008-02-08 02:57:34 +0000597 if (isa<UnknownVal>(R1) &&
598 (isa<UnknownVal>(R2) ||
599 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000600
601 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
602 return;
603 }
Ted Kremenek22031182008-02-08 02:57:34 +0000604 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000605 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
606 return;
607 }
608
609 // R1 is an expression that can evaluate to either 'true' or 'false'.
610 if (B->getOpcode() == BinaryOperator::LAnd) {
611 // hasR2 == 'false' means that LHS evaluated to 'false' and that
612 // we short-circuited, leading to a value of '0' for the '&&' expression.
613 if (hasR2 == false) {
614 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
615 return;
616 }
617 }
618 else {
619 assert (B->getOpcode() == BinaryOperator::LOr);
620 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
621 // we short-circuited, leading to a value of '1' for the '||' expression.
622 if (hasR2 == false) {
623 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
624 return;
625 }
626 }
627
628 // If we reach here we did not short-circuit. Assume R2 == true and
629 // R2 == false.
630
631 bool isFeasible;
632 StateTy St = Assume(PrevState, R2, true, isFeasible);
633
634 if (isFeasible)
635 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
636
637 St = Assume(PrevState, R2, false, isFeasible);
638
639 if (isFeasible)
640 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
641}
642
643
644
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000645void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000646 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000647
648 StmtEntryNode = builder.getLastNode();
649 CurrentStmt = S;
650 NodeSet Dst;
651 StateCleaned = false;
652
653 Visit(S, StmtEntryNode, Dst);
654
655 // If no nodes were generated, generate a new node that has all the
656 // dead mappings removed.
657 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
658 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
659 builder.generateNode(S, St, StmtEntryNode);
660 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000661
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000662 CurrentStmt = NULL;
663 StmtEntryNode = NULL;
664 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000665}
666
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000667GRExprEngine::NodeTy*
668GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000669
670 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000671 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000672 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000673
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000674 NodeTy* N = Builder->generateNode(S, St, Pred);
675 Dst.Add(N);
676 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000677}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000678
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000679void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000680 const StateTy::BufferTy& SB) {
681
682 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
683 Nodify(Dst, S, Pred, *I);
684}
685
Ted Kremenek44842c22008-02-13 18:06:44 +0000686void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000687 if (D != CurrentStmt) {
688 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
689 return;
690 }
691
692 // If we are here, we are loading the value of the decl and binding
693 // it to the block-level expression.
694
695 StateTy St = Pred->getState();
696
697 Nodify(Dst, D, Pred,
698 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
699}
700
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000701void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000702
703 QualType T = CastE->getType();
704
705 // Check for redundant casts.
706 if (E->getType() == T) {
707 Dst.Add(Pred);
708 return;
709 }
710
711 NodeSet S1;
712 Visit(E, Pred, S1);
713
714 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
715 NodeTy* N = *I1;
716 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000717 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000718 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000719 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000720}
721
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000722void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
723 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000724
725 StateTy St = Pred->getState();
726
727 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000728 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
729 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000730 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000731 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000732 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000733
734 Nodify(Dst, DS, Pred, St);
735
736 if (Dst.empty())
737 Dst.Add(Pred);
738}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000739
Ted Kremenekf233d482008-02-05 00:26:40 +0000740
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000741void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000742 NodeTy* Pred, NodeSet& Dst) {
743
744 StateTy St = Pred->getState();
745
746 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000747 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000748
749 Nodify(Dst, S, Pred, SetValue(St, S, R));
750}
751
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000752/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000753void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000754 NodeTy* Pred,
755 NodeSet& Dst) {
756
757 // 6.5.3.4 sizeof: "The result type is an integer."
758
759 QualType T = S->getArgumentType();
760
761 // FIXME: Add support for VLAs.
762 if (isa<VariableArrayType>(T.getTypePtr()))
763 return;
764
765 SourceLocation L = S->getExprLoc();
766 uint64_t size = getContext().getTypeSize(T, L) / 8;
767
768 Nodify(Dst, S, Pred,
769 SetValue(Pred->getState(), S,
770 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
771
772}
773
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000774void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
775 GRExprEngine::NodeTy* Pred,
776 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000777
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000778 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000779 UnaryOperator::Opcode Op = U->getOpcode();
780
781 // FIXME: This is a hack so that for '*' and '&' we don't recurse
782 // on visiting the subexpression if it is a DeclRefExpr. We should
783 // probably just handle AddrOf and Deref in their own methods to make
784 // this cleaner.
785 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
786 isa<DeclRefExpr>(U->getSubExpr()))
787 S1.Add(Pred);
788 else
789 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000790
791 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
792 NodeTy* N1 = *I1;
793 StateTy St = N1->getState();
794
795 switch (U->getOpcode()) {
796 case UnaryOperator::PostInc: {
797 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000798 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000799
800 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
801 GetRValueConstant(1U, U));
802
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000803 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
804 break;
805 }
806
807 case UnaryOperator::PostDec: {
808 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000809 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000810
811 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
812 GetRValueConstant(1U, U));
813
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000814 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
815 break;
816 }
817
818 case UnaryOperator::PreInc: {
819 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000820 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000821
822 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
823 GetRValueConstant(1U, U));
824
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000825 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
826 break;
827 }
828
829 case UnaryOperator::PreDec: {
830 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000831 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000832
833 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
834 GetRValueConstant(1U, U));
835
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000836 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
837 break;
838 }
839
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000840 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000841 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000842 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000843 break;
844 }
845
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000846 case UnaryOperator::Not: {
847 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000848 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000849 break;
850 }
851
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000852 case UnaryOperator::LNot: {
853 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
854 //
855 // Note: technically we do "E == 0", but this is the same in the
856 // transfer functions as "0 == E".
857
858 RValue V1 = GetValue(St, U->getSubExpr());
859
860 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000861 const LValue& L1 = cast<LValue>(V1);
862 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
863 Nodify(Dst, U, N1,
864 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
865 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000866 }
867 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000868 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000869 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000870 Nodify(Dst, U, N1,
871 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
872 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000873 }
874
875 break;
876 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000877
878 case UnaryOperator::SizeOf: {
879 // 6.5.3.4 sizeof: "The result type is an integer."
880
881 QualType T = U->getSubExpr()->getType();
882
883 // FIXME: Add support for VLAs.
884 if (isa<VariableArrayType>(T.getTypePtr()))
885 return;
886
887 SourceLocation L = U->getExprLoc();
888 uint64_t size = getContext().getTypeSize(T, L) / 8;
889
890 Nodify(Dst, U, N1,
891 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
892 getContext().IntTy, L)));
893
894 break;
895 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000896
Ted Kremenek64924852008-01-31 02:35:41 +0000897 case UnaryOperator::AddrOf: {
898 const LValue& L1 = GetLValue(St, U->getSubExpr());
899 Nodify(Dst, U, N1, SetValue(St, U, L1));
900 break;
901 }
902
903 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000904 // FIXME: Stop when dereferencing an uninitialized value.
905 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
906
907 const RValue& V = GetValue(St, U->getSubExpr());
908 const LValue& L1 = cast<LValue>(V);
909
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000910 // After a dereference, one of two possible situations arise:
911 // (1) A crash, because the pointer was NULL.
912 // (2) The pointer is not NULL, and the dereference works.
913 //
914 // We add these assumptions.
915
Ted Kremenek63a4f692008-02-07 06:04:18 +0000916 bool isFeasibleNotNull;
917
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000918 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000919 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
920
921 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000922 QualType T = U->getType();
923 Nodify(Dst, U, N1, SetValue(StNotNull, U,
924 GetValue(StNotNull, L1, &T)));
925 }
926
Ted Kremenek63a4f692008-02-07 06:04:18 +0000927 bool isFeasibleNull;
928
929 // "Assume" that the pointer is NULL.
930 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
931
932 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000933 // We don't use "Nodify" here because the node will be a sink
934 // and we have no intention of processing it later.
935 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
936
Ted Kremenek63a4f692008-02-07 06:04:18 +0000937 if (NullNode) {
938 NullNode->markAsSink();
939
940 if (isFeasibleNotNull)
941 ImplicitNullDeref.insert(NullNode);
942 else
943 ExplicitNullDeref.insert(NullNode);
944 }
945 }
946
Ted Kremenek64924852008-01-31 02:35:41 +0000947 break;
948 }
949
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000950 default: ;
951 assert (false && "Not implemented.");
952 }
953 }
954}
955
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000956void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
957 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000958
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000959 if (isa<DeclRefExpr>(E)) {
960 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000961 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000962 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000963
964 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
965 if (U->getOpcode() == UnaryOperator::Deref) {
966 Visit(U->getSubExpr(), Pred, Dst);
967 return;
968 }
969 }
970
971 Visit(E, Pred, Dst);
972}
973
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000974void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000975 GRExprEngine::NodeTy* Pred,
976 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000977 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000978
979 if (B->isAssignmentOp())
980 VisitAssignmentLHS(B->getLHS(), Pred, S1);
981 else
982 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000983
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000984 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
985 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000986
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000987 // When getting the value for the LHS, check if we are in an assignment.
988 // In such cases, we want to (initially) treat the LHS as an LValue,
989 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000990 // evaluated to LValueDecl's instead of to an NonLValue.
991 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000992 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
993 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000994
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000995 NodeSet S2;
996 Visit(B->getRHS(), N1, S2);
997
998 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000999
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001000 NodeTy* N2 = *I2;
1001 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001002 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001003
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001004 BinaryOperator::Opcode Op = B->getOpcode();
1005
1006 if (Op <= BinaryOperator::Or) {
1007
Ted Kremenek22031182008-02-08 02:57:34 +00001008 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001009 Nodify(Dst, B, N2, SetValue(St, B, V1));
1010 continue;
1011 }
1012
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001013 if (isa<LValue>(V1)) {
1014 // FIXME: Add support for RHS being a non-lvalue.
1015 const LValue& L1 = cast<LValue>(V1);
1016 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +00001017
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001018 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
1019 }
1020 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001021 const NonLValue& R1 = cast<NonLValue>(V1);
1022 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001023
1024 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001025 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001026
1027 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001028
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001029 }
1030
1031 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001032 case BinaryOperator::Assign: {
1033 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +00001034 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001035 break;
1036 }
1037
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001038 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001039
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001040 assert (B->isCompoundAssignmentOp());
1041
1042 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +00001043 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001044
Ted Kremenekda9bd092008-02-08 07:05:39 +00001045 if (Op >= BinaryOperator::AndAssign)
1046 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1047 else
1048 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001049
1050 if (isa<LValue>(V2)) {
1051 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +00001052 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001053 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +00001054 }
1055 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001056 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001057 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001058 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +00001059 }
1060
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001061 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001062 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001063 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001064 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001065 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001066 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001067}
Ted Kremenekee985462008-01-16 18:18:48 +00001068
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001069
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001070void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
1071 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001072
1073 // FIXME: add metadata to the CFG so that we can disable
1074 // this check when we KNOW that there is no block-level subexpression.
1075 // The motivation is that this check requires a hashtable lookup.
1076
1077 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1078 Dst.Add(Pred);
1079 return;
1080 }
1081
1082 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001083
1084 default:
1085 // Cases we intentionally have "default" handle:
1086 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
1087
1088 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1089 break;
1090
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001091 case Stmt::BinaryOperatorClass: {
1092 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001093
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001094 if (B->isLogicalOp()) {
1095 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001096 break;
1097 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001098 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001099 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001100 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001101 break;
1102 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001103
1104 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1105 break;
1106 }
1107
1108 case Stmt::CastExprClass: {
1109 CastExpr* C = cast<CastExpr>(S);
1110 VisitCast(C, C->getSubExpr(), Pred, Dst);
1111 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001112 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001113
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001114 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1115 ChooseExpr* C = cast<ChooseExpr>(S);
1116 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1117 break;
1118 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001119
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001120 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001121 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1122 break;
1123
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001124 case Stmt::ConditionalOperatorClass: { // '?' operator
1125 ConditionalOperator* C = cast<ConditionalOperator>(S);
1126 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1127 break;
1128 }
1129
1130 case Stmt::DeclRefExprClass:
1131 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1132 break;
1133
1134 case Stmt::DeclStmtClass:
1135 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1136 break;
1137
1138 case Stmt::ImplicitCastExprClass: {
1139 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1140 VisitCast(C, C->getSubExpr(), Pred, Dst);
1141 break;
1142 }
1143
1144 case Stmt::ParenExprClass:
1145 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1146 break;
1147
1148 case Stmt::SizeOfAlignOfTypeExprClass:
1149 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1150 break;
1151
Ted Kremenekda9bd092008-02-08 07:05:39 +00001152 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001153 StmtExpr* SE = cast<StmtExpr>(S);
1154
Ted Kremenekda9bd092008-02-08 07:05:39 +00001155 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001156 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1157 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001158 break;
1159 }
1160
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001161 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001162 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1163 Visit(R, Pred, Dst);
1164 else
1165 Dst.Add(Pred);
1166
1167 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001168 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001169
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001170 case Stmt::UnaryOperatorClass:
1171 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001172 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001173 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001174}
1175
Ted Kremenekee985462008-01-16 18:18:48 +00001176//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001177// "Assume" logic.
1178//===----------------------------------------------------------------------===//
1179
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001180GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001181 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001182 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001183
1184 switch (Cond.getSubKind()) {
1185 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001186 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001187 return St;
1188
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001189 case lval::SymbolValKind:
1190 if (Assumption)
1191 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1192 ValMgr.getZeroWithPtrWidth(), isFeasible);
1193 else
1194 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1195 ValMgr.getZeroWithPtrWidth(), isFeasible);
1196
Ted Kremenek08b66252008-02-06 04:31:33 +00001197
Ted Kremenek329f8542008-02-05 21:52:21 +00001198 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001199 isFeasible = Assumption;
1200 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001201
Ted Kremenek329f8542008-02-05 21:52:21 +00001202 case lval::ConcreteIntKind: {
1203 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001204 isFeasible = b ? Assumption : !Assumption;
1205 return St;
1206 }
1207 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001208}
1209
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001210GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001211 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001212 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001213
1214 switch (Cond.getSubKind()) {
1215 default:
1216 assert (false && "'Assume' not implemented for this NonLValue.");
1217 return St;
1218
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001219
1220 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001221 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001222 SymbolID sym = SV.getSymbol();
1223
1224 if (Assumption)
1225 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1226 isFeasible);
1227 else
1228 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1229 isFeasible);
1230 }
1231
Ted Kremenek08b66252008-02-06 04:31:33 +00001232 case nonlval::SymIntConstraintValKind:
1233 return
1234 AssumeSymInt(St, Assumption,
1235 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1236 isFeasible);
1237
Ted Kremenek329f8542008-02-05 21:52:21 +00001238 case nonlval::ConcreteIntKind: {
1239 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001240 isFeasible = b ? Assumption : !Assumption;
1241 return St;
1242 }
1243 }
1244}
1245
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001246GRExprEngine::StateTy
1247GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001248 const llvm::APSInt& V, bool& isFeasible) {
1249
1250 // First, determine if sym == X, where X != V.
1251 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1252 isFeasible = *X != V;
1253 return St;
1254 }
1255
1256 // Second, determine if sym != V.
1257 if (St.isNotEqual(sym, V)) {
1258 isFeasible = true;
1259 return St;
1260 }
1261
1262 // If we reach here, sym is not a constant and we don't know if it is != V.
1263 // Make that assumption.
1264
1265 isFeasible = true;
1266 return StateMgr.AddNE(St, sym, V);
1267}
1268
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001269GRExprEngine::StateTy
1270GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001271 const llvm::APSInt& V, bool& isFeasible) {
1272
1273 // First, determine if sym == X, where X != V.
1274 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1275 isFeasible = *X == V;
1276 return St;
1277 }
1278
1279 // Second, determine if sym != V.
1280 if (St.isNotEqual(sym, V)) {
1281 isFeasible = false;
1282 return St;
1283 }
1284
1285 // If we reach here, sym is not a constant and we don't know if it is == V.
1286 // Make that assumption.
1287
1288 isFeasible = true;
1289 return StateMgr.AddEQ(St, sym, V);
1290}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001291
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001292GRExprEngine::StateTy
1293GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001294 const SymIntConstraint& C, bool& isFeasible) {
1295
1296 switch (C.getOpcode()) {
1297 default:
1298 // No logic yet for other operators.
1299 return St;
1300
1301 case BinaryOperator::EQ:
1302 if (Assumption)
1303 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1304 else
1305 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1306
1307 case BinaryOperator::NE:
1308 if (Assumption)
1309 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1310 else
1311 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1312 }
1313}
1314
Ted Kremenekb38911f2008-01-30 23:03:39 +00001315//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001316// Driver.
1317//===----------------------------------------------------------------------===//
1318
Ted Kremenekaa66a322008-01-16 21:46:15 +00001319#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001320static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001321
Ted Kremenekaa66a322008-01-16 21:46:15 +00001322namespace llvm {
1323template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001324struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001325 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001326
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001327 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001328
1329 Out << "Variables:\\l";
1330
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001331 bool isFirst = true;
1332
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001333 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001334 E=St.vb_end(); I!=E;++I) {
1335
1336 if (isFirst)
1337 isFirst = false;
1338 else
1339 Out << "\\l";
1340
1341 Out << ' ' << I.getKey()->getName() << " : ";
1342 I.getData().print(Out);
1343 }
1344
1345 }
1346
Ted Kremeneke7d22112008-02-11 19:21:59 +00001347
Ted Kremenek44842c22008-02-13 18:06:44 +00001348 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001349
1350 bool isFirst = true;
1351
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001352 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001353 I != E;++I) {
1354
1355 if (isFirst) {
1356 Out << "\\l\\lSub-Expressions:\\l";
1357 isFirst = false;
1358 }
1359 else
1360 Out << "\\l";
1361
1362 Out << " (" << (void*) I.getKey() << ") ";
1363 I.getKey()->printPretty(Out);
1364 Out << " : ";
1365 I.getData().print(Out);
1366 }
1367 }
1368
Ted Kremenek44842c22008-02-13 18:06:44 +00001369 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001370
Ted Kremenek016f52f2008-02-08 21:10:02 +00001371 bool isFirst = true;
1372
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001373 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001374 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001375 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001376 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001377 isFirst = false;
1378 }
1379 else
1380 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001381
Ted Kremeneke7d22112008-02-11 19:21:59 +00001382 Out << " (" << (void*) I.getKey() << ") ";
1383 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001384 Out << " : ";
1385 I.getData().print(Out);
1386 }
1387 }
1388
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001389 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001390 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1391
1392 if (CE.isEmpty())
1393 return;
1394
1395 Out << "\\l\\|'==' constraints:";
1396
1397 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1398 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1399 }
1400
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001401 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001402 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1403
1404 if (NE.isEmpty())
1405 return;
1406
1407 Out << "\\l\\|'!=' constraints:";
1408
1409 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1410 I != EI; ++I){
1411
1412 Out << "\\l $" << I.getKey() << " : ";
1413 bool isFirst = true;
1414
1415 ValueState::IntSetTy::iterator J=I.getData().begin(),
1416 EJ=I.getData().end();
1417 for ( ; J != EJ; ++J) {
1418 if (isFirst) isFirst = false;
1419 else Out << ", ";
1420
1421 Out << (*J)->toString();
1422 }
1423 }
1424 }
1425
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001426 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001427 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001428
1429 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001430 ProgramPoint Loc = N->getLocation();
1431
1432 switch (Loc.getKind()) {
1433 case ProgramPoint::BlockEntranceKind:
1434 Out << "Block Entrance: B"
1435 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1436 break;
1437
1438 case ProgramPoint::BlockExitKind:
1439 assert (false);
1440 break;
1441
1442 case ProgramPoint::PostStmtKind: {
1443 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001444 Out << L.getStmt()->getStmtClassName() << ':'
1445 << (void*) L.getStmt() << ' ';
1446
Ted Kremenekaa66a322008-01-16 21:46:15 +00001447 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001448
1449 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1450 Out << "\\|Implicit-Null Dereference.\\l";
1451 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001452 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1453 Out << "\\|Explicit-Null Dereference.\\l";
1454 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001455
Ted Kremenekaa66a322008-01-16 21:46:15 +00001456 break;
1457 }
1458
1459 default: {
1460 const BlockEdge& E = cast<BlockEdge>(Loc);
1461 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1462 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001463
1464 if (Stmt* T = E.getSrc()->getTerminator()) {
1465 Out << "\\|Terminator: ";
1466 E.getSrc()->printTerminator(Out);
1467
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001468 if (isa<SwitchStmt>(T)) {
1469 Stmt* Label = E.getDst()->getLabel();
1470
1471 if (Label) {
1472 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1473 Out << "\\lcase ";
1474 C->getLHS()->printPretty(Out);
1475
1476 if (Stmt* RHS = C->getRHS()) {
1477 Out << " .. ";
1478 RHS->printPretty(Out);
1479 }
1480
1481 Out << ":";
1482 }
1483 else {
1484 assert (isa<DefaultStmt>(Label));
1485 Out << "\\ldefault:";
1486 }
1487 }
1488 else
1489 Out << "\\l(implicit) default:";
1490 }
1491 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001492 // FIXME
1493 }
1494 else {
1495 Out << "\\lCondition: ";
1496 if (*E.getSrc()->succ_begin() == E.getDst())
1497 Out << "true";
1498 else
1499 Out << "false";
1500 }
1501
1502 Out << "\\l";
1503 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001504
1505 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1506 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1507 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001508 }
1509 }
1510
Ted Kremenek9153f732008-02-05 07:17:49 +00001511 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001512
Ted Kremeneke7d22112008-02-11 19:21:59 +00001513 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001514
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001515 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001516 return Out.str();
1517 }
1518};
1519} // end llvm namespace
1520#endif
1521
Ted Kremenekee985462008-01-16 18:18:48 +00001522namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001523void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001524 Diagnostic& Diag) {
1525
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001526 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001527 GRExprEngine* CheckerState = &Engine.getCheckerState();
1528
1529 // Execute the worklist algorithm.
Ted Kremenek19227e32008-02-07 06:33:19 +00001530 Engine.ExecuteWorkList();
1531
1532 // Look for explicit-Null dereferences and warn about them.
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001533
Ted Kremenek19227e32008-02-07 06:33:19 +00001534
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001535 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001536 E=CheckerState->null_end(); I!=E; ++I) {
1537
1538 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1539 Expr* E = cast<Expr>(L.getStmt());
1540
1541 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1542 diag::chkr_null_deref_after_check);
1543 }
1544
1545
Ted Kremenekaa66a322008-01-16 21:46:15 +00001546#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001547 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001548 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001549 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001550#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001551}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001552} // end clang namespace