blob: 9fc828616af5dc16d717699b6130dab9bcbd3ad8 [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.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 Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/Analysis/PathSensitive/GREngine.h"
22#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 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 Kremenekab2b8c52008-01-23 19:59:44 +000063class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +000064
65public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000066 typedef ValueStateManager::StateTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +000067 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
68 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000069 typedef ExplodedGraph<GRConstants> GraphTy;
70 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000071
72 class NodeSet {
73 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
74 ImplTy Impl;
75 public:
76
77 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000078 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000079
Ted Kremenekb38911f2008-01-30 23:03:39 +000080 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000081
82 typedef ImplTy::iterator iterator;
83 typedef ImplTy::const_iterator const_iterator;
84
85 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000086 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000087
88 iterator begin() { return Impl.begin(); }
89 iterator end() { return Impl.end(); }
90
91 const_iterator begin() const { return Impl.begin(); }
92 const_iterator end() const { return Impl.end(); }
93 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000094
Ted Kremenekd27f8162008-01-15 23:55:06 +000095protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000096 /// G - the simulation graph.
97 GraphTy& G;
98
Ted Kremenekab2b8c52008-01-23 19:59:44 +000099 /// Liveness - live-variables information the ValueDecl* and block-level
100 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000101 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000102
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000103 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000104 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000105 StmtNodeBuilder* Builder;
106
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000107 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000108 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000109
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000110 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000111 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000112
Ted Kremenek68fd2572008-01-29 17:27:31 +0000113 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000114 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000115
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000116 /// StmtEntryNode - The immediate predecessor node.
117 NodeTy* StmtEntryNode;
118
119 /// CurrentStmt - The current block-level statement.
120 Stmt* CurrentStmt;
121
Ted Kremenekb38911f2008-01-30 23:03:39 +0000122 /// UninitBranches - Nodes in the ExplodedGraph that result from
123 /// taking a branch based on an uninitialized value.
124 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
125 UninitBranchesTy UninitBranches;
126
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000127 /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from
128 /// taking a dereference on a symbolic pointer that may be NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000129 typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy;
130 NullDerefTy ImplicitNullDeref;
131 NullDerefTy ExplicitNullDeref;
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000132
133
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000134 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000135
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000136 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000137
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000139 GRConstants(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
151 /// getCFG - Returns the CFG associated with this analysis.
152 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000153
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000154 /// getInitialState - Return the initial state used for the root vertex
155 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000156 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000157 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000158
159 // Iterate the parameters.
160 FunctionDecl& F = G.getFunctionDecl();
161
162 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000163 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000164 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000165
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000166 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000167 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000168
169 bool isUninitControlFlow(const NodeTy* N) const {
170 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
171 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000172
173 bool isImplicitNullDeref(const NodeTy* N) const {
174 return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
175 }
Ted Kremenek63a4f692008-02-07 06:04:18 +0000176
177 bool isExplicitNullDeref(const NodeTy* N) const {
178 return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0;
179 }
180
Ted Kremenekd27f8162008-01-15 23:55:06 +0000181
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000182 /// ProcessStmt - Called by GREngine. Used to generate new successor
183 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000184 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
185
186 /// ProcessBranch - Called by GREngine. Used to generate successor
187 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000188 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000189
190 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
191 /// that all subexpression mappings are removed and that any
192 /// block-level expressions that are not live at 'S' also have their
193 /// mappings removed.
194 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
195
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000196 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000197
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000198 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000199 return SetValue(St, const_cast<Stmt*>(S), V);
200 }
201
Ted Kremenekcba2e432008-02-05 19:35:18 +0000202 /// SetValue - This version of SetValue is used to batch process a set
203 /// of different possible RValues and return a set of different states.
204 const StateTy::BufferTy& SetValue(StateTy St, Stmt* S,
205 const RValue::BufferTy& V,
206 StateTy::BufferTy& RetBuf);
207
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000208 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000209
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000210 inline RValue GetValue(const StateTy& St, Stmt* S) {
211 return StateMgr.GetValue(St, S);
212 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000213
214 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
215 return StateMgr.GetValue(St, S, &hasVal);
216 }
217
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000218 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000219 return GetValue(St, const_cast<Stmt*>(S));
220 }
221
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000222 inline RValue GetValue(const StateTy& St, const LValue& LV,
223 QualType* T = NULL) {
224
225 return StateMgr.GetValue(St, LV, T);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000226 }
227
228 inline LValue GetLValue(const StateTy& St, Stmt* S) {
229 return StateMgr.GetLValue(St, S);
230 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000231
232 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
233 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
234 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000235
236 /// Assume - Create new state by assuming that a given expression
237 /// is true or false.
238 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
239 bool& isFeasible) {
240 if (isa<LValue>(Cond))
241 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
242 else
243 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
244 }
245
246 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
247 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000248
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000249 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
250 bool& isFeasible);
251
252 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
253 bool& isFeasible);
254
Ted Kremenek08b66252008-02-06 04:31:33 +0000255 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
256 bool& isFeasible);
257
Ted Kremenek63a4f692008-02-07 06:04:18 +0000258 NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St,
259 bool AlwaysMakeNode = false);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000260
Ted Kremenekcba2e432008-02-05 19:35:18 +0000261 /// Nodify - This version of Nodify is used to batch process a set of states.
262 /// The states are not guaranteed to be unique.
263 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
264
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000265 /// Visit - Transfer function logic for all statements. Dispatches to
266 /// other functions that handle specific kinds of statements.
267 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000268
269 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
270 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000271
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000272 /// VisitUnaryOperator - Transfer function logic for unary operators.
273 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
274
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000275 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000276 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
277
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000278 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000279
280 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
281 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
282
Ted Kremenek9de04c42008-01-24 20:55:43 +0000283 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000284 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
285
286 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
287 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
288 NodeTy* Pred, NodeSet& Dst);
289
290 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
291 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000292};
293} // end anonymous namespace
294
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000295
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000296GRConstants::StateTy
297GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000298
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000299 if (!StateCleaned) {
300 St = RemoveDeadBindings(CurrentStmt, St);
301 StateCleaned = true;
302 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000303
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000304 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000305
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000306 if (S == CurrentStmt) {
307 isBlkExpr = getCFG().isBlkExpr(S);
308
309 if (!isBlkExpr)
310 return St;
311 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000312
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000313 return StateMgr.SetValue(St, S, isBlkExpr, V);
314}
315
Ted Kremenekcba2e432008-02-05 19:35:18 +0000316const GRConstants::StateTy::BufferTy&
317GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
318 StateTy::BufferTy& RetBuf) {
319
320 assert (RetBuf.empty());
321
322 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
323 RetBuf.push_back(SetValue(St, S, *I));
324
325 return RetBuf;
326}
327
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000328GRConstants::StateTy
329GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
330
331 if (!LV.isValid())
332 return St;
333
334 if (!StateCleaned) {
335 St = RemoveDeadBindings(CurrentStmt, St);
336 StateCleaned = true;
337 }
338
339 return StateMgr.SetValue(St, LV, V);
340}
341
Ted Kremenekf233d482008-02-05 00:26:40 +0000342void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000343 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000344
345 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000346
Ted Kremenekb38911f2008-01-30 23:03:39 +0000347 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000348 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000349 if (I.getKey().isSubExpr())
350 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000351
Ted Kremenekf233d482008-02-05 00:26:40 +0000352 // Remove terminator-specific bindings.
353 switch (Term->getStmtClass()) {
354 default: break;
355
356 case Stmt::BinaryOperatorClass: { // '&&', '||'
357 BinaryOperator* B = cast<BinaryOperator>(Term);
358 // FIXME: Liveness analysis should probably remove these automatically.
359 // Verify later when we converge to an 'optimization' stage.
360 PrevState = StateMgr.Remove(PrevState, B->getRHS());
361 break;
362 }
363
364 case Stmt::ConditionalOperatorClass: { // '?' operator
365 ConditionalOperator* C = cast<ConditionalOperator>(Term);
366 // FIXME: Liveness analysis should probably remove these automatically.
367 // Verify later when we converge to an 'optimization' stage.
368 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
369 PrevState = StateMgr.Remove(PrevState, C->getRHS());
370 break;
371 }
372
373 case Stmt::ChooseExprClass: { // __builtin_choose_expr
374 ChooseExpr* C = cast<ChooseExpr>(Term);
375 // FIXME: Liveness analysis should probably remove these automatically.
376 // Verify later when we converge to an 'optimization' stage.
377 PrevState = StateMgr.Remove(PrevState, C->getRHS());
378 PrevState = StateMgr.Remove(PrevState, C->getRHS());
379 break;
380 }
381 }
382
Ted Kremenekb38911f2008-01-30 23:03:39 +0000383 RValue V = GetValue(PrevState, Condition);
384
385 switch (V.getBaseKind()) {
386 default:
387 break;
388
389 case RValue::InvalidKind:
390 builder.generateNode(PrevState, true);
391 builder.generateNode(PrevState, false);
392 return;
393
394 case RValue::UninitializedKind: {
395 NodeTy* N = builder.generateNode(PrevState, true);
396
397 if (N) {
398 N->markAsSink();
399 UninitBranches.insert(N);
400 }
401
402 builder.markInfeasible(false);
403 return;
404 }
405 }
406
407 // Process the true branch.
408 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000409
Ted Kremenekb38911f2008-01-30 23:03:39 +0000410 StateTy St = Assume(PrevState, V, true, isFeasible);
411
Ted Kremenekf233d482008-02-05 00:26:40 +0000412 if (isFeasible)
413 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000414 else {
415 builder.markInfeasible(true);
416 isFeasible = true;
417 }
418
419 // Process the false branch.
420 St = Assume(PrevState, V, false, isFeasible);
421
Ted Kremenekf233d482008-02-05 00:26:40 +0000422 if (isFeasible)
423 builder.generateNode(St, false);
424 else
425 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000426}
427
Ted Kremenekf233d482008-02-05 00:26:40 +0000428
429void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
430 NodeSet& Dst) {
431
432 bool hasR2;
433 StateTy PrevState = Pred->getState();
434
435 RValue R1 = GetValue(PrevState, B->getLHS());
436 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
437
438 if (isa<InvalidValue>(R1) &&
439 (isa<InvalidValue>(R2) ||
440 isa<UninitializedValue>(R2))) {
441
442 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
443 return;
444 }
445 else if (isa<UninitializedValue>(R1)) {
446 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
447 return;
448 }
449
450 // R1 is an expression that can evaluate to either 'true' or 'false'.
451 if (B->getOpcode() == BinaryOperator::LAnd) {
452 // hasR2 == 'false' means that LHS evaluated to 'false' and that
453 // we short-circuited, leading to a value of '0' for the '&&' expression.
454 if (hasR2 == false) {
455 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
456 return;
457 }
458 }
459 else {
460 assert (B->getOpcode() == BinaryOperator::LOr);
461 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
462 // we short-circuited, leading to a value of '1' for the '||' expression.
463 if (hasR2 == false) {
464 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
465 return;
466 }
467 }
468
469 // If we reach here we did not short-circuit. Assume R2 == true and
470 // R2 == false.
471
472 bool isFeasible;
473 StateTy St = Assume(PrevState, R2, true, isFeasible);
474
475 if (isFeasible)
476 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
477
478 St = Assume(PrevState, R2, false, isFeasible);
479
480 if (isFeasible)
481 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
482}
483
484
485
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000486void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000487 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000488
489 StmtEntryNode = builder.getLastNode();
490 CurrentStmt = S;
491 NodeSet Dst;
492 StateCleaned = false;
493
494 Visit(S, StmtEntryNode, Dst);
495
496 // If no nodes were generated, generate a new node that has all the
497 // dead mappings removed.
498 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
499 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
500 builder.generateNode(S, St, StmtEntryNode);
501 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000502
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000503 CurrentStmt = NULL;
504 StmtEntryNode = NULL;
505 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000506}
507
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000508GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000509
510 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
511 // The roots are any Block-level exprs and Decls that our liveness algorithm
512 // tells us are live. We then see what Decls they may reference, and keep
513 // those around. This code more than likely can be made faster, and the
514 // frequency of which this method is called should be experimented with
515 // for optimum performance.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000516
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000517 llvm::SmallVector<ValueDecl*, 10> WList;
Ted Kremenekf84469b2008-01-18 00:41:32 +0000518
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000519 for (StateTy::vb_iterator I = M.begin(), E = M.end();
520 I!=E && !I.getKey().isSymbol(); ++I) {
521
522 // Remove old bindings for subexpressions.
523 if (I.getKey().isSubExpr()) {
Ted Kremenek65cac132008-01-29 05:25:31 +0000524 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000525 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000526 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000527
528 if (I.getKey().isBlkExpr()) {
529 if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) {
530 if (isa<lval::DeclVal>(I.getData())) {
531 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
532 WList.push_back(LV.getDecl());
533 }
534 }
535 else
536 M = StateMgr.Remove(M, I.getKey());
537
538 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000539 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000540
541 assert (I.getKey().isDecl());
542
543 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
544 if (Liveness.isLive(Loc, V))
545 WList.push_back(V);
Ted Kremenek65cac132008-01-29 05:25:31 +0000546 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000547
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000548 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
549
550 while (!WList.empty()) {
551 ValueDecl* V = WList.back();
552 WList.pop_back();
553
554 if (Marked.count(V))
555 continue;
556
557 Marked.insert(V);
558
559 if (V->getType()->isPointerType()) {
560 const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V)));
561
562 if (!isa<lval::DeclVal>(LV))
563 continue;
564
565 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
566 WList.push_back(LVD.getDecl());
567 }
568 }
569
570 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I)
571 if (I.getKey().isDecl())
572 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
573 if (!Marked.count(V))
574 M = StateMgr.Remove(M, V);
575
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000576 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000577}
578
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000579GRConstants::NodeTy*
Ted Kremenek63a4f692008-02-07 06:04:18 +0000580GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St,
581 bool AlwaysMakeNode) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000582
583 // If the state hasn't changed, don't generate a new node.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000584 if (!AlwaysMakeNode && St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000585 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000586
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000587 NodeTy* N = Builder->generateNode(S, St, Pred);
588 Dst.Add(N);
589 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000590}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000591
Ted Kremenekcba2e432008-02-05 19:35:18 +0000592void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
593 const StateTy::BufferTy& SB) {
594
595 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
596 Nodify(Dst, S, Pred, *I);
597}
598
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000599void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
600 if (D != CurrentStmt) {
601 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
602 return;
603 }
604
605 // If we are here, we are loading the value of the decl and binding
606 // it to the block-level expression.
607
608 StateTy St = Pred->getState();
609
610 Nodify(Dst, D, Pred,
611 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
612}
613
Ted Kremenekcba2e432008-02-05 19:35:18 +0000614void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000615
616 QualType T = CastE->getType();
617
618 // Check for redundant casts.
619 if (E->getType() == T) {
620 Dst.Add(Pred);
621 return;
622 }
623
624 NodeSet S1;
625 Visit(E, Pred, S1);
626
627 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
628 NodeTy* N = *I1;
629 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000630 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000631 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000632 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000633}
634
635void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
636 GRConstants::NodeSet& Dst) {
637
638 StateTy St = Pred->getState();
639
640 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000641 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
642 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000643 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek403c1812008-01-28 22:51:57 +0000644 E ? GetValue(St, E) : UninitializedValue());
645 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000646
647 Nodify(Dst, DS, Pred, St);
648
649 if (Dst.empty())
650 Dst.Add(Pred);
651}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000652
Ted Kremenekf233d482008-02-05 00:26:40 +0000653
654void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
655 NodeTy* Pred, NodeSet& Dst) {
656
657 StateTy St = Pred->getState();
658
659 RValue R = GetValue(St, LHS);
660 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
661
662 Nodify(Dst, S, Pred, SetValue(St, S, R));
663}
664
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000665void GRConstants::VisitUnaryOperator(UnaryOperator* U,
666 GRConstants::NodeTy* Pred,
667 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000668
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000669 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000670 UnaryOperator::Opcode Op = U->getOpcode();
671
672 // FIXME: This is a hack so that for '*' and '&' we don't recurse
673 // on visiting the subexpression if it is a DeclRefExpr. We should
674 // probably just handle AddrOf and Deref in their own methods to make
675 // this cleaner.
676 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
677 isa<DeclRefExpr>(U->getSubExpr()))
678 S1.Add(Pred);
679 else
680 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000681
682 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
683 NodeTy* N1 = *I1;
684 StateTy St = N1->getState();
685
686 switch (U->getOpcode()) {
687 case UnaryOperator::PostInc: {
688 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000689 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000690
691 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
692 GetRValueConstant(1U, U));
693
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000694 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
695 break;
696 }
697
698 case UnaryOperator::PostDec: {
699 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000700 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000701
702 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
703 GetRValueConstant(1U, U));
704
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000705 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
706 break;
707 }
708
709 case UnaryOperator::PreInc: {
710 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000711 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000712
713 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
714 GetRValueConstant(1U, U));
715
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000716 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
717 break;
718 }
719
720 case UnaryOperator::PreDec: {
721 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000722 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000723
724 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
725 GetRValueConstant(1U, U));
726
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000727 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
728 break;
729 }
730
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000731 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000732 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000733 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000734 break;
735 }
736
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000737 case UnaryOperator::Not: {
738 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000739 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000740 break;
741 }
742
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000743 case UnaryOperator::LNot: {
744 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
745 //
746 // Note: technically we do "E == 0", but this is the same in the
747 // transfer functions as "0 == E".
748
749 RValue V1 = GetValue(St, U->getSubExpr());
750
751 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000752 const LValue& L1 = cast<LValue>(V1);
753 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
754 Nodify(Dst, U, N1,
755 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
756 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000757 }
758 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000759 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000760 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000761 Nodify(Dst, U, N1,
762 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
763 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000764 }
765
766 break;
767 }
768
Ted Kremenek64924852008-01-31 02:35:41 +0000769 case UnaryOperator::AddrOf: {
770 const LValue& L1 = GetLValue(St, U->getSubExpr());
771 Nodify(Dst, U, N1, SetValue(St, U, L1));
772 break;
773 }
774
775 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000776 // FIXME: Stop when dereferencing an uninitialized value.
777 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
778
779 const RValue& V = GetValue(St, U->getSubExpr());
780 const LValue& L1 = cast<LValue>(V);
781
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000782 // After a dereference, one of two possible situations arise:
783 // (1) A crash, because the pointer was NULL.
784 // (2) The pointer is not NULL, and the dereference works.
785 //
786 // We add these assumptions.
787
Ted Kremenek63a4f692008-02-07 06:04:18 +0000788 bool isFeasibleNotNull;
789
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000790 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000791 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
792
793 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000794 QualType T = U->getType();
795 Nodify(Dst, U, N1, SetValue(StNotNull, U,
796 GetValue(StNotNull, L1, &T)));
797 }
798
Ted Kremenek63a4f692008-02-07 06:04:18 +0000799 bool isFeasibleNull;
800
801 // "Assume" that the pointer is NULL.
802 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
803
804 if (isFeasibleNull) {
805 NodeTy* NullNode = Nodify(Dst, U, N1, StNull, true);
806 if (NullNode) {
807 NullNode->markAsSink();
808
809 if (isFeasibleNotNull)
810 ImplicitNullDeref.insert(NullNode);
811 else
812 ExplicitNullDeref.insert(NullNode);
813 }
814 }
815
Ted Kremenek64924852008-01-31 02:35:41 +0000816 break;
817 }
818
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000819 default: ;
820 assert (false && "Not implemented.");
821 }
822 }
823}
824
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000825void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
826 GRConstants::NodeSet& Dst) {
827
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000828 if (isa<DeclRefExpr>(E)) {
829 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000830 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000831 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000832
833 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
834 if (U->getOpcode() == UnaryOperator::Deref) {
835 Visit(U->getSubExpr(), Pred, Dst);
836 return;
837 }
838 }
839
840 Visit(E, Pred, Dst);
841}
842
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000843void GRConstants::VisitBinaryOperator(BinaryOperator* B,
844 GRConstants::NodeTy* Pred,
845 GRConstants::NodeSet& Dst) {
846 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000847
848 if (B->isAssignmentOp())
849 VisitAssignmentLHS(B->getLHS(), Pred, S1);
850 else
851 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000852
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000853 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
854 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000855
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000856 // When getting the value for the LHS, check if we are in an assignment.
857 // In such cases, we want to (initially) treat the LHS as an LValue,
858 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000859 // evaluated to LValueDecl's instead of to an NonLValue.
860 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
862 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000863
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000864 NodeSet S2;
865 Visit(B->getRHS(), N1, S2);
866
867 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000868
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000869 NodeTy* N2 = *I2;
870 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000871 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000872
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000873 BinaryOperator::Opcode Op = B->getOpcode();
874
875 if (Op <= BinaryOperator::Or) {
876
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000877 if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) {
878 Nodify(Dst, B, N2, SetValue(St, B, V1));
879 continue;
880 }
881
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000882 if (isa<LValue>(V1)) {
883 // FIXME: Add support for RHS being a non-lvalue.
884 const LValue& L1 = cast<LValue>(V1);
885 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000886
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000887 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
888 }
889 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000890 const NonLValue& R1 = cast<NonLValue>(V1);
891 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000892
893 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000894 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000895
896 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000897
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000898 }
899
900 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000901 case BinaryOperator::Assign: {
902 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000903 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000904 break;
905 }
906
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000907 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000908
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000909 assert (B->isCompoundAssignmentOp());
910
911 const LValue& L1 = cast<LValue>(V1);
912 RValue Result = cast<NonLValue>(InvalidValue());
913
914 Op = (BinaryOperator::Opcode)
915 (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign));
916
917 if (isa<LValue>(V2)) {
918 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000919 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000920 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000921 }
922 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000923 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000924 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000925 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000926 }
927
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000928 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000929 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000930 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000931 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000932 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000933 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000934}
Ted Kremenekee985462008-01-16 18:18:48 +0000935
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000936
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000937void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
938 GRConstants::NodeSet& Dst) {
939
940 // FIXME: add metadata to the CFG so that we can disable
941 // this check when we KNOW that there is no block-level subexpression.
942 // The motivation is that this check requires a hashtable lookup.
943
944 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
945 Dst.Add(Pred);
946 return;
947 }
948
949 switch (S->getStmtClass()) {
950 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000951
952 if (cast<BinaryOperator>(S)->isLogicalOp()) {
953 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
954 break;
955 }
956
957 // Fall-through.
958
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000959 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000960 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
961 break;
962
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000963 case Stmt::UnaryOperatorClass:
964 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
965 break;
966
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000967 case Stmt::ParenExprClass:
968 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
969 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000970
971 case Stmt::DeclRefExprClass:
972 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
973 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000974
Ted Kremenek874d63f2008-01-24 02:02:54 +0000975 case Stmt::ImplicitCastExprClass: {
976 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
977 VisitCast(C, C->getSubExpr(), Pred, Dst);
978 break;
979 }
980
981 case Stmt::CastExprClass: {
982 CastExpr* C = cast<CastExpr>(S);
983 VisitCast(C, C->getSubExpr(), Pred, Dst);
984 break;
985 }
986
Ted Kremenekf233d482008-02-05 00:26:40 +0000987 case Stmt::ConditionalOperatorClass: { // '?' operator
988 ConditionalOperator* C = cast<ConditionalOperator>(S);
989 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
990 break;
991 }
992
993 case Stmt::ChooseExprClass: { // __builtin_choose_expr
994 ChooseExpr* C = cast<ChooseExpr>(S);
995 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
996 break;
997 }
998
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000999 case Stmt::ReturnStmtClass:
1000 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1001 Visit(R, Pred, Dst);
1002 else
1003 Dst.Add(Pred);
1004
1005 break;
1006
Ted Kremenek9de04c42008-01-24 20:55:43 +00001007 case Stmt::DeclStmtClass:
1008 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1009 break;
1010
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001011 default:
1012 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1013 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001014 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001015}
1016
Ted Kremenekee985462008-01-16 18:18:48 +00001017//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001018// "Assume" logic.
1019//===----------------------------------------------------------------------===//
1020
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001021GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
1022 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001023 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001024
1025 switch (Cond.getSubKind()) {
1026 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001027 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001028 return St;
1029
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001030 case lval::SymbolValKind:
1031 if (Assumption)
1032 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1033 ValMgr.getZeroWithPtrWidth(), isFeasible);
1034 else
1035 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1036 ValMgr.getZeroWithPtrWidth(), isFeasible);
1037
Ted Kremenek08b66252008-02-06 04:31:33 +00001038
Ted Kremenek329f8542008-02-05 21:52:21 +00001039 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001040 isFeasible = Assumption;
1041 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001042
Ted Kremenek329f8542008-02-05 21:52:21 +00001043 case lval::ConcreteIntKind: {
1044 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001045 isFeasible = b ? Assumption : !Assumption;
1046 return St;
1047 }
1048 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001049}
1050
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001051GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
1052 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001053 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001054
1055 switch (Cond.getSubKind()) {
1056 default:
1057 assert (false && "'Assume' not implemented for this NonLValue.");
1058 return St;
1059
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001060
1061 case nonlval::SymbolValKind: {
1062 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1063 SymbolID sym = SV.getSymbol();
1064
1065 if (Assumption)
1066 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1067 isFeasible);
1068 else
1069 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1070 isFeasible);
1071 }
1072
Ted Kremenek08b66252008-02-06 04:31:33 +00001073 case nonlval::SymIntConstraintValKind:
1074 return
1075 AssumeSymInt(St, Assumption,
1076 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1077 isFeasible);
1078
Ted Kremenek329f8542008-02-05 21:52:21 +00001079 case nonlval::ConcreteIntKind: {
1080 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001081 isFeasible = b ? Assumption : !Assumption;
1082 return St;
1083 }
1084 }
1085}
1086
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001087GRConstants::StateTy
1088GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1089 const llvm::APSInt& V, bool& isFeasible) {
1090
1091 // First, determine if sym == X, where X != V.
1092 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1093 isFeasible = *X != V;
1094 return St;
1095 }
1096
1097 // Second, determine if sym != V.
1098 if (St.isNotEqual(sym, V)) {
1099 isFeasible = true;
1100 return St;
1101 }
1102
1103 // If we reach here, sym is not a constant and we don't know if it is != V.
1104 // Make that assumption.
1105
1106 isFeasible = true;
1107 return StateMgr.AddNE(St, sym, V);
1108}
1109
1110GRConstants::StateTy
1111GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1112 const llvm::APSInt& V, bool& isFeasible) {
1113
1114 // First, determine if sym == X, where X != V.
1115 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1116 isFeasible = *X == V;
1117 return St;
1118 }
1119
1120 // Second, determine if sym != V.
1121 if (St.isNotEqual(sym, V)) {
1122 isFeasible = false;
1123 return St;
1124 }
1125
1126 // If we reach here, sym is not a constant and we don't know if it is == V.
1127 // Make that assumption.
1128
1129 isFeasible = true;
1130 return StateMgr.AddEQ(St, sym, V);
1131}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001132
Ted Kremenek08b66252008-02-06 04:31:33 +00001133GRConstants::StateTy
1134GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1135 const SymIntConstraint& C, bool& isFeasible) {
1136
1137 switch (C.getOpcode()) {
1138 default:
1139 // No logic yet for other operators.
1140 return St;
1141
1142 case BinaryOperator::EQ:
1143 if (Assumption)
1144 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1145 else
1146 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1147
1148 case BinaryOperator::NE:
1149 if (Assumption)
1150 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1151 else
1152 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1153 }
1154}
1155
Ted Kremenekb38911f2008-01-30 23:03:39 +00001156//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001157// Driver.
1158//===----------------------------------------------------------------------===//
1159
Ted Kremenekaa66a322008-01-16 21:46:15 +00001160#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001161static GRConstants* GraphPrintCheckerState;
1162
Ted Kremenekaa66a322008-01-16 21:46:15 +00001163namespace llvm {
1164template<>
1165struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1166 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001167
Ted Kremenek9153f732008-02-05 07:17:49 +00001168 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001169 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001170 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1171 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1172 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1173 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001174 }
1175 }
1176
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001177 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001178 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001179 bool isFirst = true;
1180
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001181 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001182 if (I.getKey().getKind() != kind)
1183 continue;
1184
1185 if (isFirst) {
1186 if (!isFirstGroup) Out << "\\l\\l";
1187 PrintKindLabel(Out, kind);
1188 isFirst = false;
1189 }
1190 else
1191 Out << "\\l";
1192
1193 Out << ' ';
1194
1195 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1196 Out << V->getName();
1197 else {
1198 Stmt* E = cast<Stmt>(I.getKey());
1199 Out << " (" << (void*) E << ") ";
1200 E->printPretty(Out);
1201 }
1202
1203 Out << " : ";
1204 I.getData().print(Out);
1205 }
1206 }
1207
Ted Kremeneked4de312008-02-06 03:56:15 +00001208 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1209 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1210
1211 if (CE.isEmpty())
1212 return;
1213
1214 Out << "\\l\\|'==' constraints:";
1215
1216 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1217 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1218 }
1219
1220 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1221 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1222
1223 if (NE.isEmpty())
1224 return;
1225
1226 Out << "\\l\\|'!=' constraints:";
1227
1228 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1229 I != EI; ++I){
1230
1231 Out << "\\l $" << I.getKey() << " : ";
1232 bool isFirst = true;
1233
1234 ValueState::IntSetTy::iterator J=I.getData().begin(),
1235 EJ=I.getData().end();
1236 for ( ; J != EJ; ++J) {
1237 if (isFirst) isFirst = false;
1238 else Out << ", ";
1239
1240 Out << (*J)->toString();
1241 }
1242 }
1243 }
1244
Ted Kremenekaa66a322008-01-16 21:46:15 +00001245 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1246 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001247
1248 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001249 ProgramPoint Loc = N->getLocation();
1250
1251 switch (Loc.getKind()) {
1252 case ProgramPoint::BlockEntranceKind:
1253 Out << "Block Entrance: B"
1254 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1255 break;
1256
1257 case ProgramPoint::BlockExitKind:
1258 assert (false);
1259 break;
1260
1261 case ProgramPoint::PostStmtKind: {
1262 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001263 Out << L.getStmt()->getStmtClassName() << ':'
1264 << (void*) L.getStmt() << ' ';
1265
Ted Kremenekaa66a322008-01-16 21:46:15 +00001266 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001267
1268 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1269 Out << "\\|Implicit-Null Dereference.\\l";
1270 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001271 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1272 Out << "\\|Explicit-Null Dereference.\\l";
1273 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001274
Ted Kremenekaa66a322008-01-16 21:46:15 +00001275 break;
1276 }
1277
1278 default: {
1279 const BlockEdge& E = cast<BlockEdge>(Loc);
1280 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1281 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001282
1283 if (Stmt* T = E.getSrc()->getTerminator()) {
1284 Out << "\\|Terminator: ";
1285 E.getSrc()->printTerminator(Out);
1286
1287 if (isa<SwitchStmt>(T)) {
1288 // FIXME
1289 }
1290 else {
1291 Out << "\\lCondition: ";
1292 if (*E.getSrc()->succ_begin() == E.getDst())
1293 Out << "true";
1294 else
1295 Out << "false";
1296 }
1297
1298 Out << "\\l";
1299 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001300
1301 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1302 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1303 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001304 }
1305 }
1306
Ted Kremenek9153f732008-02-05 07:17:49 +00001307 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001308
Ted Kremenek9153f732008-02-05 07:17:49 +00001309 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1310 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1311 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001312
1313 PrintEQ(Out, N->getState());
1314 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001315
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001316 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001317 return Out.str();
1318 }
1319};
1320} // end llvm namespace
1321#endif
1322
Ted Kremenekee985462008-01-16 18:18:48 +00001323namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001324void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1325 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001326 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001327#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001328 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001329 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001330 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001331#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001332}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001333} // end clang namespace