blob: da1a0adfa45b89f90788f1c9de18ccbcd7b62aed [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 Kremenek68fd2572008-01-29 17:27:31 +000051class SymbolID {
52 unsigned Data;
53public:
54 SymbolID() : Data(~0) {}
55 SymbolID(unsigned x) : Data(x) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +000056
Ted Kremenek68fd2572008-01-29 17:27:31 +000057 bool isInitialized() const { return Data != (unsigned) ~0; }
58 operator unsigned() const { assert (isInitialized()); return Data; }
59};
60
Ted Kremenekab2b8c52008-01-23 19:59:44 +000061class VISIBILITY_HIDDEN ValueKey {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000062 uintptr_t Raw;
Ted Kremenekcc1c3652008-01-25 23:43:12 +000063 void operator=(const ValueKey& RHS); // Do not implement.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000064
Ted Kremenekd27f8162008-01-15 23:55:06 +000065public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000066 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
67 IsSymbol=0x3, // Symbol Bindings.
68 Flags=0x3 };
69
70 inline Kind getKind() const {
71 return (Kind) (Raw & Flags);
72 }
73
74 inline void* getPtr() const {
75 assert (getKind() != IsSymbol);
76 return reinterpret_cast<void*>(Raw & ~Flags);
77 }
78
79 inline SymbolID getSymbolID() const {
80 assert (getKind() == IsSymbol);
Ted Kremenek68fd2572008-01-29 17:27:31 +000081 return Raw >> 2;
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000082 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000083
Ted Kremenekab2b8c52008-01-23 19:59:44 +000084 ValueKey(const ValueDecl* VD)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000085 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
86 assert(VD && "ValueDecl cannot be NULL.");
87 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000088
Ted Kremenek5c1b9962008-01-24 19:43:37 +000089 ValueKey(Stmt* S, bool isBlkExpr = false)
Ted Kremenekcc1c3652008-01-25 23:43:12 +000090 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000091 assert(S && "Tracked statement cannot be NULL.");
Ted Kremenekcc1c3652008-01-25 23:43:12 +000092 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000093
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000094 ValueKey(SymbolID V)
95 : Raw((V << 2) | IsSymbol) {}
96
97 bool isSymbol() const { return getKind() == IsSymbol; }
Ted Kremenek565256e2008-01-24 22:44:24 +000098 bool isSubExpr() const { return getKind() == IsSubExpr; }
Ted Kremenek65cac132008-01-29 05:25:31 +000099 bool isBlkExpr() const { return getKind() == IsBlkExpr; }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000100 bool isDecl() const { return getKind() == IsDecl; }
Ted Kremenek65cac132008-01-29 05:25:31 +0000101 bool isStmt() const { return getKind() <= IsBlkExpr; }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000102
103 inline void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000104 ID.AddInteger(isSymbol() ? 1 : 0);
105
106 if (isSymbol())
Ted Kremenekf2645622008-01-28 22:25:21 +0000107 ID.AddInteger(getSymbolID());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000108 else
109 ID.AddPointer(getPtr());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000110 }
111
112 inline bool operator==(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000113 return isSymbol() ? getSymbolID() == X.getSymbolID()
114 : getPtr() == X.getPtr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000115 }
116
117 inline bool operator!=(const ValueKey& X) const {
118 return !operator==(X);
119 }
120
121 inline bool operator<(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000122 if (isSymbol())
123 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000124
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000125 return getPtr() < X.getPtr();
Ted Kremenekb3d2dca2008-01-16 23:33:44 +0000126 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000127};
128} // end anonymous namespace
129
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000130// Machinery to get cast<> and dyn_cast<> working with ValueKey.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000131namespace llvm {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000132 template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) {
133 return V.getKind() == ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000134 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000135 template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000136 return ((unsigned) V.getKind()) < ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000137 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000138 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> {
Ted Kremenekaa66a322008-01-16 21:46:15 +0000139 typedef const ValueDecl* ret_type;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000140 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000141 template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000142 typedef const Stmt* ret_type;
143 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000144 template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000145 typedef void* SimpleType;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000146 static inline SimpleType getSimplifiedValue(const ValueKey &V) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000147 return V.getPtr();
148 }
149 };
150} // end llvm namespace
151
Ted Kremenek68fd2572008-01-29 17:27:31 +0000152
153//===----------------------------------------------------------------------===//
154// SymbolManager.
155//===----------------------------------------------------------------------===//
156
157namespace {
158class VISIBILITY_HIDDEN SymbolData {
159 uintptr_t Data;
160public:
161 enum Kind { ParmKind = 0x0, Mask = 0x3 };
162
163 SymbolData(ParmVarDecl* D)
164 : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
165
166 inline Kind getKind() const { return (Kind) (Data & Mask); }
167 inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
168 inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
169};
170}
171
172// Machinery to get cast<> and dyn_cast<> working with SymbolData.
173namespace llvm {
174 template<> inline bool isa<ParmVarDecl,SymbolData>(const SymbolData& V) {
175 return V.getKind() == SymbolData::ParmKind;
176 }
177 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ParmVarDecl,SymbolData> {
178 typedef const ParmVarDecl* ret_type;
179 };
180 template<> struct VISIBILITY_HIDDEN simplify_type<SymbolData> {
181 typedef void* SimpleType;
182 static inline SimpleType getSimplifiedValue(const SymbolData &V) {
183 return V.getPtr();
184 }
185 };
186} // end llvm namespace
187
188namespace {
189class VISIBILITY_HIDDEN SymbolManager {
190 std::vector<SymbolData> SymbolToData;
191
192 typedef llvm::DenseMap<void*,SymbolID> MapTy;
193 MapTy DataToSymbol;
194
195public:
196 SymbolData getSymbolData(SymbolID id) const {
197 assert (id < SymbolToData.size());
198 return SymbolToData[id];
199 }
200
201 SymbolID getSymbol(ParmVarDecl* D);
202};
203} // end anonymous namespace
204
205SymbolID SymbolManager::getSymbol(ParmVarDecl* D) {
206 SymbolID& X = DataToSymbol[D];
207
208 if (!X.isInitialized()) {
209 X = SymbolToData.size();
210 SymbolToData.push_back(D);
211 }
212
213 return X;
214}
215
Ted Kremenekd27f8162008-01-15 23:55:06 +0000216//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000217// ValueManager.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000218//===----------------------------------------------------------------------===//
219
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000220namespace {
221
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000222typedef llvm::ImmutableSet<APSInt > APSIntSetTy;
223
224
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000225class VISIBILITY_HIDDEN ValueManager {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000226 ASTContext& Ctx;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000227
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000228 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy;
229 APSIntSetTy APSIntSet;
230
231 llvm::BumpPtrAllocator BPAlloc;
232
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000233public:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000234 ValueManager(ASTContext& ctx) : Ctx(ctx) {}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000235 ~ValueManager();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000236
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000237 ASTContext& getContext() const { return Ctx; }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000238 APSInt& getValue(const APSInt& X);
239
Ted Kremenekd27f8162008-01-15 23:55:06 +0000240};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000241} // end anonymous namespace
242
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000243ValueManager::~ValueManager() {
244 // Note that the dstor for the contents of APSIntSet will never be called,
245 // so we iterate over the set and invoke the dstor for each APSInt. This
246 // frees an aux. memory allocated to represent very large constants.
247 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
248 I->getValue().~APSInt();
249}
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000250
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000251
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000252
253APSInt& ValueManager::getValue(const APSInt& X) {
254 llvm::FoldingSetNodeID ID;
255 void* InsertPos;
256 typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000257
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000258 X.Profile(ID);
259 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000260
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000261 if (!P) {
262 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
263 new (P) FoldNodeTy(X);
264 APSIntSet.InsertNode(P, InsertPos);
265 }
266
267 return *P;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000268}
269
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000270//===----------------------------------------------------------------------===//
271// Expression Values.
272//===----------------------------------------------------------------------===//
Ted Kremenekf13794e2008-01-24 23:19:54 +0000273
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000274namespace {
275
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000276class VISIBILITY_HIDDEN RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000277public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000278 enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
279 UninitializedKind=0x2, InvalidKind=0x3, BaseFlags = 0x3 };
Ted Kremenekf13794e2008-01-24 23:19:54 +0000280
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000281private:
Ted Kremenekf2645622008-01-28 22:25:21 +0000282 void* Data;
Ted Kremenekf13794e2008-01-24 23:19:54 +0000283 unsigned Kind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000284
285protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000286 RValue(const void* d, bool isLValue, unsigned ValKind)
Ted Kremenekf2645622008-01-28 22:25:21 +0000287 : Data(const_cast<void*>(d)),
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000288 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << 2)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000289
Ted Kremenek403c1812008-01-28 22:51:57 +0000290 explicit RValue(BaseKind k)
291 : Data(0), Kind(k) {}
Ted Kremenekf2645622008-01-28 22:25:21 +0000292
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000293 void* getRawPtr() const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000294 return reinterpret_cast<void*>(Data);
295 }
296
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000297public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000298 ~RValue() {};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000299
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000300 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000301
Ted Kremenekf13794e2008-01-24 23:19:54 +0000302 unsigned getRawKind() const { return Kind; }
303 BaseKind getBaseKind() const { return (BaseKind) (Kind & 0x3); }
304 unsigned getSubKind() const { return (Kind & ~0x3) >> 2; }
305
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000306 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000307 ID.AddInteger((unsigned) getRawKind());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000308 ID.AddPointer(reinterpret_cast<void*>(Data));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000309 }
310
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000311 bool operator==(const RValue& RHS) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000312 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000313 }
314
Ted Kremenekf13794e2008-01-24 23:19:54 +0000315 inline bool isValid() const { return getRawKind() != InvalidKind; }
316 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
317
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000318 void print(std::ostream& OS) const;
319 void print() const { print(*llvm::cerr.stream()); }
320
321 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000322 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000323};
324
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000325class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000326public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000327 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000328
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000329 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000330 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000331 }
332};
Ted Kremenek403c1812008-01-28 22:51:57 +0000333
334class VISIBILITY_HIDDEN UninitializedValue : public RValue {
335public:
336 UninitializedValue() : RValue(UninitializedKind) {}
337
338 static inline bool classof(const RValue* V) {
339 return V->getBaseKind() == UninitializedKind;
340 }
341};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000342
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000343class VISIBILITY_HIDDEN LValue : public RValue {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000344protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000345 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
Ted Kremenekf13794e2008-01-24 23:19:54 +0000346
347public:
348 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000349 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000350 return V->getBaseKind() == LValueKind;
351 }
352};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000353
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000354class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000355protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000356 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000357
358public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000359 void print(std::ostream& Out) const;
360
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000361 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
362 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
363 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
364 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
365 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
366 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000367
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000368 static NonLValue GetValue(ValueManager& ValMgr, const APSInt& V);
369 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenek68fd2572008-01-29 17:27:31 +0000370
371 static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000372
373 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000374 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000375 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000376 }
377};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000378
379} // end anonymous namespace
380
381//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000382// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000383//===----------------------------------------------------------------------===//
384
385namespace {
386
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000387enum { LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000388
389class VISIBILITY_HIDDEN LValueDecl : public LValue {
390public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000391 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000392 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000393
394 ValueDecl* getDecl() const {
395 return static_cast<ValueDecl*>(getRawPtr());
396 }
397
398 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000399 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000400 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000401 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000402};
403
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000404} // end anonymous namespace
405
406//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000407// Non-LValues.
408//===----------------------------------------------------------------------===//
409
410namespace {
411
Ted Kremenekf2645622008-01-28 22:25:21 +0000412enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
413 NumNonLValueKind };
414
415class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
416public:
417 SymbolicNonLValue(unsigned SymID)
418 : NonLValue(SymbolicNonLValueKind,
419 reinterpret_cast<void*>((uintptr_t) SymID)) {}
420
421 SymbolID getSymbolID() const {
422 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
423 }
424
425 static inline bool classof(const RValue* V) {
426 return V->getSubKind() == SymbolicNonLValueKind;
427 }
428};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000429
430class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
431public:
432 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
433
434 const APSInt& getValue() const {
435 return *static_cast<APSInt*>(getRawPtr());
436 }
437
438 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
439 return ValMgr.getValue(getValue() + V.getValue());
440 }
441
442 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
443 return ValMgr.getValue(getValue() - V.getValue());
444 }
445
446 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
447 return ValMgr.getValue(getValue() * V.getValue());
448 }
449
450 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
451 return ValMgr.getValue(getValue() / V.getValue());
452 }
453
454 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
455 return ValMgr.getValue(getValue() % V.getValue());
456 }
457
458 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
459 assert (CastExpr->getType()->isIntegerType());
460
461 APSInt X(getValue());
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000462 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
463 CastExpr->getLocStart()));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000464 return ValMgr.getValue(X);
465 }
466
467 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
468 assert (U->getType() == U->getSubExpr()->getType());
469 assert (U->getType()->isIntegerType());
470 return ValMgr.getValue(-getValue());
471 }
472
473 // Implement isa<T> support.
474 static inline bool classof(const RValue* V) {
475 return V->getSubKind() == ConcreteIntKind;
476 }
477};
478
479} // end anonymous namespace
480
481//===----------------------------------------------------------------------===//
482// Transfer function dispatch.
483//===----------------------------------------------------------------------===//
484
485RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
486 switch (getSubKind()) {
487 case ConcreteIntKind:
488 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
489 default:
490 return InvalidValue();
491 }
492}
493
494NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
495 switch (getSubKind()) {
496 case ConcreteIntKind:
497 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
498 default:
499 return cast<NonLValue>(InvalidValue());
500 }
501}
502
503#define RVALUE_DISPATCH_CASE(k1,k2,Op)\
504case (k1##Kind*NumNonLValueKind+k2##Kind):\
505 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
506
507#define RVALUE_DISPATCH(Op)\
508switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
509 RVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
510 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000511 if (getBaseKind() == UninitializedKind ||\
512 RHS.getBaseKind() == UninitializedKind)\
513 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000514 assert (!isValid() || !RHS.isValid() && "Missing case.");\
515 break;\
516}\
517return cast<NonLValue>(InvalidValue());
518
519NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
520 RVALUE_DISPATCH(Add)
521}
522
523NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
524 RVALUE_DISPATCH(Sub)
525}
526
527NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
528 RVALUE_DISPATCH(Mul)
529}
530
531NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
532 RVALUE_DISPATCH(Div)
533}
534
535NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
536 RVALUE_DISPATCH(Rem)
537}
538
539
540#undef RVALUE_DISPATCH_CASE
541#undef RVALUE_DISPATCH
542
543//===----------------------------------------------------------------------===//
544// Utility methods for constructing RValues.
545//===----------------------------------------------------------------------===//
546
547NonLValue NonLValue::GetValue(ValueManager& ValMgr, const APSInt& V) {
548 return ConcreteInt(ValMgr.getValue(V));
549}
550
551NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
552 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
553 I->getType()->isUnsignedIntegerType())));
554}
555
Ted Kremenek68fd2572008-01-29 17:27:31 +0000556NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
557 return SymbolicNonLValue(SymMgr.getSymbol(D));
558}
559
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000560//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000561// Pretty-Printing.
562//===----------------------------------------------------------------------===//
563
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000564void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000565 switch (getBaseKind()) {
566 case InvalidKind:
567 Out << "Invalid";
568 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000569
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000570 case NonLValueKind:
571 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000572 break;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000573
Ted Kremenekf13794e2008-01-24 23:19:54 +0000574 case LValueKind:
575 assert (false && "FIXME: LValue printing not implemented.");
576 break;
577
Ted Kremenek403c1812008-01-28 22:51:57 +0000578 case UninitializedKind:
579 Out << "Uninitialized";
580 break;
581
Ted Kremenekf13794e2008-01-24 23:19:54 +0000582 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000583 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000584 }
585}
586
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000587void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000588 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000589 case ConcreteIntKind:
590 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000591 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000592
Ted Kremenek68fd2572008-01-29 17:27:31 +0000593 case SymbolicNonLValueKind:
594 Out << "sym-" << cast<SymbolicNonLValue>(this)->getSymbolID();
595 break;
596
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000597 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000598 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000599 break;
600 }
601}
602
603//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000604// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000605//===----------------------------------------------------------------------===//
606
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000607typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000608
609namespace clang {
610 template<>
611 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
612 static inline void* toPtr(ValueMapTy M) {
613 return reinterpret_cast<void*>(M.getRoot());
614 }
615 static inline ValueMapTy toState(void* P) {
616 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
617 }
618 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000619}
620
621//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000622// The Checker.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000623//===----------------------------------------------------------------------===//
624
625namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000626
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000627class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000628
629public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000630 typedef ValueMapTy StateTy;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000631 typedef GRNodeBuilder<GRConstants> NodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000632 typedef ExplodedGraph<GRConstants> GraphTy;
633 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000634
635 class NodeSet {
636 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
637 ImplTy Impl;
638 public:
639
640 NodeSet() {}
641 NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); }
642
643 void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); }
644
645 typedef ImplTy::iterator iterator;
646 typedef ImplTy::const_iterator const_iterator;
647
648 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000649 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000650
651 iterator begin() { return Impl.begin(); }
652 iterator end() { return Impl.end(); }
653
654 const_iterator begin() const { return Impl.begin(); }
655 const_iterator end() const { return Impl.end(); }
656 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000657
658protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000659 /// G - the simulation graph.
660 GraphTy& G;
661
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000662 /// Liveness - live-variables information the ValueDecl* and block-level
663 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000664 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000665
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000666 /// Builder - The current GRNodeBuilder which is used when building the nodes
667 /// for a given statement.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000668 NodeBuilder* Builder;
669
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000670 /// StateMgr - Object that manages the data for all created states.
671 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000672
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000673 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000674 ValueManager ValMgr;
675
Ted Kremenek68fd2572008-01-29 17:27:31 +0000676 /// SymMgr - Object that manages the symbol information.
677 SymbolManager SymMgr;
678
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000679 /// StmtEntryNode - The immediate predecessor node.
680 NodeTy* StmtEntryNode;
681
682 /// CurrentStmt - The current block-level statement.
683 Stmt* CurrentStmt;
684
685 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000686
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000687 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000688
Ted Kremenekd27f8162008-01-15 23:55:06 +0000689public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000690 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
691 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
692 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000693
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000694 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000695 Liveness.runOnCFG(G.getCFG());
696 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000697 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000698
699 /// getCFG - Returns the CFG associated with this analysis.
700 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000701
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000702 /// getInitialState - Return the initial state used for the root vertex
703 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000704 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000705 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000706
707 // Iterate the parameters.
708 FunctionDecl& F = G.getFunctionDecl();
709
710 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
711 I!=E; ++I) {
712
713 // For now we only support symbolic values for non-pointer types.
714 if ((*I)->getType()->isPointerType() ||
715 (*I)->getType()->isReferenceType())
716 continue;
717
718 // FIXME: Set these values to a symbol, not Uninitialized.
Ted Kremenek68fd2572008-01-29 17:27:31 +0000719 St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000720 }
721
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000722 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000723 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000724
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000725 /// ProcessStmt - Called by GREngine. Used to generate new successor
726 /// nodes by processing the 'effects' of a block-level statement.
727 void ProcessStmt(Stmt* S, NodeBuilder& builder);
728
729 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
730 /// that all subexpression mappings are removed and that any
731 /// block-level expressions that are not live at 'S' also have their
732 /// mappings removed.
733 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
734
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000735 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000736
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000737 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000738 return SetValue(St, const_cast<Stmt*>(S), V);
739 }
740
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000741 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000742
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000743 RValue GetValue(const StateTy& St, Stmt* S);
744 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000745 return GetValue(St, const_cast<Stmt*>(S));
746 }
747
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000748 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000749 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000750
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000751 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000752
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000753 /// Visit - Transfer function logic for all statements. Dispatches to
754 /// other functions that handle specific kinds of statements.
755 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000756
757 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
758 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000759
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000760 /// VisitUnaryOperator - Transfer function logic for unary operators.
761 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
762
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000763 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000764 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
765
766 /// VisitDeclStmt - Transfer function logic for DeclStmts.
767 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000768};
769} // end anonymous namespace
770
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000771
Ted Kremenekd27f8162008-01-15 23:55:06 +0000772void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) {
773 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000774
775 StmtEntryNode = builder.getLastNode();
776 CurrentStmt = S;
777 NodeSet Dst;
778 StateCleaned = false;
779
780 Visit(S, StmtEntryNode, Dst);
781
782 // If no nodes were generated, generate a new node that has all the
783 // dead mappings removed.
784 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
785 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
786 builder.generateNode(S, St, StmtEntryNode);
787 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000788
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000789 CurrentStmt = NULL;
790 StmtEntryNode = NULL;
791 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000792}
793
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000794
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000795RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000796 switch (LV.getSubKind()) {
797 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000798 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
799 return T ? T->getValue().second : InvalidValue();
800 }
801 default:
802 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000803 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000804 }
805
806 return InvalidValue();
807}
808
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000809RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +0000810 for (;;) {
811 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000812
813 // ParenExprs are no-ops.
814
Ted Kremenek671c9e82008-01-24 00:50:08 +0000815 case Stmt::ParenExprClass:
816 S = cast<ParenExpr>(S)->getSubExpr();
817 continue;
818
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000819 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
820 // (assuming an implicit "load") depending on the context. In this
821 // context we assume that we are retrieving the value contained
822 // within the referenced variables.
823
Ted Kremenek671c9e82008-01-24 00:50:08 +0000824 case Stmt::DeclRefExprClass:
825 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +0000826
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000827 // Integer literals evaluate to an RValue. Simply retrieve the
828 // RValue for the literal.
829
Ted Kremenek671c9e82008-01-24 00:50:08 +0000830 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000831 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
832
833 // Casts where the source and target type are the same
834 // are no-ops. We blast through these to get the descendant
835 // subexpression that has a value.
836
Ted Kremenek874d63f2008-01-24 02:02:54 +0000837 case Stmt::ImplicitCastExprClass: {
838 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
839 if (C->getType() == C->getSubExpr()->getType()) {
840 S = C->getSubExpr();
841 continue;
842 }
843 break;
844 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000845
Ted Kremenek874d63f2008-01-24 02:02:54 +0000846 case Stmt::CastExprClass: {
847 CastExpr* C = cast<CastExpr>(S);
848 if (C->getType() == C->getSubExpr()->getType()) {
849 S = C->getSubExpr();
850 continue;
851 }
852 break;
853 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000854
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000855 // Handle all other Stmt* using a lookup.
856
Ted Kremenek671c9e82008-01-24 00:50:08 +0000857 default:
858 break;
859 };
860
861 break;
862 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000863
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000864 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000865
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000866 return T ? T->getValue().second : InvalidValue();
867}
868
869LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000870 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
871 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000872
873 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
874 return LValueDecl(DR->getDecl());
875
876 return cast<LValue>(GetValue(St, S));
877}
878
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000879
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000880GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000881 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000882 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000883
884 if (!StateCleaned) {
885 St = RemoveDeadBindings(CurrentStmt, St);
886 StateCleaned = true;
887 }
888
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000889 bool isBlkExpr = false;
890
891 if (S == CurrentStmt) {
892 isBlkExpr = getCFG().isBlkExpr(S);
893
894 if (!isBlkExpr)
895 return St;
896 }
Ted Kremenekdaadf452008-01-24 19:28:01 +0000897
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000898 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
899 : St;
900}
901
902GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000903 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000904 if (!LV.isValid())
905 return St;
906
907 if (!StateCleaned) {
908 St = RemoveDeadBindings(CurrentStmt, St);
909 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +0000910 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +0000911
Ted Kremenekf13794e2008-01-24 23:19:54 +0000912 switch (LV.getSubKind()) {
913 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000914 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
915 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
916
917 default:
918 assert ("SetValue for given LValue type not yet implemented.");
919 return St;
920 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000921}
922
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000923GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +0000924 // Note: in the code below, we can assign a new map to M since the
925 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000926 StateTy::iterator I = M.begin(), E = M.end();
927
Ted Kremenekf84469b2008-01-18 00:41:32 +0000928
Ted Kremenek65cac132008-01-29 05:25:31 +0000929 for (; I!=E && !I.getKey().isSymbol(); ++I) {
930 // Remove old bindings for subexpressions and "dead"
931 // block-level expressions.
932 if (I.getKey().isSubExpr() ||
933 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
934 M = StateMgr.Remove(M, I.getKey());
935 }
936 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
937 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
938 if (!Liveness.isLive(Loc, V))
939 M = StateMgr.Remove(M, I.getKey());
940 }
941 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000942
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000943 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000944}
945
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000946void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
947 GRConstants::StateTy St) {
948
949 // If the state hasn't changed, don't generate a new node.
950 if (St == Pred->getState())
951 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000952
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000953 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000954}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000955
Ted Kremenek874d63f2008-01-24 02:02:54 +0000956void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
957 GRConstants::NodeSet& Dst) {
958
959 QualType T = CastE->getType();
960
961 // Check for redundant casts.
962 if (E->getType() == T) {
963 Dst.Add(Pred);
964 return;
965 }
966
967 NodeSet S1;
968 Visit(E, Pred, S1);
969
970 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
971 NodeTy* N = *I1;
972 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000973 const RValue& V = GetValue(St, E);
974 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000975 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000976}
977
978void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
979 GRConstants::NodeSet& Dst) {
980
981 StateTy St = Pred->getState();
982
983 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000984 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
985 const Expr* E = VD->getInit();
986 St = SetValue(St, LValueDecl(VD),
987 E ? GetValue(St, E) : UninitializedValue());
988 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000989
990 Nodify(Dst, DS, Pred, St);
991
992 if (Dst.empty())
993 Dst.Add(Pred);
994}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000995
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000996void GRConstants::VisitUnaryOperator(UnaryOperator* U,
997 GRConstants::NodeTy* Pred,
998 GRConstants::NodeSet& Dst) {
999 NodeSet S1;
1000 Visit(U->getSubExpr(), Pred, S1);
1001
1002 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1003 NodeTy* N1 = *I1;
1004 StateTy St = N1->getState();
1005
1006 switch (U->getOpcode()) {
1007 case UnaryOperator::PostInc: {
1008 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001009 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001010
1011 QualType T = U->getType();
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001012 unsigned bits = getContext().getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001013 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001014 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001015
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001016 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001017 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1018 break;
1019 }
1020
1021 case UnaryOperator::PostDec: {
1022 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001023 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001024
1025 QualType T = U->getType();
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001026 unsigned bits = getContext().getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001027 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001028 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001029
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001030 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001031 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1032 break;
1033 }
1034
1035 case UnaryOperator::PreInc: {
1036 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001037 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001038
1039 QualType T = U->getType();
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001040 unsigned bits = getContext().getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001041 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001042 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001043
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001044 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001045 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1046 break;
1047 }
1048
1049 case UnaryOperator::PreDec: {
1050 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001051 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001052
1053 QualType T = U->getType();
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001054 unsigned bits = getContext().getTypeSize(T, U->getLocStart());
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001055 APSInt One(llvm::APInt(bits, 1), T->isUnsignedIntegerType());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001056 NonLValue R2 = NonLValue::GetValue(ValMgr, One);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001057
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001058 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001059 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1060 break;
1061 }
1062
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001063 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001064 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1065 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001066 break;
1067 }
1068
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001069 default: ;
1070 assert (false && "Not implemented.");
1071 }
1072 }
1073}
1074
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001075void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1076 GRConstants::NodeTy* Pred,
1077 GRConstants::NodeSet& Dst) {
1078 NodeSet S1;
1079 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001080
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001081 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1082 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001083
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001084 // When getting the value for the LHS, check if we are in an assignment.
1085 // In such cases, we want to (initially) treat the LHS as an LValue,
1086 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001087 // evaluated to LValueDecl's instead of to an NonLValue.
1088 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001089 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1090 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001091
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001092 NodeSet S2;
1093 Visit(B->getRHS(), N1, S2);
1094
1095 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1096 NodeTy* N2 = *I2;
1097 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001098 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001099
1100 switch (B->getOpcode()) {
1101 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001102 const NonLValue& R1 = cast<NonLValue>(V1);
1103 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001104
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001105 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001106 break;
1107 }
1108
1109 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001110 const NonLValue& R1 = cast<NonLValue>(V1);
1111 const NonLValue& R2 = cast<NonLValue>(V2);
1112 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001113 break;
1114 }
1115
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001116 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001117 const NonLValue& R1 = cast<NonLValue>(V1);
1118 const NonLValue& R2 = cast<NonLValue>(V2);
1119 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001120 break;
1121 }
1122
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001123 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001124 const NonLValue& R1 = cast<NonLValue>(V1);
1125 const NonLValue& R2 = cast<NonLValue>(V2);
1126 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001127 break;
1128 }
1129
Ted Kremenekcce207d2008-01-28 22:26:15 +00001130 case BinaryOperator::Rem: {
1131 const NonLValue& R1 = cast<NonLValue>(V1);
1132 const NonLValue& R2 = cast<NonLValue>(V2);
1133 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1134 break;
1135 }
1136
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001137 case BinaryOperator::Assign: {
1138 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001139 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001140 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1141 break;
1142 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001143
1144 case BinaryOperator::AddAssign: {
1145 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001146 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1147 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001148 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1149 break;
1150 }
1151
1152 case BinaryOperator::SubAssign: {
1153 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001154 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1155 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001156 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1157 break;
1158 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001159
1160 case BinaryOperator::MulAssign: {
1161 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001162 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1163 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001164 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1165 break;
1166 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001167
1168 case BinaryOperator::DivAssign: {
1169 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001170 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1171 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001172 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1173 break;
1174 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001175
1176 case BinaryOperator::RemAssign: {
1177 const LValue& L1 = cast<LValue>(V1);
1178 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1179 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1180 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1181 break;
1182 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001183
1184 default:
1185 Dst.Add(N2);
1186 break;
1187 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001188 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001189 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001190}
Ted Kremenekee985462008-01-16 18:18:48 +00001191
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001192
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001193void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1194 GRConstants::NodeSet& Dst) {
1195
1196 // FIXME: add metadata to the CFG so that we can disable
1197 // this check when we KNOW that there is no block-level subexpression.
1198 // The motivation is that this check requires a hashtable lookup.
1199
1200 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1201 Dst.Add(Pred);
1202 return;
1203 }
1204
1205 switch (S->getStmtClass()) {
1206 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001207 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001208 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1209 break;
1210
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001211 case Stmt::UnaryOperatorClass:
1212 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1213 break;
1214
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001215 case Stmt::ParenExprClass:
1216 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1217 break;
1218
Ted Kremenek874d63f2008-01-24 02:02:54 +00001219 case Stmt::ImplicitCastExprClass: {
1220 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1221 VisitCast(C, C->getSubExpr(), Pred, Dst);
1222 break;
1223 }
1224
1225 case Stmt::CastExprClass: {
1226 CastExpr* C = cast<CastExpr>(S);
1227 VisitCast(C, C->getSubExpr(), Pred, Dst);
1228 break;
1229 }
1230
Ted Kremenek9de04c42008-01-24 20:55:43 +00001231 case Stmt::DeclStmtClass:
1232 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1233 break;
1234
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001235 default:
1236 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1237 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001238 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001239}
1240
Ted Kremenekee985462008-01-16 18:18:48 +00001241//===----------------------------------------------------------------------===//
1242// Driver.
1243//===----------------------------------------------------------------------===//
1244
Ted Kremenekaa66a322008-01-16 21:46:15 +00001245#ifndef NDEBUG
1246namespace llvm {
1247template<>
1248struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1249 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001250
1251 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001252 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001253 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001254 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1255 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1256 default: assert (false && "Unknown ValueKey type.");
1257 }
1258 }
1259
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001260 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1261 ValueKey::Kind kind, bool isFirstGroup = false) {
1262 bool isFirst = true;
1263
1264 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1265 if (I.getKey().getKind() != kind)
1266 continue;
1267
1268 if (isFirst) {
1269 if (!isFirstGroup) Out << "\\l\\l";
1270 PrintKindLabel(Out, kind);
1271 isFirst = false;
1272 }
1273 else
1274 Out << "\\l";
1275
1276 Out << ' ';
1277
1278 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1279 Out << V->getName();
1280 else {
1281 Stmt* E = cast<Stmt>(I.getKey());
1282 Out << " (" << (void*) E << ") ";
1283 E->printPretty(Out);
1284 }
1285
1286 Out << " : ";
1287 I.getData().print(Out);
1288 }
1289 }
1290
Ted Kremenekaa66a322008-01-16 21:46:15 +00001291 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1292 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001293
1294 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001295 ProgramPoint Loc = N->getLocation();
1296
1297 switch (Loc.getKind()) {
1298 case ProgramPoint::BlockEntranceKind:
1299 Out << "Block Entrance: B"
1300 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1301 break;
1302
1303 case ProgramPoint::BlockExitKind:
1304 assert (false);
1305 break;
1306
1307 case ProgramPoint::PostStmtKind: {
1308 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001309 Out << L.getStmt()->getStmtClassName() << ':'
1310 << (void*) L.getStmt() << ' ';
1311
Ted Kremenekaa66a322008-01-16 21:46:15 +00001312 L.getStmt()->printPretty(Out);
1313 break;
1314 }
1315
1316 default: {
1317 const BlockEdge& E = cast<BlockEdge>(Loc);
1318 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1319 << E.getDst()->getBlockID() << ')';
1320 }
1321 }
1322
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001323 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001324
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001325 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1326 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001327 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001328
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001329 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001330 return Out.str();
1331 }
1332};
1333} // end llvm namespace
1334#endif
1335
Ted Kremenekee985462008-01-16 18:18:48 +00001336namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001337void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1338 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001339 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001340#ifndef NDEBUG
1341 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
1342#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001343}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001344} // end clang namespace