blob: 16abcc425b00eb9a03bb71e852953d3fe69c786d [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 Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000022#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek19227e32008-02-07 06:33:19 +000024#include "clang/Basic/Diagnostic.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000025
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/DataTypes.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000031#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000032#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000034#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000035#include "llvm/Support/Streams.h"
36
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000037#include <functional>
38
Ted Kremenekaa66a322008-01-16 21:46:15 +000039#ifndef NDEBUG
40#include "llvm/Support/GraphWriter.h"
41#include <sstream>
42#endif
43
Ted Kremenekd27f8162008-01-15 23:55:06 +000044using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000045using llvm::dyn_cast;
46using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000047using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000048
49//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000050// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000051//
52// FIXME: This checker logic should be eventually broken into two components.
53// The first is the "meta"-level checking logic; the code that
54// does the Stmt visitation, fetching values from the map, etc.
55// The second part does the actual state manipulation. This way we
56// get more of a separate of concerns of these two pieces, with the
57// latter potentially being refactored back into the main checking
58// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000059//===----------------------------------------------------------------------===//
60
61namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000062
Ted Kremenek4d4dd852008-02-13 17:41:41 +000063class VISIBILITY_HIDDEN GRExprEngine {
Ted Kremenekd27f8162008-01-15 23:55:06 +000064
65public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000066 typedef ValueStateManager::StateTy StateTy;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000067 typedef ExplodedGraph<GRExprEngine> GraphTy;
68 typedef GraphTy::NodeTy NodeTy;
69
70 // Builders.
Ted Kremenek4d4dd852008-02-13 17:41:41 +000071 typedef GRStmtNodeBuilder<GRExprEngine> StmtNodeBuilder;
72 typedef GRBranchNodeBuilder<GRExprEngine> BranchNodeBuilder;
73 typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +000074 typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder;
75
Ted Kremenekab2b8c52008-01-23 19:59:44 +000076
77 class NodeSet {
78 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
79 ImplTy Impl;
80 public:
81
82 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000083 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000084
Ted Kremenekb38911f2008-01-30 23:03:39 +000085 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000086
87 typedef ImplTy::iterator iterator;
88 typedef ImplTy::const_iterator const_iterator;
89
90 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000091 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000092
93 iterator begin() { return Impl.begin(); }
94 iterator end() { return Impl.end(); }
95
96 const_iterator begin() const { return Impl.begin(); }
97 const_iterator end() const { return Impl.end(); }
98 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000099
Ted Kremenekd27f8162008-01-15 23:55:06 +0000100protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000101 /// G - the simulation graph.
102 GraphTy& G;
103
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000104 /// Liveness - live-variables information the ValueDecl* and block-level
105 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000106 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000107
Ted Kremenek44842c22008-02-13 18:06:44 +0000108 /// Builder - The current GRStmtNodeBuilder which is used when building the
109 /// nodes for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000110 StmtNodeBuilder* Builder;
111
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000112 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000113 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000114
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000115 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000116 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000117
Ted Kremenek68fd2572008-01-29 17:27:31 +0000118 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000119 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000120
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000121 /// StmtEntryNode - The immediate predecessor node.
122 NodeTy* StmtEntryNode;
123
124 /// CurrentStmt - The current block-level statement.
125 Stmt* CurrentStmt;
126
Ted Kremenekb38911f2008-01-30 23:03:39 +0000127 /// UninitBranches - Nodes in the ExplodedGraph that result from
128 /// taking a branch based on an uninitialized value.
129 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
130 UninitBranchesTy UninitBranches;
131
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000132 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
133 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000134 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
135 NullDerefTy ImplicitNullDeref;
136 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000137
138
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000139 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000140
Ted Kremenekd27f8162008-01-15 23:55:06 +0000141public:
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000142 GRExprEngine(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000143 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000144 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000145 ValMgr(StateMgr.getValueManager()),
146 SymMgr(StateMgr.getSymbolManager()),
147 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000148
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000149 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000150 Liveness.runOnCFG(G.getCFG());
151 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000152 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000153
Ted Kremenek19227e32008-02-07 06:33:19 +0000154 /// getContext - Return the ASTContext associated with this analysis.
155 ASTContext& getContext() const { return G.getContext(); }
156
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000157 /// getCFG - Returns the CFG associated with this analysis.
158 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000159
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000160 /// getInitialState - Return the initial state used for the root vertex
161 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000162 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000163 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000164
165 // Iterate the parameters.
166 FunctionDecl& F = G.getFunctionDecl();
167
168 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000169 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000170 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000171
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000172 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000173 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000174
175 bool isUninitControlFlow(const NodeTy* N) const {
176 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
177 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000178
179 bool isImplicitNullDeref(const NodeTy* N) const {
180 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
181 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000182
183 bool isExplicitNullDeref(const NodeTy* N) const {
184 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
185 }
186
Ted Kremenek19227e32008-02-07 06:33:19 +0000187 typedef NullDerefTy::iterator null_iterator;
188 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
189 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000190
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000191 /// ProcessStmt - Called by GRCoreEngine. Used to generate new successor
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000192 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000193 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
194
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000195 /// ProcessBranch - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000196 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000197 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000198
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000199 /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000200 /// nodes by processing the 'effects' of a computed goto jump.
201 void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder);
202
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000203 /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
204 /// nodes by processing the 'effects' of a switch statement.
205 void ProcessSwitch(SwitchNodeBuilder& builder);
206
Ted Kremenekb87d9092008-02-08 19:17:19 +0000207 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000208 /// that all subexpression mappings are removed and that any
209 /// block-level expressions that are not live at 'S' also have their
210 /// mappings removed.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000211 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
212 return StateMgr.RemoveDeadBindings(St, S, Liveness);
213 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000214
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000215 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000216
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000217 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
218 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000219 }
220
Ted Kremenekcba2e432008-02-05 19:35:18 +0000221 /// SetValue - This version of SetValue is used to batch process a set
222 /// of different possible RValues and return a set of different states.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000223 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000224 const RValue::BufferTy& V,
225 StateTy::BufferTy& RetBuf);
226
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000227 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000228
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000229 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000230 return StateMgr.GetValue(St, S);
231 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000232
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000233 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000234 return StateMgr.GetValue(St, S, &hasVal);
235 }
236
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000237 inline RValue GetValue(const StateTy& St, const Expr* S) {
238 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000239 }
240
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000241 inline RValue GetValue(const StateTy& St, const LValue& LV,
242 QualType* T = NULL) {
243
244 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000245 }
246
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000247 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000248 return StateMgr.GetLValue(St, S);
249 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000250
251 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
252 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
253 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000254
255 /// Assume - Create new state by assuming that a given expression
256 /// is true or false.
257 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
258 bool& isFeasible) {
259 if (isa<LValue>(Cond))
260 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
261 else
262 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
263 }
264
265 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
266 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000267
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000268 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
269 bool& isFeasible);
270
271 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
272 bool& isFeasible);
273
Ted Kremenek08b66252008-02-06 04:31:33 +0000274 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
275 bool& isFeasible);
276
Ted Kremenek7e593362008-02-07 15:20:13 +0000277 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000278
Ted Kremenekcba2e432008-02-05 19:35:18 +0000279 /// Nodify - This version of Nodify is used to batch process a set of states.
280 /// The states are not guaranteed to be unique.
281 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
282
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000283 /// Visit - Transfer function logic for all statements. Dispatches to
284 /// other functions that handle specific kinds of statements.
285 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000286
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000287 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000288 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
289
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000290 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000291
Ted Kremenek230aaab2008-02-12 21:37:25 +0000292 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
293 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
294
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000295 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
296 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
297
Ted Kremenek9de04c42008-01-24 20:55:43 +0000298 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000299 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
300
301 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000302 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000303 NodeTy* Pred, NodeSet& Dst);
304
305 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
306 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000307
308 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
309 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
310 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000311
312 /// VisitUnaryOperator - Transfer function logic for unary operators.
313 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
314
Ted Kremenekd27f8162008-01-15 23:55:06 +0000315};
316} // end anonymous namespace
317
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000318
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000319GRExprEngine::StateTy
320GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000321
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000322 if (!StateCleaned) {
323 St = RemoveDeadBindings(CurrentStmt, St);
324 StateCleaned = true;
325 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000326
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000327 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000328
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000329 if (S == CurrentStmt) {
330 isBlkExpr = getCFG().isBlkExpr(S);
331
332 if (!isBlkExpr)
333 return St;
334 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000335
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000336 return StateMgr.SetValue(St, S, isBlkExpr, V);
337}
338
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000339const GRExprEngine::StateTy::BufferTy&
340GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000341 StateTy::BufferTy& RetBuf) {
342
343 assert (RetBuf.empty());
344
345 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
346 RetBuf.push_back(SetValue(St, S, *I));
347
348 return RetBuf;
349}
350
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000351GRExprEngine::StateTy
352GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000353
Ted Kremenek53c641a2008-02-08 03:02:48 +0000354 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000355 return St;
356
357 if (!StateCleaned) {
358 St = RemoveDeadBindings(CurrentStmt, St);
359 StateCleaned = true;
360 }
361
362 return StateMgr.SetValue(St, LV, V);
363}
364
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000365void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000366 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000367
Ted Kremeneke7d22112008-02-11 19:21:59 +0000368 // Remove old bindings for subexpressions.
369 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000370
Ted Kremenekb38911f2008-01-30 23:03:39 +0000371 RValue V = GetValue(PrevState, Condition);
372
373 switch (V.getBaseKind()) {
374 default:
375 break;
376
Ted Kremenek53c641a2008-02-08 03:02:48 +0000377 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000378 builder.generateNode(PrevState, true);
379 builder.generateNode(PrevState, false);
380 return;
381
382 case RValue::UninitializedKind: {
383 NodeTy* N = builder.generateNode(PrevState, true);
384
385 if (N) {
386 N->markAsSink();
387 UninitBranches.insert(N);
388 }
389
390 builder.markInfeasible(false);
391 return;
392 }
393 }
394
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000395 // Get the current block counter.
396 GRBlockCounter BC = builder.getBlockCounter();
397
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000398 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
399 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000400
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000401 if (isa<nonlval::ConcreteInt>(V) ||
402 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
403
404 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000405
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000406 bool isFeasible = true;
407
408 StateTy St = Assume(PrevState, V, true, isFeasible);
409
410 if (isFeasible)
411 builder.generateNode(St, true);
412 else
413 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000414 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000415 else
416 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000417
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000418 BlockID = builder.getTargetBlock(false)->getBlockID();
419 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000420
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000421 if (isa<nonlval::ConcreteInt>(V) ||
422 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
423
424 // Process the false branch.
425
426 bool isFeasible = false;
427
428 StateTy St = Assume(PrevState, V, false, isFeasible);
429
430 if (isFeasible)
431 builder.generateNode(St, false);
432 else
433 builder.markInfeasible(false);
434 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000435 else
436 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000437}
438
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000439/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000440/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000441void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000442
443 StateTy St = builder.getState();
444 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
445
446 // Three possibilities:
447 //
448 // (1) We know the computed label.
449 // (2) The label is NULL (or some other constant), or Uninitialized.
450 // (3) We have no clue about the label. Dispatch to all targets.
451 //
452
453 typedef IndirectGotoNodeBuilder::iterator iterator;
454
455 if (isa<lval::GotoLabel>(V)) {
456 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
457
458 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000459 if (I.getLabel() == L) {
460 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000461 return;
462 }
463 }
464
465 assert (false && "No block with label.");
466 return;
467 }
468
469 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
470 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000471 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000472 UninitBranches.insert(N);
473 return;
474 }
475
476 // This is really a catch-all. We don't support symbolics yet.
477
478 assert (isa<UnknownVal>(V));
479
480 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000481 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000482}
Ted Kremenekf233d482008-02-05 00:26:40 +0000483
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000484/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
485/// nodes by processing the 'effects' of a switch statement.
486void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
487
488 typedef SwitchNodeBuilder::iterator iterator;
489
490 StateTy St = builder.getState();
491 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
492
493 if (isa<UninitializedVal>(CondV)) {
494 NodeTy* N = builder.generateDefaultCaseNode(St, true);
495 UninitBranches.insert(N);
496 return;
497 }
498
499 StateTy DefaultSt = St;
500
501 // While most of this can be assumed (such as the signedness), having it
502 // just computed makes sure everything makes the same assumptions end-to-end.
503 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
504 APSInt V1(bits, false);
505 APSInt V2 = V1;
506
507 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
508
509 CaseStmt* Case = cast<CaseStmt>(I.getCase());
510
511 // Evaluate the case.
512 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
513 assert (false && "Case condition must evaluate to an integer constant.");
514 return;
515 }
516
517 // Get the RHS of the case, if it exists.
518
519 if (Expr* E = Case->getRHS()) {
520 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
521 assert (false &&
522 "Case condition (RHS) must evaluate to an integer constant.");
523 return ;
524 }
525
526 assert (V1 <= V2);
527 }
528 else V2 = V1;
529
530 // FIXME: Eventually we should replace the logic below with a range
531 // comparison, rather than concretize the values within the range.
532 // This should be easy once we have "ranges" for NonLValues.
533
534 do {
535 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
536
537 NonLValue Result =
538 CondV.EvalBinaryOp(ValMgr, BinaryOperator::EQ, CaseVal);
539
540 // Now "assume" that the case matches.
541 bool isFeasible;
542
543 StateTy StNew = Assume(St, Result, true, isFeasible);
544
545 if (isFeasible) {
546 builder.generateCaseStmtNode(I, StNew);
547
548 // If CondV evaluates to a constant, then we know that this
549 // is the *only* case that we can take, so stop evaluating the
550 // others.
551 if (isa<nonlval::ConcreteInt>(CondV))
552 return;
553 }
554
555 // Now "assume" that the case doesn't match. Add this state
556 // to the default state (if it is feasible).
557
558 StNew = Assume(DefaultSt, Result, false, isFeasible);
559
560 if (isFeasible)
561 DefaultSt = StNew;
562
563 // Concretize the next value in the range.
564 ++V1;
565
566 } while (V1 < V2);
567 }
568
569 // If we reach here, than we know that the default branch is
570 // possible.
571 builder.generateDefaultCaseNode(DefaultSt);
572}
573
574
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000575void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000576 NodeSet& Dst) {
577
578 bool hasR2;
579 StateTy PrevState = Pred->getState();
580
581 RValue R1 = GetValue(PrevState, B->getLHS());
582 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
583
Ted Kremenek22031182008-02-08 02:57:34 +0000584 if (isa<UnknownVal>(R1) &&
585 (isa<UnknownVal>(R2) ||
586 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000587
588 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
589 return;
590 }
Ted Kremenek22031182008-02-08 02:57:34 +0000591 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000592 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
593 return;
594 }
595
596 // R1 is an expression that can evaluate to either 'true' or 'false'.
597 if (B->getOpcode() == BinaryOperator::LAnd) {
598 // hasR2 == 'false' means that LHS evaluated to 'false' and that
599 // we short-circuited, leading to a value of '0' for the '&&' expression.
600 if (hasR2 == false) {
601 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
602 return;
603 }
604 }
605 else {
606 assert (B->getOpcode() == BinaryOperator::LOr);
607 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
608 // we short-circuited, leading to a value of '1' for the '||' expression.
609 if (hasR2 == false) {
610 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
611 return;
612 }
613 }
614
615 // If we reach here we did not short-circuit. Assume R2 == true and
616 // R2 == false.
617
618 bool isFeasible;
619 StateTy St = Assume(PrevState, R2, true, isFeasible);
620
621 if (isFeasible)
622 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
623
624 St = Assume(PrevState, R2, false, isFeasible);
625
626 if (isFeasible)
627 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
628}
629
630
631
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000632void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000633 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000634
635 StmtEntryNode = builder.getLastNode();
636 CurrentStmt = S;
637 NodeSet Dst;
638 StateCleaned = false;
639
640 Visit(S, StmtEntryNode, Dst);
641
642 // If no nodes were generated, generate a new node that has all the
643 // dead mappings removed.
644 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
645 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
646 builder.generateNode(S, St, StmtEntryNode);
647 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000648
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000649 CurrentStmt = NULL;
650 StmtEntryNode = NULL;
651 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000652}
653
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000654GRExprEngine::NodeTy*
655GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000656
657 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000658 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000659 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000660
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000661 NodeTy* N = Builder->generateNode(S, St, Pred);
662 Dst.Add(N);
663 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000664}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000665
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000666void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000667 const StateTy::BufferTy& SB) {
668
669 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
670 Nodify(Dst, S, Pred, *I);
671}
672
Ted Kremenek44842c22008-02-13 18:06:44 +0000673void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000674 if (D != CurrentStmt) {
675 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
676 return;
677 }
678
679 // If we are here, we are loading the value of the decl and binding
680 // it to the block-level expression.
681
682 StateTy St = Pred->getState();
683
684 Nodify(Dst, D, Pred,
685 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
686}
687
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000688void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000689
690 QualType T = CastE->getType();
691
692 // Check for redundant casts.
693 if (E->getType() == T) {
694 Dst.Add(Pred);
695 return;
696 }
697
698 NodeSet S1;
699 Visit(E, Pred, S1);
700
701 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
702 NodeTy* N = *I1;
703 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000704 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000705 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000706 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000707}
708
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000709void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
710 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000711
712 StateTy St = Pred->getState();
713
714 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000715 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
716 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000717 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000718 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000719 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000720
721 Nodify(Dst, DS, Pred, St);
722
723 if (Dst.empty())
724 Dst.Add(Pred);
725}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000726
Ted Kremenekf233d482008-02-05 00:26:40 +0000727
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000728void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000729 NodeTy* Pred, NodeSet& Dst) {
730
731 StateTy St = Pred->getState();
732
733 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000734 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000735
736 Nodify(Dst, S, Pred, SetValue(St, S, R));
737}
738
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000739/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000740void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000741 NodeTy* Pred,
742 NodeSet& Dst) {
743
744 // 6.5.3.4 sizeof: "The result type is an integer."
745
746 QualType T = S->getArgumentType();
747
748 // FIXME: Add support for VLAs.
749 if (isa<VariableArrayType>(T.getTypePtr()))
750 return;
751
752 SourceLocation L = S->getExprLoc();
753 uint64_t size = getContext().getTypeSize(T, L) / 8;
754
755 Nodify(Dst, S, Pred,
756 SetValue(Pred->getState(), S,
757 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
758
759}
760
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000761void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
762 GRExprEngine::NodeTy* Pred,
763 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000764
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000765 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000766 UnaryOperator::Opcode Op = U->getOpcode();
767
768 // FIXME: This is a hack so that for '*' and '&' we don't recurse
769 // on visiting the subexpression if it is a DeclRefExpr. We should
770 // probably just handle AddrOf and Deref in their own methods to make
771 // this cleaner.
772 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
773 isa<DeclRefExpr>(U->getSubExpr()))
774 S1.Add(Pred);
775 else
776 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000777
778 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
779 NodeTy* N1 = *I1;
780 StateTy St = N1->getState();
781
782 switch (U->getOpcode()) {
783 case UnaryOperator::PostInc: {
784 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000785 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000786
787 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
788 GetRValueConstant(1U, U));
789
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000790 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
791 break;
792 }
793
794 case UnaryOperator::PostDec: {
795 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000796 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000797
798 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
799 GetRValueConstant(1U, U));
800
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000801 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
802 break;
803 }
804
805 case UnaryOperator::PreInc: {
806 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000807 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000808
809 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
810 GetRValueConstant(1U, U));
811
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000812 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
813 break;
814 }
815
816 case UnaryOperator::PreDec: {
817 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000818 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000819
820 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
821 GetRValueConstant(1U, U));
822
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000823 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
824 break;
825 }
826
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000827 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000828 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000829 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000830 break;
831 }
832
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000833 case UnaryOperator::Not: {
834 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000835 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000836 break;
837 }
838
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000839 case UnaryOperator::LNot: {
840 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
841 //
842 // Note: technically we do "E == 0", but this is the same in the
843 // transfer functions as "0 == E".
844
845 RValue V1 = GetValue(St, U->getSubExpr());
846
847 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000848 const LValue& L1 = cast<LValue>(V1);
849 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
850 Nodify(Dst, U, N1,
851 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
852 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000853 }
854 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000855 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000856 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000857 Nodify(Dst, U, N1,
858 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
859 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000860 }
861
862 break;
863 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000864
865 case UnaryOperator::SizeOf: {
866 // 6.5.3.4 sizeof: "The result type is an integer."
867
868 QualType T = U->getSubExpr()->getType();
869
870 // FIXME: Add support for VLAs.
871 if (isa<VariableArrayType>(T.getTypePtr()))
872 return;
873
874 SourceLocation L = U->getExprLoc();
875 uint64_t size = getContext().getTypeSize(T, L) / 8;
876
877 Nodify(Dst, U, N1,
878 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
879 getContext().IntTy, L)));
880
881 break;
882 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000883
Ted Kremenek64924852008-01-31 02:35:41 +0000884 case UnaryOperator::AddrOf: {
885 const LValue& L1 = GetLValue(St, U->getSubExpr());
886 Nodify(Dst, U, N1, SetValue(St, U, L1));
887 break;
888 }
889
890 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000891 // FIXME: Stop when dereferencing an uninitialized value.
892 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
893
894 const RValue& V = GetValue(St, U->getSubExpr());
895 const LValue& L1 = cast<LValue>(V);
896
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000897 // After a dereference, one of two possible situations arise:
898 // (1) A crash, because the pointer was NULL.
899 // (2) The pointer is not NULL, and the dereference works.
900 //
901 // We add these assumptions.
902
Ted Kremenek63a4f692008-02-07 06:04:18 +0000903 bool isFeasibleNotNull;
904
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000905 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000906 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
907
908 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000909 QualType T = U->getType();
910 Nodify(Dst, U, N1, SetValue(StNotNull, U,
911 GetValue(StNotNull, L1, &T)));
912 }
913
Ted Kremenek63a4f692008-02-07 06:04:18 +0000914 bool isFeasibleNull;
915
916 // "Assume" that the pointer is NULL.
917 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
918
919 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000920 // We don't use "Nodify" here because the node will be a sink
921 // and we have no intention of processing it later.
922 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
923
Ted Kremenek63a4f692008-02-07 06:04:18 +0000924 if (NullNode) {
925 NullNode->markAsSink();
926
927 if (isFeasibleNotNull)
928 ImplicitNullDeref.insert(NullNode);
929 else
930 ExplicitNullDeref.insert(NullNode);
931 }
932 }
933
Ted Kremenek64924852008-01-31 02:35:41 +0000934 break;
935 }
936
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000937 default: ;
938 assert (false && "Not implemented.");
939 }
940 }
941}
942
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000943void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
944 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000945
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000946 if (isa<DeclRefExpr>(E)) {
947 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000948 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000949 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000950
951 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
952 if (U->getOpcode() == UnaryOperator::Deref) {
953 Visit(U->getSubExpr(), Pred, Dst);
954 return;
955 }
956 }
957
958 Visit(E, Pred, Dst);
959}
960
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000961void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000962 GRExprEngine::NodeTy* Pred,
963 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000964 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000965
966 if (B->isAssignmentOp())
967 VisitAssignmentLHS(B->getLHS(), Pred, S1);
968 else
969 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000970
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000971 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
972 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000973
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000974 // When getting the value for the LHS, check if we are in an assignment.
975 // In such cases, we want to (initially) treat the LHS as an LValue,
976 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000977 // evaluated to LValueDecl's instead of to an NonLValue.
978 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000979 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
980 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000981
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000982 NodeSet S2;
983 Visit(B->getRHS(), N1, S2);
984
985 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000986
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000987 NodeTy* N2 = *I2;
988 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000989 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000990
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000991 BinaryOperator::Opcode Op = B->getOpcode();
992
993 if (Op <= BinaryOperator::Or) {
994
Ted Kremenek22031182008-02-08 02:57:34 +0000995 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000996 Nodify(Dst, B, N2, SetValue(St, B, V1));
997 continue;
998 }
999
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001000 if (isa<LValue>(V1)) {
1001 // FIXME: Add support for RHS being a non-lvalue.
1002 const LValue& L1 = cast<LValue>(V1);
1003 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +00001004
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001005 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
1006 }
1007 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001008 const NonLValue& R1 = cast<NonLValue>(V1);
1009 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001010
1011 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001012 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001013
1014 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001015
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001016 }
1017
1018 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001019 case BinaryOperator::Assign: {
1020 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +00001021 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001022 break;
1023 }
1024
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001025 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001026
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001027 assert (B->isCompoundAssignmentOp());
1028
1029 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +00001030 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001031
Ted Kremenekda9bd092008-02-08 07:05:39 +00001032 if (Op >= BinaryOperator::AndAssign)
1033 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1034 else
1035 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001036
1037 if (isa<LValue>(V2)) {
1038 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +00001039 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001040 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +00001041 }
1042 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001043 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001044 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001045 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +00001046 }
1047
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001048 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001049 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001050 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001051 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001052 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001053 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001054}
Ted Kremenekee985462008-01-16 18:18:48 +00001055
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001056
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001057void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
1058 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001059
1060 // FIXME: add metadata to the CFG so that we can disable
1061 // this check when we KNOW that there is no block-level subexpression.
1062 // The motivation is that this check requires a hashtable lookup.
1063
1064 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1065 Dst.Add(Pred);
1066 return;
1067 }
1068
1069 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001070
1071 default:
1072 // Cases we intentionally have "default" handle:
1073 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
1074
1075 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1076 break;
1077
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001078 case Stmt::BinaryOperatorClass: {
1079 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001080
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001081 if (B->isLogicalOp()) {
1082 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001083 break;
1084 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001085 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001086 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001087 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001088 break;
1089 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001090
1091 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1092 break;
1093 }
1094
1095 case Stmt::CastExprClass: {
1096 CastExpr* C = cast<CastExpr>(S);
1097 VisitCast(C, C->getSubExpr(), Pred, Dst);
1098 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001099 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001100
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001101 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1102 ChooseExpr* C = cast<ChooseExpr>(S);
1103 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1104 break;
1105 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001106
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001107 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001108 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1109 break;
1110
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001111 case Stmt::ConditionalOperatorClass: { // '?' operator
1112 ConditionalOperator* C = cast<ConditionalOperator>(S);
1113 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1114 break;
1115 }
1116
1117 case Stmt::DeclRefExprClass:
1118 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1119 break;
1120
1121 case Stmt::DeclStmtClass:
1122 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1123 break;
1124
1125 case Stmt::ImplicitCastExprClass: {
1126 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1127 VisitCast(C, C->getSubExpr(), Pred, Dst);
1128 break;
1129 }
1130
1131 case Stmt::ParenExprClass:
1132 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1133 break;
1134
1135 case Stmt::SizeOfAlignOfTypeExprClass:
1136 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1137 break;
1138
Ted Kremenekda9bd092008-02-08 07:05:39 +00001139 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001140 StmtExpr* SE = cast<StmtExpr>(S);
1141
Ted Kremenekda9bd092008-02-08 07:05:39 +00001142 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001143 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1144 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001145 break;
1146 }
1147
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001148 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001149 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1150 Visit(R, Pred, Dst);
1151 else
1152 Dst.Add(Pred);
1153
1154 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001155 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001156
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001157 case Stmt::UnaryOperatorClass:
1158 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001159 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001160 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001161}
1162
Ted Kremenekee985462008-01-16 18:18:48 +00001163//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001164// "Assume" logic.
1165//===----------------------------------------------------------------------===//
1166
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001167GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001168 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001169 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001170
1171 switch (Cond.getSubKind()) {
1172 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001173 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001174 return St;
1175
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001176 case lval::SymbolValKind:
1177 if (Assumption)
1178 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1179 ValMgr.getZeroWithPtrWidth(), isFeasible);
1180 else
1181 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1182 ValMgr.getZeroWithPtrWidth(), isFeasible);
1183
Ted Kremenek08b66252008-02-06 04:31:33 +00001184
Ted Kremenek329f8542008-02-05 21:52:21 +00001185 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001186 isFeasible = Assumption;
1187 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001188
Ted Kremenek329f8542008-02-05 21:52:21 +00001189 case lval::ConcreteIntKind: {
1190 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001191 isFeasible = b ? Assumption : !Assumption;
1192 return St;
1193 }
1194 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001195}
1196
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001197GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001198 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001199 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001200
1201 switch (Cond.getSubKind()) {
1202 default:
1203 assert (false && "'Assume' not implemented for this NonLValue.");
1204 return St;
1205
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001206
1207 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001208 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001209 SymbolID sym = SV.getSymbol();
1210
1211 if (Assumption)
1212 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1213 isFeasible);
1214 else
1215 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1216 isFeasible);
1217 }
1218
Ted Kremenek08b66252008-02-06 04:31:33 +00001219 case nonlval::SymIntConstraintValKind:
1220 return
1221 AssumeSymInt(St, Assumption,
1222 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1223 isFeasible);
1224
Ted Kremenek329f8542008-02-05 21:52:21 +00001225 case nonlval::ConcreteIntKind: {
1226 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001227 isFeasible = b ? Assumption : !Assumption;
1228 return St;
1229 }
1230 }
1231}
1232
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001233GRExprEngine::StateTy
1234GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001235 const llvm::APSInt& V, bool& isFeasible) {
1236
1237 // First, determine if sym == X, where X != V.
1238 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1239 isFeasible = *X != V;
1240 return St;
1241 }
1242
1243 // Second, determine if sym != V.
1244 if (St.isNotEqual(sym, V)) {
1245 isFeasible = true;
1246 return St;
1247 }
1248
1249 // If we reach here, sym is not a constant and we don't know if it is != V.
1250 // Make that assumption.
1251
1252 isFeasible = true;
1253 return StateMgr.AddNE(St, sym, V);
1254}
1255
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001256GRExprEngine::StateTy
1257GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001258 const llvm::APSInt& V, bool& isFeasible) {
1259
1260 // First, determine if sym == X, where X != V.
1261 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1262 isFeasible = *X == V;
1263 return St;
1264 }
1265
1266 // Second, determine if sym != V.
1267 if (St.isNotEqual(sym, V)) {
1268 isFeasible = false;
1269 return St;
1270 }
1271
1272 // If we reach here, sym is not a constant and we don't know if it is == V.
1273 // Make that assumption.
1274
1275 isFeasible = true;
1276 return StateMgr.AddEQ(St, sym, V);
1277}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001278
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001279GRExprEngine::StateTy
1280GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001281 const SymIntConstraint& C, bool& isFeasible) {
1282
1283 switch (C.getOpcode()) {
1284 default:
1285 // No logic yet for other operators.
1286 return St;
1287
1288 case BinaryOperator::EQ:
1289 if (Assumption)
1290 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1291 else
1292 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1293
1294 case BinaryOperator::NE:
1295 if (Assumption)
1296 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1297 else
1298 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1299 }
1300}
1301
Ted Kremenekb38911f2008-01-30 23:03:39 +00001302//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001303// Driver.
1304//===----------------------------------------------------------------------===//
1305
Ted Kremenekaa66a322008-01-16 21:46:15 +00001306#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001307static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001308
Ted Kremenekaa66a322008-01-16 21:46:15 +00001309namespace llvm {
1310template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001311struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001312 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001313
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001314 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001315
1316 Out << "Variables:\\l";
1317
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001318 bool isFirst = true;
1319
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001320 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001321 E=St.vb_end(); I!=E;++I) {
1322
1323 if (isFirst)
1324 isFirst = false;
1325 else
1326 Out << "\\l";
1327
1328 Out << ' ' << I.getKey()->getName() << " : ";
1329 I.getData().print(Out);
1330 }
1331
1332 }
1333
Ted Kremeneke7d22112008-02-11 19:21:59 +00001334
Ted Kremenek44842c22008-02-13 18:06:44 +00001335 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001336
1337 bool isFirst = true;
1338
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001339 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001340 I != E;++I) {
1341
1342 if (isFirst) {
1343 Out << "\\l\\lSub-Expressions:\\l";
1344 isFirst = false;
1345 }
1346 else
1347 Out << "\\l";
1348
1349 Out << " (" << (void*) I.getKey() << ") ";
1350 I.getKey()->printPretty(Out);
1351 Out << " : ";
1352 I.getData().print(Out);
1353 }
1354 }
1355
Ted Kremenek44842c22008-02-13 18:06:44 +00001356 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001357
Ted Kremenek016f52f2008-02-08 21:10:02 +00001358 bool isFirst = true;
1359
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001360 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001361 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001362 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001363 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001364 isFirst = false;
1365 }
1366 else
1367 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001368
Ted Kremeneke7d22112008-02-11 19:21:59 +00001369 Out << " (" << (void*) I.getKey() << ") ";
1370 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001371 Out << " : ";
1372 I.getData().print(Out);
1373 }
1374 }
1375
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001376 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001377 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1378
1379 if (CE.isEmpty())
1380 return;
1381
1382 Out << "\\l\\|'==' constraints:";
1383
1384 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1385 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1386 }
1387
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001388 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001389 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1390
1391 if (NE.isEmpty())
1392 return;
1393
1394 Out << "\\l\\|'!=' constraints:";
1395
1396 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1397 I != EI; ++I){
1398
1399 Out << "\\l $" << I.getKey() << " : ";
1400 bool isFirst = true;
1401
1402 ValueState::IntSetTy::iterator J=I.getData().begin(),
1403 EJ=I.getData().end();
1404 for ( ; J != EJ; ++J) {
1405 if (isFirst) isFirst = false;
1406 else Out << ", ";
1407
1408 Out << (*J)->toString();
1409 }
1410 }
1411 }
1412
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001413 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001414 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001415
1416 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001417 ProgramPoint Loc = N->getLocation();
1418
1419 switch (Loc.getKind()) {
1420 case ProgramPoint::BlockEntranceKind:
1421 Out << "Block Entrance: B"
1422 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1423 break;
1424
1425 case ProgramPoint::BlockExitKind:
1426 assert (false);
1427 break;
1428
1429 case ProgramPoint::PostStmtKind: {
1430 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001431 Out << L.getStmt()->getStmtClassName() << ':'
1432 << (void*) L.getStmt() << ' ';
1433
Ted Kremenekaa66a322008-01-16 21:46:15 +00001434 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001435
1436 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1437 Out << "\\|Implicit-Null Dereference.\\l";
1438 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001439 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1440 Out << "\\|Explicit-Null Dereference.\\l";
1441 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001442
Ted Kremenekaa66a322008-01-16 21:46:15 +00001443 break;
1444 }
1445
1446 default: {
1447 const BlockEdge& E = cast<BlockEdge>(Loc);
1448 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1449 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001450
1451 if (Stmt* T = E.getSrc()->getTerminator()) {
1452 Out << "\\|Terminator: ";
1453 E.getSrc()->printTerminator(Out);
1454
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001455 if (isa<SwitchStmt>(T)) {
1456 Stmt* Label = E.getDst()->getLabel();
1457
1458 if (Label) {
1459 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1460 Out << "\\lcase ";
1461 C->getLHS()->printPretty(Out);
1462
1463 if (Stmt* RHS = C->getRHS()) {
1464 Out << " .. ";
1465 RHS->printPretty(Out);
1466 }
1467
1468 Out << ":";
1469 }
1470 else {
1471 assert (isa<DefaultStmt>(Label));
1472 Out << "\\ldefault:";
1473 }
1474 }
1475 else
1476 Out << "\\l(implicit) default:";
1477 }
1478 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001479 // FIXME
1480 }
1481 else {
1482 Out << "\\lCondition: ";
1483 if (*E.getSrc()->succ_begin() == E.getDst())
1484 Out << "true";
1485 else
1486 Out << "false";
1487 }
1488
1489 Out << "\\l";
1490 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001491
1492 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1493 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1494 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001495 }
1496 }
1497
Ted Kremenek9153f732008-02-05 07:17:49 +00001498 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001499
Ted Kremeneke7d22112008-02-11 19:21:59 +00001500 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001501
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001502 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001503 return Out.str();
1504 }
1505};
1506} // end llvm namespace
1507#endif
1508
Ted Kremenekee985462008-01-16 18:18:48 +00001509namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001510void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001511 Diagnostic& Diag) {
1512
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001513 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001514 Engine.ExecuteWorkList();
1515
1516 // Look for explicit-Null dereferences and warn about them.
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001517 GRExprEngine* CheckerState = &Engine.getCheckerState();
Ted Kremenek19227e32008-02-07 06:33:19 +00001518
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001519 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001520 E=CheckerState->null_end(); I!=E; ++I) {
1521
1522 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1523 Expr* E = cast<Expr>(L.getStmt());
1524
1525 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1526 diag::chkr_null_deref_after_check);
1527 }
1528
1529
Ted Kremenekaa66a322008-01-16 21:46:15 +00001530#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001531 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001532 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001533 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001534#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001535}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001536} // end clang namespace