blob: 5ef4c0ba3c8691505d969855da80b502c94c0ef7 [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())
628 return NonLValue::GetIntTruthValue(ValMgr, false);
629
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 Kremenekd27f8162008-01-15 23:55:06 +0000733 typedef GRNodeBuilder<GRConstants> NodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000734 typedef ExplodedGraph<GRConstants> GraphTy;
735 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000736
737 class NodeSet {
738 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
739 ImplTy Impl;
740 public:
741
742 NodeSet() {}
743 NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); }
744
745 void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); }
746
747 typedef ImplTy::iterator iterator;
748 typedef ImplTy::const_iterator const_iterator;
749
750 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000751 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000752
753 iterator begin() { return Impl.begin(); }
754 iterator end() { return Impl.end(); }
755
756 const_iterator begin() const { return Impl.begin(); }
757 const_iterator end() const { return Impl.end(); }
758 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000759
760protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000761 /// G - the simulation graph.
762 GraphTy& G;
763
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000764 /// Liveness - live-variables information the ValueDecl* and block-level
765 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000766 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000767
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000768 /// Builder - The current GRNodeBuilder which is used when building the nodes
769 /// for a given statement.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000770 NodeBuilder* Builder;
771
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000772 /// StateMgr - Object that manages the data for all created states.
773 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000774
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000775 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000776 ValueManager ValMgr;
777
Ted Kremenek68fd2572008-01-29 17:27:31 +0000778 /// SymMgr - Object that manages the symbol information.
779 SymbolManager SymMgr;
780
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000781 /// StmtEntryNode - The immediate predecessor node.
782 NodeTy* StmtEntryNode;
783
784 /// CurrentStmt - The current block-level statement.
785 Stmt* CurrentStmt;
786
787 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000788
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000789 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000790
Ted Kremenekd27f8162008-01-15 23:55:06 +0000791public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000792 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
793 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
794 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000795
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000796 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000797 Liveness.runOnCFG(G.getCFG());
798 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000799 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000800
801 /// getCFG - Returns the CFG associated with this analysis.
802 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000803
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000804 /// getInitialState - Return the initial state used for the root vertex
805 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000806 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000807 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000808
809 // Iterate the parameters.
810 FunctionDecl& F = G.getFunctionDecl();
811
812 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
813 I!=E; ++I) {
814
815 // For now we only support symbolic values for non-pointer types.
816 if ((*I)->getType()->isPointerType() ||
817 (*I)->getType()->isReferenceType())
818 continue;
819
820 // FIXME: Set these values to a symbol, not Uninitialized.
Ted Kremenek68fd2572008-01-29 17:27:31 +0000821 St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000822 }
823
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000824 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000826
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000827 /// ProcessStmt - Called by GREngine. Used to generate new successor
828 /// nodes by processing the 'effects' of a block-level statement.
829 void ProcessStmt(Stmt* S, NodeBuilder& builder);
830
831 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
832 /// that all subexpression mappings are removed and that any
833 /// block-level expressions that are not live at 'S' also have their
834 /// mappings removed.
835 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
836
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000837 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000838
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000839 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000840 return SetValue(St, const_cast<Stmt*>(S), V);
841 }
842
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000843 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000844
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000845 RValue GetValue(const StateTy& St, Stmt* S);
846 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000847 return GetValue(St, const_cast<Stmt*>(S));
848 }
849
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000850 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000851 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000852
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000853 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000854
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000855 /// Visit - Transfer function logic for all statements. Dispatches to
856 /// other functions that handle specific kinds of statements.
857 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000858
859 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
860 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000862 /// VisitUnaryOperator - Transfer function logic for unary operators.
863 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
864
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000865 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000866 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
867
868 /// VisitDeclStmt - Transfer function logic for DeclStmts.
869 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000870};
871} // end anonymous namespace
872
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000873
Ted Kremenekd27f8162008-01-15 23:55:06 +0000874void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) {
875 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000876
877 StmtEntryNode = builder.getLastNode();
878 CurrentStmt = S;
879 NodeSet Dst;
880 StateCleaned = false;
881
882 Visit(S, StmtEntryNode, Dst);
883
884 // If no nodes were generated, generate a new node that has all the
885 // dead mappings removed.
886 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
887 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
888 builder.generateNode(S, St, StmtEntryNode);
889 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000890
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000891 CurrentStmt = NULL;
892 StmtEntryNode = NULL;
893 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000894}
895
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000896
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000897RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000898 switch (LV.getSubKind()) {
899 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000900 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
901 return T ? T->getValue().second : InvalidValue();
902 }
903 default:
904 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000905 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000906 }
907
908 return InvalidValue();
909}
910
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000911RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +0000912 for (;;) {
913 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000914
915 // ParenExprs are no-ops.
916
Ted Kremenek671c9e82008-01-24 00:50:08 +0000917 case Stmt::ParenExprClass:
918 S = cast<ParenExpr>(S)->getSubExpr();
919 continue;
920
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000921 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
922 // (assuming an implicit "load") depending on the context. In this
923 // context we assume that we are retrieving the value contained
924 // within the referenced variables.
925
Ted Kremenek671c9e82008-01-24 00:50:08 +0000926 case Stmt::DeclRefExprClass:
927 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +0000928
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000929 // Integer literals evaluate to an RValue. Simply retrieve the
930 // RValue for the literal.
931
Ted Kremenek671c9e82008-01-24 00:50:08 +0000932 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000933 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
934
935 // Casts where the source and target type are the same
936 // are no-ops. We blast through these to get the descendant
937 // subexpression that has a value.
938
Ted Kremenek874d63f2008-01-24 02:02:54 +0000939 case Stmt::ImplicitCastExprClass: {
940 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
941 if (C->getType() == C->getSubExpr()->getType()) {
942 S = C->getSubExpr();
943 continue;
944 }
945 break;
946 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000947
Ted Kremenek874d63f2008-01-24 02:02:54 +0000948 case Stmt::CastExprClass: {
949 CastExpr* C = cast<CastExpr>(S);
950 if (C->getType() == C->getSubExpr()->getType()) {
951 S = C->getSubExpr();
952 continue;
953 }
954 break;
955 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000956
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000957 // Handle all other Stmt* using a lookup.
958
Ted Kremenek671c9e82008-01-24 00:50:08 +0000959 default:
960 break;
961 };
962
963 break;
964 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000965
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000966 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000967
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000968 return T ? T->getValue().second : InvalidValue();
969}
970
971LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000972 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
973 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000974
975 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
976 return LValueDecl(DR->getDecl());
977
978 return cast<LValue>(GetValue(St, S));
979}
980
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000981
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000982GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000983 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000984 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000985
986 if (!StateCleaned) {
987 St = RemoveDeadBindings(CurrentStmt, St);
988 StateCleaned = true;
989 }
990
Ted Kremenek9ff731d2008-01-24 22:27:20 +0000991 bool isBlkExpr = false;
992
993 if (S == CurrentStmt) {
994 isBlkExpr = getCFG().isBlkExpr(S);
995
996 if (!isBlkExpr)
997 return St;
998 }
Ted Kremenekdaadf452008-01-24 19:28:01 +0000999
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001000 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1001 : St;
1002}
1003
1004GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001005 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001006 if (!LV.isValid())
1007 return St;
1008
1009 if (!StateCleaned) {
1010 St = RemoveDeadBindings(CurrentStmt, St);
1011 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001012 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001013
Ted Kremenekf13794e2008-01-24 23:19:54 +00001014 switch (LV.getSubKind()) {
1015 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001016 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1017 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1018
1019 default:
1020 assert ("SetValue for given LValue type not yet implemented.");
1021 return St;
1022 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001023}
1024
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001025GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001026 // Note: in the code below, we can assign a new map to M since the
1027 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001028 StateTy::iterator I = M.begin(), E = M.end();
1029
Ted Kremenekf84469b2008-01-18 00:41:32 +00001030
Ted Kremenek65cac132008-01-29 05:25:31 +00001031 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1032 // Remove old bindings for subexpressions and "dead"
1033 // block-level expressions.
1034 if (I.getKey().isSubExpr() ||
1035 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1036 M = StateMgr.Remove(M, I.getKey());
1037 }
1038 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1039 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1040 if (!Liveness.isLive(Loc, V))
1041 M = StateMgr.Remove(M, I.getKey());
1042 }
1043 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001044
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001045 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001046}
1047
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001048void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1049 GRConstants::StateTy St) {
1050
1051 // If the state hasn't changed, don't generate a new node.
1052 if (St == Pred->getState())
1053 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001054
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001055 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001056}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001057
Ted Kremenek874d63f2008-01-24 02:02:54 +00001058void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1059 GRConstants::NodeSet& Dst) {
1060
1061 QualType T = CastE->getType();
1062
1063 // Check for redundant casts.
1064 if (E->getType() == T) {
1065 Dst.Add(Pred);
1066 return;
1067 }
1068
1069 NodeSet S1;
1070 Visit(E, Pred, S1);
1071
1072 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1073 NodeTy* N = *I1;
1074 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001075 const RValue& V = GetValue(St, E);
1076 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001077 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001078}
1079
1080void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1081 GRConstants::NodeSet& Dst) {
1082
1083 StateTy St = Pred->getState();
1084
1085 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001086 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1087 const Expr* E = VD->getInit();
1088 St = SetValue(St, LValueDecl(VD),
1089 E ? GetValue(St, E) : UninitializedValue());
1090 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001091
1092 Nodify(Dst, DS, Pred, St);
1093
1094 if (Dst.empty())
1095 Dst.Add(Pred);
1096}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001097
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001098void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1099 GRConstants::NodeTy* Pred,
1100 GRConstants::NodeSet& Dst) {
1101 NodeSet S1;
1102 Visit(U->getSubExpr(), Pred, S1);
1103
1104 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1105 NodeTy* N1 = *I1;
1106 StateTy St = N1->getState();
1107
1108 switch (U->getOpcode()) {
1109 case UnaryOperator::PostInc: {
1110 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001111 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001112 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1113 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001114
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001115 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001116 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1117 break;
1118 }
1119
1120 case UnaryOperator::PostDec: {
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 Kremenek7b8009a2008-01-24 02:28:56 +00001125
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001126 NonLValue Result = R1.Sub(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::PreInc: {
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.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001138 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1139 break;
1140 }
1141
1142 case UnaryOperator::PreDec: {
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.Sub(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
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001153 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001154 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1155 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001156 break;
1157 }
1158
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001159 default: ;
1160 assert (false && "Not implemented.");
1161 }
1162 }
1163}
1164
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001165void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1166 GRConstants::NodeTy* Pred,
1167 GRConstants::NodeSet& Dst) {
1168 NodeSet S1;
1169 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001170
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001171 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1172 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001173
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001174 // When getting the value for the LHS, check if we are in an assignment.
1175 // In such cases, we want to (initially) treat the LHS as an LValue,
1176 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001177 // evaluated to LValueDecl's instead of to an NonLValue.
1178 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001179 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1180 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001181
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001182 NodeSet S2;
1183 Visit(B->getRHS(), N1, S2);
1184
1185 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1186 NodeTy* N2 = *I2;
1187 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001188 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001189
1190 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001191 default:
1192 Dst.Add(N2);
1193 break;
1194
1195 // Arithmetic opreators.
1196
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001197 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001198 const NonLValue& R1 = cast<NonLValue>(V1);
1199 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001200
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001201 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001202 break;
1203 }
1204
1205 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001206 const NonLValue& R1 = cast<NonLValue>(V1);
1207 const NonLValue& R2 = cast<NonLValue>(V2);
1208 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001209 break;
1210 }
1211
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001212 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001213 const NonLValue& R1 = cast<NonLValue>(V1);
1214 const NonLValue& R2 = cast<NonLValue>(V2);
1215 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001216 break;
1217 }
1218
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001219 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001220 const NonLValue& R1 = cast<NonLValue>(V1);
1221 const NonLValue& R2 = cast<NonLValue>(V2);
1222 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001223 break;
1224 }
1225
Ted Kremenekcce207d2008-01-28 22:26:15 +00001226 case BinaryOperator::Rem: {
1227 const NonLValue& R1 = cast<NonLValue>(V1);
1228 const NonLValue& R2 = cast<NonLValue>(V2);
1229 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1230 break;
1231 }
1232
Ted Kremenek687af802008-01-29 19:43:15 +00001233 // Assignment operators.
1234
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001235 case BinaryOperator::Assign: {
1236 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001237 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001238 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1239 break;
1240 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001241
1242 case BinaryOperator::AddAssign: {
1243 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001244 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1245 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001246 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1247 break;
1248 }
1249
1250 case BinaryOperator::SubAssign: {
1251 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001252 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1253 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001254 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1255 break;
1256 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001257
1258 case BinaryOperator::MulAssign: {
1259 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001260 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1261 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001262 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1263 break;
1264 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001265
1266 case BinaryOperator::DivAssign: {
1267 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001268 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1269 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001270 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1271 break;
1272 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001273
1274 case BinaryOperator::RemAssign: {
1275 const LValue& L1 = cast<LValue>(V1);
1276 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1277 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1278 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1279 break;
1280 }
Ted Kremenek687af802008-01-29 19:43:15 +00001281
1282 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001283
Ted Kremenek687af802008-01-29 19:43:15 +00001284 case BinaryOperator::EQ:
1285 // FIXME: should we allow XX.EQ() to return a set of values,
1286 // allowing state bifurcation? In such cases, they will also
1287 // modify the state (meaning that a new state will be returned
1288 // as well).
1289 assert (B->getType() == getContext().IntTy);
1290
1291 if (isa<LValue>(V1)) {
1292 const LValue& L1 = cast<LValue>(V1);
1293 const LValue& L2 = cast<LValue>(V2);
1294 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1295 }
1296 else {
1297 const NonLValue& R1 = cast<NonLValue>(V1);
1298 const NonLValue& R2 = cast<NonLValue>(V2);
1299 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1300 }
1301
1302 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001303 break;
1304 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001305 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001306 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001307}
Ted Kremenekee985462008-01-16 18:18:48 +00001308
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001309
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001310void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1311 GRConstants::NodeSet& Dst) {
1312
1313 // FIXME: add metadata to the CFG so that we can disable
1314 // this check when we KNOW that there is no block-level subexpression.
1315 // The motivation is that this check requires a hashtable lookup.
1316
1317 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1318 Dst.Add(Pred);
1319 return;
1320 }
1321
1322 switch (S->getStmtClass()) {
1323 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001324 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001325 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1326 break;
1327
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001328 case Stmt::UnaryOperatorClass:
1329 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1330 break;
1331
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001332 case Stmt::ParenExprClass:
1333 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1334 break;
1335
Ted Kremenek874d63f2008-01-24 02:02:54 +00001336 case Stmt::ImplicitCastExprClass: {
1337 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1338 VisitCast(C, C->getSubExpr(), Pred, Dst);
1339 break;
1340 }
1341
1342 case Stmt::CastExprClass: {
1343 CastExpr* C = cast<CastExpr>(S);
1344 VisitCast(C, C->getSubExpr(), Pred, Dst);
1345 break;
1346 }
1347
Ted Kremenek9de04c42008-01-24 20:55:43 +00001348 case Stmt::DeclStmtClass:
1349 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1350 break;
1351
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001352 default:
1353 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1354 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001355 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001356}
1357
Ted Kremenekee985462008-01-16 18:18:48 +00001358//===----------------------------------------------------------------------===//
1359// Driver.
1360//===----------------------------------------------------------------------===//
1361
Ted Kremenekaa66a322008-01-16 21:46:15 +00001362#ifndef NDEBUG
1363namespace llvm {
1364template<>
1365struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1366 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001367
1368 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001369 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001370 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001371 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1372 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1373 default: assert (false && "Unknown ValueKey type.");
1374 }
1375 }
1376
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001377 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1378 ValueKey::Kind kind, bool isFirstGroup = false) {
1379 bool isFirst = true;
1380
1381 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1382 if (I.getKey().getKind() != kind)
1383 continue;
1384
1385 if (isFirst) {
1386 if (!isFirstGroup) Out << "\\l\\l";
1387 PrintKindLabel(Out, kind);
1388 isFirst = false;
1389 }
1390 else
1391 Out << "\\l";
1392
1393 Out << ' ';
1394
1395 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1396 Out << V->getName();
1397 else {
1398 Stmt* E = cast<Stmt>(I.getKey());
1399 Out << " (" << (void*) E << ") ";
1400 E->printPretty(Out);
1401 }
1402
1403 Out << " : ";
1404 I.getData().print(Out);
1405 }
1406 }
1407
Ted Kremenekaa66a322008-01-16 21:46:15 +00001408 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1409 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001410
1411 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001412 ProgramPoint Loc = N->getLocation();
1413
1414 switch (Loc.getKind()) {
1415 case ProgramPoint::BlockEntranceKind:
1416 Out << "Block Entrance: B"
1417 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1418 break;
1419
1420 case ProgramPoint::BlockExitKind:
1421 assert (false);
1422 break;
1423
1424 case ProgramPoint::PostStmtKind: {
1425 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001426 Out << L.getStmt()->getStmtClassName() << ':'
1427 << (void*) L.getStmt() << ' ';
1428
Ted Kremenekaa66a322008-01-16 21:46:15 +00001429 L.getStmt()->printPretty(Out);
1430 break;
1431 }
1432
1433 default: {
1434 const BlockEdge& E = cast<BlockEdge>(Loc);
1435 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1436 << E.getDst()->getBlockID() << ')';
1437 }
1438 }
1439
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001440 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001441
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001442 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1443 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001444 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001445
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001446 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001447 return Out.str();
1448 }
1449};
1450} // end llvm namespace
1451#endif
1452
Ted Kremenekee985462008-01-16 18:18:48 +00001453namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001454void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1455 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001456 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001457#ifndef NDEBUG
1458 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
1459#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001460}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001461} // end clang namespace