blob: 9ec43d911c391d47872b1f40cff8830d35040e27 [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 Kremenekd27f8162008-01-15 23:55:06 +0000336};
337} // end anonymous namespace
338
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000339
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000340GRExprEngine::StateTy
341GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000342
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000343 if (!StateCleaned) {
344 St = RemoveDeadBindings(CurrentStmt, St);
345 StateCleaned = true;
346 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000347
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000348 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000349
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000350 if (S == CurrentStmt) {
351 isBlkExpr = getCFG().isBlkExpr(S);
352
353 if (!isBlkExpr)
354 return St;
355 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000356
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000357 return StateMgr.SetValue(St, S, isBlkExpr, V);
358}
359
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000360const GRExprEngine::StateTy::BufferTy&
361GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000362 StateTy::BufferTy& RetBuf) {
363
364 assert (RetBuf.empty());
365
366 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
367 RetBuf.push_back(SetValue(St, S, *I));
368
369 return RetBuf;
370}
371
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000372GRExprEngine::StateTy
373GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000374
Ted Kremenek53c641a2008-02-08 03:02:48 +0000375 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000376 return St;
377
378 if (!StateCleaned) {
379 St = RemoveDeadBindings(CurrentStmt, St);
380 StateCleaned = true;
381 }
382
383 return StateMgr.SetValue(St, LV, V);
384}
385
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000386void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000387 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000388
Ted Kremeneke7d22112008-02-11 19:21:59 +0000389 // Remove old bindings for subexpressions.
390 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000391
Ted Kremenekb38911f2008-01-30 23:03:39 +0000392 RValue V = GetValue(PrevState, Condition);
393
394 switch (V.getBaseKind()) {
395 default:
396 break;
397
Ted Kremenek53c641a2008-02-08 03:02:48 +0000398 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000399 builder.generateNode(PrevState, true);
400 builder.generateNode(PrevState, false);
401 return;
402
403 case RValue::UninitializedKind: {
404 NodeTy* N = builder.generateNode(PrevState, true);
405
406 if (N) {
407 N->markAsSink();
408 UninitBranches.insert(N);
409 }
410
411 builder.markInfeasible(false);
412 return;
413 }
414 }
415
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000416 // Get the current block counter.
417 GRBlockCounter BC = builder.getBlockCounter();
418
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000419 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
420 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000421
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000422 if (isa<nonlval::ConcreteInt>(V) ||
423 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
424
425 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000426
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000427 bool isFeasible = true;
428
429 StateTy St = Assume(PrevState, V, true, isFeasible);
430
431 if (isFeasible)
432 builder.generateNode(St, true);
433 else
434 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000435 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000436 else
437 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000438
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000439 BlockID = builder.getTargetBlock(false)->getBlockID();
440 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000441
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000442 if (isa<nonlval::ConcreteInt>(V) ||
443 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
444
445 // Process the false branch.
446
447 bool isFeasible = false;
448
449 StateTy St = Assume(PrevState, V, false, isFeasible);
450
451 if (isFeasible)
452 builder.generateNode(St, false);
453 else
454 builder.markInfeasible(false);
455 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000456 else
457 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000458}
459
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000460/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000461/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000462void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000463
464 StateTy St = builder.getState();
465 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
466
467 // Three possibilities:
468 //
469 // (1) We know the computed label.
470 // (2) The label is NULL (or some other constant), or Uninitialized.
471 // (3) We have no clue about the label. Dispatch to all targets.
472 //
473
474 typedef IndirectGotoNodeBuilder::iterator iterator;
475
476 if (isa<lval::GotoLabel>(V)) {
477 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
478
479 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000480 if (I.getLabel() == L) {
481 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000482 return;
483 }
484 }
485
486 assert (false && "No block with label.");
487 return;
488 }
489
490 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
491 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000492 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000493 UninitBranches.insert(N);
494 return;
495 }
496
497 // This is really a catch-all. We don't support symbolics yet.
498
499 assert (isa<UnknownVal>(V));
500
501 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000502 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000503}
Ted Kremenekf233d482008-02-05 00:26:40 +0000504
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000505/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
506/// nodes by processing the 'effects' of a switch statement.
507void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
508
509 typedef SwitchNodeBuilder::iterator iterator;
510
511 StateTy St = builder.getState();
512 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
513
514 if (isa<UninitializedVal>(CondV)) {
515 NodeTy* N = builder.generateDefaultCaseNode(St, true);
516 UninitBranches.insert(N);
517 return;
518 }
519
520 StateTy DefaultSt = St;
521
522 // While most of this can be assumed (such as the signedness), having it
523 // just computed makes sure everything makes the same assumptions end-to-end.
524 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
525 APSInt V1(bits, false);
526 APSInt V2 = V1;
527
528 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
529
530 CaseStmt* Case = cast<CaseStmt>(I.getCase());
531
532 // Evaluate the case.
533 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
534 assert (false && "Case condition must evaluate to an integer constant.");
535 return;
536 }
537
538 // Get the RHS of the case, if it exists.
539
540 if (Expr* E = Case->getRHS()) {
541 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
542 assert (false &&
543 "Case condition (RHS) must evaluate to an integer constant.");
544 return ;
545 }
546
547 assert (V1 <= V2);
548 }
549 else V2 = V1;
550
551 // FIXME: Eventually we should replace the logic below with a range
552 // comparison, rather than concretize the values within the range.
553 // This should be easy once we have "ranges" for NonLValues.
554
555 do {
556 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
557
558 NonLValue Result =
559 CondV.EvalBinaryOp(ValMgr, BinaryOperator::EQ, CaseVal);
560
561 // Now "assume" that the case matches.
562 bool isFeasible;
563
564 StateTy StNew = Assume(St, Result, true, isFeasible);
565
566 if (isFeasible) {
567 builder.generateCaseStmtNode(I, StNew);
568
569 // If CondV evaluates to a constant, then we know that this
570 // is the *only* case that we can take, so stop evaluating the
571 // others.
572 if (isa<nonlval::ConcreteInt>(CondV))
573 return;
574 }
575
576 // Now "assume" that the case doesn't match. Add this state
577 // to the default state (if it is feasible).
578
579 StNew = Assume(DefaultSt, Result, false, isFeasible);
580
581 if (isFeasible)
582 DefaultSt = StNew;
583
584 // Concretize the next value in the range.
585 ++V1;
586
587 } while (V1 < V2);
588 }
589
590 // If we reach here, than we know that the default branch is
591 // possible.
592 builder.generateDefaultCaseNode(DefaultSt);
593}
594
595
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000596void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000597 NodeSet& Dst) {
598
599 bool hasR2;
600 StateTy PrevState = Pred->getState();
601
602 RValue R1 = GetValue(PrevState, B->getLHS());
603 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
604
Ted Kremenek22031182008-02-08 02:57:34 +0000605 if (isa<UnknownVal>(R1) &&
606 (isa<UnknownVal>(R2) ||
607 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000608
609 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
610 return;
611 }
Ted Kremenek22031182008-02-08 02:57:34 +0000612 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000613 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
614 return;
615 }
616
617 // R1 is an expression that can evaluate to either 'true' or 'false'.
618 if (B->getOpcode() == BinaryOperator::LAnd) {
619 // hasR2 == 'false' means that LHS evaluated to 'false' and that
620 // we short-circuited, leading to a value of '0' for the '&&' expression.
621 if (hasR2 == false) {
622 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
623 return;
624 }
625 }
626 else {
627 assert (B->getOpcode() == BinaryOperator::LOr);
628 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
629 // we short-circuited, leading to a value of '1' for the '||' expression.
630 if (hasR2 == false) {
631 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
632 return;
633 }
634 }
635
636 // If we reach here we did not short-circuit. Assume R2 == true and
637 // R2 == false.
638
639 bool isFeasible;
640 StateTy St = Assume(PrevState, R2, true, isFeasible);
641
642 if (isFeasible)
643 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
644
645 St = Assume(PrevState, R2, false, isFeasible);
646
647 if (isFeasible)
648 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
649}
650
651
652
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000653void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000654 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000655
656 StmtEntryNode = builder.getLastNode();
657 CurrentStmt = S;
658 NodeSet Dst;
659 StateCleaned = false;
660
661 Visit(S, StmtEntryNode, Dst);
662
663 // If no nodes were generated, generate a new node that has all the
664 // dead mappings removed.
665 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
666 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
667 builder.generateNode(S, St, StmtEntryNode);
668 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000669
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000670 CurrentStmt = NULL;
671 StmtEntryNode = NULL;
672 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000673}
674
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000675GRExprEngine::NodeTy*
676GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000677
678 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000679 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000680 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000681
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000682 NodeTy* N = Builder->generateNode(S, St, Pred);
683 Dst.Add(N);
684 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000685}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000686
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000687void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000688 const StateTy::BufferTy& SB) {
689
690 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
691 Nodify(Dst, S, Pred, *I);
692}
693
Ted Kremenek44842c22008-02-13 18:06:44 +0000694void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000695 if (D != CurrentStmt) {
696 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
697 return;
698 }
699
700 // If we are here, we are loading the value of the decl and binding
701 // it to the block-level expression.
702
703 StateTy St = Pred->getState();
704
705 Nodify(Dst, D, Pred,
706 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
707}
708
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000709void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000710
711 QualType T = CastE->getType();
712
713 // Check for redundant casts.
714 if (E->getType() == T) {
715 Dst.Add(Pred);
716 return;
717 }
718
719 NodeSet S1;
720 Visit(E, Pred, S1);
721
722 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
723 NodeTy* N = *I1;
724 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000725 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000726 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000727 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000728}
729
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000730void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
731 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000732
733 StateTy St = Pred->getState();
734
735 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000736 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
737 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000738 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000739 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000740 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000741
742 Nodify(Dst, DS, Pred, St);
743
744 if (Dst.empty())
745 Dst.Add(Pred);
746}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000747
Ted Kremenekf233d482008-02-05 00:26:40 +0000748
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000749void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000750 NodeTy* Pred, NodeSet& Dst) {
751
752 StateTy St = Pred->getState();
753
754 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000755 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000756
757 Nodify(Dst, S, Pred, SetValue(St, S, R));
758}
759
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000760/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000761void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000762 NodeTy* Pred,
763 NodeSet& Dst) {
764
765 // 6.5.3.4 sizeof: "The result type is an integer."
766
767 QualType T = S->getArgumentType();
768
769 // FIXME: Add support for VLAs.
770 if (isa<VariableArrayType>(T.getTypePtr()))
771 return;
772
773 SourceLocation L = S->getExprLoc();
774 uint64_t size = getContext().getTypeSize(T, L) / 8;
775
776 Nodify(Dst, S, Pred,
777 SetValue(Pred->getState(), S,
778 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
779
780}
781
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000782void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
783 GRExprEngine::NodeTy* Pred,
784 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000785
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000786 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000787 UnaryOperator::Opcode Op = U->getOpcode();
788
789 // FIXME: This is a hack so that for '*' and '&' we don't recurse
790 // on visiting the subexpression if it is a DeclRefExpr. We should
791 // probably just handle AddrOf and Deref in their own methods to make
792 // this cleaner.
793 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
794 isa<DeclRefExpr>(U->getSubExpr()))
795 S1.Add(Pred);
796 else
797 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000798
799 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
800 NodeTy* N1 = *I1;
801 StateTy St = N1->getState();
802
803 switch (U->getOpcode()) {
804 case UnaryOperator::PostInc: {
805 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000806 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000807
808 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
809 GetRValueConstant(1U, U));
810
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000811 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
812 break;
813 }
814
815 case UnaryOperator::PostDec: {
816 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000817 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000818
819 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
820 GetRValueConstant(1U, U));
821
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000822 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
823 break;
824 }
825
826 case UnaryOperator::PreInc: {
827 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000828 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000829
830 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
831 GetRValueConstant(1U, U));
832
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000833 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
834 break;
835 }
836
837 case UnaryOperator::PreDec: {
838 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000839 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000840
841 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
842 GetRValueConstant(1U, U));
843
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000844 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
845 break;
846 }
847
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000848 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000849 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000850 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000851 break;
852 }
853
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000854 case UnaryOperator::Not: {
855 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000856 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000857 break;
858 }
859
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000860 case UnaryOperator::LNot: {
861 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
862 //
863 // Note: technically we do "E == 0", but this is the same in the
864 // transfer functions as "0 == E".
865
866 RValue V1 = GetValue(St, U->getSubExpr());
867
868 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000869 const LValue& L1 = cast<LValue>(V1);
870 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
871 Nodify(Dst, U, N1,
872 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
873 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000874 }
875 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000876 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000877 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000878 Nodify(Dst, U, N1,
879 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
880 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000881 }
882
883 break;
884 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000885
886 case UnaryOperator::SizeOf: {
887 // 6.5.3.4 sizeof: "The result type is an integer."
888
889 QualType T = U->getSubExpr()->getType();
890
891 // FIXME: Add support for VLAs.
892 if (isa<VariableArrayType>(T.getTypePtr()))
893 return;
894
895 SourceLocation L = U->getExprLoc();
896 uint64_t size = getContext().getTypeSize(T, L) / 8;
897
898 Nodify(Dst, U, N1,
899 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
900 getContext().IntTy, L)));
901
902 break;
903 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000904
Ted Kremenek64924852008-01-31 02:35:41 +0000905 case UnaryOperator::AddrOf: {
906 const LValue& L1 = GetLValue(St, U->getSubExpr());
907 Nodify(Dst, U, N1, SetValue(St, U, L1));
908 break;
909 }
910
911 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000912 // FIXME: Stop when dereferencing an uninitialized value.
913 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
914
915 const RValue& V = GetValue(St, U->getSubExpr());
916 const LValue& L1 = cast<LValue>(V);
917
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000918 // After a dereference, one of two possible situations arise:
919 // (1) A crash, because the pointer was NULL.
920 // (2) The pointer is not NULL, and the dereference works.
921 //
922 // We add these assumptions.
923
Ted Kremenek63a4f692008-02-07 06:04:18 +0000924 bool isFeasibleNotNull;
925
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000926 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000927 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
928
929 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000930 QualType T = U->getType();
931 Nodify(Dst, U, N1, SetValue(StNotNull, U,
932 GetValue(StNotNull, L1, &T)));
933 }
934
Ted Kremenek63a4f692008-02-07 06:04:18 +0000935 bool isFeasibleNull;
936
937 // "Assume" that the pointer is NULL.
938 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
939
940 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000941 // We don't use "Nodify" here because the node will be a sink
942 // and we have no intention of processing it later.
943 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
944
Ted Kremenek63a4f692008-02-07 06:04:18 +0000945 if (NullNode) {
946 NullNode->markAsSink();
947
948 if (isFeasibleNotNull)
949 ImplicitNullDeref.insert(NullNode);
950 else
951 ExplicitNullDeref.insert(NullNode);
952 }
953 }
954
Ted Kremenek64924852008-01-31 02:35:41 +0000955 break;
956 }
957
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000958 default: ;
959 assert (false && "Not implemented.");
960 }
961 }
962}
963
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000964void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
965 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000966
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000967 if (isa<DeclRefExpr>(E)) {
968 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000969 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000970 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000971
972 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
973 if (U->getOpcode() == UnaryOperator::Deref) {
974 Visit(U->getSubExpr(), Pred, Dst);
975 return;
976 }
977 }
978
979 Visit(E, Pred, Dst);
980}
981
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000982void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000983 GRExprEngine::NodeTy* Pred,
984 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000985 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000986
987 if (B->isAssignmentOp())
988 VisitAssignmentLHS(B->getLHS(), Pred, S1);
989 else
990 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000991
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000992 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
993 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000994
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000995 // When getting the value for the LHS, check if we are in an assignment.
996 // In such cases, we want to (initially) treat the LHS as an LValue,
997 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000998 // evaluated to LValueDecl's instead of to an NonLValue.
999 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001000 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1001 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001002
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001003 NodeSet S2;
1004 Visit(B->getRHS(), N1, S2);
1005
1006 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001007
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001008 NodeTy* N2 = *I2;
1009 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001010 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001011
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001012 BinaryOperator::Opcode Op = B->getOpcode();
1013
1014 if (Op <= BinaryOperator::Or) {
1015
Ted Kremenek22031182008-02-08 02:57:34 +00001016 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001017 Nodify(Dst, B, N2, SetValue(St, B, V1));
1018 continue;
1019 }
1020
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001021 if (isa<LValue>(V1)) {
1022 // FIXME: Add support for RHS being a non-lvalue.
1023 const LValue& L1 = cast<LValue>(V1);
1024 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +00001025
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001026 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
1027 }
1028 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001029 const NonLValue& R1 = cast<NonLValue>(V1);
1030 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001031
1032 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001033 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001034
1035 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001036
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001037 }
1038
1039 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001040 case BinaryOperator::Assign: {
1041 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +00001042 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001043 break;
1044 }
1045
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001046 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001047
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001048 assert (B->isCompoundAssignmentOp());
1049
1050 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +00001051 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001052
Ted Kremenekda9bd092008-02-08 07:05:39 +00001053 if (Op >= BinaryOperator::AndAssign)
1054 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1055 else
1056 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001057
1058 if (isa<LValue>(V2)) {
1059 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +00001060 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001061 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +00001062 }
1063 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001064 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001065 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001066 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +00001067 }
1068
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001069 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001070 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001071 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001072 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001073 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001074 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001075}
Ted Kremenekee985462008-01-16 18:18:48 +00001076
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001077
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001078void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
1079 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001080
1081 // FIXME: add metadata to the CFG so that we can disable
1082 // this check when we KNOW that there is no block-level subexpression.
1083 // The motivation is that this check requires a hashtable lookup.
1084
1085 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1086 Dst.Add(Pred);
1087 return;
1088 }
1089
1090 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001091
1092 default:
1093 // Cases we intentionally have "default" handle:
1094 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
1095
1096 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1097 break;
1098
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001099 case Stmt::BinaryOperatorClass: {
1100 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001101
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001102 if (B->isLogicalOp()) {
1103 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001104 break;
1105 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001106 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001107 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001108 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001109 break;
1110 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001111
1112 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1113 break;
1114 }
1115
1116 case Stmt::CastExprClass: {
1117 CastExpr* C = cast<CastExpr>(S);
1118 VisitCast(C, C->getSubExpr(), Pred, Dst);
1119 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001120 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001121
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001122 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1123 ChooseExpr* C = cast<ChooseExpr>(S);
1124 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1125 break;
1126 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001127
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001128 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001129 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1130 break;
1131
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001132 case Stmt::ConditionalOperatorClass: { // '?' operator
1133 ConditionalOperator* C = cast<ConditionalOperator>(S);
1134 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1135 break;
1136 }
1137
1138 case Stmt::DeclRefExprClass:
1139 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1140 break;
1141
1142 case Stmt::DeclStmtClass:
1143 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1144 break;
1145
1146 case Stmt::ImplicitCastExprClass: {
1147 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1148 VisitCast(C, C->getSubExpr(), Pred, Dst);
1149 break;
1150 }
1151
1152 case Stmt::ParenExprClass:
1153 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1154 break;
1155
1156 case Stmt::SizeOfAlignOfTypeExprClass:
1157 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1158 break;
1159
Ted Kremenekda9bd092008-02-08 07:05:39 +00001160 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001161 StmtExpr* SE = cast<StmtExpr>(S);
1162
Ted Kremenekda9bd092008-02-08 07:05:39 +00001163 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001164 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1165 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001166 break;
1167 }
1168
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001169 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001170 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1171 Visit(R, Pred, Dst);
1172 else
1173 Dst.Add(Pred);
1174
1175 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001176 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001177
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001178 case Stmt::UnaryOperatorClass:
1179 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001180 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001181 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001182}
1183
Ted Kremenekee985462008-01-16 18:18:48 +00001184//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001185// "Assume" logic.
1186//===----------------------------------------------------------------------===//
1187
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001188GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001189 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001190 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001191
1192 switch (Cond.getSubKind()) {
1193 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001194 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001195 return St;
1196
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001197 case lval::SymbolValKind:
1198 if (Assumption)
1199 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1200 ValMgr.getZeroWithPtrWidth(), isFeasible);
1201 else
1202 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1203 ValMgr.getZeroWithPtrWidth(), isFeasible);
1204
Ted Kremenek08b66252008-02-06 04:31:33 +00001205
Ted Kremenek329f8542008-02-05 21:52:21 +00001206 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001207 isFeasible = Assumption;
1208 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001209
Ted Kremenek329f8542008-02-05 21:52:21 +00001210 case lval::ConcreteIntKind: {
1211 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001212 isFeasible = b ? Assumption : !Assumption;
1213 return St;
1214 }
1215 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001216}
1217
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001218GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001219 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001220 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001221
1222 switch (Cond.getSubKind()) {
1223 default:
1224 assert (false && "'Assume' not implemented for this NonLValue.");
1225 return St;
1226
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001227
1228 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001229 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001230 SymbolID sym = SV.getSymbol();
1231
1232 if (Assumption)
1233 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1234 isFeasible);
1235 else
1236 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1237 isFeasible);
1238 }
1239
Ted Kremenek08b66252008-02-06 04:31:33 +00001240 case nonlval::SymIntConstraintValKind:
1241 return
1242 AssumeSymInt(St, Assumption,
1243 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1244 isFeasible);
1245
Ted Kremenek329f8542008-02-05 21:52:21 +00001246 case nonlval::ConcreteIntKind: {
1247 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001248 isFeasible = b ? Assumption : !Assumption;
1249 return St;
1250 }
1251 }
1252}
1253
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001254GRExprEngine::StateTy
1255GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001256 const llvm::APSInt& V, bool& isFeasible) {
1257
1258 // First, determine if sym == X, where X != V.
1259 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1260 isFeasible = *X != V;
1261 return St;
1262 }
1263
1264 // Second, determine if sym != V.
1265 if (St.isNotEqual(sym, V)) {
1266 isFeasible = true;
1267 return St;
1268 }
1269
1270 // If we reach here, sym is not a constant and we don't know if it is != V.
1271 // Make that assumption.
1272
1273 isFeasible = true;
1274 return StateMgr.AddNE(St, sym, V);
1275}
1276
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001277GRExprEngine::StateTy
1278GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001279 const llvm::APSInt& V, bool& isFeasible) {
1280
1281 // First, determine if sym == X, where X != V.
1282 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1283 isFeasible = *X == V;
1284 return St;
1285 }
1286
1287 // Second, determine if sym != V.
1288 if (St.isNotEqual(sym, V)) {
1289 isFeasible = false;
1290 return St;
1291 }
1292
1293 // If we reach here, sym is not a constant and we don't know if it is == V.
1294 // Make that assumption.
1295
1296 isFeasible = true;
1297 return StateMgr.AddEQ(St, sym, V);
1298}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001299
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001300GRExprEngine::StateTy
1301GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001302 const SymIntConstraint& C, bool& isFeasible) {
1303
1304 switch (C.getOpcode()) {
1305 default:
1306 // No logic yet for other operators.
1307 return St;
1308
1309 case BinaryOperator::EQ:
1310 if (Assumption)
1311 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1312 else
1313 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1314
1315 case BinaryOperator::NE:
1316 if (Assumption)
1317 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1318 else
1319 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1320 }
1321}
1322
Ted Kremenekb38911f2008-01-30 23:03:39 +00001323//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001324// Driver.
1325//===----------------------------------------------------------------------===//
1326
Ted Kremenekaa66a322008-01-16 21:46:15 +00001327#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001328static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001329
Ted Kremenekaa66a322008-01-16 21:46:15 +00001330namespace llvm {
1331template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001332struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001333 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001334
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001335 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001336
1337 Out << "Variables:\\l";
1338
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001339 bool isFirst = true;
1340
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001341 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001342 E=St.vb_end(); I!=E;++I) {
1343
1344 if (isFirst)
1345 isFirst = false;
1346 else
1347 Out << "\\l";
1348
1349 Out << ' ' << I.getKey()->getName() << " : ";
1350 I.getData().print(Out);
1351 }
1352
1353 }
1354
Ted Kremeneke7d22112008-02-11 19:21:59 +00001355
Ted Kremenek44842c22008-02-13 18:06:44 +00001356 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001357
1358 bool isFirst = true;
1359
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001360 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001361 I != E;++I) {
1362
1363 if (isFirst) {
1364 Out << "\\l\\lSub-Expressions:\\l";
1365 isFirst = false;
1366 }
1367 else
1368 Out << "\\l";
1369
1370 Out << " (" << (void*) I.getKey() << ") ";
1371 I.getKey()->printPretty(Out);
1372 Out << " : ";
1373 I.getData().print(Out);
1374 }
1375 }
1376
Ted Kremenek44842c22008-02-13 18:06:44 +00001377 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001378
Ted Kremenek016f52f2008-02-08 21:10:02 +00001379 bool isFirst = true;
1380
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001381 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001382 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001383 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001384 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001385 isFirst = false;
1386 }
1387 else
1388 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001389
Ted Kremeneke7d22112008-02-11 19:21:59 +00001390 Out << " (" << (void*) I.getKey() << ") ";
1391 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001392 Out << " : ";
1393 I.getData().print(Out);
1394 }
1395 }
1396
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001397 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001398 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1399
1400 if (CE.isEmpty())
1401 return;
1402
1403 Out << "\\l\\|'==' constraints:";
1404
1405 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1406 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1407 }
1408
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001409 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001410 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1411
1412 if (NE.isEmpty())
1413 return;
1414
1415 Out << "\\l\\|'!=' constraints:";
1416
1417 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1418 I != EI; ++I){
1419
1420 Out << "\\l $" << I.getKey() << " : ";
1421 bool isFirst = true;
1422
1423 ValueState::IntSetTy::iterator J=I.getData().begin(),
1424 EJ=I.getData().end();
1425 for ( ; J != EJ; ++J) {
1426 if (isFirst) isFirst = false;
1427 else Out << ", ";
1428
1429 Out << (*J)->toString();
1430 }
1431 }
1432 }
1433
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001434 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001435 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001436
1437 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001438 ProgramPoint Loc = N->getLocation();
1439
1440 switch (Loc.getKind()) {
1441 case ProgramPoint::BlockEntranceKind:
1442 Out << "Block Entrance: B"
1443 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1444 break;
1445
1446 case ProgramPoint::BlockExitKind:
1447 assert (false);
1448 break;
1449
1450 case ProgramPoint::PostStmtKind: {
1451 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001452 Out << L.getStmt()->getStmtClassName() << ':'
1453 << (void*) L.getStmt() << ' ';
1454
Ted Kremenekaa66a322008-01-16 21:46:15 +00001455 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001456
1457 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1458 Out << "\\|Implicit-Null Dereference.\\l";
1459 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001460 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1461 Out << "\\|Explicit-Null Dereference.\\l";
1462 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001463
Ted Kremenekaa66a322008-01-16 21:46:15 +00001464 break;
1465 }
1466
1467 default: {
1468 const BlockEdge& E = cast<BlockEdge>(Loc);
1469 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1470 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001471
1472 if (Stmt* T = E.getSrc()->getTerminator()) {
1473 Out << "\\|Terminator: ";
1474 E.getSrc()->printTerminator(Out);
1475
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001476 if (isa<SwitchStmt>(T)) {
1477 Stmt* Label = E.getDst()->getLabel();
1478
1479 if (Label) {
1480 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1481 Out << "\\lcase ";
1482 C->getLHS()->printPretty(Out);
1483
1484 if (Stmt* RHS = C->getRHS()) {
1485 Out << " .. ";
1486 RHS->printPretty(Out);
1487 }
1488
1489 Out << ":";
1490 }
1491 else {
1492 assert (isa<DefaultStmt>(Label));
1493 Out << "\\ldefault:";
1494 }
1495 }
1496 else
1497 Out << "\\l(implicit) default:";
1498 }
1499 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001500 // FIXME
1501 }
1502 else {
1503 Out << "\\lCondition: ";
1504 if (*E.getSrc()->succ_begin() == E.getDst())
1505 Out << "true";
1506 else
1507 Out << "false";
1508 }
1509
1510 Out << "\\l";
1511 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001512
1513 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1514 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1515 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001516 }
1517 }
1518
Ted Kremenek9153f732008-02-05 07:17:49 +00001519 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001520
Ted Kremeneke7d22112008-02-11 19:21:59 +00001521 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001522
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001523 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001524 return Out.str();
1525 }
1526};
1527} // end llvm namespace
1528#endif
1529
Ted Kremenekee985462008-01-16 18:18:48 +00001530namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001531void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001532 Diagnostic& Diag) {
1533
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001534 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001535 GRExprEngine* CheckerState = &Engine.getCheckerState();
1536
1537 // Execute the worklist algorithm.
Ted Kremenek19227e32008-02-07 06:33:19 +00001538 Engine.ExecuteWorkList();
1539
1540 // Look for explicit-Null dereferences and warn about them.
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001541
Ted Kremenek19227e32008-02-07 06:33:19 +00001542
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001543 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001544 E=CheckerState->null_end(); I!=E; ++I) {
1545
1546 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1547 Expr* E = cast<Expr>(L.getStmt());
1548
1549 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1550 diag::chkr_null_deref_after_check);
1551 }
1552
1553
Ted Kremenekaa66a322008-01-16 21:46:15 +00001554#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001555 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001556 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001557 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001558#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001559}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001560} // end clang namespace