blob: 196c28b5cb1c8d3edcb1cf63c2af41cc562dd53c [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
2//
Ted Kremenekab2b8c52008-01-23 19:59:44 +00003// The LLValM 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
18#include "clang/Analysis/PathSensitive/GREngine.h"
19#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000020#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000022
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/DataTypes.h"
25#include "llvm/ADT/APSInt.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000028#include "llvm/ADT/SmallVector.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000029#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000030#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000031#include "llvm/Support/Streams.h"
32
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000033#include <functional>
34
Ted Kremenekaa66a322008-01-16 21:46:15 +000035#ifndef NDEBUG
36#include "llvm/Support/GraphWriter.h"
37#include <sstream>
38#endif
39
Ted Kremenekd27f8162008-01-15 23:55:06 +000040using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000041using llvm::dyn_cast;
42using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000043using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000044
45//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +000046/// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a
Ted Kremenekd27f8162008-01-15 23:55:06 +000047/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
48//===----------------------------------------------------------------------===//
49namespace {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000050
Ted Kremenekf2645622008-01-28 22:25:21 +000051typedef unsigned SymbolID;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000052
53class VISIBILITY_HIDDEN ValueKey {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000054 uintptr_t Raw;
Ted Kremenekcc1c3652008-01-25 23:43:12 +000055 void operator=(const ValueKey& RHS); // Do not implement.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000056
Ted Kremenekd27f8162008-01-15 23:55:06 +000057public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000058 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
59 IsSymbol=0x3, // Symbol Bindings.
60 Flags=0x3 };
61
62 inline Kind getKind() const {
63 return (Kind) (Raw & Flags);
64 }
65
66 inline void* getPtr() const {
67 assert (getKind() != IsSymbol);
68 return reinterpret_cast<void*>(Raw & ~Flags);
69 }
70
71 inline SymbolID getSymbolID() const {
72 assert (getKind() == IsSymbol);
Ted Kremenekf2645622008-01-28 22:25:21 +000073 return (SymbolID) (Raw >> 2);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000074 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000075
Ted Kremenekab2b8c52008-01-23 19:59:44 +000076 ValueKey(const ValueDecl* VD)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000077 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
78 assert(VD && "ValueDecl cannot be NULL.");
79 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000080
Ted Kremenek5c1b9962008-01-24 19:43:37 +000081 ValueKey(Stmt* S, bool isBlkExpr = false)
Ted Kremenekcc1c3652008-01-25 23:43:12 +000082 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000083 assert(S && "Tracked statement cannot be NULL.");
Ted Kremenekcc1c3652008-01-25 23:43:12 +000084 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000085
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000086 ValueKey(SymbolID V)
87 : Raw((V << 2) | IsSymbol) {}
88
89 bool isSymbol() const { return getKind() == IsSymbol; }
Ted Kremenek565256e2008-01-24 22:44:24 +000090 bool isSubExpr() const { return getKind() == IsSubExpr; }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000091 bool isDecl() const { return getKind() == IsDecl; }
Ted Kremenekd27f8162008-01-15 23:55:06 +000092
93 inline void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000094 ID.AddInteger(isSymbol() ? 1 : 0);
95
96 if (isSymbol())
Ted Kremenekf2645622008-01-28 22:25:21 +000097 ID.AddInteger(getSymbolID());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000098 else
99 ID.AddPointer(getPtr());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000100 }
101
102 inline bool operator==(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000103 return isSymbol() ? getSymbolID() == X.getSymbolID()
104 : getPtr() == X.getPtr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000105 }
106
107 inline bool operator!=(const ValueKey& X) const {
108 return !operator==(X);
109 }
110
111 inline bool operator<(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000112 if (isSymbol())
113 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000114
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000115 return getPtr() < X.getPtr();
Ted Kremenekb3d2dca2008-01-16 23:33:44 +0000116 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000117};
118} // end anonymous namespace
119
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000120// Machinery to get cast<> and dyn_cast<> working with ValueKey.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000121namespace llvm {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000122 template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) {
123 return V.getKind() == ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000124 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000125 template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000126 return ((unsigned) V.getKind()) < ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000127 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000128 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> {
Ted Kremenekaa66a322008-01-16 21:46:15 +0000129 typedef const ValueDecl* ret_type;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000130 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000131 template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000132 typedef const Stmt* ret_type;
133 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000134 template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000135 typedef void* SimpleType;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000136 static inline SimpleType getSimplifiedValue(const ValueKey &V) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000137 return V.getPtr();
138 }
139 };
140} // end llvm namespace
141
142//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000143// ValueManager.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000144//===----------------------------------------------------------------------===//
145
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000146namespace {
147
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000148typedef llvm::ImmutableSet<APSInt > APSIntSetTy;
149
150
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000151class VISIBILITY_HIDDEN ValueManager {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000152 ASTContext* Ctx;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000153
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000154 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy;
155 APSIntSetTy APSIntSet;
156
157 llvm::BumpPtrAllocator BPAlloc;
158
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000159public:
160 ValueManager() {}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000161 ~ValueManager();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000162
Ted Kremenek874d63f2008-01-24 02:02:54 +0000163 void setContext(ASTContext* ctx) { Ctx = ctx; }
164 ASTContext* getContext() const { return Ctx; }
165
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000166 APSInt& getValue(const APSInt& X);
167
Ted Kremenekd27f8162008-01-15 23:55:06 +0000168};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000169} // end anonymous namespace
170
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000171ValueManager::~ValueManager() {
172 // Note that the dstor for the contents of APSIntSet will never be called,
173 // so we iterate over the set and invoke the dstor for each APSInt. This
174 // frees an aux. memory allocated to represent very large constants.
175 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
176 I->getValue().~APSInt();
177}
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000178
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000179
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000180
181APSInt& ValueManager::getValue(const APSInt& X) {
182 llvm::FoldingSetNodeID ID;
183 void* InsertPos;
184 typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000185
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000186 X.Profile(ID);
187 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000188
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000189 if (!P) {
190 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
191 new (P) FoldNodeTy(X);
192 APSIntSet.InsertNode(P, InsertPos);
193 }
194
195 return *P;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000196}
197
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000198//===----------------------------------------------------------------------===//
199// Expression Values.
200//===----------------------------------------------------------------------===//
Ted Kremenekf13794e2008-01-24 23:19:54 +0000201
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000202namespace {
203
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000204class VISIBILITY_HIDDEN RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000205public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000206 enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
207 UninitializedKind=0x2, InvalidKind=0x3, BaseFlags = 0x3 };
Ted Kremenekf13794e2008-01-24 23:19:54 +0000208
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000209private:
Ted Kremenekf2645622008-01-28 22:25:21 +0000210 void* Data;
Ted Kremenekf13794e2008-01-24 23:19:54 +0000211 unsigned Kind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000212
213protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000214 RValue(const void* d, bool isLValue, unsigned ValKind)
Ted Kremenekf2645622008-01-28 22:25:21 +0000215 : Data(const_cast<void*>(d)),
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000216 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << 2)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000217
Ted Kremenek403c1812008-01-28 22:51:57 +0000218 explicit RValue(BaseKind k)
219 : Data(0), Kind(k) {}
Ted Kremenekf2645622008-01-28 22:25:21 +0000220
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000221 void* getRawPtr() const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000222 return reinterpret_cast<void*>(Data);
223 }
224
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000225public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000226 ~RValue() {};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000227
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000228 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000229
Ted Kremenekf13794e2008-01-24 23:19:54 +0000230 unsigned getRawKind() const { return Kind; }
231 BaseKind getBaseKind() const { return (BaseKind) (Kind & 0x3); }
232 unsigned getSubKind() const { return (Kind & ~0x3) >> 2; }
233
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000234 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000235 ID.AddInteger((unsigned) getRawKind());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000236 ID.AddPointer(reinterpret_cast<void*>(Data));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000237 }
238
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000239 bool operator==(const RValue& RHS) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000240 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000241 }
242
Ted Kremenekf13794e2008-01-24 23:19:54 +0000243 inline bool isValid() const { return getRawKind() != InvalidKind; }
244 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
245
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000246 void print(std::ostream& OS) const;
247 void print() const { print(*llvm::cerr.stream()); }
248
249 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000250 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000251};
252
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000253class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000254public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000255 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000256
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000257 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000258 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000259 }
260};
Ted Kremenek403c1812008-01-28 22:51:57 +0000261
262class VISIBILITY_HIDDEN UninitializedValue : public RValue {
263public:
264 UninitializedValue() : RValue(UninitializedKind) {}
265
266 static inline bool classof(const RValue* V) {
267 return V->getBaseKind() == UninitializedKind;
268 }
269};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000270
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000271class VISIBILITY_HIDDEN LValue : public RValue {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000272protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000273 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
Ted Kremenekf13794e2008-01-24 23:19:54 +0000274
275public:
276 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000277 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000278 return V->getBaseKind() == LValueKind;
279 }
280};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000281
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000282class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000283protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000284 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000285
286public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000287 void print(std::ostream& Out) const;
288
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000289 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
290 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
291 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
292 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
293 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
294 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000295
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000296 static NonLValue GetValue(ValueManager& ValMgr, const APSInt& V);
297 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000298
299 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000300 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000301 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000302 }
303};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000304
305} // end anonymous namespace
306
307//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000308// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000309//===----------------------------------------------------------------------===//
310
311namespace {
312
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000313enum { LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000314
315class VISIBILITY_HIDDEN LValueDecl : public LValue {
316public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000317 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000318 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000319
320 ValueDecl* getDecl() const {
321 return static_cast<ValueDecl*>(getRawPtr());
322 }
323
324 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000325 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000326 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000327 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000328};
329
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000330} // end anonymous namespace
331
332//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000333// Non-LValues.
334//===----------------------------------------------------------------------===//
335
336namespace {
337
Ted Kremenekf2645622008-01-28 22:25:21 +0000338enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
339 NumNonLValueKind };
340
341class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
342public:
343 SymbolicNonLValue(unsigned SymID)
344 : NonLValue(SymbolicNonLValueKind,
345 reinterpret_cast<void*>((uintptr_t) SymID)) {}
346
347 SymbolID getSymbolID() const {
348 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
349 }
350
351 static inline bool classof(const RValue* V) {
352 return V->getSubKind() == SymbolicNonLValueKind;
353 }
354};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000355
356class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
357public:
358 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
359
360 const APSInt& getValue() const {
361 return *static_cast<APSInt*>(getRawPtr());
362 }
363
364 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
365 return ValMgr.getValue(getValue() + V.getValue());
366 }
367
368 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
369 return ValMgr.getValue(getValue() - V.getValue());
370 }
371
372 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
373 return ValMgr.getValue(getValue() * V.getValue());
374 }
375
376 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
377 return ValMgr.getValue(getValue() / V.getValue());
378 }
379
380 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
381 return ValMgr.getValue(getValue() % V.getValue());
382 }
383
384 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
385 assert (CastExpr->getType()->isIntegerType());
386
387 APSInt X(getValue());
388 X.extOrTrunc(ValMgr.getContext()->getTypeSize(CastExpr->getType(),
389 CastExpr->getLocStart()));
390 return ValMgr.getValue(X);
391 }
392
393 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
394 assert (U->getType() == U->getSubExpr()->getType());
395 assert (U->getType()->isIntegerType());
396 return ValMgr.getValue(-getValue());
397 }
398
399 // Implement isa<T> support.
400 static inline bool classof(const RValue* V) {
401 return V->getSubKind() == ConcreteIntKind;
402 }
403};
404
405} // end anonymous namespace
406
407//===----------------------------------------------------------------------===//
408// Transfer function dispatch.
409//===----------------------------------------------------------------------===//
410
411RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
412 switch (getSubKind()) {
413 case ConcreteIntKind:
414 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
415 default:
416 return InvalidValue();
417 }
418}
419
420NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
421 switch (getSubKind()) {
422 case ConcreteIntKind:
423 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
424 default:
425 return cast<NonLValue>(InvalidValue());
426 }
427}
428
429#define RVALUE_DISPATCH_CASE(k1,k2,Op)\
430case (k1##Kind*NumNonLValueKind+k2##Kind):\
431 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
432
433#define RVALUE_DISPATCH(Op)\
434switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
435 RVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
436 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000437 if (getBaseKind() == UninitializedKind ||\
438 RHS.getBaseKind() == UninitializedKind)\
439 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000440 assert (!isValid() || !RHS.isValid() && "Missing case.");\
441 break;\
442}\
443return cast<NonLValue>(InvalidValue());
444
445NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
446 RVALUE_DISPATCH(Add)
447}
448
449NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
450 RVALUE_DISPATCH(Sub)
451}
452
453NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
454 RVALUE_DISPATCH(Mul)
455}
456
457NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
458 RVALUE_DISPATCH(Div)
459}
460
461NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
462 RVALUE_DISPATCH(Rem)
463}
464
465
466#undef RVALUE_DISPATCH_CASE
467#undef RVALUE_DISPATCH
468
469//===----------------------------------------------------------------------===//
470// Utility methods for constructing RValues.
471//===----------------------------------------------------------------------===//
472
473NonLValue NonLValue::GetValue(ValueManager& ValMgr, const APSInt& V) {
474 return ConcreteInt(ValMgr.getValue(V));
475}
476
477NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
478 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
479 I->getType()->isUnsignedIntegerType())));
480}
481
482//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000483// Pretty-Printing.
484//===----------------------------------------------------------------------===//
485
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000486void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000487 switch (getBaseKind()) {
488 case InvalidKind:
489 Out << "Invalid";
490 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000491
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000492 case NonLValueKind:
493 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000494 break;
495
496 case LValueKind:
497 assert (false && "FIXME: LValue printing not implemented.");
498 break;
499
Ted Kremenek403c1812008-01-28 22:51:57 +0000500 case UninitializedKind:
501 Out << "Uninitialized";
502 break;
503
Ted Kremenekf13794e2008-01-24 23:19:54 +0000504 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000505 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000506 }
507}
508
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000509void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000510 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000511 case ConcreteIntKind:
512 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000513 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000514
515 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000516 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000517 break;
518 }
519}
520
521//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000522// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000523//===----------------------------------------------------------------------===//
524
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000525typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000526
527namespace clang {
528 template<>
529 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
530 static inline void* toPtr(ValueMapTy M) {
531 return reinterpret_cast<void*>(M.getRoot());
532 }
533 static inline ValueMapTy toState(void* P) {
534 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
535 }
536 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000537}
538
539//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000540// The Checker.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000541//===----------------------------------------------------------------------===//
542
543namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000544
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000545class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000546
547public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000548 typedef ValueMapTy StateTy;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000549 typedef GRNodeBuilder<GRConstants> NodeBuilder;
550 typedef ExplodedNode<StateTy> NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000551
552 class NodeSet {
553 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
554 ImplTy Impl;
555 public:
556
557 NodeSet() {}
558 NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); }
559
560 void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); }
561
562 typedef ImplTy::iterator iterator;
563 typedef ImplTy::const_iterator const_iterator;
564
565 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000566 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000567
568 iterator begin() { return Impl.begin(); }
569 iterator end() { return Impl.end(); }
570
571 const_iterator begin() const { return Impl.begin(); }
572 const_iterator end() const { return Impl.end(); }
573 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000574
575protected:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000576 /// Liveness - live-variables information the ValueDecl* and block-level
577 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000578 LiveVariables* Liveness;
579
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000580 /// Builder - The current GRNodeBuilder which is used when building the nodes
581 /// for a given statement.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000582 NodeBuilder* Builder;
583
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000584 /// StateMgr - Object that manages the data for all created states.
585 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000586
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000587 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000588 ValueManager ValMgr;
589
590 /// cfg - the current CFG.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000591 CFG* cfg;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000592
593 /// StmtEntryNode - The immediate predecessor node.
594 NodeTy* StmtEntryNode;
595
596 /// CurrentStmt - The current block-level statement.
597 Stmt* CurrentStmt;
598
599 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000600
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000601 ASTContext* getContext() const { return ValMgr.getContext(); }
602
Ted Kremenekd27f8162008-01-15 23:55:06 +0000603public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000604 GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL),
605 StmtEntryNode(NULL), CurrentStmt(NULL) {}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000606
607 ~GRConstants() { delete Liveness; }
608
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000609 /// getCFG - Returns the CFG associated with this analysis.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000610 CFG& getCFG() { assert (cfg); return *cfg; }
611
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000612 /// Initialize - Initialize the checker's state based on the specified
613 /// CFG. This results in liveness information being computed for
614 /// each block-level statement in the CFG.
Ted Kremenek874d63f2008-01-24 02:02:54 +0000615 void Initialize(CFG& c, ASTContext& ctx) {
616 cfg = &c;
617 ValMgr.setContext(&ctx);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000618 Liveness = new LiveVariables(c);
619 Liveness->runOnCFG(c);
Ted Kremenek79649df2008-01-17 18:25:22 +0000620 Liveness->runOnAllBlocks(c, NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000621 }
622
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000623 /// getInitialState - Return the initial state used for the root vertex
624 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000625 StateTy getInitialState() {
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000626 return StateMgr.GetEmptyMap();
Ted Kremenekd27f8162008-01-15 23:55:06 +0000627 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000628
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000629 /// ProcessStmt - Called by GREngine. Used to generate new successor
630 /// nodes by processing the 'effects' of a block-level statement.
631 void ProcessStmt(Stmt* S, NodeBuilder& builder);
632
633 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
634 /// that all subexpression mappings are removed and that any
635 /// block-level expressions that are not live at 'S' also have their
636 /// mappings removed.
637 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
638
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000639 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000640
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000641 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000642 return SetValue(St, const_cast<Stmt*>(S), V);
643 }
644
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000645 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000646
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000647 RValue GetValue(const StateTy& St, Stmt* S);
648 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000649 return GetValue(St, const_cast<Stmt*>(S));
650 }
651
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000652 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000653 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000654
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000655 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000656
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000657 /// Visit - Transfer function logic for all statements. Dispatches to
658 /// other functions that handle specific kinds of statements.
659 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000660
661 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
662 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000663
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000664 /// VisitUnaryOperator - Transfer function logic for unary operators.
665 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
666
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000667 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000668 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
669
670 /// VisitDeclStmt - Transfer function logic for DeclStmts.
671 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000672};
673} // end anonymous namespace
674
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000675
Ted Kremenekd27f8162008-01-15 23:55:06 +0000676void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) {
677 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000678
679 StmtEntryNode = builder.getLastNode();
680 CurrentStmt = S;
681 NodeSet Dst;
682 StateCleaned = false;
683
684 Visit(S, StmtEntryNode, Dst);
685
686 // If no nodes were generated, generate a new node that has all the
687 // dead mappings removed.
688 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
689 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
690 builder.generateNode(S, St, StmtEntryNode);
691 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000692
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000693 CurrentStmt = NULL;
694 StmtEntryNode = NULL;
695 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000696}
697
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000698
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000699RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000700 switch (LV.getSubKind()) {
701 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000702 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
703 return T ? T->getValue().second : InvalidValue();
704 }
705 default:
706 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000707 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000708 }
709
710 return InvalidValue();
711}
712
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000713RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +0000714 for (;;) {
715 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000716
717 // ParenExprs are no-ops.
718
Ted Kremenek671c9e82008-01-24 00:50:08 +0000719 case Stmt::ParenExprClass:
720 S = cast<ParenExpr>(S)->getSubExpr();
721 continue;
722
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000723 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
724 // (assuming an implicit "load") depending on the context. In this
725 // context we assume that we are retrieving the value contained
726 // within the referenced variables.
727
Ted Kremenek671c9e82008-01-24 00:50:08 +0000728 case Stmt::DeclRefExprClass:
729 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +0000730
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000731 // Integer literals evaluate to an RValue. Simply retrieve the
732 // RValue for the literal.
733
Ted Kremenek671c9e82008-01-24 00:50:08 +0000734 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000735 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
736
737 // Casts where the source and target type are the same
738 // are no-ops. We blast through these to get the descendant
739 // subexpression that has a value.
740
Ted Kremenek874d63f2008-01-24 02:02:54 +0000741 case Stmt::ImplicitCastExprClass: {
742 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
743 if (C->getType() == C->getSubExpr()->getType()) {
744 S = C->getSubExpr();
745 continue;
746 }
747 break;
748 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000749
Ted Kremenek874d63f2008-01-24 02:02:54 +0000750 case Stmt::CastExprClass: {
751 CastExpr* C = cast<CastExpr>(S);
752 if (C->getType() == C->getSubExpr()->getType()) {
753 S = C->getSubExpr();
754 continue;
755 }
756 break;
757 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000758
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000759 // Handle all other Stmt* using a lookup.
760
Ted Kremenek671c9e82008-01-24 00:50:08 +0000761 default:
762 break;
763 };
764
765 break;
766 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000767
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000768 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000769
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000770 return T ? T->getValue().second : InvalidValue();
771}
772
773LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000774 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
775 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000776
777 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
778 return LValueDecl(DR->getDecl());
779
780 return cast<LValue>(GetValue(St, S));
781}
782
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000783
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000784GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000785 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000786 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000787
788 if (!StateCleaned) {
789 St = RemoveDeadBindings(CurrentStmt, St);
790 StateCleaned = true;
791 }
792
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000793 bool isBlkExpr = false;
794
795 if (S == CurrentStmt) {
796 isBlkExpr = getCFG().isBlkExpr(S);
797
798 if (!isBlkExpr)
799 return St;
800 }
Ted Kremenekdaadf452008-01-24 19:28:01 +0000801
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000802 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
803 : St;
804}
805
806GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000807 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000808 if (!LV.isValid())
809 return St;
810
811 if (!StateCleaned) {
812 St = RemoveDeadBindings(CurrentStmt, St);
813 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +0000814 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +0000815
Ted Kremenekf13794e2008-01-24 23:19:54 +0000816 switch (LV.getSubKind()) {
817 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
819 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
820
821 default:
822 assert ("SetValue for given LValue type not yet implemented.");
823 return St;
824 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825}
826
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000827GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000828 // Note: in the code below, we can assign a new map to M since the
829 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000830 StateTy::iterator I = M.begin(), E = M.end();
831
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000832 // Remove old bindings for subexpressions and "dead" block-level expressions.
833 for (; I!=E && !I.getKey().isDecl(); ++I) {
834 if (I.getKey().isSubExpr() || !Liveness->isLive(Loc,cast<Stmt>(I.getKey())))
835 M = StateMgr.Remove(M, I.getKey());
836 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000837
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000838 // Remove bindings for "dead" decls.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000839 for (; I!=E && I.getKey().isDecl(); ++I)
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000840 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
841 if (!Liveness->isLive(Loc, V))
842 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek565256e2008-01-24 22:44:24 +0000843
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000844 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000845}
846
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000847void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
848 GRConstants::StateTy St) {
849
850 // If the state hasn't changed, don't generate a new node.
851 if (St == Pred->getState())
852 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000853
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000854 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000855}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000856
Ted Kremenek874d63f2008-01-24 02:02:54 +0000857void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
858 GRConstants::NodeSet& Dst) {
859
860 QualType T = CastE->getType();
861
862 // Check for redundant casts.
863 if (E->getType() == T) {
864 Dst.Add(Pred);
865 return;
866 }
867
868 NodeSet S1;
869 Visit(E, Pred, S1);
870
871 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
872 NodeTy* N = *I1;
873 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000874 const RValue& V = GetValue(St, E);
875 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000876 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000877}
878
879void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
880 GRConstants::NodeSet& Dst) {
881
882 StateTy St = Pred->getState();
883
884 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000885 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
886 const Expr* E = VD->getInit();
887 St = SetValue(St, LValueDecl(VD),
888 E ? GetValue(St, E) : UninitializedValue());
889 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000890
891 Nodify(Dst, DS, Pred, St);
892
893 if (Dst.empty())
894 Dst.Add(Pred);
895}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000896
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000897void GRConstants::VisitUnaryOperator(UnaryOperator* U,
898 GRConstants::NodeTy* Pred,
899 GRConstants::NodeSet& Dst) {
900 NodeSet S1;
901 Visit(U->getSubExpr(), Pred, S1);
902
903 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
904 NodeTy* N1 = *I1;
905 StateTy St = N1->getState();
906
907 switch (U->getOpcode()) {
908 case UnaryOperator::PostInc: {
909 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000910 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000911
912 QualType T = U->getType();
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000913 unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000914 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000915 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000916
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000917 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000918 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
919 break;
920 }
921
922 case UnaryOperator::PostDec: {
923 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000924 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000925
926 QualType T = U->getType();
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000927 unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000928 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000929 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000930
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000931 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000932 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
933 break;
934 }
935
936 case UnaryOperator::PreInc: {
937 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000938 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000939
940 QualType T = U->getType();
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000941 unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000942 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000943 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000944
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000945 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000946 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
947 break;
948 }
949
950 case UnaryOperator::PreDec: {
951 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000952 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000953
954 QualType T = U->getType();
Ted Kremeneke0cf9c82008-01-24 19:00:57 +0000955 unsigned bits = getContext()->getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000956 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000957 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000958
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000959 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000960 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
961 break;
962 }
963
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000964 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000965 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
966 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000967 break;
968 }
969
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000970 default: ;
971 assert (false && "Not implemented.");
972 }
973 }
974}
975
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000976void GRConstants::VisitBinaryOperator(BinaryOperator* B,
977 GRConstants::NodeTy* Pred,
978 GRConstants::NodeSet& Dst) {
979 NodeSet S1;
980 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000981
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000982 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
983 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000984
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000985 // When getting the value for the LHS, check if we are in an assignment.
986 // In such cases, we want to (initially) treat the LHS as an LValue,
987 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000988 // evaluated to LValueDecl's instead of to an NonLValue.
989 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000990 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
991 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000992
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000993 NodeSet S2;
994 Visit(B->getRHS(), N1, S2);
995
996 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
997 NodeTy* N2 = *I2;
998 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000999 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001000
1001 switch (B->getOpcode()) {
1002 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001003 const NonLValue& R1 = cast<NonLValue>(V1);
1004 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001005
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001006 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001007 break;
1008 }
1009
1010 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001011 const NonLValue& R1 = cast<NonLValue>(V1);
1012 const NonLValue& R2 = cast<NonLValue>(V2);
1013 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001014 break;
1015 }
1016
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001017 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001018 const NonLValue& R1 = cast<NonLValue>(V1);
1019 const NonLValue& R2 = cast<NonLValue>(V2);
1020 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001021 break;
1022 }
1023
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001024 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001025 const NonLValue& R1 = cast<NonLValue>(V1);
1026 const NonLValue& R2 = cast<NonLValue>(V2);
1027 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001028 break;
1029 }
1030
Ted Kremenekcce207d2008-01-28 22:26:15 +00001031 case BinaryOperator::Rem: {
1032 const NonLValue& R1 = cast<NonLValue>(V1);
1033 const NonLValue& R2 = cast<NonLValue>(V2);
1034 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1035 break;
1036 }
1037
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001038 case BinaryOperator::Assign: {
1039 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001040 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001041 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1042 break;
1043 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001044
1045 case BinaryOperator::AddAssign: {
1046 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001047 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1048 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001049 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1050 break;
1051 }
1052
1053 case BinaryOperator::SubAssign: {
1054 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001055 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1056 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001057 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1058 break;
1059 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001060
1061 case BinaryOperator::MulAssign: {
1062 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001063 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1064 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001065 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1066 break;
1067 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001068
1069 case BinaryOperator::DivAssign: {
1070 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001071 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1072 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001073 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1074 break;
1075 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001076
1077 case BinaryOperator::RemAssign: {
1078 const LValue& L1 = cast<LValue>(V1);
1079 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1080 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1081 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1082 break;
1083 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001084
1085 default:
1086 Dst.Add(N2);
1087 break;
1088 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001089 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001090 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001091}
Ted Kremenekee985462008-01-16 18:18:48 +00001092
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001093
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001094void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1095 GRConstants::NodeSet& Dst) {
1096
1097 // FIXME: add metadata to the CFG so that we can disable
1098 // this check when we KNOW that there is no block-level subexpression.
1099 // The motivation is that this check requires a hashtable lookup.
1100
1101 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1102 Dst.Add(Pred);
1103 return;
1104 }
1105
1106 switch (S->getStmtClass()) {
1107 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001108 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001109 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1110 break;
1111
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001112 case Stmt::UnaryOperatorClass:
1113 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1114 break;
1115
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001116 case Stmt::ParenExprClass:
1117 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1118 break;
1119
Ted Kremenek874d63f2008-01-24 02:02:54 +00001120 case Stmt::ImplicitCastExprClass: {
1121 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1122 VisitCast(C, C->getSubExpr(), Pred, Dst);
1123 break;
1124 }
1125
1126 case Stmt::CastExprClass: {
1127 CastExpr* C = cast<CastExpr>(S);
1128 VisitCast(C, C->getSubExpr(), Pred, Dst);
1129 break;
1130 }
1131
Ted Kremenek9de04c42008-01-24 20:55:43 +00001132 case Stmt::DeclStmtClass:
1133 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1134 break;
1135
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001136 default:
1137 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1138 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001139 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001140}
1141
Ted Kremenekee985462008-01-16 18:18:48 +00001142//===----------------------------------------------------------------------===//
1143// Driver.
1144//===----------------------------------------------------------------------===//
1145
Ted Kremenekaa66a322008-01-16 21:46:15 +00001146#ifndef NDEBUG
1147namespace llvm {
1148template<>
1149struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1150 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001151
1152 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001153 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001154 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001155 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1156 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1157 default: assert (false && "Unknown ValueKey type.");
1158 }
1159 }
1160
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001161 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1162 ValueKey::Kind kind, bool isFirstGroup = false) {
1163 bool isFirst = true;
1164
1165 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1166 if (I.getKey().getKind() != kind)
1167 continue;
1168
1169 if (isFirst) {
1170 if (!isFirstGroup) Out << "\\l\\l";
1171 PrintKindLabel(Out, kind);
1172 isFirst = false;
1173 }
1174 else
1175 Out << "\\l";
1176
1177 Out << ' ';
1178
1179 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1180 Out << V->getName();
1181 else {
1182 Stmt* E = cast<Stmt>(I.getKey());
1183 Out << " (" << (void*) E << ") ";
1184 E->printPretty(Out);
1185 }
1186
1187 Out << " : ";
1188 I.getData().print(Out);
1189 }
1190 }
1191
Ted Kremenekaa66a322008-01-16 21:46:15 +00001192 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1193 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001194
1195 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001196 ProgramPoint Loc = N->getLocation();
1197
1198 switch (Loc.getKind()) {
1199 case ProgramPoint::BlockEntranceKind:
1200 Out << "Block Entrance: B"
1201 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1202 break;
1203
1204 case ProgramPoint::BlockExitKind:
1205 assert (false);
1206 break;
1207
1208 case ProgramPoint::PostStmtKind: {
1209 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001210 Out << L.getStmt()->getStmtClassName() << ':'
1211 << (void*) L.getStmt() << ' ';
1212
Ted Kremenekaa66a322008-01-16 21:46:15 +00001213 L.getStmt()->printPretty(Out);
1214 break;
1215 }
1216
1217 default: {
1218 const BlockEdge& E = cast<BlockEdge>(Loc);
1219 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1220 << E.getDst()->getBlockID() << ')';
1221 }
1222 }
1223
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001224 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001225
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001226 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1227 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001228 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001229
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001230 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001231 return Out.str();
1232 }
1233};
1234} // end llvm namespace
1235#endif
1236
Ted Kremenekee985462008-01-16 18:18:48 +00001237namespace clang {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001238void RunGRConstants(CFG& cfg, ASTContext& Ctx) {
1239 GREngine<GRConstants> Engine(cfg, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001240 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001241#ifndef NDEBUG
1242 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
1243#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001244}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001245} // end clang namespace