blob: 2229e473efc2efcbf80028889d89ffcb21c6f91e [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 Kremeneka90ccfe2008-01-31 19:34:24 +000066 typedef ValueState 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 Kremenekd27f8162008-01-15 23:55:06 +000094
95protected:
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 Kremeneka90ccfe2008-01-31 19:34:24 +0000108 StateTy::Factory 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 Kremenekab2b8c52008-01-23 19:59:44 +0000111 ValueManager ValMgr;
112
Ted Kremenek68fd2572008-01-29 17:27:31 +0000113 /// SymMgr - Object that manages the symbol information.
114 SymbolManager SymMgr;
115
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 Kremenekab2b8c52008-01-23 19:59:44 +0000127 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000128
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000129 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000130
Ted Kremenekd27f8162008-01-15 23:55:06 +0000131public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000132 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
133 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
134 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000135
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000136 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000137 Liveness.runOnCFG(G.getCFG());
138 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000139 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000140
141 /// getCFG - Returns the CFG associated with this analysis.
142 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000143
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000144 /// getInitialState - Return the initial state used for the root vertex
145 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000147 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000148
149 // Iterate the parameters.
150 FunctionDecl& F = G.getFunctionDecl();
151
152 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000153 I!=E; ++I)
154 St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000155
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000156 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000157 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000158
159 bool isUninitControlFlow(const NodeTy* N) const {
160 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
161 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000162
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000163 /// ProcessStmt - Called by GREngine. Used to generate new successor
164 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000165 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
166
167 /// ProcessBranch - Called by GREngine. Used to generate successor
168 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000169 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000170
171 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
172 /// that all subexpression mappings are removed and that any
173 /// block-level expressions that are not live at 'S' also have their
174 /// mappings removed.
175 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
176
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000177 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000178
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000179 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000180 return SetValue(St, const_cast<Stmt*>(S), V);
181 }
182
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000183 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000184
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000185 RValue GetValue(const StateTy& St, Stmt* S);
186 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000187 return GetValue(St, const_cast<Stmt*>(S));
188 }
189
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000190 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000191 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000192
193 /// Assume - Create new state by assuming that a given expression
194 /// is true or false.
195 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
196 bool& isFeasible) {
197 if (isa<LValue>(Cond))
198 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
199 else
200 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
201 }
202
203 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
204 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000205
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000206 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000207
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000208 /// Visit - Transfer function logic for all statements. Dispatches to
209 /// other functions that handle specific kinds of statements.
210 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000211
212 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
213 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000214
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000215 /// VisitUnaryOperator - Transfer function logic for unary operators.
216 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
217
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000218 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000219 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
220
221 /// VisitDeclStmt - Transfer function logic for DeclStmts.
222 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000223};
224} // end anonymous namespace
225
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000226
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000227void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
228 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000229
230 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000231
Ted Kremenekb38911f2008-01-30 23:03:39 +0000232 // Remove old bindings for subexpressions.
233 for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
234 if (I.getKey().isSubExpr())
235 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000236
Ted Kremenekb38911f2008-01-30 23:03:39 +0000237 RValue V = GetValue(PrevState, Condition);
238
239 switch (V.getBaseKind()) {
240 default:
241 break;
242
243 case RValue::InvalidKind:
244 builder.generateNode(PrevState, true);
245 builder.generateNode(PrevState, false);
246 return;
247
248 case RValue::UninitializedKind: {
249 NodeTy* N = builder.generateNode(PrevState, true);
250
251 if (N) {
252 N->markAsSink();
253 UninitBranches.insert(N);
254 }
255
256 builder.markInfeasible(false);
257 return;
258 }
259 }
260
261 // Process the true branch.
262 bool isFeasible = true;
263 StateTy St = Assume(PrevState, V, true, isFeasible);
264
265 if (isFeasible) builder.generateNode(St, true);
266 else {
267 builder.markInfeasible(true);
268 isFeasible = true;
269 }
270
271 // Process the false branch.
272 St = Assume(PrevState, V, false, isFeasible);
273
274 if (isFeasible) builder.generateNode(St, false);
275 else builder.markInfeasible(false);
276
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000277}
278
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000279void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000280 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000281
282 StmtEntryNode = builder.getLastNode();
283 CurrentStmt = S;
284 NodeSet Dst;
285 StateCleaned = false;
286
287 Visit(S, StmtEntryNode, Dst);
288
289 // If no nodes were generated, generate a new node that has all the
290 // dead mappings removed.
291 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
292 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
293 builder.generateNode(S, St, StmtEntryNode);
294 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000295
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000296 CurrentStmt = NULL;
297 StmtEntryNode = NULL;
298 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000299}
300
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000301
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000302RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000303 switch (LV.getSubKind()) {
304 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000305 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
306 return T ? T->getValue().second : InvalidValue();
307 }
308 default:
309 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000310 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000311 }
312
313 return InvalidValue();
314}
315
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000316RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +0000317 for (;;) {
318 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000319
320 // ParenExprs are no-ops.
321
Ted Kremenek671c9e82008-01-24 00:50:08 +0000322 case Stmt::ParenExprClass:
323 S = cast<ParenExpr>(S)->getSubExpr();
324 continue;
325
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000326 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
327 // (assuming an implicit "load") depending on the context. In this
328 // context we assume that we are retrieving the value contained
329 // within the referenced variables.
330
Ted Kremenek671c9e82008-01-24 00:50:08 +0000331 case Stmt::DeclRefExprClass:
332 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +0000333
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000334 // Integer literals evaluate to an RValue. Simply retrieve the
335 // RValue for the literal.
336
Ted Kremenek671c9e82008-01-24 00:50:08 +0000337 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000338 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
339
340 // Casts where the source and target type are the same
341 // are no-ops. We blast through these to get the descendant
342 // subexpression that has a value.
343
Ted Kremenek874d63f2008-01-24 02:02:54 +0000344 case Stmt::ImplicitCastExprClass: {
345 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
346 if (C->getType() == C->getSubExpr()->getType()) {
347 S = C->getSubExpr();
348 continue;
349 }
350 break;
351 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000352
Ted Kremenek874d63f2008-01-24 02:02:54 +0000353 case Stmt::CastExprClass: {
354 CastExpr* C = cast<CastExpr>(S);
355 if (C->getType() == C->getSubExpr()->getType()) {
356 S = C->getSubExpr();
357 continue;
358 }
359 break;
360 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000361
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000362 // Handle all other Stmt* using a lookup.
363
Ted Kremenek671c9e82008-01-24 00:50:08 +0000364 default:
365 break;
366 };
367
368 break;
369 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000370
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000371 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000372
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000373 return T ? T->getValue().second : InvalidValue();
374}
375
376LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000377 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
378 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000379
380 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
381 return LValueDecl(DR->getDecl());
382
383 return cast<LValue>(GetValue(St, S));
384}
385
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000386
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000387GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000388 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000389 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000390
391 if (!StateCleaned) {
392 St = RemoveDeadBindings(CurrentStmt, St);
393 StateCleaned = true;
394 }
395
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000396 bool isBlkExpr = false;
397
398 if (S == CurrentStmt) {
399 isBlkExpr = getCFG().isBlkExpr(S);
400
401 if (!isBlkExpr)
402 return St;
403 }
Ted Kremenekdaadf452008-01-24 19:28:01 +0000404
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000405 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
406 : St;
407}
408
409GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000410 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000411 if (!LV.isValid())
412 return St;
413
414 if (!StateCleaned) {
415 St = RemoveDeadBindings(CurrentStmt, St);
416 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +0000417 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +0000418
Ted Kremenekf13794e2008-01-24 23:19:54 +0000419 switch (LV.getSubKind()) {
420 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000421 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
422 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
423
424 default:
425 assert ("SetValue for given LValue type not yet implemented.");
426 return St;
427 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000428}
429
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000430GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000431 // Note: in the code below, we can assign a new map to M since the
432 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000433 StateTy::iterator I = M.begin(), E = M.end();
434
Ted Kremenekf84469b2008-01-18 00:41:32 +0000435
Ted Kremenek65cac132008-01-29 05:25:31 +0000436 for (; I!=E && !I.getKey().isSymbol(); ++I) {
437 // Remove old bindings for subexpressions and "dead"
438 // block-level expressions.
439 if (I.getKey().isSubExpr() ||
440 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
441 M = StateMgr.Remove(M, I.getKey());
442 }
443 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
444 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
445 if (!Liveness.isLive(Loc, V))
446 M = StateMgr.Remove(M, I.getKey());
447 }
448 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000449
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000450 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000451}
452
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000453void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
454 GRConstants::StateTy St) {
455
456 // If the state hasn't changed, don't generate a new node.
457 if (St == Pred->getState())
458 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000459
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000460 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000461}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000462
Ted Kremenek874d63f2008-01-24 02:02:54 +0000463void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
464 GRConstants::NodeSet& Dst) {
465
466 QualType T = CastE->getType();
467
468 // Check for redundant casts.
469 if (E->getType() == T) {
470 Dst.Add(Pred);
471 return;
472 }
473
474 NodeSet S1;
475 Visit(E, Pred, S1);
476
477 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
478 NodeTy* N = *I1;
479 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000480 const RValue& V = GetValue(St, E);
481 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000482 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000483}
484
485void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
486 GRConstants::NodeSet& Dst) {
487
488 StateTy St = Pred->getState();
489
490 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000491 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
492 const Expr* E = VD->getInit();
493 St = SetValue(St, LValueDecl(VD),
494 E ? GetValue(St, E) : UninitializedValue());
495 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000496
497 Nodify(Dst, DS, Pred, St);
498
499 if (Dst.empty())
500 Dst.Add(Pred);
501}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000502
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000503void GRConstants::VisitUnaryOperator(UnaryOperator* U,
504 GRConstants::NodeTy* Pred,
505 GRConstants::NodeSet& Dst) {
506 NodeSet S1;
507 Visit(U->getSubExpr(), Pred, S1);
508
509 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
510 NodeTy* N1 = *I1;
511 StateTy St = N1->getState();
512
513 switch (U->getOpcode()) {
514 case UnaryOperator::PostInc: {
515 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000516 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000517 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
518 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000519
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000520 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000521 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
522 break;
523 }
524
525 case UnaryOperator::PostDec: {
526 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000527 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000528 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
529 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000530
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000531 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000532 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
533 break;
534 }
535
536 case UnaryOperator::PreInc: {
537 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000538 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000539 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
540 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000541
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000542 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000543 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
544 break;
545 }
546
547 case UnaryOperator::PreDec: {
548 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000549 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000550 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
551 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000552
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000553 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000554 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
555 break;
556 }
557
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000558 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000559 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
560 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000561 break;
562 }
563
Ted Kremenek64924852008-01-31 02:35:41 +0000564 case UnaryOperator::AddrOf: {
565 const LValue& L1 = GetLValue(St, U->getSubExpr());
566 Nodify(Dst, U, N1, SetValue(St, U, L1));
567 break;
568 }
569
570 case UnaryOperator::Deref: {
571 const LValue& L1 = GetLValue(St, U->getSubExpr());
572 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
573 break;
574 }
575
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000576 default: ;
577 assert (false && "Not implemented.");
578 }
579 }
580}
581
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000582void GRConstants::VisitBinaryOperator(BinaryOperator* B,
583 GRConstants::NodeTy* Pred,
584 GRConstants::NodeSet& Dst) {
585 NodeSet S1;
586 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000587
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000588 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
589 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000590
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000591 // When getting the value for the LHS, check if we are in an assignment.
592 // In such cases, we want to (initially) treat the LHS as an LValue,
593 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000594 // evaluated to LValueDecl's instead of to an NonLValue.
595 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000596 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
597 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000598
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000599 NodeSet S2;
600 Visit(B->getRHS(), N1, S2);
601
602 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
603 NodeTy* N2 = *I2;
604 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000605 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000606
607 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +0000608 default:
609 Dst.Add(N2);
610 break;
611
612 // Arithmetic opreators.
613
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000614 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000615 const NonLValue& R1 = cast<NonLValue>(V1);
616 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000617
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000618 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000619 break;
620 }
621
622 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000623 const NonLValue& R1 = cast<NonLValue>(V1);
624 const NonLValue& R2 = cast<NonLValue>(V2);
625 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000626 break;
627 }
628
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000629 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000630 const NonLValue& R1 = cast<NonLValue>(V1);
631 const NonLValue& R2 = cast<NonLValue>(V2);
632 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000633 break;
634 }
635
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000636 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000637 const NonLValue& R1 = cast<NonLValue>(V1);
638 const NonLValue& R2 = cast<NonLValue>(V2);
639 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000640 break;
641 }
642
Ted Kremenekcce207d2008-01-28 22:26:15 +0000643 case BinaryOperator::Rem: {
644 const NonLValue& R1 = cast<NonLValue>(V1);
645 const NonLValue& R2 = cast<NonLValue>(V2);
646 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
647 break;
648 }
649
Ted Kremenek687af802008-01-29 19:43:15 +0000650 // Assignment operators.
651
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000652 case BinaryOperator::Assign: {
653 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000654 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000655 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
656 break;
657 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000658
659 case BinaryOperator::AddAssign: {
660 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000661 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
662 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000663 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
664 break;
665 }
666
667 case BinaryOperator::SubAssign: {
668 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000669 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
670 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000671 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
672 break;
673 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000674
675 case BinaryOperator::MulAssign: {
676 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000677 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
678 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +0000679 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
680 break;
681 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000682
683 case BinaryOperator::DivAssign: {
684 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000685 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
686 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +0000687 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
688 break;
689 }
Ted Kremenek10099a62008-01-28 22:28:54 +0000690
691 case BinaryOperator::RemAssign: {
692 const LValue& L1 = cast<LValue>(V1);
693 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
694 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
695 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
696 break;
697 }
Ted Kremenek687af802008-01-29 19:43:15 +0000698
699 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000700
Ted Kremenek687af802008-01-29 19:43:15 +0000701 case BinaryOperator::EQ:
702 // FIXME: should we allow XX.EQ() to return a set of values,
703 // allowing state bifurcation? In such cases, they will also
704 // modify the state (meaning that a new state will be returned
705 // as well).
706 assert (B->getType() == getContext().IntTy);
707
708 if (isa<LValue>(V1)) {
709 const LValue& L1 = cast<LValue>(V1);
710 const LValue& L2 = cast<LValue>(V2);
711 St = SetValue(St, B, L1.EQ(ValMgr, L2));
712 }
713 else {
714 const NonLValue& R1 = cast<NonLValue>(V1);
715 const NonLValue& R2 = cast<NonLValue>(V2);
716 St = SetValue(St, B, R1.EQ(ValMgr, R2));
717 }
718
719 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000720 break;
721 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000722 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000723 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000724}
Ted Kremenekee985462008-01-16 18:18:48 +0000725
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000726
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000727void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
728 GRConstants::NodeSet& Dst) {
729
730 // FIXME: add metadata to the CFG so that we can disable
731 // this check when we KNOW that there is no block-level subexpression.
732 // The motivation is that this check requires a hashtable lookup.
733
734 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
735 Dst.Add(Pred);
736 return;
737 }
738
739 switch (S->getStmtClass()) {
740 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000741 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000742 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
743 break;
744
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000745 case Stmt::UnaryOperatorClass:
746 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
747 break;
748
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000749 case Stmt::ParenExprClass:
750 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
751 break;
752
Ted Kremenek874d63f2008-01-24 02:02:54 +0000753 case Stmt::ImplicitCastExprClass: {
754 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
755 VisitCast(C, C->getSubExpr(), Pred, Dst);
756 break;
757 }
758
759 case Stmt::CastExprClass: {
760 CastExpr* C = cast<CastExpr>(S);
761 VisitCast(C, C->getSubExpr(), Pred, Dst);
762 break;
763 }
764
Ted Kremenek9de04c42008-01-24 20:55:43 +0000765 case Stmt::DeclStmtClass:
766 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
767 break;
768
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000769 default:
770 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
771 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000772 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000773}
774
Ted Kremenekee985462008-01-16 18:18:48 +0000775//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000776// "Assume" logic.
777//===----------------------------------------------------------------------===//
778
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000779GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
780 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000781 return St;
782}
783
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000784GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
785 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000786
787 switch (Cond.getSubKind()) {
788 default:
789 assert (false && "'Assume' not implemented for this NonLValue.");
790 return St;
791
792 case ConcreteIntKind: {
793 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
794 isFeasible = b ? Assumption : !Assumption;
795 return St;
796 }
797 }
798}
799
800
801//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +0000802// Driver.
803//===----------------------------------------------------------------------===//
804
Ted Kremenekaa66a322008-01-16 21:46:15 +0000805#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000806static GRConstants* GraphPrintCheckerState;
807
Ted Kremenekaa66a322008-01-16 21:46:15 +0000808namespace llvm {
809template<>
810struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
811 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000812
813 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000814 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +0000815 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000816 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
817 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
818 default: assert (false && "Unknown ValueKey type.");
819 }
820 }
821
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000822 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
823 ValueKey::Kind kind, bool isFirstGroup = false) {
824 bool isFirst = true;
825
826 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
827 if (I.getKey().getKind() != kind)
828 continue;
829
830 if (isFirst) {
831 if (!isFirstGroup) Out << "\\l\\l";
832 PrintKindLabel(Out, kind);
833 isFirst = false;
834 }
835 else
836 Out << "\\l";
837
838 Out << ' ';
839
840 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
841 Out << V->getName();
842 else {
843 Stmt* E = cast<Stmt>(I.getKey());
844 Out << " (" << (void*) E << ") ";
845 E->printPretty(Out);
846 }
847
848 Out << " : ";
849 I.getData().print(Out);
850 }
851 }
852
Ted Kremenekaa66a322008-01-16 21:46:15 +0000853 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
854 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000855
856 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +0000857 ProgramPoint Loc = N->getLocation();
858
859 switch (Loc.getKind()) {
860 case ProgramPoint::BlockEntranceKind:
861 Out << "Block Entrance: B"
862 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
863 break;
864
865 case ProgramPoint::BlockExitKind:
866 assert (false);
867 break;
868
869 case ProgramPoint::PostStmtKind: {
870 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000871 Out << L.getStmt()->getStmtClassName() << ':'
872 << (void*) L.getStmt() << ' ';
873
Ted Kremenekaa66a322008-01-16 21:46:15 +0000874 L.getStmt()->printPretty(Out);
875 break;
876 }
877
878 default: {
879 const BlockEdge& E = cast<BlockEdge>(Loc);
880 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
881 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +0000882
883 if (Stmt* T = E.getSrc()->getTerminator()) {
884 Out << "\\|Terminator: ";
885 E.getSrc()->printTerminator(Out);
886
887 if (isa<SwitchStmt>(T)) {
888 // FIXME
889 }
890 else {
891 Out << "\\lCondition: ";
892 if (*E.getSrc()->succ_begin() == E.getDst())
893 Out << "true";
894 else
895 Out << "false";
896 }
897
898 Out << "\\l";
899 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000900
901 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
902 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
903 }
Ted Kremenekaa66a322008-01-16 21:46:15 +0000904 }
905 }
906
Ted Kremenek64924852008-01-31 02:35:41 +0000907 Out << "\\|StateID: " << (void*) N->getState().getRoot() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +0000908
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000909 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
910 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +0000911 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000912
Ted Kremenek803c9ed2008-01-23 22:30:44 +0000913 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +0000914 return Out.str();
915 }
916};
917} // end llvm namespace
918#endif
919
Ted Kremenekee985462008-01-16 18:18:48 +0000920namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000921void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
922 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +0000923 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +0000924#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000925 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +0000926 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000927 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +0000928#endif
Ted Kremenekee985462008-01-16 18:18:48 +0000929}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000930} // end clang namespace