blob: ad00b608c8870183423ba5e9e0a62e6c400eec51 [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 Kremenek687af802008-01-29 19:43:15 +0000238 APSInt& getValue(const APSInt& X);
239 APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
240 APSInt& getValue(uint64_t X, QualType T,
241 SourceLocation Loc = SourceLocation());
Ted Kremenekd27f8162008-01-15 23:55:06 +0000242};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000243} // end anonymous namespace
244
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000245ValueManager::~ValueManager() {
246 // Note that the dstor for the contents of APSIntSet will never be called,
247 // so we iterate over the set and invoke the dstor for each APSInt. This
248 // frees an aux. memory allocated to represent very large constants.
249 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
250 I->getValue().~APSInt();
251}
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 Kremenek687af802008-01-29 19:43:15 +0000270APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) {
271 APSInt V(BitWidth, isUnsigned);
272 V = X;
273 return getValue(V);
274}
275
276APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) {
277 unsigned bits = Ctx.getTypeSize(T, Loc);
278 APSInt V(bits, T->isUnsignedIntegerType());
279 V = X;
280 return getValue(V);
281}
282
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000283//===----------------------------------------------------------------------===//
284// Expression Values.
285//===----------------------------------------------------------------------===//
Ted Kremenekf13794e2008-01-24 23:19:54 +0000286
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000287namespace {
288
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000289class VISIBILITY_HIDDEN RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000290public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000291 enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
292 UninitializedKind=0x2, InvalidKind=0x3, BaseFlags = 0x3 };
Ted Kremenekf13794e2008-01-24 23:19:54 +0000293
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000294private:
Ted Kremenekf2645622008-01-28 22:25:21 +0000295 void* Data;
Ted Kremenekf13794e2008-01-24 23:19:54 +0000296 unsigned Kind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000297
298protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000299 RValue(const void* d, bool isLValue, unsigned ValKind)
Ted Kremenekf2645622008-01-28 22:25:21 +0000300 : Data(const_cast<void*>(d)),
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000301 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << 2)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000302
Ted Kremenek403c1812008-01-28 22:51:57 +0000303 explicit RValue(BaseKind k)
304 : Data(0), Kind(k) {}
Ted Kremenekf2645622008-01-28 22:25:21 +0000305
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000306 void* getRawPtr() const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000307 return reinterpret_cast<void*>(Data);
308 }
309
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000310public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000311 ~RValue() {};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000312
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000313 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000314
Ted Kremenekf13794e2008-01-24 23:19:54 +0000315 unsigned getRawKind() const { return Kind; }
316 BaseKind getBaseKind() const { return (BaseKind) (Kind & 0x3); }
317 unsigned getSubKind() const { return (Kind & ~0x3) >> 2; }
318
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000319 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000320 ID.AddInteger((unsigned) getRawKind());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000321 ID.AddPointer(reinterpret_cast<void*>(Data));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000322 }
323
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000324 bool operator==(const RValue& RHS) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000325 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000326 }
327
Ted Kremenekf13794e2008-01-24 23:19:54 +0000328 inline bool isValid() const { return getRawKind() != InvalidKind; }
329 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
330
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000331 void print(std::ostream& OS) const;
332 void print() const { print(*llvm::cerr.stream()); }
333
334 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000335 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000336};
337
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000338class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000339public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000340 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000341
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000342 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000343 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000344 }
345};
Ted Kremenek403c1812008-01-28 22:51:57 +0000346
347class VISIBILITY_HIDDEN UninitializedValue : public RValue {
348public:
349 UninitializedValue() : RValue(UninitializedKind) {}
350
351 static inline bool classof(const RValue* V) {
352 return V->getBaseKind() == UninitializedKind;
353 }
354};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000355
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000356class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000357protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000358 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000359
360public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000361 void print(std::ostream& Out) const;
362
Ted Kremenek687af802008-01-29 19:43:15 +0000363 // Arithmetic operators.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000364 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
365 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
366 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
367 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
368 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
369 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000370
Ted Kremenek687af802008-01-29 19:43:15 +0000371 // Equality operators.
372 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
373 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000374
Ted Kremenek687af802008-01-29 19:43:15 +0000375 // Utility methods to create NonLValues.
376 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
377 SourceLocation Loc = SourceLocation());
378
379 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenek68fd2572008-01-29 17:27:31 +0000380 static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000381
Ted Kremenek687af802008-01-29 19:43:15 +0000382 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
383 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
384 }
385
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000386 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000387 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000388 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000389 }
390};
Ted Kremenek687af802008-01-29 19:43:15 +0000391
392class VISIBILITY_HIDDEN LValue : public RValue {
393protected:
394 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
395
396public:
397
398 // Equality operators.
399 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
400 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
401
402 // Implement isa<T> support.
403 static inline bool classof(const RValue* V) {
404 return V->getBaseKind() == LValueKind;
405 }
406};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000407
408} // end anonymous namespace
409
410//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000411// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000412//===----------------------------------------------------------------------===//
413
414namespace {
415
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000416enum { LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000417
418class VISIBILITY_HIDDEN LValueDecl : public LValue {
419public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000420 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000421 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000422
423 ValueDecl* getDecl() const {
424 return static_cast<ValueDecl*>(getRawPtr());
425 }
426
Ted Kremenek687af802008-01-29 19:43:15 +0000427 inline bool operator==(const LValueDecl& R) const {
428 return getDecl() == R.getDecl();
429 }
430
431 inline bool operator!=(const LValueDecl& R) const {
432 return getDecl() != R.getDecl();
433 }
434
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000435 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000436 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000437 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000438 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000439};
440
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000441} // end anonymous namespace
442
443//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000444// Non-LValues.
445//===----------------------------------------------------------------------===//
446
447namespace {
448
Ted Kremenekf2645622008-01-28 22:25:21 +0000449enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
450 NumNonLValueKind };
451
452class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
453public:
454 SymbolicNonLValue(unsigned SymID)
455 : NonLValue(SymbolicNonLValueKind,
456 reinterpret_cast<void*>((uintptr_t) SymID)) {}
457
458 SymbolID getSymbolID() const {
459 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
460 }
461
462 static inline bool classof(const RValue* V) {
463 return V->getSubKind() == SymbolicNonLValueKind;
464 }
465};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000466
467class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
468public:
469 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
470
471 const APSInt& getValue() const {
472 return *static_cast<APSInt*>(getRawPtr());
473 }
474
Ted Kremenek687af802008-01-29 19:43:15 +0000475 // Arithmetic operators.
476
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000477 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
478 return ValMgr.getValue(getValue() + V.getValue());
479 }
Ted Kremenek687af802008-01-29 19:43:15 +0000480
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000481 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
482 return ValMgr.getValue(getValue() - V.getValue());
483 }
484
485 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
486 return ValMgr.getValue(getValue() * V.getValue());
487 }
488
489 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
490 return ValMgr.getValue(getValue() / V.getValue());
491 }
492
493 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
494 return ValMgr.getValue(getValue() % V.getValue());
495 }
496
Ted Kremenek687af802008-01-29 19:43:15 +0000497 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
498 assert (U->getType() == U->getSubExpr()->getType());
499 assert (U->getType()->isIntegerType());
500 return ValMgr.getValue(-getValue());
501 }
502
503 // Casting.
504
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000505 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
506 assert (CastExpr->getType()->isIntegerType());
507
508 APSInt X(getValue());
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000509 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
510 CastExpr->getLocStart()));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000511 return ValMgr.getValue(X);
512 }
513
Ted Kremenek687af802008-01-29 19:43:15 +0000514 // Equality operators.
515
516 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
517 const APSInt& Val = getValue();
518 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
519 Val.getBitWidth(), Val.isUnsigned());
520 }
521
522 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
523 const APSInt& Val = getValue();
524 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
525 Val.getBitWidth(), Val.isUnsigned());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000526 }
527
528 // Implement isa<T> support.
529 static inline bool classof(const RValue* V) {
530 return V->getSubKind() == ConcreteIntKind;
531 }
532};
533
534} // end anonymous namespace
535
536//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000537// Transfer function dispatch for Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000538//===----------------------------------------------------------------------===//
539
540RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
541 switch (getSubKind()) {
542 case ConcreteIntKind:
543 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
544 default:
545 return InvalidValue();
546 }
547}
548
549NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
550 switch (getSubKind()) {
551 case ConcreteIntKind:
552 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
553 default:
554 return cast<NonLValue>(InvalidValue());
555 }
556}
557
Ted Kremenek687af802008-01-29 19:43:15 +0000558#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000559case (k1##Kind*NumNonLValueKind+k2##Kind):\
560 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
561
Ted Kremenek687af802008-01-29 19:43:15 +0000562#define NONLVALUE_DISPATCH(Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000563switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
Ted Kremenek687af802008-01-29 19:43:15 +0000564 NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000565 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000566 if (getBaseKind() == UninitializedKind ||\
567 RHS.getBaseKind() == UninitializedKind)\
568 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000569 assert (!isValid() || !RHS.isValid() && "Missing case.");\
570 break;\
571}\
572return cast<NonLValue>(InvalidValue());
573
574NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000575 NONLVALUE_DISPATCH(Add)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000576}
577
578NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000579 NONLVALUE_DISPATCH(Sub)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000580}
581
582NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000583 NONLVALUE_DISPATCH(Mul)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000584}
585
586NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000587 NONLVALUE_DISPATCH(Div)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000588}
589
590NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000591 NONLVALUE_DISPATCH(Rem)
592}
593
594NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
595 NONLVALUE_DISPATCH(EQ)
596}
597
598NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
599 NONLVALUE_DISPATCH(NE)
600}
601
602#undef NONLVALUE_DISPATCH_CASE
603#undef NONLVALUE_DISPATCH
604
605//===----------------------------------------------------------------------===//
606// Transfer function dispatch for LValues.
607//===----------------------------------------------------------------------===//
608
609
610NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
611 if (getSubKind() != RHS.getSubKind())
612 return NonLValue::GetIntTruthValue(ValMgr, false);
613
614 switch (getSubKind()) {
615 default:
616 assert(false && "EQ not implemented for this LValue.");
617 return cast<NonLValue>(InvalidValue());
618
619 case LValueDeclKind: {
620 bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS);
621 return NonLValue::GetIntTruthValue(ValMgr, b);
622 }
623 }
624}
625
626NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
627 if (getSubKind() != RHS.getSubKind())
Ted Kremenek03701672008-01-29 21:27:49 +0000628 return NonLValue::GetIntTruthValue(ValMgr, true);
Ted Kremenek687af802008-01-29 19:43:15 +0000629
630 switch (getSubKind()) {
631 default:
632 assert(false && "EQ not implemented for this LValue.");
633 return cast<NonLValue>(InvalidValue());
634
635 case LValueDeclKind: {
636 bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
637 return NonLValue::GetIntTruthValue(ValMgr, b);
638 }
639 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000640}
641
642
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000643//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000644// Utility methods for constructing Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000645//===----------------------------------------------------------------------===//
646
Ted Kremenek687af802008-01-29 19:43:15 +0000647NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
648 SourceLocation Loc) {
649
650 return ConcreteInt(ValMgr.getValue(X, T, Loc));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000651}
652
653NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
654 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
655 I->getType()->isUnsignedIntegerType())));
656}
657
Ted Kremenek68fd2572008-01-29 17:27:31 +0000658NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
659 return SymbolicNonLValue(SymMgr.getSymbol(D));
660}
661
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000662//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000663// Pretty-Printing.
664//===----------------------------------------------------------------------===//
665
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000666void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000667 switch (getBaseKind()) {
668 case InvalidKind:
669 Out << "Invalid";
670 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000671
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000672 case NonLValueKind:
673 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000674 break;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000675
Ted Kremenekf13794e2008-01-24 23:19:54 +0000676 case LValueKind:
677 assert (false && "FIXME: LValue printing not implemented.");
678 break;
679
Ted Kremenek403c1812008-01-28 22:51:57 +0000680 case UninitializedKind:
681 Out << "Uninitialized";
682 break;
683
Ted Kremenekf13794e2008-01-24 23:19:54 +0000684 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000685 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000686 }
687}
688
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000689void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000690 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000691 case ConcreteIntKind:
692 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000693 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000694
Ted Kremenek68fd2572008-01-29 17:27:31 +0000695 case SymbolicNonLValueKind:
696 Out << "sym-" << cast<SymbolicNonLValue>(this)->getSymbolID();
697 break;
698
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000699 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000700 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000701 break;
702 }
703}
704
705//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000706// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000707//===----------------------------------------------------------------------===//
708
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000710
711namespace clang {
712 template<>
713 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
714 static inline void* toPtr(ValueMapTy M) {
715 return reinterpret_cast<void*>(M.getRoot());
716 }
717 static inline ValueMapTy toState(void* P) {
718 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
719 }
720 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000721}
722
723//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000724// The Checker.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000725//===----------------------------------------------------------------------===//
726
727namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000728
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000729class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000730
731public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000732 typedef ValueMapTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000733 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
734 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000735 typedef ExplodedGraph<GRConstants> GraphTy;
736 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000737
738 class NodeSet {
739 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
740 ImplTy Impl;
741 public:
742
743 NodeSet() {}
744 NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); }
745
746 void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); }
747
748 typedef ImplTy::iterator iterator;
749 typedef ImplTy::const_iterator const_iterator;
750
751 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000752 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000753
754 iterator begin() { return Impl.begin(); }
755 iterator end() { return Impl.end(); }
756
757 const_iterator begin() const { return Impl.begin(); }
758 const_iterator end() const { return Impl.end(); }
759 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000760
761protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000762 /// G - the simulation graph.
763 GraphTy& G;
764
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000765 /// Liveness - live-variables information the ValueDecl* and block-level
766 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000767 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000768
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000769 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000770 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000771 StmtNodeBuilder* Builder;
772
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000773 /// StateMgr - Object that manages the data for all created states.
774 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000775
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000776 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000777 ValueManager ValMgr;
778
Ted Kremenek68fd2572008-01-29 17:27:31 +0000779 /// SymMgr - Object that manages the symbol information.
780 SymbolManager SymMgr;
781
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000782 /// StmtEntryNode - The immediate predecessor node.
783 NodeTy* StmtEntryNode;
784
785 /// CurrentStmt - The current block-level statement.
786 Stmt* CurrentStmt;
787
788 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000789
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000790 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000791
Ted Kremenekd27f8162008-01-15 23:55:06 +0000792public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000793 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
794 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
795 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000796
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000797 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000798 Liveness.runOnCFG(G.getCFG());
799 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000800 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000801
802 /// getCFG - Returns the CFG associated with this analysis.
803 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000804
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000805 /// getInitialState - Return the initial state used for the root vertex
806 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000807 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000808 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000809
810 // Iterate the parameters.
811 FunctionDecl& F = G.getFunctionDecl();
812
813 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
814 I!=E; ++I) {
815
816 // For now we only support symbolic values for non-pointer types.
817 if ((*I)->getType()->isPointerType() ||
818 (*I)->getType()->isReferenceType())
819 continue;
820
821 // FIXME: Set these values to a symbol, not Uninitialized.
Ted Kremenek68fd2572008-01-29 17:27:31 +0000822 St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000823 }
824
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000825 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000826 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000827
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000828 /// ProcessStmt - Called by GREngine. Used to generate new successor
829 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000830 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
831
832 /// ProcessBranch - Called by GREngine. Used to generate successor
833 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000834 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000835
836 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
837 /// that all subexpression mappings are removed and that any
838 /// block-level expressions that are not live at 'S' also have their
839 /// mappings removed.
840 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
841
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000842 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000843
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000844 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000845 return SetValue(St, const_cast<Stmt*>(S), V);
846 }
847
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000848 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000849
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000850 RValue GetValue(const StateTy& St, Stmt* S);
851 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000852 return GetValue(St, const_cast<Stmt*>(S));
853 }
854
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000855 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000856 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000857
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000858 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000859
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000860 /// Visit - Transfer function logic for all statements. Dispatches to
861 /// other functions that handle specific kinds of statements.
862 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000863
864 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
865 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000866
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000867 /// VisitUnaryOperator - Transfer function logic for unary operators.
868 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
869
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000870 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000871 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
872
873 /// VisitDeclStmt - Transfer function logic for DeclStmts.
874 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000875};
876} // end anonymous namespace
877
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000878
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000879void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
880 BranchNodeBuilder& builder) {
881
882
883}
884
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000885void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000886 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000887
888 StmtEntryNode = builder.getLastNode();
889 CurrentStmt = S;
890 NodeSet Dst;
891 StateCleaned = false;
892
893 Visit(S, StmtEntryNode, Dst);
894
895 // If no nodes were generated, generate a new node that has all the
896 // dead mappings removed.
897 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
898 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
899 builder.generateNode(S, St, StmtEntryNode);
900 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000901
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000902 CurrentStmt = NULL;
903 StmtEntryNode = NULL;
904 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000905}
906
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000907
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000908RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000909 switch (LV.getSubKind()) {
910 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000911 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
912 return T ? T->getValue().second : InvalidValue();
913 }
914 default:
915 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000916 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000917 }
918
919 return InvalidValue();
920}
921
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000922RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +0000923 for (;;) {
924 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000925
926 // ParenExprs are no-ops.
927
Ted Kremenek671c9e82008-01-24 00:50:08 +0000928 case Stmt::ParenExprClass:
929 S = cast<ParenExpr>(S)->getSubExpr();
930 continue;
931
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000932 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
933 // (assuming an implicit "load") depending on the context. In this
934 // context we assume that we are retrieving the value contained
935 // within the referenced variables.
936
Ted Kremenek671c9e82008-01-24 00:50:08 +0000937 case Stmt::DeclRefExprClass:
938 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +0000939
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000940 // Integer literals evaluate to an RValue. Simply retrieve the
941 // RValue for the literal.
942
Ted Kremenek671c9e82008-01-24 00:50:08 +0000943 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000944 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
945
946 // Casts where the source and target type are the same
947 // are no-ops. We blast through these to get the descendant
948 // subexpression that has a value.
949
Ted Kremenek874d63f2008-01-24 02:02:54 +0000950 case Stmt::ImplicitCastExprClass: {
951 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
952 if (C->getType() == C->getSubExpr()->getType()) {
953 S = C->getSubExpr();
954 continue;
955 }
956 break;
957 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000958
Ted Kremenek874d63f2008-01-24 02:02:54 +0000959 case Stmt::CastExprClass: {
960 CastExpr* C = cast<CastExpr>(S);
961 if (C->getType() == C->getSubExpr()->getType()) {
962 S = C->getSubExpr();
963 continue;
964 }
965 break;
966 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000967
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000968 // Handle all other Stmt* using a lookup.
969
Ted Kremenek671c9e82008-01-24 00:50:08 +0000970 default:
971 break;
972 };
973
974 break;
975 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000976
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000977 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000978
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000979 return T ? T->getValue().second : InvalidValue();
980}
981
982LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000983 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
984 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000985
986 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
987 return LValueDecl(DR->getDecl());
988
989 return cast<LValue>(GetValue(St, S));
990}
991
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000992
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000993GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000994 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000995 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000996
997 if (!StateCleaned) {
998 St = RemoveDeadBindings(CurrentStmt, St);
999 StateCleaned = true;
1000 }
1001
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001002 bool isBlkExpr = false;
1003
1004 if (S == CurrentStmt) {
1005 isBlkExpr = getCFG().isBlkExpr(S);
1006
1007 if (!isBlkExpr)
1008 return St;
1009 }
Ted Kremenekdaadf452008-01-24 19:28:01 +00001010
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001011 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1012 : St;
1013}
1014
1015GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001016 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001017 if (!LV.isValid())
1018 return St;
1019
1020 if (!StateCleaned) {
1021 St = RemoveDeadBindings(CurrentStmt, St);
1022 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001023 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001024
Ted Kremenekf13794e2008-01-24 23:19:54 +00001025 switch (LV.getSubKind()) {
1026 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001027 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1028 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1029
1030 default:
1031 assert ("SetValue for given LValue type not yet implemented.");
1032 return St;
1033 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001034}
1035
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001036GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001037 // Note: in the code below, we can assign a new map to M since the
1038 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001039 StateTy::iterator I = M.begin(), E = M.end();
1040
Ted Kremenekf84469b2008-01-18 00:41:32 +00001041
Ted Kremenek65cac132008-01-29 05:25:31 +00001042 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1043 // Remove old bindings for subexpressions and "dead"
1044 // block-level expressions.
1045 if (I.getKey().isSubExpr() ||
1046 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1047 M = StateMgr.Remove(M, I.getKey());
1048 }
1049 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1050 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1051 if (!Liveness.isLive(Loc, V))
1052 M = StateMgr.Remove(M, I.getKey());
1053 }
1054 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001055
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001056 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001057}
1058
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001059void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1060 GRConstants::StateTy St) {
1061
1062 // If the state hasn't changed, don't generate a new node.
1063 if (St == Pred->getState())
1064 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001065
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001066 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001067}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001068
Ted Kremenek874d63f2008-01-24 02:02:54 +00001069void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1070 GRConstants::NodeSet& Dst) {
1071
1072 QualType T = CastE->getType();
1073
1074 // Check for redundant casts.
1075 if (E->getType() == T) {
1076 Dst.Add(Pred);
1077 return;
1078 }
1079
1080 NodeSet S1;
1081 Visit(E, Pred, S1);
1082
1083 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1084 NodeTy* N = *I1;
1085 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001086 const RValue& V = GetValue(St, E);
1087 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001088 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001089}
1090
1091void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1092 GRConstants::NodeSet& Dst) {
1093
1094 StateTy St = Pred->getState();
1095
1096 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001097 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1098 const Expr* E = VD->getInit();
1099 St = SetValue(St, LValueDecl(VD),
1100 E ? GetValue(St, E) : UninitializedValue());
1101 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001102
1103 Nodify(Dst, DS, Pred, St);
1104
1105 if (Dst.empty())
1106 Dst.Add(Pred);
1107}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001108
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001109void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1110 GRConstants::NodeTy* Pred,
1111 GRConstants::NodeSet& Dst) {
1112 NodeSet S1;
1113 Visit(U->getSubExpr(), Pred, S1);
1114
1115 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1116 NodeTy* N1 = *I1;
1117 StateTy St = N1->getState();
1118
1119 switch (U->getOpcode()) {
1120 case UnaryOperator::PostInc: {
1121 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001122 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001123 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1124 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001125
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001126 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001127 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1128 break;
1129 }
1130
1131 case UnaryOperator::PostDec: {
1132 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001133 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001134 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1135 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001136
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001137 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001138 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1139 break;
1140 }
1141
1142 case UnaryOperator::PreInc: {
1143 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001144 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001145 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1146 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001147
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001148 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001149 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1150 break;
1151 }
1152
1153 case UnaryOperator::PreDec: {
1154 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001155 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001156 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1157 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001158
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001159 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001160 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1161 break;
1162 }
1163
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001164 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001165 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1166 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001167 break;
1168 }
1169
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001170 default: ;
1171 assert (false && "Not implemented.");
1172 }
1173 }
1174}
1175
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001176void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1177 GRConstants::NodeTy* Pred,
1178 GRConstants::NodeSet& Dst) {
1179 NodeSet S1;
1180 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001181
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001182 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1183 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001184
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001185 // When getting the value for the LHS, check if we are in an assignment.
1186 // In such cases, we want to (initially) treat the LHS as an LValue,
1187 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001188 // evaluated to LValueDecl's instead of to an NonLValue.
1189 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001190 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1191 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001192
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001193 NodeSet S2;
1194 Visit(B->getRHS(), N1, S2);
1195
1196 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1197 NodeTy* N2 = *I2;
1198 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001199 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001200
1201 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001202 default:
1203 Dst.Add(N2);
1204 break;
1205
1206 // Arithmetic opreators.
1207
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001208 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001209 const NonLValue& R1 = cast<NonLValue>(V1);
1210 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001211
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001212 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001213 break;
1214 }
1215
1216 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001217 const NonLValue& R1 = cast<NonLValue>(V1);
1218 const NonLValue& R2 = cast<NonLValue>(V2);
1219 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001220 break;
1221 }
1222
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001223 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001224 const NonLValue& R1 = cast<NonLValue>(V1);
1225 const NonLValue& R2 = cast<NonLValue>(V2);
1226 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001227 break;
1228 }
1229
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001230 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001231 const NonLValue& R1 = cast<NonLValue>(V1);
1232 const NonLValue& R2 = cast<NonLValue>(V2);
1233 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001234 break;
1235 }
1236
Ted Kremenekcce207d2008-01-28 22:26:15 +00001237 case BinaryOperator::Rem: {
1238 const NonLValue& R1 = cast<NonLValue>(V1);
1239 const NonLValue& R2 = cast<NonLValue>(V2);
1240 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1241 break;
1242 }
1243
Ted Kremenek687af802008-01-29 19:43:15 +00001244 // Assignment operators.
1245
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001246 case BinaryOperator::Assign: {
1247 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001248 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001249 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1250 break;
1251 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001252
1253 case BinaryOperator::AddAssign: {
1254 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001255 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1256 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001257 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1258 break;
1259 }
1260
1261 case BinaryOperator::SubAssign: {
1262 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001263 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1264 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001265 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1266 break;
1267 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001268
1269 case BinaryOperator::MulAssign: {
1270 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001271 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1272 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001273 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1274 break;
1275 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001276
1277 case BinaryOperator::DivAssign: {
1278 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001279 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1280 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001281 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1282 break;
1283 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001284
1285 case BinaryOperator::RemAssign: {
1286 const LValue& L1 = cast<LValue>(V1);
1287 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1288 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1289 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1290 break;
1291 }
Ted Kremenek687af802008-01-29 19:43:15 +00001292
1293 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001294
Ted Kremenek687af802008-01-29 19:43:15 +00001295 case BinaryOperator::EQ:
1296 // FIXME: should we allow XX.EQ() to return a set of values,
1297 // allowing state bifurcation? In such cases, they will also
1298 // modify the state (meaning that a new state will be returned
1299 // as well).
1300 assert (B->getType() == getContext().IntTy);
1301
1302 if (isa<LValue>(V1)) {
1303 const LValue& L1 = cast<LValue>(V1);
1304 const LValue& L2 = cast<LValue>(V2);
1305 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1306 }
1307 else {
1308 const NonLValue& R1 = cast<NonLValue>(V1);
1309 const NonLValue& R2 = cast<NonLValue>(V2);
1310 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1311 }
1312
1313 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001314 break;
1315 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001316 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001317 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001318}
Ted Kremenekee985462008-01-16 18:18:48 +00001319
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001320
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001321void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1322 GRConstants::NodeSet& Dst) {
1323
1324 // FIXME: add metadata to the CFG so that we can disable
1325 // this check when we KNOW that there is no block-level subexpression.
1326 // The motivation is that this check requires a hashtable lookup.
1327
1328 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1329 Dst.Add(Pred);
1330 return;
1331 }
1332
1333 switch (S->getStmtClass()) {
1334 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001335 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001336 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1337 break;
1338
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001339 case Stmt::UnaryOperatorClass:
1340 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1341 break;
1342
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001343 case Stmt::ParenExprClass:
1344 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1345 break;
1346
Ted Kremenek874d63f2008-01-24 02:02:54 +00001347 case Stmt::ImplicitCastExprClass: {
1348 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1349 VisitCast(C, C->getSubExpr(), Pred, Dst);
1350 break;
1351 }
1352
1353 case Stmt::CastExprClass: {
1354 CastExpr* C = cast<CastExpr>(S);
1355 VisitCast(C, C->getSubExpr(), Pred, Dst);
1356 break;
1357 }
1358
Ted Kremenek9de04c42008-01-24 20:55:43 +00001359 case Stmt::DeclStmtClass:
1360 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1361 break;
1362
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001363 default:
1364 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1365 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001366 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001367}
1368
Ted Kremenekee985462008-01-16 18:18:48 +00001369//===----------------------------------------------------------------------===//
1370// Driver.
1371//===----------------------------------------------------------------------===//
1372
Ted Kremenekaa66a322008-01-16 21:46:15 +00001373#ifndef NDEBUG
1374namespace llvm {
1375template<>
1376struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1377 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001378
1379 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001380 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001381 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001382 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1383 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1384 default: assert (false && "Unknown ValueKey type.");
1385 }
1386 }
1387
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001388 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1389 ValueKey::Kind kind, bool isFirstGroup = false) {
1390 bool isFirst = true;
1391
1392 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1393 if (I.getKey().getKind() != kind)
1394 continue;
1395
1396 if (isFirst) {
1397 if (!isFirstGroup) Out << "\\l\\l";
1398 PrintKindLabel(Out, kind);
1399 isFirst = false;
1400 }
1401 else
1402 Out << "\\l";
1403
1404 Out << ' ';
1405
1406 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1407 Out << V->getName();
1408 else {
1409 Stmt* E = cast<Stmt>(I.getKey());
1410 Out << " (" << (void*) E << ") ";
1411 E->printPretty(Out);
1412 }
1413
1414 Out << " : ";
1415 I.getData().print(Out);
1416 }
1417 }
1418
Ted Kremenekaa66a322008-01-16 21:46:15 +00001419 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1420 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001421
1422 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001423 ProgramPoint Loc = N->getLocation();
1424
1425 switch (Loc.getKind()) {
1426 case ProgramPoint::BlockEntranceKind:
1427 Out << "Block Entrance: B"
1428 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1429 break;
1430
1431 case ProgramPoint::BlockExitKind:
1432 assert (false);
1433 break;
1434
1435 case ProgramPoint::PostStmtKind: {
1436 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001437 Out << L.getStmt()->getStmtClassName() << ':'
1438 << (void*) L.getStmt() << ' ';
1439
Ted Kremenekaa66a322008-01-16 21:46:15 +00001440 L.getStmt()->printPretty(Out);
1441 break;
1442 }
1443
1444 default: {
1445 const BlockEdge& E = cast<BlockEdge>(Loc);
1446 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1447 << E.getDst()->getBlockID() << ')';
1448 }
1449 }
1450
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001451 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001452
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001453 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1454 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001455 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001456
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001457 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001458 return Out.str();
1459 }
1460};
1461} // end llvm namespace
1462#endif
1463
Ted Kremenekee985462008-01-16 18:18:48 +00001464namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001465void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1466 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001467 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001468#ifndef NDEBUG
1469 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
1470#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001471}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001472} // end clang namespace