blob: 7addb5c0531d4a232f0162687ce7f32bfa6b05a3 [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 "RValues.h"
19#include "ValueState.h"
20
Ted Kremenek4d4dd852008-02-13 17:41:41 +000021#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000022#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000023#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000024#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek19227e32008-02-07 06:33:19 +000025#include "clang/Basic/Diagnostic.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000026
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/DataTypes.h"
29#include "llvm/ADT/APSInt.h"
30#include "llvm/ADT/FoldingSet.h"
31#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000032#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000033#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000034#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000035#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036#include "llvm/Support/Streams.h"
37
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000038#include <functional>
39
Ted Kremenekaa66a322008-01-16 21:46:15 +000040#ifndef NDEBUG
41#include "llvm/Support/GraphWriter.h"
42#include <sstream>
43#endif
44
Ted Kremenekd27f8162008-01-15 23:55:06 +000045using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000046using llvm::dyn_cast;
47using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000048using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000049
50//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000051// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000052//
53// FIXME: This checker logic should be eventually broken into two components.
54// The first is the "meta"-level checking logic; the code that
55// does the Stmt visitation, fetching values from the map, etc.
56// The second part does the actual state manipulation. This way we
57// get more of a separate of concerns of these two pieces, with the
58// latter potentially being refactored back into the main checking
59// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000060//===----------------------------------------------------------------------===//
61
62namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000063
Ted Kremenek4d4dd852008-02-13 17:41:41 +000064class VISIBILITY_HIDDEN GRExprEngine {
Ted Kremenekd27f8162008-01-15 23:55:06 +000065
66public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000067 typedef ValueStateManager::StateTy StateTy;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000068 typedef ExplodedGraph<GRExprEngine> GraphTy;
69 typedef GraphTy::NodeTy NodeTy;
70
71 // Builders.
Ted Kremenek4d4dd852008-02-13 17:41:41 +000072 typedef GRStmtNodeBuilder<GRExprEngine> StmtNodeBuilder;
73 typedef GRBranchNodeBuilder<GRExprEngine> BranchNodeBuilder;
74 typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000075 typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder;
76
Ted Kremenekab2b8c52008-01-23 19:59:44 +000077
78 class NodeSet {
79 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
80 ImplTy Impl;
81 public:
82
83 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000084 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000085
Ted Kremenekb38911f2008-01-30 23:03:39 +000086 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000087
88 typedef ImplTy::iterator iterator;
89 typedef ImplTy::const_iterator const_iterator;
90
91 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000092 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000093
94 iterator begin() { return Impl.begin(); }
95 iterator end() { return Impl.end(); }
96
97 const_iterator begin() const { return Impl.begin(); }
98 const_iterator end() const { return Impl.end(); }
99 };
Ted Kremenekcba2e432008-02-05 19:35:18 +0000100
Ted Kremenekd27f8162008-01-15 23:55:06 +0000101protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000102 /// G - the simulation graph.
103 GraphTy& G;
104
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000105 /// Liveness - live-variables information the ValueDecl* and block-level
106 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000107 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000108
Ted Kremenek44842c22008-02-13 18:06:44 +0000109 /// Builder - The current GRStmtNodeBuilder which is used when building the
110 /// nodes for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000111 StmtNodeBuilder* Builder;
112
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000113 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000114 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000115
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000116 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000117 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000118
Ted Kremenek68fd2572008-01-29 17:27:31 +0000119 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000120 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000121
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000122 /// StmtEntryNode - The immediate predecessor node.
123 NodeTy* StmtEntryNode;
124
125 /// CurrentStmt - The current block-level statement.
126 Stmt* CurrentStmt;
127
Ted Kremenekb38911f2008-01-30 23:03:39 +0000128 /// UninitBranches - Nodes in the ExplodedGraph that result from
129 /// taking a branch based on an uninitialized value.
130 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
131 UninitBranchesTy UninitBranches;
132
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000133 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
134 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000135 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
136 NullDerefTy ImplicitNullDeref;
137 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000138
139
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000140 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000141
Ted Kremenekd27f8162008-01-15 23:55:06 +0000142public:
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000143 GRExprEngine(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000144 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000145 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000146 ValMgr(StateMgr.getValueManager()),
147 SymMgr(StateMgr.getSymbolManager()),
148 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000149
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000150 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000151 Liveness.runOnCFG(G.getCFG());
152 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000153 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000154
Ted Kremenek19227e32008-02-07 06:33:19 +0000155 /// getContext - Return the ASTContext associated with this analysis.
156 ASTContext& getContext() const { return G.getContext(); }
157
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000158 /// getCFG - Returns the CFG associated with this analysis.
159 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000160
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000161 /// getInitialState - Return the initial state used for the root vertex
162 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000163 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000164 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000165
166 // Iterate the parameters.
167 FunctionDecl& F = G.getFunctionDecl();
168
169 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000170 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000171 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000172
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000173 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000174 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000175
176 bool isUninitControlFlow(const NodeTy* N) const {
177 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
178 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000179
180 bool isImplicitNullDeref(const NodeTy* N) const {
181 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
182 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000183
184 bool isExplicitNullDeref(const NodeTy* N) const {
185 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
186 }
187
Ted Kremenek19227e32008-02-07 06:33:19 +0000188 typedef NullDerefTy::iterator null_iterator;
189 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
190 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000191
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000192 /// ProcessStmt - Called by GRCoreEngine. Used to generate new successor
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000193 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000194 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
195
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000196 /// ProcessBranch - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000197 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000198 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000199
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000200 /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000201 /// nodes by processing the 'effects' of a computed goto jump.
202 void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder);
203
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000204 /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
205 /// nodes by processing the 'effects' of a switch statement.
206 void ProcessSwitch(SwitchNodeBuilder& builder);
207
Ted Kremenekb87d9092008-02-08 19:17:19 +0000208 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000209 /// that all subexpression mappings are removed and that any
210 /// block-level expressions that are not live at 'S' also have their
211 /// mappings removed.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000212 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
213 return StateMgr.RemoveDeadBindings(St, S, Liveness);
214 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000215
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000216 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000217
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000218 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
219 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000220 }
221
Ted Kremenekcba2e432008-02-05 19:35:18 +0000222 /// SetValue - This version of SetValue is used to batch process a set
223 /// of different possible RValues and return a set of different states.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000224 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000225 const RValue::BufferTy& V,
226 StateTy::BufferTy& RetBuf);
227
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000228 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000229
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000230 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000231 return StateMgr.GetValue(St, S);
232 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000233
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000234 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000235 return StateMgr.GetValue(St, S, &hasVal);
236 }
237
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000238 inline RValue GetValue(const StateTy& St, const Expr* S) {
239 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000240 }
241
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000242 inline RValue GetValue(const StateTy& St, const LValue& LV,
243 QualType* T = NULL) {
244
245 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000246 }
247
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000248 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000249 return StateMgr.GetLValue(St, S);
250 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000251
252 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
253 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
254 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000255
256 /// Assume - Create new state by assuming that a given expression
257 /// is true or false.
258 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
259 bool& isFeasible) {
260 if (isa<LValue>(Cond))
261 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
262 else
263 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
264 }
265
266 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
267 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000268
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000269 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
270 bool& isFeasible);
271
272 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
273 bool& isFeasible);
274
Ted Kremenek08b66252008-02-06 04:31:33 +0000275 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
276 bool& isFeasible);
277
Ted Kremenek7e593362008-02-07 15:20:13 +0000278 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000279
Ted Kremenekcba2e432008-02-05 19:35:18 +0000280 /// Nodify - This version of Nodify is used to batch process a set of states.
281 /// The states are not guaranteed to be unique.
282 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
283
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000284 /// Visit - Transfer function logic for all statements. Dispatches to
285 /// other functions that handle specific kinds of statements.
286 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000287
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000288 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000289 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
290
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000291 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000292
Ted Kremenek230aaab2008-02-12 21:37:25 +0000293 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
294 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
295
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000296 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
297 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
298
Ted Kremenek9de04c42008-01-24 20:55:43 +0000299 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000300 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
301
302 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000303 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000304 NodeTy* Pred, NodeSet& Dst);
305
306 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
307 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000308
309 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
310 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
311 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000312
313 /// VisitUnaryOperator - Transfer function logic for unary operators.
314 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
315
Ted Kremenekd27f8162008-01-15 23:55:06 +0000316};
317} // end anonymous namespace
318
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000319
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000320GRExprEngine::StateTy
321GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000322
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000323 if (!StateCleaned) {
324 St = RemoveDeadBindings(CurrentStmt, St);
325 StateCleaned = true;
326 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000327
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000328 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000329
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000330 if (S == CurrentStmt) {
331 isBlkExpr = getCFG().isBlkExpr(S);
332
333 if (!isBlkExpr)
334 return St;
335 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000336
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000337 return StateMgr.SetValue(St, S, isBlkExpr, V);
338}
339
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000340const GRExprEngine::StateTy::BufferTy&
341GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000342 StateTy::BufferTy& RetBuf) {
343
344 assert (RetBuf.empty());
345
346 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
347 RetBuf.push_back(SetValue(St, S, *I));
348
349 return RetBuf;
350}
351
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000352GRExprEngine::StateTy
353GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000354
Ted Kremenek53c641a2008-02-08 03:02:48 +0000355 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000356 return St;
357
358 if (!StateCleaned) {
359 St = RemoveDeadBindings(CurrentStmt, St);
360 StateCleaned = true;
361 }
362
363 return StateMgr.SetValue(St, LV, V);
364}
365
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000366void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000367 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000368
Ted Kremeneke7d22112008-02-11 19:21:59 +0000369 // Remove old bindings for subexpressions.
370 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000371
Ted Kremenekb38911f2008-01-30 23:03:39 +0000372 RValue V = GetValue(PrevState, Condition);
373
374 switch (V.getBaseKind()) {
375 default:
376 break;
377
Ted Kremenek53c641a2008-02-08 03:02:48 +0000378 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000379 builder.generateNode(PrevState, true);
380 builder.generateNode(PrevState, false);
381 return;
382
383 case RValue::UninitializedKind: {
384 NodeTy* N = builder.generateNode(PrevState, true);
385
386 if (N) {
387 N->markAsSink();
388 UninitBranches.insert(N);
389 }
390
391 builder.markInfeasible(false);
392 return;
393 }
394 }
395
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000396 // Get the current block counter.
397 GRBlockCounter BC = builder.getBlockCounter();
398
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000399 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
400 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000401
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000402 if (isa<nonlval::ConcreteInt>(V) ||
403 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
404
405 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000406
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000407 bool isFeasible = true;
408
409 StateTy St = Assume(PrevState, V, true, isFeasible);
410
411 if (isFeasible)
412 builder.generateNode(St, true);
413 else
414 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000415 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000416 else
417 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000418
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000419 BlockID = builder.getTargetBlock(false)->getBlockID();
420 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000421
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000422 if (isa<nonlval::ConcreteInt>(V) ||
423 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
424
425 // Process the false branch.
426
427 bool isFeasible = false;
428
429 StateTy St = Assume(PrevState, V, false, isFeasible);
430
431 if (isFeasible)
432 builder.generateNode(St, false);
433 else
434 builder.markInfeasible(false);
435 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000436 else
437 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000438}
439
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000440/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000441/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000442void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000443
444 StateTy St = builder.getState();
445 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
446
447 // Three possibilities:
448 //
449 // (1) We know the computed label.
450 // (2) The label is NULL (or some other constant), or Uninitialized.
451 // (3) We have no clue about the label. Dispatch to all targets.
452 //
453
454 typedef IndirectGotoNodeBuilder::iterator iterator;
455
456 if (isa<lval::GotoLabel>(V)) {
457 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
458
459 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000460 if (I.getLabel() == L) {
461 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000462 return;
463 }
464 }
465
466 assert (false && "No block with label.");
467 return;
468 }
469
470 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
471 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000472 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000473 UninitBranches.insert(N);
474 return;
475 }
476
477 // This is really a catch-all. We don't support symbolics yet.
478
479 assert (isa<UnknownVal>(V));
480
481 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000482 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000483}
Ted Kremenekf233d482008-02-05 00:26:40 +0000484
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000485/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
486/// nodes by processing the 'effects' of a switch statement.
487void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
488
489 typedef SwitchNodeBuilder::iterator iterator;
490
491 StateTy St = builder.getState();
492 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
493
494 if (isa<UninitializedVal>(CondV)) {
495 NodeTy* N = builder.generateDefaultCaseNode(St, true);
496 UninitBranches.insert(N);
497 return;
498 }
499
500 StateTy DefaultSt = St;
501
502 // While most of this can be assumed (such as the signedness), having it
503 // just computed makes sure everything makes the same assumptions end-to-end.
504 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
505 APSInt V1(bits, false);
506 APSInt V2 = V1;
507
508 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
509
510 CaseStmt* Case = cast<CaseStmt>(I.getCase());
511
512 // Evaluate the case.
513 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
514 assert (false && "Case condition must evaluate to an integer constant.");
515 return;
516 }
517
518 // Get the RHS of the case, if it exists.
519
520 if (Expr* E = Case->getRHS()) {
521 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
522 assert (false &&
523 "Case condition (RHS) must evaluate to an integer constant.");
524 return ;
525 }
526
527 assert (V1 <= V2);
528 }
529 else V2 = V1;
530
531 // FIXME: Eventually we should replace the logic below with a range
532 // comparison, rather than concretize the values within the range.
533 // This should be easy once we have "ranges" for NonLValues.
534
535 do {
536 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
537
538 NonLValue Result =
539 CondV.EvalBinaryOp(ValMgr, BinaryOperator::EQ, CaseVal);
540
541 // Now "assume" that the case matches.
542 bool isFeasible;
543
544 StateTy StNew = Assume(St, Result, true, isFeasible);
545
546 if (isFeasible) {
547 builder.generateCaseStmtNode(I, StNew);
548
549 // If CondV evaluates to a constant, then we know that this
550 // is the *only* case that we can take, so stop evaluating the
551 // others.
552 if (isa<nonlval::ConcreteInt>(CondV))
553 return;
554 }
555
556 // Now "assume" that the case doesn't match. Add this state
557 // to the default state (if it is feasible).
558
559 StNew = Assume(DefaultSt, Result, false, isFeasible);
560
561 if (isFeasible)
562 DefaultSt = StNew;
563
564 // Concretize the next value in the range.
565 ++V1;
566
567 } while (V1 < V2);
568 }
569
570 // If we reach here, than we know that the default branch is
571 // possible.
572 builder.generateDefaultCaseNode(DefaultSt);
573}
574
575
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000576void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000577 NodeSet& Dst) {
578
579 bool hasR2;
580 StateTy PrevState = Pred->getState();
581
582 RValue R1 = GetValue(PrevState, B->getLHS());
583 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
584
Ted Kremenek22031182008-02-08 02:57:34 +0000585 if (isa<UnknownVal>(R1) &&
586 (isa<UnknownVal>(R2) ||
587 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000588
589 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
590 return;
591 }
Ted Kremenek22031182008-02-08 02:57:34 +0000592 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000593 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
594 return;
595 }
596
597 // R1 is an expression that can evaluate to either 'true' or 'false'.
598 if (B->getOpcode() == BinaryOperator::LAnd) {
599 // hasR2 == 'false' means that LHS evaluated to 'false' and that
600 // we short-circuited, leading to a value of '0' for the '&&' expression.
601 if (hasR2 == false) {
602 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
603 return;
604 }
605 }
606 else {
607 assert (B->getOpcode() == BinaryOperator::LOr);
608 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
609 // we short-circuited, leading to a value of '1' for the '||' expression.
610 if (hasR2 == false) {
611 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
612 return;
613 }
614 }
615
616 // If we reach here we did not short-circuit. Assume R2 == true and
617 // R2 == false.
618
619 bool isFeasible;
620 StateTy St = Assume(PrevState, R2, true, isFeasible);
621
622 if (isFeasible)
623 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
624
625 St = Assume(PrevState, R2, false, isFeasible);
626
627 if (isFeasible)
628 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
629}
630
631
632
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000633void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000634 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000635
636 StmtEntryNode = builder.getLastNode();
637 CurrentStmt = S;
638 NodeSet Dst;
639 StateCleaned = false;
640
641 Visit(S, StmtEntryNode, Dst);
642
643 // If no nodes were generated, generate a new node that has all the
644 // dead mappings removed.
645 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
646 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
647 builder.generateNode(S, St, StmtEntryNode);
648 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000649
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000650 CurrentStmt = NULL;
651 StmtEntryNode = NULL;
652 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000653}
654
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000655GRExprEngine::NodeTy*
656GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000657
658 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000659 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000660 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000661
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000662 NodeTy* N = Builder->generateNode(S, St, Pred);
663 Dst.Add(N);
664 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000665}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000666
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000667void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000668 const StateTy::BufferTy& SB) {
669
670 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
671 Nodify(Dst, S, Pred, *I);
672}
673
Ted Kremenek44842c22008-02-13 18:06:44 +0000674void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000675 if (D != CurrentStmt) {
676 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
677 return;
678 }
679
680 // If we are here, we are loading the value of the decl and binding
681 // it to the block-level expression.
682
683 StateTy St = Pred->getState();
684
685 Nodify(Dst, D, Pred,
686 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
687}
688
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000689void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000690
691 QualType T = CastE->getType();
692
693 // Check for redundant casts.
694 if (E->getType() == T) {
695 Dst.Add(Pred);
696 return;
697 }
698
699 NodeSet S1;
700 Visit(E, Pred, S1);
701
702 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
703 NodeTy* N = *I1;
704 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000705 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000706 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000707 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000708}
709
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000710void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
711 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000712
713 StateTy St = Pred->getState();
714
715 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000716 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
717 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000718 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000719 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000720 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000721
722 Nodify(Dst, DS, Pred, St);
723
724 if (Dst.empty())
725 Dst.Add(Pred);
726}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000727
Ted Kremenekf233d482008-02-05 00:26:40 +0000728
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000729void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000730 NodeTy* Pred, NodeSet& Dst) {
731
732 StateTy St = Pred->getState();
733
734 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000735 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000736
737 Nodify(Dst, S, Pred, SetValue(St, S, R));
738}
739
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000740/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000741void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000742 NodeTy* Pred,
743 NodeSet& Dst) {
744
745 // 6.5.3.4 sizeof: "The result type is an integer."
746
747 QualType T = S->getArgumentType();
748
749 // FIXME: Add support for VLAs.
750 if (isa<VariableArrayType>(T.getTypePtr()))
751 return;
752
753 SourceLocation L = S->getExprLoc();
754 uint64_t size = getContext().getTypeSize(T, L) / 8;
755
756 Nodify(Dst, S, Pred,
757 SetValue(Pred->getState(), S,
758 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
759
760}
761
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000762void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
763 GRExprEngine::NodeTy* Pred,
764 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000765
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000766 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000767 UnaryOperator::Opcode Op = U->getOpcode();
768
769 // FIXME: This is a hack so that for '*' and '&' we don't recurse
770 // on visiting the subexpression if it is a DeclRefExpr. We should
771 // probably just handle AddrOf and Deref in their own methods to make
772 // this cleaner.
773 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
774 isa<DeclRefExpr>(U->getSubExpr()))
775 S1.Add(Pred);
776 else
777 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000778
779 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
780 NodeTy* N1 = *I1;
781 StateTy St = N1->getState();
782
783 switch (U->getOpcode()) {
784 case UnaryOperator::PostInc: {
785 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000786 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000787
788 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
789 GetRValueConstant(1U, U));
790
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000791 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
792 break;
793 }
794
795 case UnaryOperator::PostDec: {
796 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000797 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000798
799 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
800 GetRValueConstant(1U, U));
801
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000802 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
803 break;
804 }
805
806 case UnaryOperator::PreInc: {
807 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000808 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000809
810 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
811 GetRValueConstant(1U, U));
812
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000813 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
814 break;
815 }
816
817 case UnaryOperator::PreDec: {
818 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000819 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000820
821 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
822 GetRValueConstant(1U, U));
823
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000824 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
825 break;
826 }
827
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000828 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000829 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000830 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000831 break;
832 }
833
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000834 case UnaryOperator::Not: {
835 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000836 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000837 break;
838 }
839
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000840 case UnaryOperator::LNot: {
841 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
842 //
843 // Note: technically we do "E == 0", but this is the same in the
844 // transfer functions as "0 == E".
845
846 RValue V1 = GetValue(St, U->getSubExpr());
847
848 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000849 const LValue& L1 = cast<LValue>(V1);
850 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
851 Nodify(Dst, U, N1,
852 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
853 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000854 }
855 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000856 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000857 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000858 Nodify(Dst, U, N1,
859 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
860 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000861 }
862
863 break;
864 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000865
866 case UnaryOperator::SizeOf: {
867 // 6.5.3.4 sizeof: "The result type is an integer."
868
869 QualType T = U->getSubExpr()->getType();
870
871 // FIXME: Add support for VLAs.
872 if (isa<VariableArrayType>(T.getTypePtr()))
873 return;
874
875 SourceLocation L = U->getExprLoc();
876 uint64_t size = getContext().getTypeSize(T, L) / 8;
877
878 Nodify(Dst, U, N1,
879 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
880 getContext().IntTy, L)));
881
882 break;
883 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000884
Ted Kremenek64924852008-01-31 02:35:41 +0000885 case UnaryOperator::AddrOf: {
886 const LValue& L1 = GetLValue(St, U->getSubExpr());
887 Nodify(Dst, U, N1, SetValue(St, U, L1));
888 break;
889 }
890
891 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000892 // FIXME: Stop when dereferencing an uninitialized value.
893 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
894
895 const RValue& V = GetValue(St, U->getSubExpr());
896 const LValue& L1 = cast<LValue>(V);
897
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000898 // After a dereference, one of two possible situations arise:
899 // (1) A crash, because the pointer was NULL.
900 // (2) The pointer is not NULL, and the dereference works.
901 //
902 // We add these assumptions.
903
Ted Kremenek63a4f692008-02-07 06:04:18 +0000904 bool isFeasibleNotNull;
905
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000906 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000907 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
908
909 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000910 QualType T = U->getType();
911 Nodify(Dst, U, N1, SetValue(StNotNull, U,
912 GetValue(StNotNull, L1, &T)));
913 }
914
Ted Kremenek63a4f692008-02-07 06:04:18 +0000915 bool isFeasibleNull;
916
917 // "Assume" that the pointer is NULL.
918 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
919
920 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000921 // We don't use "Nodify" here because the node will be a sink
922 // and we have no intention of processing it later.
923 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
924
Ted Kremenek63a4f692008-02-07 06:04:18 +0000925 if (NullNode) {
926 NullNode->markAsSink();
927
928 if (isFeasibleNotNull)
929 ImplicitNullDeref.insert(NullNode);
930 else
931 ExplicitNullDeref.insert(NullNode);
932 }
933 }
934
Ted Kremenek64924852008-01-31 02:35:41 +0000935 break;
936 }
937
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000938 default: ;
939 assert (false && "Not implemented.");
940 }
941 }
942}
943
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000944void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
945 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000946
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000947 if (isa<DeclRefExpr>(E)) {
948 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000949 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000950 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000951
952 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
953 if (U->getOpcode() == UnaryOperator::Deref) {
954 Visit(U->getSubExpr(), Pred, Dst);
955 return;
956 }
957 }
958
959 Visit(E, Pred, Dst);
960}
961
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000962void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000963 GRExprEngine::NodeTy* Pred,
964 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000965 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000966
967 if (B->isAssignmentOp())
968 VisitAssignmentLHS(B->getLHS(), Pred, S1);
969 else
970 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000971
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000972 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
973 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000974
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000975 // When getting the value for the LHS, check if we are in an assignment.
976 // In such cases, we want to (initially) treat the LHS as an LValue,
977 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000978 // evaluated to LValueDecl's instead of to an NonLValue.
979 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000980 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
981 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000982
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000983 NodeSet S2;
984 Visit(B->getRHS(), N1, S2);
985
986 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000987
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000988 NodeTy* N2 = *I2;
989 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000990 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000991
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000992 BinaryOperator::Opcode Op = B->getOpcode();
993
994 if (Op <= BinaryOperator::Or) {
995
Ted Kremenek22031182008-02-08 02:57:34 +0000996 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000997 Nodify(Dst, B, N2, SetValue(St, B, V1));
998 continue;
999 }
1000
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001001 if (isa<LValue>(V1)) {
1002 // FIXME: Add support for RHS being a non-lvalue.
1003 const LValue& L1 = cast<LValue>(V1);
1004 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +00001005
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001006 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
1007 }
1008 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001009 const NonLValue& R1 = cast<NonLValue>(V1);
1010 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001011
1012 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001013 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001014
1015 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001016
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001017 }
1018
1019 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001020 case BinaryOperator::Assign: {
1021 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +00001022 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001023 break;
1024 }
1025
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001026 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001027
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001028 assert (B->isCompoundAssignmentOp());
1029
1030 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +00001031 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001032
Ted Kremenekda9bd092008-02-08 07:05:39 +00001033 if (Op >= BinaryOperator::AndAssign)
1034 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1035 else
1036 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001037
1038 if (isa<LValue>(V2)) {
1039 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +00001040 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001041 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +00001042 }
1043 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001044 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001045 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001046 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +00001047 }
1048
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001049 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001050 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001051 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001052 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001053 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001054 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001055}
Ted Kremenekee985462008-01-16 18:18:48 +00001056
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001057
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001058void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
1059 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001060
1061 // FIXME: add metadata to the CFG so that we can disable
1062 // this check when we KNOW that there is no block-level subexpression.
1063 // The motivation is that this check requires a hashtable lookup.
1064
1065 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1066 Dst.Add(Pred);
1067 return;
1068 }
1069
1070 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001071
1072 default:
1073 // Cases we intentionally have "default" handle:
1074 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
1075
1076 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1077 break;
1078
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001079 case Stmt::BinaryOperatorClass: {
1080 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001081
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001082 if (B->isLogicalOp()) {
1083 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001084 break;
1085 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001086 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001087 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001088 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001089 break;
1090 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001091
1092 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1093 break;
1094 }
1095
1096 case Stmt::CastExprClass: {
1097 CastExpr* C = cast<CastExpr>(S);
1098 VisitCast(C, C->getSubExpr(), Pred, Dst);
1099 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001100 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001101
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001102 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1103 ChooseExpr* C = cast<ChooseExpr>(S);
1104 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1105 break;
1106 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001107
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001108 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001109 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1110 break;
1111
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001112 case Stmt::ConditionalOperatorClass: { // '?' operator
1113 ConditionalOperator* C = cast<ConditionalOperator>(S);
1114 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1115 break;
1116 }
1117
1118 case Stmt::DeclRefExprClass:
1119 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1120 break;
1121
1122 case Stmt::DeclStmtClass:
1123 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1124 break;
1125
1126 case Stmt::ImplicitCastExprClass: {
1127 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1128 VisitCast(C, C->getSubExpr(), Pred, Dst);
1129 break;
1130 }
1131
1132 case Stmt::ParenExprClass:
1133 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1134 break;
1135
1136 case Stmt::SizeOfAlignOfTypeExprClass:
1137 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1138 break;
1139
Ted Kremenekda9bd092008-02-08 07:05:39 +00001140 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001141 StmtExpr* SE = cast<StmtExpr>(S);
1142
Ted Kremenekda9bd092008-02-08 07:05:39 +00001143 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001144 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1145 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001146 break;
1147 }
1148
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001149 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001150 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1151 Visit(R, Pred, Dst);
1152 else
1153 Dst.Add(Pred);
1154
1155 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001156 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001157
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001158 case Stmt::UnaryOperatorClass:
1159 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001160 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001161 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001162}
1163
Ted Kremenekee985462008-01-16 18:18:48 +00001164//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001165// "Assume" logic.
1166//===----------------------------------------------------------------------===//
1167
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001168GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001169 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001170 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001171
1172 switch (Cond.getSubKind()) {
1173 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001174 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001175 return St;
1176
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001177 case lval::SymbolValKind:
1178 if (Assumption)
1179 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1180 ValMgr.getZeroWithPtrWidth(), isFeasible);
1181 else
1182 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1183 ValMgr.getZeroWithPtrWidth(), isFeasible);
1184
Ted Kremenek08b66252008-02-06 04:31:33 +00001185
Ted Kremenek329f8542008-02-05 21:52:21 +00001186 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001187 isFeasible = Assumption;
1188 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001189
Ted Kremenek329f8542008-02-05 21:52:21 +00001190 case lval::ConcreteIntKind: {
1191 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001192 isFeasible = b ? Assumption : !Assumption;
1193 return St;
1194 }
1195 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001196}
1197
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001198GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001199 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001200 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001201
1202 switch (Cond.getSubKind()) {
1203 default:
1204 assert (false && "'Assume' not implemented for this NonLValue.");
1205 return St;
1206
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001207
1208 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001209 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001210 SymbolID sym = SV.getSymbol();
1211
1212 if (Assumption)
1213 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1214 isFeasible);
1215 else
1216 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1217 isFeasible);
1218 }
1219
Ted Kremenek08b66252008-02-06 04:31:33 +00001220 case nonlval::SymIntConstraintValKind:
1221 return
1222 AssumeSymInt(St, Assumption,
1223 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1224 isFeasible);
1225
Ted Kremenek329f8542008-02-05 21:52:21 +00001226 case nonlval::ConcreteIntKind: {
1227 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001228 isFeasible = b ? Assumption : !Assumption;
1229 return St;
1230 }
1231 }
1232}
1233
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001234GRExprEngine::StateTy
1235GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001236 const llvm::APSInt& V, bool& isFeasible) {
1237
1238 // First, determine if sym == X, where X != V.
1239 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1240 isFeasible = *X != V;
1241 return St;
1242 }
1243
1244 // Second, determine if sym != V.
1245 if (St.isNotEqual(sym, V)) {
1246 isFeasible = true;
1247 return St;
1248 }
1249
1250 // If we reach here, sym is not a constant and we don't know if it is != V.
1251 // Make that assumption.
1252
1253 isFeasible = true;
1254 return StateMgr.AddNE(St, sym, V);
1255}
1256
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001257GRExprEngine::StateTy
1258GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001259 const llvm::APSInt& V, bool& isFeasible) {
1260
1261 // First, determine if sym == X, where X != V.
1262 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1263 isFeasible = *X == V;
1264 return St;
1265 }
1266
1267 // Second, determine if sym != V.
1268 if (St.isNotEqual(sym, V)) {
1269 isFeasible = false;
1270 return St;
1271 }
1272
1273 // If we reach here, sym is not a constant and we don't know if it is == V.
1274 // Make that assumption.
1275
1276 isFeasible = true;
1277 return StateMgr.AddEQ(St, sym, V);
1278}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001279
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001280GRExprEngine::StateTy
1281GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001282 const SymIntConstraint& C, bool& isFeasible) {
1283
1284 switch (C.getOpcode()) {
1285 default:
1286 // No logic yet for other operators.
1287 return St;
1288
1289 case BinaryOperator::EQ:
1290 if (Assumption)
1291 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1292 else
1293 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1294
1295 case BinaryOperator::NE:
1296 if (Assumption)
1297 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1298 else
1299 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1300 }
1301}
1302
Ted Kremenekb38911f2008-01-30 23:03:39 +00001303//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001304// Driver.
1305//===----------------------------------------------------------------------===//
1306
Ted Kremenekaa66a322008-01-16 21:46:15 +00001307#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001308static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001309
Ted Kremenekaa66a322008-01-16 21:46:15 +00001310namespace llvm {
1311template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001312struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001313 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001314
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001315 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001316
1317 Out << "Variables:\\l";
1318
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001319 bool isFirst = true;
1320
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001321 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001322 E=St.vb_end(); I!=E;++I) {
1323
1324 if (isFirst)
1325 isFirst = false;
1326 else
1327 Out << "\\l";
1328
1329 Out << ' ' << I.getKey()->getName() << " : ";
1330 I.getData().print(Out);
1331 }
1332
1333 }
1334
Ted Kremeneke7d22112008-02-11 19:21:59 +00001335
Ted Kremenek44842c22008-02-13 18:06:44 +00001336 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001337
1338 bool isFirst = true;
1339
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001340 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001341 I != E;++I) {
1342
1343 if (isFirst) {
1344 Out << "\\l\\lSub-Expressions:\\l";
1345 isFirst = false;
1346 }
1347 else
1348 Out << "\\l";
1349
1350 Out << " (" << (void*) I.getKey() << ") ";
1351 I.getKey()->printPretty(Out);
1352 Out << " : ";
1353 I.getData().print(Out);
1354 }
1355 }
1356
Ted Kremenek44842c22008-02-13 18:06:44 +00001357 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001358
Ted Kremenek016f52f2008-02-08 21:10:02 +00001359 bool isFirst = true;
1360
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001361 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001362 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001363 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001364 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001365 isFirst = false;
1366 }
1367 else
1368 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001369
Ted Kremeneke7d22112008-02-11 19:21:59 +00001370 Out << " (" << (void*) I.getKey() << ") ";
1371 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001372 Out << " : ";
1373 I.getData().print(Out);
1374 }
1375 }
1376
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001377 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001378 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1379
1380 if (CE.isEmpty())
1381 return;
1382
1383 Out << "\\l\\|'==' constraints:";
1384
1385 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1386 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1387 }
1388
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001389 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001390 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1391
1392 if (NE.isEmpty())
1393 return;
1394
1395 Out << "\\l\\|'!=' constraints:";
1396
1397 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1398 I != EI; ++I){
1399
1400 Out << "\\l $" << I.getKey() << " : ";
1401 bool isFirst = true;
1402
1403 ValueState::IntSetTy::iterator J=I.getData().begin(),
1404 EJ=I.getData().end();
1405 for ( ; J != EJ; ++J) {
1406 if (isFirst) isFirst = false;
1407 else Out << ", ";
1408
1409 Out << (*J)->toString();
1410 }
1411 }
1412 }
1413
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001414 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001415 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001416
1417 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001418 ProgramPoint Loc = N->getLocation();
1419
1420 switch (Loc.getKind()) {
1421 case ProgramPoint::BlockEntranceKind:
1422 Out << "Block Entrance: B"
1423 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1424 break;
1425
1426 case ProgramPoint::BlockExitKind:
1427 assert (false);
1428 break;
1429
1430 case ProgramPoint::PostStmtKind: {
1431 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001432 Out << L.getStmt()->getStmtClassName() << ':'
1433 << (void*) L.getStmt() << ' ';
1434
Ted Kremenekaa66a322008-01-16 21:46:15 +00001435 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001436
1437 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1438 Out << "\\|Implicit-Null Dereference.\\l";
1439 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001440 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1441 Out << "\\|Explicit-Null Dereference.\\l";
1442 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001443
Ted Kremenekaa66a322008-01-16 21:46:15 +00001444 break;
1445 }
1446
1447 default: {
1448 const BlockEdge& E = cast<BlockEdge>(Loc);
1449 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1450 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001451
1452 if (Stmt* T = E.getSrc()->getTerminator()) {
1453 Out << "\\|Terminator: ";
1454 E.getSrc()->printTerminator(Out);
1455
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001456 if (isa<SwitchStmt>(T)) {
1457 Stmt* Label = E.getDst()->getLabel();
1458
1459 if (Label) {
1460 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1461 Out << "\\lcase ";
1462 C->getLHS()->printPretty(Out);
1463
1464 if (Stmt* RHS = C->getRHS()) {
1465 Out << " .. ";
1466 RHS->printPretty(Out);
1467 }
1468
1469 Out << ":";
1470 }
1471 else {
1472 assert (isa<DefaultStmt>(Label));
1473 Out << "\\ldefault:";
1474 }
1475 }
1476 else
1477 Out << "\\l(implicit) default:";
1478 }
1479 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001480 // FIXME
1481 }
1482 else {
1483 Out << "\\lCondition: ";
1484 if (*E.getSrc()->succ_begin() == E.getDst())
1485 Out << "true";
1486 else
1487 Out << "false";
1488 }
1489
1490 Out << "\\l";
1491 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001492
1493 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1494 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1495 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001496 }
1497 }
1498
Ted Kremenek9153f732008-02-05 07:17:49 +00001499 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001500
Ted Kremeneke7d22112008-02-11 19:21:59 +00001501 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001502
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001503 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001504 return Out.str();
1505 }
1506};
1507} // end llvm namespace
1508#endif
1509
Ted Kremenekee985462008-01-16 18:18:48 +00001510namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001511void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001512 Diagnostic& Diag) {
1513
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001514 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001515 Engine.ExecuteWorkList();
1516
1517 // Look for explicit-Null dereferences and warn about them.
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001518 GRExprEngine* CheckerState = &Engine.getCheckerState();
Ted Kremenek19227e32008-02-07 06:33:19 +00001519
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001520 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001521 E=CheckerState->null_end(); I!=E; ++I) {
1522
1523 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1524 Expr* E = cast<Expr>(L.getStmt());
1525
1526 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1527 diag::chkr_null_deref_after_check);
1528 }
1529
1530
Ted Kremenekaa66a322008-01-16 21:46:15 +00001531#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001532 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001533 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001534 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001535#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001536}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001537} // end clang namespace