blob: e2373e8b2d78ae4cd4306aedef44f7b4b05d1b42 [file] [log] [blame]
Ted Kremenek4d4dd852008-02-13 17:41:41 +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 Kremenek4d4dd852008-02-13 17:41:41 +000068 typedef GRStmtNodeBuilder<GRExprEngine> StmtNodeBuilder;
69 typedef GRBranchNodeBuilder<GRExprEngine> BranchNodeBuilder;
70 typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder;
71 typedef ExplodedGraph<GRExprEngine> GraphTy;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000072 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000073
74 class NodeSet {
75 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
76 ImplTy Impl;
77 public:
78
79 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000080 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000081
Ted Kremenekb38911f2008-01-30 23:03:39 +000082 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000083
84 typedef ImplTy::iterator iterator;
85 typedef ImplTy::const_iterator const_iterator;
86
87 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000088 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000089
90 iterator begin() { return Impl.begin(); }
91 iterator end() { return Impl.end(); }
92
93 const_iterator begin() const { return Impl.begin(); }
94 const_iterator end() const { return Impl.end(); }
95 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000096
Ted Kremenekd27f8162008-01-15 23:55:06 +000097protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000098 /// G - the simulation graph.
99 GraphTy& G;
100
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000101 /// Liveness - live-variables information the ValueDecl* and block-level
102 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000103 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000104
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000105 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000106 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000107 StmtNodeBuilder* Builder;
108
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000109 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000110 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000111
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000112 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000113 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000114
Ted Kremenek68fd2572008-01-29 17:27:31 +0000115 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000116 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000117
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000118 /// StmtEntryNode - The immediate predecessor node.
119 NodeTy* StmtEntryNode;
120
121 /// CurrentStmt - The current block-level statement.
122 Stmt* CurrentStmt;
123
Ted Kremenekb38911f2008-01-30 23:03:39 +0000124 /// UninitBranches - Nodes in the ExplodedGraph that result from
125 /// taking a branch based on an uninitialized value.
126 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
127 UninitBranchesTy UninitBranches;
128
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000129 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
130 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000131 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
132 NullDerefTy ImplicitNullDeref;
133 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000134
135
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000136 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000137
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138public:
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000139 GRExprEngine(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000140 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000141 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000142 ValMgr(StateMgr.getValueManager()),
143 SymMgr(StateMgr.getSymbolManager()),
144 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000145
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000146 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000147 Liveness.runOnCFG(G.getCFG());
148 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000149 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000150
Ted Kremenek19227e32008-02-07 06:33:19 +0000151 /// getContext - Return the ASTContext associated with this analysis.
152 ASTContext& getContext() const { return G.getContext(); }
153
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000154 /// getCFG - Returns the CFG associated with this analysis.
155 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000156
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000157 /// getInitialState - Return the initial state used for the root vertex
158 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000159 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000160 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000161
162 // Iterate the parameters.
163 FunctionDecl& F = G.getFunctionDecl();
164
165 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000166 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000167 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000168
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000169 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000170 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000171
172 bool isUninitControlFlow(const NodeTy* N) const {
173 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
174 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000175
176 bool isImplicitNullDeref(const NodeTy* N) const {
177 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
178 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000179
180 bool isExplicitNullDeref(const NodeTy* N) const {
181 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
182 }
183
Ted Kremenek19227e32008-02-07 06:33:19 +0000184 typedef NullDerefTy::iterator null_iterator;
185 null_iterator null_begin() { return ExplicitNullDeref.begin(); }
186 null_iterator null_end() { return ExplicitNullDeref.end(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000187
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000188 /// ProcessStmt - Called by GRCoreEngine. Used to generate new successor
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000189 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000190 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
191
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000192 /// ProcessBranch - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000193 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000194 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000195
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000196 /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000197 /// nodes by processing the 'effects' of a computed goto jump.
198 void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder);
199
Ted Kremenekb87d9092008-02-08 19:17:19 +0000200 /// RemoveDeadBindings - Return a new state that is the same as 'St' except
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000201 /// that all subexpression mappings are removed and that any
202 /// block-level expressions that are not live at 'S' also have their
203 /// mappings removed.
Ted Kremenekb87d9092008-02-08 19:17:19 +0000204 inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) {
205 return StateMgr.RemoveDeadBindings(St, S, Liveness);
206 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000207
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000208 StateTy SetValue(StateTy St, Expr* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000209
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000210 StateTy SetValue(StateTy St, const Expr* S, const RValue& V) {
211 return SetValue(St, const_cast<Expr*>(S), V);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000212 }
213
Ted Kremenekcba2e432008-02-05 19:35:18 +0000214 /// SetValue - This version of SetValue is used to batch process a set
215 /// of different possible RValues and return a set of different states.
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000216 const StateTy::BufferTy& SetValue(StateTy St, Expr* S,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000217 const RValue::BufferTy& V,
218 StateTy::BufferTy& RetBuf);
219
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000220 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000221
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000222 inline RValue GetValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000223 return StateMgr.GetValue(St, S);
224 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000225
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000226 inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000227 return StateMgr.GetValue(St, S, &hasVal);
228 }
229
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000230 inline RValue GetValue(const StateTy& St, const Expr* S) {
231 return GetValue(St, const_cast<Expr*>(S));
Ted Kremenek9de04c42008-01-24 20:55:43 +0000232 }
233
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000234 inline RValue GetValue(const StateTy& St, const LValue& LV,
235 QualType* T = NULL) {
236
237 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000238 }
239
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000240 inline LValue GetLValue(const StateTy& St, Expr* S) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000241 return StateMgr.GetLValue(St, S);
242 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000243
244 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
245 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
246 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000247
248 /// Assume - Create new state by assuming that a given expression
249 /// is true or false.
250 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
251 bool& isFeasible) {
252 if (isa<LValue>(Cond))
253 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
254 else
255 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
256 }
257
258 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
259 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000260
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000261 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
262 bool& isFeasible);
263
264 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
265 bool& isFeasible);
266
Ted Kremenek08b66252008-02-06 04:31:33 +0000267 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
268 bool& isFeasible);
269
Ted Kremenek7e593362008-02-07 15:20:13 +0000270 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000271
Ted Kremenekcba2e432008-02-05 19:35:18 +0000272 /// Nodify - This version of Nodify is used to batch process a set of states.
273 /// The states are not guaranteed to be unique.
274 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
275
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000276 /// Visit - Transfer function logic for all statements. Dispatches to
277 /// other functions that handle specific kinds of statements.
278 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000279
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000280 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000281 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
282
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000283 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000284
Ted Kremenek230aaab2008-02-12 21:37:25 +0000285 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
286 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
287
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000288 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
289 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
290
Ted Kremenek9de04c42008-01-24 20:55:43 +0000291 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000292 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
293
294 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000295 void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000296 NodeTy* Pred, NodeSet& Dst);
297
298 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
299 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000300
301 /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
302 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred,
303 NodeSet& Dst);
Ted Kremenek230aaab2008-02-12 21:37:25 +0000304
305 /// VisitUnaryOperator - Transfer function logic for unary operators.
306 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
307
Ted Kremenekd27f8162008-01-15 23:55:06 +0000308};
309} // end anonymous namespace
310
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000311
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000312GRExprEngine::StateTy
313GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000314
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000315 if (!StateCleaned) {
316 St = RemoveDeadBindings(CurrentStmt, St);
317 StateCleaned = true;
318 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000319
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000320 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000321
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000322 if (S == CurrentStmt) {
323 isBlkExpr = getCFG().isBlkExpr(S);
324
325 if (!isBlkExpr)
326 return St;
327 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000328
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000329 return StateMgr.SetValue(St, S, isBlkExpr, V);
330}
331
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000332const GRExprEngine::StateTy::BufferTy&
333GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000334 StateTy::BufferTy& RetBuf) {
335
336 assert (RetBuf.empty());
337
338 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
339 RetBuf.push_back(SetValue(St, S, *I));
340
341 return RetBuf;
342}
343
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000344GRExprEngine::StateTy
345GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000346
Ted Kremenek53c641a2008-02-08 03:02:48 +0000347 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000348 return St;
349
350 if (!StateCleaned) {
351 St = RemoveDeadBindings(CurrentStmt, St);
352 StateCleaned = true;
353 }
354
355 return StateMgr.SetValue(St, LV, V);
356}
357
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000358void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000359 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000360
Ted Kremeneke7d22112008-02-11 19:21:59 +0000361 // Remove old bindings for subexpressions.
362 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000363
Ted Kremenekb38911f2008-01-30 23:03:39 +0000364 RValue V = GetValue(PrevState, Condition);
365
366 switch (V.getBaseKind()) {
367 default:
368 break;
369
Ted Kremenek53c641a2008-02-08 03:02:48 +0000370 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +0000371 builder.generateNode(PrevState, true);
372 builder.generateNode(PrevState, false);
373 return;
374
375 case RValue::UninitializedKind: {
376 NodeTy* N = builder.generateNode(PrevState, true);
377
378 if (N) {
379 N->markAsSink();
380 UninitBranches.insert(N);
381 }
382
383 builder.markInfeasible(false);
384 return;
385 }
386 }
387
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000388 // Get the current block counter.
389 GRBlockCounter BC = builder.getBlockCounter();
390
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000391 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
392 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000393
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000394 if (isa<nonlval::ConcreteInt>(V) ||
395 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
396
397 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000398
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000399 bool isFeasible = true;
400
401 StateTy St = Assume(PrevState, V, true, isFeasible);
402
403 if (isFeasible)
404 builder.generateNode(St, true);
405 else
406 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000407 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000408 else
409 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000410
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000411 BlockID = builder.getTargetBlock(false)->getBlockID();
412 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000413
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000414 if (isa<nonlval::ConcreteInt>(V) ||
415 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
416
417 // Process the false branch.
418
419 bool isFeasible = false;
420
421 StateTy St = Assume(PrevState, V, false, isFeasible);
422
423 if (isFeasible)
424 builder.generateNode(St, false);
425 else
426 builder.markInfeasible(false);
427 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000428 else
429 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000430}
431
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000432/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000433/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000434void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000435
436 StateTy St = builder.getState();
437 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
438
439 // Three possibilities:
440 //
441 // (1) We know the computed label.
442 // (2) The label is NULL (or some other constant), or Uninitialized.
443 // (3) We have no clue about the label. Dispatch to all targets.
444 //
445
446 typedef IndirectGotoNodeBuilder::iterator iterator;
447
448 if (isa<lval::GotoLabel>(V)) {
449 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
450
451 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000452 if (I.getLabel() == L) {
453 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000454 return;
455 }
456 }
457
458 assert (false && "No block with label.");
459 return;
460 }
461
462 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
463 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000464 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000465 UninitBranches.insert(N);
466 return;
467 }
468
469 // This is really a catch-all. We don't support symbolics yet.
470
471 assert (isa<UnknownVal>(V));
472
473 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000474 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000475}
Ted Kremenekf233d482008-02-05 00:26:40 +0000476
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000477void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000478 NodeSet& Dst) {
479
480 bool hasR2;
481 StateTy PrevState = Pred->getState();
482
483 RValue R1 = GetValue(PrevState, B->getLHS());
484 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
485
Ted Kremenek22031182008-02-08 02:57:34 +0000486 if (isa<UnknownVal>(R1) &&
487 (isa<UnknownVal>(R2) ||
488 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000489
490 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
491 return;
492 }
Ted Kremenek22031182008-02-08 02:57:34 +0000493 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000494 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
495 return;
496 }
497
498 // R1 is an expression that can evaluate to either 'true' or 'false'.
499 if (B->getOpcode() == BinaryOperator::LAnd) {
500 // hasR2 == 'false' means that LHS evaluated to 'false' and that
501 // we short-circuited, leading to a value of '0' for the '&&' expression.
502 if (hasR2 == false) {
503 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
504 return;
505 }
506 }
507 else {
508 assert (B->getOpcode() == BinaryOperator::LOr);
509 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
510 // we short-circuited, leading to a value of '1' for the '||' expression.
511 if (hasR2 == false) {
512 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
513 return;
514 }
515 }
516
517 // If we reach here we did not short-circuit. Assume R2 == true and
518 // R2 == false.
519
520 bool isFeasible;
521 StateTy St = Assume(PrevState, R2, true, isFeasible);
522
523 if (isFeasible)
524 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
525
526 St = Assume(PrevState, R2, false, isFeasible);
527
528 if (isFeasible)
529 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
530}
531
532
533
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000534void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000535 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000536
537 StmtEntryNode = builder.getLastNode();
538 CurrentStmt = S;
539 NodeSet Dst;
540 StateCleaned = false;
541
542 Visit(S, StmtEntryNode, Dst);
543
544 // If no nodes were generated, generate a new node that has all the
545 // dead mappings removed.
546 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
547 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
548 builder.generateNode(S, St, StmtEntryNode);
549 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000550
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000551 CurrentStmt = NULL;
552 StmtEntryNode = NULL;
553 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000554}
555
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000556GRExprEngine::NodeTy*
557GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000558
559 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000560 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000561 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000562
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000563 NodeTy* N = Builder->generateNode(S, St, Pred);
564 Dst.Add(N);
565 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000566}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000567
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000568void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000569 const StateTy::BufferTy& SB) {
570
571 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
572 Nodify(Dst, S, Pred, *I);
573}
574
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000575void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000576 if (D != CurrentStmt) {
577 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
578 return;
579 }
580
581 // If we are here, we are loading the value of the decl and binding
582 // it to the block-level expression.
583
584 StateTy St = Pred->getState();
585
586 Nodify(Dst, D, Pred,
587 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
588}
589
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000590void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000591
592 QualType T = CastE->getType();
593
594 // Check for redundant casts.
595 if (E->getType() == T) {
596 Dst.Add(Pred);
597 return;
598 }
599
600 NodeSet S1;
601 Visit(E, Pred, S1);
602
603 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
604 NodeTy* N = *I1;
605 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000606 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000607 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000608 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000609}
610
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000611void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
612 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000613
614 StateTy St = Pred->getState();
615
616 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000617 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
618 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000619 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000620 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000621 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000622
623 Nodify(Dst, DS, Pred, St);
624
625 if (Dst.empty())
626 Dst.Add(Pred);
627}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000628
Ted Kremenekf233d482008-02-05 00:26:40 +0000629
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000630void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000631 NodeTy* Pred, NodeSet& Dst) {
632
633 StateTy St = Pred->getState();
634
635 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000636 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000637
638 Nodify(Dst, S, Pred, SetValue(St, S, R));
639}
640
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000641/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000642void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000643 NodeTy* Pred,
644 NodeSet& Dst) {
645
646 // 6.5.3.4 sizeof: "The result type is an integer."
647
648 QualType T = S->getArgumentType();
649
650 // FIXME: Add support for VLAs.
651 if (isa<VariableArrayType>(T.getTypePtr()))
652 return;
653
654 SourceLocation L = S->getExprLoc();
655 uint64_t size = getContext().getTypeSize(T, L) / 8;
656
657 Nodify(Dst, S, Pred,
658 SetValue(Pred->getState(), S,
659 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
660
661}
662
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000663void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
664 GRExprEngine::NodeTy* Pred,
665 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000666
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000667 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000668 UnaryOperator::Opcode Op = U->getOpcode();
669
670 // FIXME: This is a hack so that for '*' and '&' we don't recurse
671 // on visiting the subexpression if it is a DeclRefExpr. We should
672 // probably just handle AddrOf and Deref in their own methods to make
673 // this cleaner.
674 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
675 isa<DeclRefExpr>(U->getSubExpr()))
676 S1.Add(Pred);
677 else
678 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000679
680 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
681 NodeTy* N1 = *I1;
682 StateTy St = N1->getState();
683
684 switch (U->getOpcode()) {
685 case UnaryOperator::PostInc: {
686 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000687 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000688
689 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
690 GetRValueConstant(1U, U));
691
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000692 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
693 break;
694 }
695
696 case UnaryOperator::PostDec: {
697 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000698 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000699
700 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
701 GetRValueConstant(1U, U));
702
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000703 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
704 break;
705 }
706
707 case UnaryOperator::PreInc: {
708 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000710
711 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
712 GetRValueConstant(1U, U));
713
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000714 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
715 break;
716 }
717
718 case UnaryOperator::PreDec: {
719 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000720 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000721
722 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
723 GetRValueConstant(1U, U));
724
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000725 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
726 break;
727 }
728
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000729 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000730 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000731 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000732 break;
733 }
734
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000735 case UnaryOperator::Not: {
736 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000737 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000738 break;
739 }
740
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000741 case UnaryOperator::LNot: {
742 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
743 //
744 // Note: technically we do "E == 0", but this is the same in the
745 // transfer functions as "0 == E".
746
747 RValue V1 = GetValue(St, U->getSubExpr());
748
749 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000750 const LValue& L1 = cast<LValue>(V1);
751 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
752 Nodify(Dst, U, N1,
753 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
754 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000755 }
756 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000757 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000758 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000759 Nodify(Dst, U, N1,
760 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
761 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000762 }
763
764 break;
765 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000766
767 case UnaryOperator::SizeOf: {
768 // 6.5.3.4 sizeof: "The result type is an integer."
769
770 QualType T = U->getSubExpr()->getType();
771
772 // FIXME: Add support for VLAs.
773 if (isa<VariableArrayType>(T.getTypePtr()))
774 return;
775
776 SourceLocation L = U->getExprLoc();
777 uint64_t size = getContext().getTypeSize(T, L) / 8;
778
779 Nodify(Dst, U, N1,
780 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
781 getContext().IntTy, L)));
782
783 break;
784 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000785
Ted Kremenek64924852008-01-31 02:35:41 +0000786 case UnaryOperator::AddrOf: {
787 const LValue& L1 = GetLValue(St, U->getSubExpr());
788 Nodify(Dst, U, N1, SetValue(St, U, L1));
789 break;
790 }
791
792 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000793 // FIXME: Stop when dereferencing an uninitialized value.
794 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
795
796 const RValue& V = GetValue(St, U->getSubExpr());
797 const LValue& L1 = cast<LValue>(V);
798
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000799 // After a dereference, one of two possible situations arise:
800 // (1) A crash, because the pointer was NULL.
801 // (2) The pointer is not NULL, and the dereference works.
802 //
803 // We add these assumptions.
804
Ted Kremenek63a4f692008-02-07 06:04:18 +0000805 bool isFeasibleNotNull;
806
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000807 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000808 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
809
810 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000811 QualType T = U->getType();
812 Nodify(Dst, U, N1, SetValue(StNotNull, U,
813 GetValue(StNotNull, L1, &T)));
814 }
815
Ted Kremenek63a4f692008-02-07 06:04:18 +0000816 bool isFeasibleNull;
817
818 // "Assume" that the pointer is NULL.
819 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
820
821 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000822 // We don't use "Nodify" here because the node will be a sink
823 // and we have no intention of processing it later.
824 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
825
Ted Kremenek63a4f692008-02-07 06:04:18 +0000826 if (NullNode) {
827 NullNode->markAsSink();
828
829 if (isFeasibleNotNull)
830 ImplicitNullDeref.insert(NullNode);
831 else
832 ExplicitNullDeref.insert(NullNode);
833 }
834 }
835
Ted Kremenek64924852008-01-31 02:35:41 +0000836 break;
837 }
838
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000839 default: ;
840 assert (false && "Not implemented.");
841 }
842 }
843}
844
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000845void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
846 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000847
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000848 if (isa<DeclRefExpr>(E)) {
849 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000850 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000851 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000852
853 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
854 if (U->getOpcode() == UnaryOperator::Deref) {
855 Visit(U->getSubExpr(), Pred, Dst);
856 return;
857 }
858 }
859
860 Visit(E, Pred, Dst);
861}
862
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000863void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
864 GRExprEngine::NodeTy* Pred,
865 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000866 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000867
868 if (B->isAssignmentOp())
869 VisitAssignmentLHS(B->getLHS(), Pred, S1);
870 else
871 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000872
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000873 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
874 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000875
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000876 // When getting the value for the LHS, check if we are in an assignment.
877 // In such cases, we want to (initially) treat the LHS as an LValue,
878 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000879 // evaluated to LValueDecl's instead of to an NonLValue.
880 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000881 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
882 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000883
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000884 NodeSet S2;
885 Visit(B->getRHS(), N1, S2);
886
887 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000888
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000889 NodeTy* N2 = *I2;
890 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000891 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000892
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000893 BinaryOperator::Opcode Op = B->getOpcode();
894
895 if (Op <= BinaryOperator::Or) {
896
Ted Kremenek22031182008-02-08 02:57:34 +0000897 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000898 Nodify(Dst, B, N2, SetValue(St, B, V1));
899 continue;
900 }
901
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000902 if (isa<LValue>(V1)) {
903 // FIXME: Add support for RHS being a non-lvalue.
904 const LValue& L1 = cast<LValue>(V1);
905 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000906
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000907 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
908 }
909 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000910 const NonLValue& R1 = cast<NonLValue>(V1);
911 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000912
913 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000914 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000915
916 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000917
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000918 }
919
920 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000921 case BinaryOperator::Assign: {
922 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000923 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000924 break;
925 }
926
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000927 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000928
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000929 assert (B->isCompoundAssignmentOp());
930
931 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000932 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000933
Ted Kremenekda9bd092008-02-08 07:05:39 +0000934 if (Op >= BinaryOperator::AndAssign)
935 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
936 else
937 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000938
939 if (isa<LValue>(V2)) {
940 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000941 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000942 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000943 }
944 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000945 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000946 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000947 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000948 }
949
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000950 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000951 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000952 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000953 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000954 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000955 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000956}
Ted Kremenekee985462008-01-16 18:18:48 +0000957
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000958
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000959void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
960 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000961
962 // FIXME: add metadata to the CFG so that we can disable
963 // this check when we KNOW that there is no block-level subexpression.
964 // The motivation is that this check requires a hashtable lookup.
965
966 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
967 Dst.Add(Pred);
968 return;
969 }
970
971 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000972
973 default:
974 // Cases we intentionally have "default" handle:
975 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
976
977 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
978 break;
979
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000980 case Stmt::BinaryOperatorClass: {
981 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000982
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000983 if (B->isLogicalOp()) {
984 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000985 break;
986 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000987 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000988 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000989 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000990 break;
991 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000992
993 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
994 break;
995 }
996
997 case Stmt::CastExprClass: {
998 CastExpr* C = cast<CastExpr>(S);
999 VisitCast(C, C->getSubExpr(), Pred, Dst);
1000 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001001 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001002
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001003 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1004 ChooseExpr* C = cast<ChooseExpr>(S);
1005 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1006 break;
1007 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001008
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001009 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001010 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1011 break;
1012
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001013 case Stmt::ConditionalOperatorClass: { // '?' operator
1014 ConditionalOperator* C = cast<ConditionalOperator>(S);
1015 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1016 break;
1017 }
1018
1019 case Stmt::DeclRefExprClass:
1020 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1021 break;
1022
1023 case Stmt::DeclStmtClass:
1024 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1025 break;
1026
1027 case Stmt::ImplicitCastExprClass: {
1028 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1029 VisitCast(C, C->getSubExpr(), Pred, Dst);
1030 break;
1031 }
1032
1033 case Stmt::ParenExprClass:
1034 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1035 break;
1036
1037 case Stmt::SizeOfAlignOfTypeExprClass:
1038 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1039 break;
1040
Ted Kremenekda9bd092008-02-08 07:05:39 +00001041 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001042 StmtExpr* SE = cast<StmtExpr>(S);
1043
Ted Kremenekda9bd092008-02-08 07:05:39 +00001044 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001045 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
1046 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001047 break;
1048 }
1049
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001050 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001051 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1052 Visit(R, Pred, Dst);
1053 else
1054 Dst.Add(Pred);
1055
1056 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001057 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001058
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001059 case Stmt::UnaryOperatorClass:
1060 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001061 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001062 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001063}
1064
Ted Kremenekee985462008-01-16 18:18:48 +00001065//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001066// "Assume" logic.
1067//===----------------------------------------------------------------------===//
1068
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001069GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001070 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001071 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001072
1073 switch (Cond.getSubKind()) {
1074 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001075 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001076 return St;
1077
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001078 case lval::SymbolValKind:
1079 if (Assumption)
1080 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1081 ValMgr.getZeroWithPtrWidth(), isFeasible);
1082 else
1083 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1084 ValMgr.getZeroWithPtrWidth(), isFeasible);
1085
Ted Kremenek08b66252008-02-06 04:31:33 +00001086
Ted Kremenek329f8542008-02-05 21:52:21 +00001087 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001088 isFeasible = Assumption;
1089 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001090
Ted Kremenek329f8542008-02-05 21:52:21 +00001091 case lval::ConcreteIntKind: {
1092 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001093 isFeasible = b ? Assumption : !Assumption;
1094 return St;
1095 }
1096 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001097}
1098
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001099GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001100 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001101 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001102
1103 switch (Cond.getSubKind()) {
1104 default:
1105 assert (false && "'Assume' not implemented for this NonLValue.");
1106 return St;
1107
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001108
1109 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001110 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001111 SymbolID sym = SV.getSymbol();
1112
1113 if (Assumption)
1114 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1115 isFeasible);
1116 else
1117 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1118 isFeasible);
1119 }
1120
Ted Kremenek08b66252008-02-06 04:31:33 +00001121 case nonlval::SymIntConstraintValKind:
1122 return
1123 AssumeSymInt(St, Assumption,
1124 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1125 isFeasible);
1126
Ted Kremenek329f8542008-02-05 21:52:21 +00001127 case nonlval::ConcreteIntKind: {
1128 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001129 isFeasible = b ? Assumption : !Assumption;
1130 return St;
1131 }
1132 }
1133}
1134
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001135GRExprEngine::StateTy
1136GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001137 const llvm::APSInt& V, bool& isFeasible) {
1138
1139 // First, determine if sym == X, where X != V.
1140 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1141 isFeasible = *X != V;
1142 return St;
1143 }
1144
1145 // Second, determine if sym != V.
1146 if (St.isNotEqual(sym, V)) {
1147 isFeasible = true;
1148 return St;
1149 }
1150
1151 // If we reach here, sym is not a constant and we don't know if it is != V.
1152 // Make that assumption.
1153
1154 isFeasible = true;
1155 return StateMgr.AddNE(St, sym, V);
1156}
1157
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001158GRExprEngine::StateTy
1159GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001160 const llvm::APSInt& V, bool& isFeasible) {
1161
1162 // First, determine if sym == X, where X != V.
1163 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1164 isFeasible = *X == V;
1165 return St;
1166 }
1167
1168 // Second, determine if sym != V.
1169 if (St.isNotEqual(sym, V)) {
1170 isFeasible = false;
1171 return St;
1172 }
1173
1174 // If we reach here, sym is not a constant and we don't know if it is == V.
1175 // Make that assumption.
1176
1177 isFeasible = true;
1178 return StateMgr.AddEQ(St, sym, V);
1179}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001180
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001181GRExprEngine::StateTy
1182GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001183 const SymIntConstraint& C, bool& isFeasible) {
1184
1185 switch (C.getOpcode()) {
1186 default:
1187 // No logic yet for other operators.
1188 return St;
1189
1190 case BinaryOperator::EQ:
1191 if (Assumption)
1192 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1193 else
1194 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1195
1196 case BinaryOperator::NE:
1197 if (Assumption)
1198 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1199 else
1200 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1201 }
1202}
1203
Ted Kremenekb38911f2008-01-30 23:03:39 +00001204//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001205// Driver.
1206//===----------------------------------------------------------------------===//
1207
Ted Kremenekaa66a322008-01-16 21:46:15 +00001208#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001209static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001210
Ted Kremenekaa66a322008-01-16 21:46:15 +00001211namespace llvm {
1212template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001213struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001214 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001215
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001216 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001217
1218 Out << "Variables:\\l";
1219
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001220 bool isFirst = true;
1221
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001222 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001223 E=St.vb_end(); I!=E;++I) {
1224
1225 if (isFirst)
1226 isFirst = false;
1227 else
1228 Out << "\\l";
1229
1230 Out << ' ' << I.getKey()->getName() << " : ";
1231 I.getData().print(Out);
1232 }
1233
1234 }
1235
Ted Kremeneke7d22112008-02-11 19:21:59 +00001236
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001237 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001238
1239 bool isFirst = true;
1240
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001241 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001242 I != E;++I) {
1243
1244 if (isFirst) {
1245 Out << "\\l\\lSub-Expressions:\\l";
1246 isFirst = false;
1247 }
1248 else
1249 Out << "\\l";
1250
1251 Out << " (" << (void*) I.getKey() << ") ";
1252 I.getKey()->printPretty(Out);
1253 Out << " : ";
1254 I.getData().print(Out);
1255 }
1256 }
1257
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001258 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001259
Ted Kremenek016f52f2008-02-08 21:10:02 +00001260 bool isFirst = true;
1261
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001262 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001263 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001264 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001265 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001266 isFirst = false;
1267 }
1268 else
1269 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001270
Ted Kremeneke7d22112008-02-11 19:21:59 +00001271 Out << " (" << (void*) I.getKey() << ") ";
1272 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001273 Out << " : ";
1274 I.getData().print(Out);
1275 }
1276 }
1277
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001278 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001279 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1280
1281 if (CE.isEmpty())
1282 return;
1283
1284 Out << "\\l\\|'==' constraints:";
1285
1286 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1287 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1288 }
1289
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001290 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001291 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1292
1293 if (NE.isEmpty())
1294 return;
1295
1296 Out << "\\l\\|'!=' constraints:";
1297
1298 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1299 I != EI; ++I){
1300
1301 Out << "\\l $" << I.getKey() << " : ";
1302 bool isFirst = true;
1303
1304 ValueState::IntSetTy::iterator J=I.getData().begin(),
1305 EJ=I.getData().end();
1306 for ( ; J != EJ; ++J) {
1307 if (isFirst) isFirst = false;
1308 else Out << ", ";
1309
1310 Out << (*J)->toString();
1311 }
1312 }
1313 }
1314
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001315 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001316 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001317
1318 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001319 ProgramPoint Loc = N->getLocation();
1320
1321 switch (Loc.getKind()) {
1322 case ProgramPoint::BlockEntranceKind:
1323 Out << "Block Entrance: B"
1324 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1325 break;
1326
1327 case ProgramPoint::BlockExitKind:
1328 assert (false);
1329 break;
1330
1331 case ProgramPoint::PostStmtKind: {
1332 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001333 Out << L.getStmt()->getStmtClassName() << ':'
1334 << (void*) L.getStmt() << ' ';
1335
Ted Kremenekaa66a322008-01-16 21:46:15 +00001336 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001337
1338 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1339 Out << "\\|Implicit-Null Dereference.\\l";
1340 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001341 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1342 Out << "\\|Explicit-Null Dereference.\\l";
1343 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001344
Ted Kremenekaa66a322008-01-16 21:46:15 +00001345 break;
1346 }
1347
1348 default: {
1349 const BlockEdge& E = cast<BlockEdge>(Loc);
1350 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1351 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001352
1353 if (Stmt* T = E.getSrc()->getTerminator()) {
1354 Out << "\\|Terminator: ";
1355 E.getSrc()->printTerminator(Out);
1356
Ted Kremenek754607e2008-02-13 00:24:44 +00001357 if (isa<SwitchStmt>(T) || isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001358 // FIXME
1359 }
1360 else {
1361 Out << "\\lCondition: ";
1362 if (*E.getSrc()->succ_begin() == E.getDst())
1363 Out << "true";
1364 else
1365 Out << "false";
1366 }
1367
1368 Out << "\\l";
1369 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001370
1371 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1372 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1373 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001374 }
1375 }
1376
Ted Kremenek9153f732008-02-05 07:17:49 +00001377 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001378
Ted Kremeneke7d22112008-02-11 19:21:59 +00001379 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001380
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001381 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001382 return Out.str();
1383 }
1384};
1385} // end llvm namespace
1386#endif
1387
Ted Kremenekee985462008-01-16 18:18:48 +00001388namespace clang {
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001389void RunGRExprEngine(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001390 Diagnostic& Diag) {
1391
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001392 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenek19227e32008-02-07 06:33:19 +00001393 Engine.ExecuteWorkList();
1394
1395 // Look for explicit-Null dereferences and warn about them.
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001396 GRExprEngine* CheckerState = &Engine.getCheckerState();
Ted Kremenek19227e32008-02-07 06:33:19 +00001397
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001398 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001399 E=CheckerState->null_end(); I!=E; ++I) {
1400
1401 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1402 Expr* E = cast<Expr>(L.getStmt());
1403
1404 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1405 diag::chkr_null_deref_after_check);
1406 }
1407
1408
Ted Kremenekaa66a322008-01-16 21:46:15 +00001409#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001410 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001411 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001412 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001413#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001414}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001415} // end clang namespace