blob: 73ae7286eb53f86cae3a6429b6145fbed2c7362e [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 Kremenekc3f261d2008-02-14 18:40:24 +0000328 inline NonLValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U,
329 NonLValue X) {
330 return TF.EvalMinus(ValMgr, U, X);
331 }
332
333 inline NonLValue EvalComplement(ValueManager& ValMgr, NonLValue X) {
334 return TF.EvalComplement(ValMgr, X);
335 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000336
337 inline NonLValue EvalBinaryOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
338 NonLValue LHS, NonLValue RHS) {
339 return TF.EvalBinaryOp(ValMgr, Op, LHS, RHS);
340 }
341
342 inline RValue EvalBinaryOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
343 LValue LHS, LValue RHS) {
344 return TF.EvalBinaryOp(ValMgr, Op, LHS, RHS);
345 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000346};
347} // end anonymous namespace
348
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000349
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000350GRExprEngine::StateTy
351GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000352
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000353 if (!StateCleaned) {
354 St = RemoveDeadBindings(CurrentStmt, St);
355 StateCleaned = true;
356 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000357
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000358 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000359
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000360 if (S == CurrentStmt) {
361 isBlkExpr = getCFG().isBlkExpr(S);
362
363 if (!isBlkExpr)
364 return St;
365 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000366
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000367 return StateMgr.SetValue(St, S, isBlkExpr, V);
368}
369
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000370const GRExprEngine::StateTy::BufferTy&
371GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000372 StateTy::BufferTy& RetBuf) {
373
374 assert (RetBuf.empty());
375
376 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
377 RetBuf.push_back(SetValue(St, S, *I));
378
379 return RetBuf;
380}
381
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000382GRExprEngine::StateTy
383GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000384
Ted Kremenek53c641a2008-02-08 03:02:48 +0000385 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000386 return St;
387
388 if (!StateCleaned) {
389 St = RemoveDeadBindings(CurrentStmt, St);
390 StateCleaned = true;
391 }
392
393 return StateMgr.SetValue(St, LV, V);
394}
395
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000396void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000397 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000398
Ted Kremeneke7d22112008-02-11 19:21:59 +0000399 // Remove old bindings for subexpressions.
400 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000401
Ted Kremenekb38911f2008-01-30 23:03:39 +0000402 RValue V = GetValue(PrevState, Condition);
403
404 switch (V.getBaseKind()) {
405 default:
406 break;
407
Ted Kremenek53c641a2008-02-08 03:02:48 +0000408 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000409 builder.generateNode(PrevState, true);
410 builder.generateNode(PrevState, false);
411 return;
412
413 case RValue::UninitializedKind: {
414 NodeTy* N = builder.generateNode(PrevState, true);
415
416 if (N) {
417 N->markAsSink();
418 UninitBranches.insert(N);
419 }
420
421 builder.markInfeasible(false);
422 return;
423 }
424 }
425
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000426 // Get the current block counter.
427 GRBlockCounter BC = builder.getBlockCounter();
428
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000429 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
430 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000431
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000432 if (isa<nonlval::ConcreteInt>(V) ||
433 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
434
435 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000436
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000437 bool isFeasible = true;
438
439 StateTy St = Assume(PrevState, V, true, isFeasible);
440
441 if (isFeasible)
442 builder.generateNode(St, true);
443 else
444 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000445 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000446 else
447 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000448
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000449 BlockID = builder.getTargetBlock(false)->getBlockID();
450 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000451
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000452 if (isa<nonlval::ConcreteInt>(V) ||
453 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
454
455 // Process the false branch.
456
457 bool isFeasible = false;
458
459 StateTy St = Assume(PrevState, V, false, isFeasible);
460
461 if (isFeasible)
462 builder.generateNode(St, false);
463 else
464 builder.markInfeasible(false);
465 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000466 else
467 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000468}
469
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000470/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000471/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000472void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000473
474 StateTy St = builder.getState();
475 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
476
477 // Three possibilities:
478 //
479 // (1) We know the computed label.
480 // (2) The label is NULL (or some other constant), or Uninitialized.
481 // (3) We have no clue about the label. Dispatch to all targets.
482 //
483
484 typedef IndirectGotoNodeBuilder::iterator iterator;
485
486 if (isa<lval::GotoLabel>(V)) {
487 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
488
489 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000490 if (I.getLabel() == L) {
491 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000492 return;
493 }
494 }
495
496 assert (false && "No block with label.");
497 return;
498 }
499
500 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
501 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000502 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000503 UninitBranches.insert(N);
504 return;
505 }
506
507 // This is really a catch-all. We don't support symbolics yet.
508
509 assert (isa<UnknownVal>(V));
510
511 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000512 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000513}
Ted Kremenekf233d482008-02-05 00:26:40 +0000514
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000515/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
516/// nodes by processing the 'effects' of a switch statement.
517void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
518
519 typedef SwitchNodeBuilder::iterator iterator;
520
521 StateTy St = builder.getState();
522 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
523
524 if (isa<UninitializedVal>(CondV)) {
525 NodeTy* N = builder.generateDefaultCaseNode(St, true);
526 UninitBranches.insert(N);
527 return;
528 }
529
530 StateTy DefaultSt = St;
531
532 // While most of this can be assumed (such as the signedness), having it
533 // just computed makes sure everything makes the same assumptions end-to-end.
534 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
535 APSInt V1(bits, false);
536 APSInt V2 = V1;
537
538 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
539
540 CaseStmt* Case = cast<CaseStmt>(I.getCase());
541
542 // Evaluate the case.
543 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
544 assert (false && "Case condition must evaluate to an integer constant.");
545 return;
546 }
547
548 // Get the RHS of the case, if it exists.
549
550 if (Expr* E = Case->getRHS()) {
551 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
552 assert (false &&
553 "Case condition (RHS) must evaluate to an integer constant.");
554 return ;
555 }
556
557 assert (V1 <= V2);
558 }
559 else V2 = V1;
560
561 // FIXME: Eventually we should replace the logic below with a range
562 // comparison, rather than concretize the values within the range.
563 // This should be easy once we have "ranges" for NonLValues.
564
565 do {
566 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
567
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000568 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000569
570 // Now "assume" that the case matches.
571 bool isFeasible;
572
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000573 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000574
575 if (isFeasible) {
576 builder.generateCaseStmtNode(I, StNew);
577
578 // If CondV evaluates to a constant, then we know that this
579 // is the *only* case that we can take, so stop evaluating the
580 // others.
581 if (isa<nonlval::ConcreteInt>(CondV))
582 return;
583 }
584
585 // Now "assume" that the case doesn't match. Add this state
586 // to the default state (if it is feasible).
587
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000588 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000589
590 if (isFeasible)
591 DefaultSt = StNew;
592
593 // Concretize the next value in the range.
594 ++V1;
595
596 } while (V1 < V2);
597 }
598
599 // If we reach here, than we know that the default branch is
600 // possible.
601 builder.generateDefaultCaseNode(DefaultSt);
602}
603
604
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000605void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000606 NodeSet& Dst) {
607
608 bool hasR2;
609 StateTy PrevState = Pred->getState();
610
611 RValue R1 = GetValue(PrevState, B->getLHS());
612 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
613
Ted Kremenek22031182008-02-08 02:57:34 +0000614 if (isa<UnknownVal>(R1) &&
615 (isa<UnknownVal>(R2) ||
616 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000617
618 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
619 return;
620 }
Ted Kremenek22031182008-02-08 02:57:34 +0000621 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000622 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
623 return;
624 }
625
626 // R1 is an expression that can evaluate to either 'true' or 'false'.
627 if (B->getOpcode() == BinaryOperator::LAnd) {
628 // hasR2 == 'false' means that LHS evaluated to 'false' and that
629 // we short-circuited, leading to a value of '0' for the '&&' expression.
630 if (hasR2 == false) {
631 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
632 return;
633 }
634 }
635 else {
636 assert (B->getOpcode() == BinaryOperator::LOr);
637 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
638 // we short-circuited, leading to a value of '1' for the '||' expression.
639 if (hasR2 == false) {
640 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
641 return;
642 }
643 }
644
645 // If we reach here we did not short-circuit. Assume R2 == true and
646 // R2 == false.
647
648 bool isFeasible;
649 StateTy St = Assume(PrevState, R2, true, isFeasible);
650
651 if (isFeasible)
652 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
653
654 St = Assume(PrevState, R2, false, isFeasible);
655
656 if (isFeasible)
657 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
658}
659
660
661
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000662void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000663 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000664
665 StmtEntryNode = builder.getLastNode();
666 CurrentStmt = S;
667 NodeSet Dst;
668 StateCleaned = false;
669
670 Visit(S, StmtEntryNode, Dst);
671
672 // If no nodes were generated, generate a new node that has all the
673 // dead mappings removed.
674 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
675 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
676 builder.generateNode(S, St, StmtEntryNode);
677 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000678
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000679 CurrentStmt = NULL;
680 StmtEntryNode = NULL;
681 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000682}
683
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000684GRExprEngine::NodeTy*
685GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000686
687 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000688 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000689 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000690
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000691 NodeTy* N = Builder->generateNode(S, St, Pred);
692 Dst.Add(N);
693 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000694}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000695
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000696void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000697 const StateTy::BufferTy& SB) {
698
699 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
700 Nodify(Dst, S, Pred, *I);
701}
702
Ted Kremenek44842c22008-02-13 18:06:44 +0000703void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000704 if (D != CurrentStmt) {
705 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
706 return;
707 }
708
709 // If we are here, we are loading the value of the decl and binding
710 // it to the block-level expression.
711
712 StateTy St = Pred->getState();
713
714 Nodify(Dst, D, Pred,
715 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
716}
717
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000718void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000719
720 QualType T = CastE->getType();
721
722 // Check for redundant casts.
723 if (E->getType() == T) {
724 Dst.Add(Pred);
725 return;
726 }
727
728 NodeSet S1;
729 Visit(E, Pred, S1);
730
731 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
732 NodeTy* N = *I1;
733 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000734 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000735 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000736 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000737}
738
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000739void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
740 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000741
742 StateTy St = Pred->getState();
743
744 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000745 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
746 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000747 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000748 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000749 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000750
751 Nodify(Dst, DS, Pred, St);
752
753 if (Dst.empty())
754 Dst.Add(Pred);
755}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000756
Ted Kremenekf233d482008-02-05 00:26:40 +0000757
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000758void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000759 NodeTy* Pred, NodeSet& Dst) {
760
761 StateTy St = Pred->getState();
762
763 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000764 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000765
766 Nodify(Dst, S, Pred, SetValue(St, S, R));
767}
768
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000769/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000770void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000771 NodeTy* Pred,
772 NodeSet& Dst) {
773
774 // 6.5.3.4 sizeof: "The result type is an integer."
775
776 QualType T = S->getArgumentType();
777
778 // FIXME: Add support for VLAs.
779 if (isa<VariableArrayType>(T.getTypePtr()))
780 return;
781
782 SourceLocation L = S->getExprLoc();
783 uint64_t size = getContext().getTypeSize(T, L) / 8;
784
785 Nodify(Dst, S, Pred,
786 SetValue(Pred->getState(), S,
787 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
788
789}
790
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000791void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
792 GRExprEngine::NodeTy* Pred,
793 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000794
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000795 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000796 UnaryOperator::Opcode Op = U->getOpcode();
797
798 // FIXME: This is a hack so that for '*' and '&' we don't recurse
799 // on visiting the subexpression if it is a DeclRefExpr. We should
800 // probably just handle AddrOf and Deref in their own methods to make
801 // this cleaner.
802 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
803 isa<DeclRefExpr>(U->getSubExpr()))
804 S1.Add(Pred);
805 else
806 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000807
808 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
809 NodeTy* N1 = *I1;
810 StateTy St = N1->getState();
811
812 switch (U->getOpcode()) {
813 case UnaryOperator::PostInc: {
814 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000815 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000816
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000817 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
818 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000819
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000820 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
821 break;
822 }
823
824 case UnaryOperator::PostDec: {
825 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000826 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000827
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000828 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
829 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000830
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000831 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
832 break;
833 }
834
835 case UnaryOperator::PreInc: {
836 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000837 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000838
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000839 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
840 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000841
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000842 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
843 break;
844 }
845
846 case UnaryOperator::PreDec: {
847 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000848 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000849
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000850 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
851 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000852
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000853 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
854 break;
855 }
856
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000857 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000858 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000859 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000860 break;
861 }
862
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000863 case UnaryOperator::Not: {
864 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000865 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000866 break;
867 }
868
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000869 case UnaryOperator::LNot: {
870 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
871 //
872 // Note: technically we do "E == 0", but this is the same in the
873 // transfer functions as "0 == E".
874
875 RValue V1 = GetValue(St, U->getSubExpr());
876
877 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000878 const LValue& L1 = cast<LValue>(V1);
879 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
880 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000881 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
882 L1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000883 }
884 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000885 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000886 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000887 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000888 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
889 R1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000890 }
891
892 break;
893 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000894
895 case UnaryOperator::SizeOf: {
896 // 6.5.3.4 sizeof: "The result type is an integer."
897
898 QualType T = U->getSubExpr()->getType();
899
900 // FIXME: Add support for VLAs.
901 if (isa<VariableArrayType>(T.getTypePtr()))
902 return;
903
904 SourceLocation L = U->getExprLoc();
905 uint64_t size = getContext().getTypeSize(T, L) / 8;
906
907 Nodify(Dst, U, N1,
908 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
909 getContext().IntTy, L)));
910
911 break;
912 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000913
Ted Kremenek64924852008-01-31 02:35:41 +0000914 case UnaryOperator::AddrOf: {
915 const LValue& L1 = GetLValue(St, U->getSubExpr());
916 Nodify(Dst, U, N1, SetValue(St, U, L1));
917 break;
918 }
919
920 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000921 // FIXME: Stop when dereferencing an uninitialized value.
922 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
923
924 const RValue& V = GetValue(St, U->getSubExpr());
925 const LValue& L1 = cast<LValue>(V);
926
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000927 // After a dereference, one of two possible situations arise:
928 // (1) A crash, because the pointer was NULL.
929 // (2) The pointer is not NULL, and the dereference works.
930 //
931 // We add these assumptions.
932
Ted Kremenek63a4f692008-02-07 06:04:18 +0000933 bool isFeasibleNotNull;
934
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000935 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000936 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
937
938 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000939 QualType T = U->getType();
940 Nodify(Dst, U, N1, SetValue(StNotNull, U,
941 GetValue(StNotNull, L1, &T)));
942 }
943
Ted Kremenek63a4f692008-02-07 06:04:18 +0000944 bool isFeasibleNull;
945
946 // "Assume" that the pointer is NULL.
947 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
948
949 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000950 // We don't use "Nodify" here because the node will be a sink
951 // and we have no intention of processing it later.
952 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
953
Ted Kremenek63a4f692008-02-07 06:04:18 +0000954 if (NullNode) {
955 NullNode->markAsSink();
956
957 if (isFeasibleNotNull)
958 ImplicitNullDeref.insert(NullNode);
959 else
960 ExplicitNullDeref.insert(NullNode);
961 }
962 }
963
Ted Kremenek64924852008-01-31 02:35:41 +0000964 break;
965 }
966
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000967 default: ;
968 assert (false && "Not implemented.");
969 }
970 }
971}
972
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000973void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
974 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000975
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000976 if (isa<DeclRefExpr>(E)) {
977 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000978 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000979 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000980
981 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
982 if (U->getOpcode() == UnaryOperator::Deref) {
983 Visit(U->getSubExpr(), Pred, Dst);
984 return;
985 }
986 }
987
988 Visit(E, Pred, Dst);
989}
990
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000991void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000992 GRExprEngine::NodeTy* Pred,
993 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000994 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000995
996 if (B->isAssignmentOp())
997 VisitAssignmentLHS(B->getLHS(), Pred, S1);
998 else
999 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001000
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001001 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1002 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001003
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001004 // When getting the value for the LHS, check if we are in an assignment.
1005 // In such cases, we want to (initially) treat the LHS as an LValue,
1006 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001007 // evaluated to LValueDecl's instead of to an NonLValue.
1008 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001009 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1010 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001011
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001012 NodeSet S2;
1013 Visit(B->getRHS(), N1, S2);
1014
1015 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001016
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001017 NodeTy* N2 = *I2;
1018 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001019 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001020
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001021 BinaryOperator::Opcode Op = B->getOpcode();
1022
1023 if (Op <= BinaryOperator::Or) {
1024
Ted Kremenek22031182008-02-08 02:57:34 +00001025 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001026 Nodify(Dst, B, N2, SetValue(St, B, V1));
1027 continue;
1028 }
1029
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001030 if (isa<LValue>(V1)) {
1031 // FIXME: Add support for RHS being a non-lvalue.
1032 const LValue& L1 = cast<LValue>(V1);
1033 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +00001034
Ted Kremenek6cb0b542008-02-14 19:37:24 +00001035 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, L1, L2)));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001036 }
1037 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001038 const NonLValue& R1 = cast<NonLValue>(V1);
1039 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001040
Ted Kremenek6cb0b542008-02-14 19:37:24 +00001041 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, R1, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001042 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001043
1044 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001045
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001046 }
1047
1048 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001049 case BinaryOperator::Assign: {
1050 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +00001051 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001052 break;
1053 }
1054
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001055 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001056
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001057 assert (B->isCompoundAssignmentOp());
1058
1059 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +00001060 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001061
Ted Kremenekda9bd092008-02-08 07:05:39 +00001062 if (Op >= BinaryOperator::AndAssign)
1063 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1064 else
1065 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001066
1067 if (isa<LValue>(V2)) {
1068 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +00001069 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +00001070 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
Ted Kremenek687af802008-01-29 19:43:15 +00001071 }
1072 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001073 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001074 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +00001075 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +00001076 }
1077
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001078 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001079 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001080 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001081 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001082 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001083 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001084}
Ted Kremenekee985462008-01-16 18:18:48 +00001085
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001086
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001087void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
1088 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001089
1090 // FIXME: add metadata to the CFG so that we can disable
1091 // this check when we KNOW that there is no block-level subexpression.
1092 // The motivation is that this check requires a hashtable lookup.
1093
1094 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1095 Dst.Add(Pred);
1096 return;
1097 }
1098
1099 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001100
1101 default:
1102 // Cases we intentionally have "default" handle:
1103 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
1104
1105 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1106 break;
1107
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001108 case Stmt::BinaryOperatorClass: {
1109 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001110
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001111 if (B->isLogicalOp()) {
1112 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001113 break;
1114 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001115 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001116 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001117 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001118 break;
1119 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001120
1121 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1122 break;
1123 }
1124
1125 case Stmt::CastExprClass: {
1126 CastExpr* C = cast<CastExpr>(S);
1127 VisitCast(C, C->getSubExpr(), Pred, Dst);
1128 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001129 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001130
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001131 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1132 ChooseExpr* C = cast<ChooseExpr>(S);
1133 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1134 break;
1135 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001136
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001137 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001138 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1139 break;
1140
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001141 case Stmt::ConditionalOperatorClass: { // '?' operator
1142 ConditionalOperator* C = cast<ConditionalOperator>(S);
1143 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1144 break;
1145 }
1146
1147 case Stmt::DeclRefExprClass:
1148 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1149 break;
1150
1151 case Stmt::DeclStmtClass:
1152 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1153 break;
1154
1155 case Stmt::ImplicitCastExprClass: {
1156 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1157 VisitCast(C, C->getSubExpr(), Pred, Dst);
1158 break;
1159 }
1160
1161 case Stmt::ParenExprClass:
1162 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1163 break;
1164
1165 case Stmt::SizeOfAlignOfTypeExprClass:
1166 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1167 break;
1168
Ted Kremenekda9bd092008-02-08 07:05:39 +00001169 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001170 StmtExpr* SE = cast<StmtExpr>(S);
1171
Ted Kremenekda9bd092008-02-08 07:05:39 +00001172 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001173 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1174 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001175 break;
1176 }
1177
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001178 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001179 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1180 Visit(R, Pred, Dst);
1181 else
1182 Dst.Add(Pred);
1183
1184 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001185 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001186
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001187 case Stmt::UnaryOperatorClass:
1188 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001189 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001190 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001191}
1192
Ted Kremenekee985462008-01-16 18:18:48 +00001193//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001194// "Assume" logic.
1195//===----------------------------------------------------------------------===//
1196
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001197GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001198 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001199 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001200
1201 switch (Cond.getSubKind()) {
1202 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001203 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001204 return St;
1205
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001206 case lval::SymbolValKind:
1207 if (Assumption)
1208 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1209 ValMgr.getZeroWithPtrWidth(), isFeasible);
1210 else
1211 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1212 ValMgr.getZeroWithPtrWidth(), isFeasible);
1213
Ted Kremenek08b66252008-02-06 04:31:33 +00001214
Ted Kremenek329f8542008-02-05 21:52:21 +00001215 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001216 isFeasible = Assumption;
1217 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001218
Ted Kremenek329f8542008-02-05 21:52:21 +00001219 case lval::ConcreteIntKind: {
1220 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001221 isFeasible = b ? Assumption : !Assumption;
1222 return St;
1223 }
1224 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001225}
1226
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001227GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001228 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001229 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001230
1231 switch (Cond.getSubKind()) {
1232 default:
1233 assert (false && "'Assume' not implemented for this NonLValue.");
1234 return St;
1235
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001236
1237 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001238 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001239 SymbolID sym = SV.getSymbol();
1240
1241 if (Assumption)
1242 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1243 isFeasible);
1244 else
1245 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1246 isFeasible);
1247 }
1248
Ted Kremenek08b66252008-02-06 04:31:33 +00001249 case nonlval::SymIntConstraintValKind:
1250 return
1251 AssumeSymInt(St, Assumption,
1252 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1253 isFeasible);
1254
Ted Kremenek329f8542008-02-05 21:52:21 +00001255 case nonlval::ConcreteIntKind: {
1256 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001257 isFeasible = b ? Assumption : !Assumption;
1258 return St;
1259 }
1260 }
1261}
1262
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001263GRExprEngine::StateTy
1264GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001265 const llvm::APSInt& V, bool& isFeasible) {
1266
1267 // First, determine if sym == X, where X != V.
1268 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1269 isFeasible = *X != V;
1270 return St;
1271 }
1272
1273 // Second, determine if sym != V.
1274 if (St.isNotEqual(sym, V)) {
1275 isFeasible = true;
1276 return St;
1277 }
1278
1279 // If we reach here, sym is not a constant and we don't know if it is != V.
1280 // Make that assumption.
1281
1282 isFeasible = true;
1283 return StateMgr.AddNE(St, sym, V);
1284}
1285
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001286GRExprEngine::StateTy
1287GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001288 const llvm::APSInt& V, bool& isFeasible) {
1289
1290 // First, determine if sym == X, where X != V.
1291 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1292 isFeasible = *X == V;
1293 return St;
1294 }
1295
1296 // Second, determine if sym != V.
1297 if (St.isNotEqual(sym, V)) {
1298 isFeasible = false;
1299 return St;
1300 }
1301
1302 // If we reach here, sym is not a constant and we don't know if it is == V.
1303 // Make that assumption.
1304
1305 isFeasible = true;
1306 return StateMgr.AddEQ(St, sym, V);
1307}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001308
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001309GRExprEngine::StateTy
1310GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001311 const SymIntConstraint& C, bool& isFeasible) {
1312
1313 switch (C.getOpcode()) {
1314 default:
1315 // No logic yet for other operators.
1316 return St;
1317
1318 case BinaryOperator::EQ:
1319 if (Assumption)
1320 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1321 else
1322 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1323
1324 case BinaryOperator::NE:
1325 if (Assumption)
1326 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1327 else
1328 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1329 }
1330}
1331
Ted Kremenekb38911f2008-01-30 23:03:39 +00001332//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001333// Driver.
1334//===----------------------------------------------------------------------===//
1335
Ted Kremenekaa66a322008-01-16 21:46:15 +00001336#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001337static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001338
Ted Kremenekaa66a322008-01-16 21:46:15 +00001339namespace llvm {
1340template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001341struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001342 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001343
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001344 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001345
1346 Out << "Variables:\\l";
1347
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001348 bool isFirst = true;
1349
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001350 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001351 E=St.vb_end(); I!=E;++I) {
1352
1353 if (isFirst)
1354 isFirst = false;
1355 else
1356 Out << "\\l";
1357
1358 Out << ' ' << I.getKey()->getName() << " : ";
1359 I.getData().print(Out);
1360 }
1361
1362 }
1363
Ted Kremeneke7d22112008-02-11 19:21:59 +00001364
Ted Kremenek44842c22008-02-13 18:06:44 +00001365 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001366
1367 bool isFirst = true;
1368
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001369 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001370 I != E;++I) {
1371
1372 if (isFirst) {
1373 Out << "\\l\\lSub-Expressions:\\l";
1374 isFirst = false;
1375 }
1376 else
1377 Out << "\\l";
1378
1379 Out << " (" << (void*) I.getKey() << ") ";
1380 I.getKey()->printPretty(Out);
1381 Out << " : ";
1382 I.getData().print(Out);
1383 }
1384 }
1385
Ted Kremenek44842c22008-02-13 18:06:44 +00001386 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001387
Ted Kremenek016f52f2008-02-08 21:10:02 +00001388 bool isFirst = true;
1389
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001390 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001391 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001392 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001393 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001394 isFirst = false;
1395 }
1396 else
1397 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001398
Ted Kremeneke7d22112008-02-11 19:21:59 +00001399 Out << " (" << (void*) I.getKey() << ") ";
1400 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001401 Out << " : ";
1402 I.getData().print(Out);
1403 }
1404 }
1405
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001406 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001407 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1408
1409 if (CE.isEmpty())
1410 return;
1411
1412 Out << "\\l\\|'==' constraints:";
1413
1414 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1415 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1416 }
1417
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001418 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001419 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1420
1421 if (NE.isEmpty())
1422 return;
1423
1424 Out << "\\l\\|'!=' constraints:";
1425
1426 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1427 I != EI; ++I){
1428
1429 Out << "\\l $" << I.getKey() << " : ";
1430 bool isFirst = true;
1431
1432 ValueState::IntSetTy::iterator J=I.getData().begin(),
1433 EJ=I.getData().end();
1434 for ( ; J != EJ; ++J) {
1435 if (isFirst) isFirst = false;
1436 else Out << ", ";
1437
1438 Out << (*J)->toString();
1439 }
1440 }
1441 }
1442
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001443 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001444 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001445
1446 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001447 ProgramPoint Loc = N->getLocation();
1448
1449 switch (Loc.getKind()) {
1450 case ProgramPoint::BlockEntranceKind:
1451 Out << "Block Entrance: B"
1452 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1453 break;
1454
1455 case ProgramPoint::BlockExitKind:
1456 assert (false);
1457 break;
1458
1459 case ProgramPoint::PostStmtKind: {
1460 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001461 Out << L.getStmt()->getStmtClassName() << ':'
1462 << (void*) L.getStmt() << ' ';
1463
Ted Kremenekaa66a322008-01-16 21:46:15 +00001464 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001465
1466 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1467 Out << "\\|Implicit-Null Dereference.\\l";
1468 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001469 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1470 Out << "\\|Explicit-Null Dereference.\\l";
1471 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001472
Ted Kremenekaa66a322008-01-16 21:46:15 +00001473 break;
1474 }
1475
1476 default: {
1477 const BlockEdge& E = cast<BlockEdge>(Loc);
1478 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1479 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001480
1481 if (Stmt* T = E.getSrc()->getTerminator()) {
1482 Out << "\\|Terminator: ";
1483 E.getSrc()->printTerminator(Out);
1484
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001485 if (isa<SwitchStmt>(T)) {
1486 Stmt* Label = E.getDst()->getLabel();
1487
1488 if (Label) {
1489 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1490 Out << "\\lcase ";
1491 C->getLHS()->printPretty(Out);
1492
1493 if (Stmt* RHS = C->getRHS()) {
1494 Out << " .. ";
1495 RHS->printPretty(Out);
1496 }
1497
1498 Out << ":";
1499 }
1500 else {
1501 assert (isa<DefaultStmt>(Label));
1502 Out << "\\ldefault:";
1503 }
1504 }
1505 else
1506 Out << "\\l(implicit) default:";
1507 }
1508 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001509 // FIXME
1510 }
1511 else {
1512 Out << "\\lCondition: ";
1513 if (*E.getSrc()->succ_begin() == E.getDst())
1514 Out << "true";
1515 else
1516 Out << "false";
1517 }
1518
1519 Out << "\\l";
1520 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001521
1522 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1523 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1524 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001525 }
1526 }
1527
Ted Kremenek9153f732008-02-05 07:17:49 +00001528 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001529
Ted Kremeneke7d22112008-02-11 19:21:59 +00001530 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001531
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001532 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001533 return Out.str();
1534 }
1535};
1536} // end llvm namespace
1537#endif
1538
Ted Kremenekee985462008-01-16 18:18:48 +00001539namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001540void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001541 Diagnostic& Diag) {
1542
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001543 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001544 GRExprEngine* CheckerState = &Engine.getCheckerState();
1545
1546 // Execute the worklist algorithm.
Ted Kremenek19227e32008-02-07 06:33:19 +00001547 Engine.ExecuteWorkList();
1548
1549 // Look for explicit-Null dereferences and warn about them.
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001550
Ted Kremenek19227e32008-02-07 06:33:19 +00001551
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001552 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001553 E=CheckerState->null_end(); I!=E; ++I) {
1554
1555 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1556 Expr* E = cast<Expr>(L.getStmt());
1557
1558 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1559 diag::chkr_null_deref_after_check);
1560 }
1561
1562
Ted Kremenekaa66a322008-01-16 21:46:15 +00001563#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001564 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001565 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001566 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001567#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001568}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001569} // end clang namespace