blob: 6d5bdbd1199de47b727309da86a828f808824e14 [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 Kremenekb38911f2008-01-30 23:03:39 +000029#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000030#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000031#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000032#include "llvm/Support/Streams.h"
33
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000034#include <functional>
35
Ted Kremenekaa66a322008-01-16 21:46:15 +000036#ifndef NDEBUG
37#include "llvm/Support/GraphWriter.h"
38#include <sstream>
39#endif
40
Ted Kremenekd27f8162008-01-15 23:55:06 +000041using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000042using llvm::dyn_cast;
43using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000044using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000045
46//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +000047/// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a
Ted Kremenekd27f8162008-01-15 23:55:06 +000048/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
49//===----------------------------------------------------------------------===//
50namespace {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000051
Ted Kremenek68fd2572008-01-29 17:27:31 +000052class SymbolID {
53 unsigned Data;
54public:
55 SymbolID() : Data(~0) {}
56 SymbolID(unsigned x) : Data(x) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +000057
Ted Kremenek68fd2572008-01-29 17:27:31 +000058 bool isInitialized() const { return Data != (unsigned) ~0; }
59 operator unsigned() const { assert (isInitialized()); return Data; }
60};
61
Ted Kremenekab2b8c52008-01-23 19:59:44 +000062class VISIBILITY_HIDDEN ValueKey {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000063 uintptr_t Raw;
Ted Kremenekcc1c3652008-01-25 23:43:12 +000064 void operator=(const ValueKey& RHS); // Do not implement.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000065
Ted Kremenekd27f8162008-01-15 23:55:06 +000066public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000067 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
68 IsSymbol=0x3, // Symbol Bindings.
69 Flags=0x3 };
70
71 inline Kind getKind() const {
72 return (Kind) (Raw & Flags);
73 }
74
75 inline void* getPtr() const {
76 assert (getKind() != IsSymbol);
77 return reinterpret_cast<void*>(Raw & ~Flags);
78 }
79
80 inline SymbolID getSymbolID() const {
81 assert (getKind() == IsSymbol);
Ted Kremenek68fd2572008-01-29 17:27:31 +000082 return Raw >> 2;
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000083 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000084
Ted Kremenekab2b8c52008-01-23 19:59:44 +000085 ValueKey(const ValueDecl* VD)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000086 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
87 assert(VD && "ValueDecl cannot be NULL.");
88 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000089
Ted Kremenek5c1b9962008-01-24 19:43:37 +000090 ValueKey(Stmt* S, bool isBlkExpr = false)
Ted Kremenekcc1c3652008-01-25 23:43:12 +000091 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000092 assert(S && "Tracked statement cannot be NULL.");
Ted Kremenekcc1c3652008-01-25 23:43:12 +000093 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000094
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000095 ValueKey(SymbolID V)
96 : Raw((V << 2) | IsSymbol) {}
97
98 bool isSymbol() const { return getKind() == IsSymbol; }
Ted Kremenek565256e2008-01-24 22:44:24 +000099 bool isSubExpr() const { return getKind() == IsSubExpr; }
Ted Kremenek65cac132008-01-29 05:25:31 +0000100 bool isBlkExpr() const { return getKind() == IsBlkExpr; }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000101 bool isDecl() const { return getKind() == IsDecl; }
Ted Kremenek65cac132008-01-29 05:25:31 +0000102 bool isStmt() const { return getKind() <= IsBlkExpr; }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000103
104 inline void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000105 ID.AddInteger(isSymbol() ? 1 : 0);
106
107 if (isSymbol())
Ted Kremenekf2645622008-01-28 22:25:21 +0000108 ID.AddInteger(getSymbolID());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000109 else
110 ID.AddPointer(getPtr());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000111 }
112
113 inline bool operator==(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000114 return isSymbol() ? getSymbolID() == X.getSymbolID()
115 : getPtr() == X.getPtr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000116 }
117
118 inline bool operator!=(const ValueKey& X) const {
119 return !operator==(X);
120 }
121
122 inline bool operator<(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000123 if (isSymbol())
124 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000125
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000126 return getPtr() < X.getPtr();
Ted Kremenekb3d2dca2008-01-16 23:33:44 +0000127 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000128};
129} // end anonymous namespace
130
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000131// Machinery to get cast<> and dyn_cast<> working with ValueKey.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000132namespace llvm {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000133 template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) {
134 return V.getKind() == ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000135 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000136 template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000137 return ((unsigned) V.getKind()) < ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000139 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> {
Ted Kremenekaa66a322008-01-16 21:46:15 +0000140 typedef const ValueDecl* ret_type;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000141 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000142 template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000143 typedef const Stmt* ret_type;
144 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000145 template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146 typedef void* SimpleType;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000147 static inline SimpleType getSimplifiedValue(const ValueKey &V) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000148 return V.getPtr();
149 }
150 };
151} // end llvm namespace
152
Ted Kremenek68fd2572008-01-29 17:27:31 +0000153
154//===----------------------------------------------------------------------===//
155// SymbolManager.
156//===----------------------------------------------------------------------===//
157
158namespace {
159class VISIBILITY_HIDDEN SymbolData {
160 uintptr_t Data;
161public:
162 enum Kind { ParmKind = 0x0, Mask = 0x3 };
163
164 SymbolData(ParmVarDecl* D)
165 : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
166
167 inline Kind getKind() const { return (Kind) (Data & Mask); }
168 inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
169 inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
170};
171}
172
173// Machinery to get cast<> and dyn_cast<> working with SymbolData.
174namespace llvm {
175 template<> inline bool isa<ParmVarDecl,SymbolData>(const SymbolData& V) {
176 return V.getKind() == SymbolData::ParmKind;
177 }
178 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ParmVarDecl,SymbolData> {
179 typedef const ParmVarDecl* ret_type;
180 };
181 template<> struct VISIBILITY_HIDDEN simplify_type<SymbolData> {
182 typedef void* SimpleType;
183 static inline SimpleType getSimplifiedValue(const SymbolData &V) {
184 return V.getPtr();
185 }
186 };
187} // end llvm namespace
188
189namespace {
190class VISIBILITY_HIDDEN SymbolManager {
191 std::vector<SymbolData> SymbolToData;
192
193 typedef llvm::DenseMap<void*,SymbolID> MapTy;
194 MapTy DataToSymbol;
195
196public:
197 SymbolData getSymbolData(SymbolID id) const {
198 assert (id < SymbolToData.size());
199 return SymbolToData[id];
200 }
201
202 SymbolID getSymbol(ParmVarDecl* D);
203};
204} // end anonymous namespace
205
206SymbolID SymbolManager::getSymbol(ParmVarDecl* D) {
207 SymbolID& X = DataToSymbol[D];
208
209 if (!X.isInitialized()) {
210 X = SymbolToData.size();
211 SymbolToData.push_back(D);
212 }
213
214 return X;
215}
216
Ted Kremenekd27f8162008-01-15 23:55:06 +0000217//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000218// ValueManager.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000219//===----------------------------------------------------------------------===//
220
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000221namespace {
222
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000223typedef llvm::ImmutableSet<APSInt > APSIntSetTy;
224
225
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000226class VISIBILITY_HIDDEN ValueManager {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000227 ASTContext& Ctx;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000228
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000229 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy;
230 APSIntSetTy APSIntSet;
231
232 llvm::BumpPtrAllocator BPAlloc;
233
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000234public:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000235 ValueManager(ASTContext& ctx) : Ctx(ctx) {}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000236 ~ValueManager();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000237
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000238 ASTContext& getContext() const { return Ctx; }
Ted Kremenek687af802008-01-29 19:43:15 +0000239 APSInt& getValue(const APSInt& X);
240 APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
241 APSInt& getValue(uint64_t X, QualType T,
242 SourceLocation Loc = SourceLocation());
Ted Kremenekd27f8162008-01-15 23:55:06 +0000243};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000244} // end anonymous namespace
245
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000246ValueManager::~ValueManager() {
247 // Note that the dstor for the contents of APSIntSet will never be called,
248 // so we iterate over the set and invoke the dstor for each APSInt. This
249 // frees an aux. memory allocated to represent very large constants.
250 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
251 I->getValue().~APSInt();
252}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000253
254APSInt& ValueManager::getValue(const APSInt& X) {
255 llvm::FoldingSetNodeID ID;
256 void* InsertPos;
257 typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000258
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000259 X.Profile(ID);
260 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000261
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000262 if (!P) {
263 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
264 new (P) FoldNodeTy(X);
265 APSIntSet.InsertNode(P, InsertPos);
266 }
267
268 return *P;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000269}
270
Ted Kremenek687af802008-01-29 19:43:15 +0000271APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) {
272 APSInt V(BitWidth, isUnsigned);
273 V = X;
274 return getValue(V);
275}
276
277APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) {
278 unsigned bits = Ctx.getTypeSize(T, Loc);
279 APSInt V(bits, T->isUnsignedIntegerType());
280 V = X;
281 return getValue(V);
282}
283
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000284//===----------------------------------------------------------------------===//
285// Expression Values.
286//===----------------------------------------------------------------------===//
Ted Kremenekf13794e2008-01-24 23:19:54 +0000287
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000288namespace {
289
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000290class VISIBILITY_HIDDEN RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000291public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000292 enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
Ted Kremenek6753fe32008-01-30 18:54:06 +0000293 UninitializedKind=0x2, InvalidKind=0x3 };
294
295 enum { BaseBits = 2, BaseMask = 0x3 };
Ted Kremenekf13794e2008-01-24 23:19:54 +0000296
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000297private:
Ted Kremenekf2645622008-01-28 22:25:21 +0000298 void* Data;
Ted Kremenekf13794e2008-01-24 23:19:54 +0000299 unsigned Kind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000300
301protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000302 RValue(const void* d, bool isLValue, unsigned ValKind)
Ted Kremenekf2645622008-01-28 22:25:21 +0000303 : Data(const_cast<void*>(d)),
Ted Kremenek6753fe32008-01-30 18:54:06 +0000304 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000305
Ted Kremenek403c1812008-01-28 22:51:57 +0000306 explicit RValue(BaseKind k)
307 : Data(0), Kind(k) {}
Ted Kremenekf2645622008-01-28 22:25:21 +0000308
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000309 void* getRawPtr() const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000310 return reinterpret_cast<void*>(Data);
311 }
312
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000313public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000314 ~RValue() {};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000315
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000316 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000317
Ted Kremenekf13794e2008-01-24 23:19:54 +0000318 unsigned getRawKind() const { return Kind; }
Ted Kremenek6753fe32008-01-30 18:54:06 +0000319 BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
320 unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
Ted Kremenekf13794e2008-01-24 23:19:54 +0000321
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000322 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000323 ID.AddInteger((unsigned) getRawKind());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000324 ID.AddPointer(reinterpret_cast<void*>(Data));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000325 }
326
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000327 bool operator==(const RValue& RHS) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000328 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000329 }
330
Ted Kremenekf13794e2008-01-24 23:19:54 +0000331 inline bool isValid() const { return getRawKind() != InvalidKind; }
332 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
333
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000334 void print(std::ostream& OS) const;
335 void print() const { print(*llvm::cerr.stream()); }
336
337 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000338 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000339};
340
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000341class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000342public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000343 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000344
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000345 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000346 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000347 }
348};
Ted Kremenek403c1812008-01-28 22:51:57 +0000349
350class VISIBILITY_HIDDEN UninitializedValue : public RValue {
351public:
352 UninitializedValue() : RValue(UninitializedKind) {}
353
354 static inline bool classof(const RValue* V) {
355 return V->getBaseKind() == UninitializedKind;
356 }
357};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000358
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000359class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000360protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000361 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000362
363public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000364 void print(std::ostream& Out) const;
365
Ted Kremenek687af802008-01-29 19:43:15 +0000366 // Arithmetic operators.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000367 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
368 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
369 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
370 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
371 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
372 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000373
Ted Kremenek687af802008-01-29 19:43:15 +0000374 // Equality operators.
375 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
376 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000377
Ted Kremenek687af802008-01-29 19:43:15 +0000378 // Utility methods to create NonLValues.
379 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
380 SourceLocation Loc = SourceLocation());
381
382 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenek68fd2572008-01-29 17:27:31 +0000383 static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000384
Ted Kremenek687af802008-01-29 19:43:15 +0000385 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
386 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
387 }
388
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000389 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000390 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000391 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000392 }
393};
Ted Kremenek687af802008-01-29 19:43:15 +0000394
395class VISIBILITY_HIDDEN LValue : public RValue {
396protected:
397 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
398
399public:
400
401 // Equality operators.
402 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
403 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
404
405 // Implement isa<T> support.
406 static inline bool classof(const RValue* V) {
407 return V->getBaseKind() == LValueKind;
408 }
409};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000410
411} // end anonymous namespace
412
413//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000414// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000415//===----------------------------------------------------------------------===//
416
417namespace {
418
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000419enum { LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000420
421class VISIBILITY_HIDDEN LValueDecl : public LValue {
422public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000423 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000424 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000425
426 ValueDecl* getDecl() const {
427 return static_cast<ValueDecl*>(getRawPtr());
428 }
429
Ted Kremenek687af802008-01-29 19:43:15 +0000430 inline bool operator==(const LValueDecl& R) const {
431 return getDecl() == R.getDecl();
432 }
433
434 inline bool operator!=(const LValueDecl& R) const {
435 return getDecl() != R.getDecl();
436 }
437
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000438 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000439 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000440 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000441 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000442};
443
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000444} // end anonymous namespace
445
446//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000447// Non-LValues.
448//===----------------------------------------------------------------------===//
449
450namespace {
451
Ted Kremenekf2645622008-01-28 22:25:21 +0000452enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
453 NumNonLValueKind };
454
455class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
456public:
457 SymbolicNonLValue(unsigned SymID)
458 : NonLValue(SymbolicNonLValueKind,
459 reinterpret_cast<void*>((uintptr_t) SymID)) {}
460
461 SymbolID getSymbolID() const {
462 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
463 }
464
465 static inline bool classof(const RValue* V) {
466 return V->getSubKind() == SymbolicNonLValueKind;
467 }
468};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000469
470class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
471public:
472 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
473
474 const APSInt& getValue() const {
475 return *static_cast<APSInt*>(getRawPtr());
476 }
477
Ted Kremenek687af802008-01-29 19:43:15 +0000478 // Arithmetic operators.
479
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000480 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
481 return ValMgr.getValue(getValue() + V.getValue());
482 }
Ted Kremenek687af802008-01-29 19:43:15 +0000483
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000484 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
485 return ValMgr.getValue(getValue() - V.getValue());
486 }
487
488 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
489 return ValMgr.getValue(getValue() * V.getValue());
490 }
491
492 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
493 return ValMgr.getValue(getValue() / V.getValue());
494 }
495
496 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
497 return ValMgr.getValue(getValue() % V.getValue());
498 }
499
Ted Kremenek687af802008-01-29 19:43:15 +0000500 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
501 assert (U->getType() == U->getSubExpr()->getType());
502 assert (U->getType()->isIntegerType());
503 return ValMgr.getValue(-getValue());
504 }
505
506 // Casting.
507
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000508 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
509 assert (CastExpr->getType()->isIntegerType());
510
511 APSInt X(getValue());
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000512 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
513 CastExpr->getLocStart()));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000514 return ValMgr.getValue(X);
515 }
516
Ted Kremenek687af802008-01-29 19:43:15 +0000517 // Equality operators.
518
519 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
520 const APSInt& Val = getValue();
521 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
522 Val.getBitWidth(), Val.isUnsigned());
523 }
524
525 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
526 const APSInt& Val = getValue();
527 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
528 Val.getBitWidth(), Val.isUnsigned());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000529 }
530
531 // Implement isa<T> support.
532 static inline bool classof(const RValue* V) {
533 return V->getSubKind() == ConcreteIntKind;
534 }
535};
536
537} // end anonymous namespace
538
539//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000540// Transfer function dispatch for Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000541//===----------------------------------------------------------------------===//
542
543RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
544 switch (getSubKind()) {
545 case ConcreteIntKind:
546 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
547 default:
548 return InvalidValue();
549 }
550}
551
552NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
553 switch (getSubKind()) {
554 case ConcreteIntKind:
555 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
556 default:
557 return cast<NonLValue>(InvalidValue());
558 }
559}
560
Ted Kremenek687af802008-01-29 19:43:15 +0000561#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000562case (k1##Kind*NumNonLValueKind+k2##Kind):\
563 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
564
Ted Kremenek687af802008-01-29 19:43:15 +0000565#define NONLVALUE_DISPATCH(Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000566switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
Ted Kremenek687af802008-01-29 19:43:15 +0000567 NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000568 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000569 if (getBaseKind() == UninitializedKind ||\
570 RHS.getBaseKind() == UninitializedKind)\
571 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000572 assert (!isValid() || !RHS.isValid() && "Missing case.");\
573 break;\
574}\
575return cast<NonLValue>(InvalidValue());
576
577NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000578 NONLVALUE_DISPATCH(Add)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000579}
580
581NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000582 NONLVALUE_DISPATCH(Sub)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000583}
584
585NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000586 NONLVALUE_DISPATCH(Mul)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000587}
588
589NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000590 NONLVALUE_DISPATCH(Div)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000591}
592
593NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000594 NONLVALUE_DISPATCH(Rem)
595}
596
597NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
598 NONLVALUE_DISPATCH(EQ)
599}
600
601NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
602 NONLVALUE_DISPATCH(NE)
603}
604
605#undef NONLVALUE_DISPATCH_CASE
606#undef NONLVALUE_DISPATCH
607
608//===----------------------------------------------------------------------===//
609// Transfer function dispatch for LValues.
610//===----------------------------------------------------------------------===//
611
612
613NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
614 if (getSubKind() != RHS.getSubKind())
615 return NonLValue::GetIntTruthValue(ValMgr, false);
616
617 switch (getSubKind()) {
618 default:
619 assert(false && "EQ not implemented for this LValue.");
620 return cast<NonLValue>(InvalidValue());
621
622 case LValueDeclKind: {
623 bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS);
624 return NonLValue::GetIntTruthValue(ValMgr, b);
625 }
626 }
627}
628
629NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
630 if (getSubKind() != RHS.getSubKind())
Ted Kremenek03701672008-01-29 21:27:49 +0000631 return NonLValue::GetIntTruthValue(ValMgr, true);
Ted Kremenek687af802008-01-29 19:43:15 +0000632
633 switch (getSubKind()) {
634 default:
635 assert(false && "EQ not implemented for this LValue.");
636 return cast<NonLValue>(InvalidValue());
637
638 case LValueDeclKind: {
639 bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
640 return NonLValue::GetIntTruthValue(ValMgr, b);
641 }
642 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000643}
644
645
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000646//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000647// Utility methods for constructing Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000648//===----------------------------------------------------------------------===//
649
Ted Kremenek687af802008-01-29 19:43:15 +0000650NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
651 SourceLocation Loc) {
652
653 return ConcreteInt(ValMgr.getValue(X, T, Loc));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000654}
655
656NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
657 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
658 I->getType()->isUnsignedIntegerType())));
659}
660
Ted Kremenek68fd2572008-01-29 17:27:31 +0000661NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
662 return SymbolicNonLValue(SymMgr.getSymbol(D));
663}
664
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000665//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000666// Pretty-Printing.
667//===----------------------------------------------------------------------===//
668
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000669void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000670 switch (getBaseKind()) {
671 case InvalidKind:
672 Out << "Invalid";
673 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000674
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000675 case NonLValueKind:
676 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000677 break;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000678
Ted Kremenekf13794e2008-01-24 23:19:54 +0000679 case LValueKind:
680 assert (false && "FIXME: LValue printing not implemented.");
681 break;
682
Ted Kremenek403c1812008-01-28 22:51:57 +0000683 case UninitializedKind:
684 Out << "Uninitialized";
685 break;
686
Ted Kremenekf13794e2008-01-24 23:19:54 +0000687 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000688 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000689 }
690}
691
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000692void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000693 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000694 case ConcreteIntKind:
695 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000696 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000697
Ted Kremenek68fd2572008-01-29 17:27:31 +0000698 case SymbolicNonLValueKind:
Ted Kremenek6753fe32008-01-30 18:54:06 +0000699 Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID();
Ted Kremenek68fd2572008-01-29 17:27:31 +0000700 break;
701
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000702 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000703 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000704 break;
705 }
706}
707
708//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000710//===----------------------------------------------------------------------===//
711
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000712typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000713
714namespace clang {
715 template<>
716 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
717 static inline void* toPtr(ValueMapTy M) {
718 return reinterpret_cast<void*>(M.getRoot());
719 }
720 static inline ValueMapTy toState(void* P) {
721 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
722 }
723 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000724}
725
Ted Kremenekb38911f2008-01-30 23:03:39 +0000726typedef ValueMapTy StateTy;
727
Ted Kremenekd27f8162008-01-15 23:55:06 +0000728//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000729// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000730//
731// FIXME: This checker logic should be eventually broken into two components.
732// The first is the "meta"-level checking logic; the code that
733// does the Stmt visitation, fetching values from the map, etc.
734// The second part does the actual state manipulation. This way we
735// get more of a separate of concerns of these two pieces, with the
736// latter potentially being refactored back into the main checking
737// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000738//===----------------------------------------------------------------------===//
739
740namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000741
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000742class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000743
744public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000745 typedef ValueMapTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000746 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
747 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000748 typedef ExplodedGraph<GRConstants> GraphTy;
749 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000750
751 class NodeSet {
752 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
753 ImplTy Impl;
754 public:
755
756 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000757 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000758
Ted Kremenekb38911f2008-01-30 23:03:39 +0000759 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000760
761 typedef ImplTy::iterator iterator;
762 typedef ImplTy::const_iterator const_iterator;
763
764 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000765 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000766
767 iterator begin() { return Impl.begin(); }
768 iterator end() { return Impl.end(); }
769
770 const_iterator begin() const { return Impl.begin(); }
771 const_iterator end() const { return Impl.end(); }
772 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000773
774protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000775 /// G - the simulation graph.
776 GraphTy& G;
777
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000778 /// Liveness - live-variables information the ValueDecl* and block-level
779 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000780 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000781
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000782 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000783 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000784 StmtNodeBuilder* Builder;
785
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000786 /// StateMgr - Object that manages the data for all created states.
787 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000788
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000789 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000790 ValueManager ValMgr;
791
Ted Kremenek68fd2572008-01-29 17:27:31 +0000792 /// SymMgr - Object that manages the symbol information.
793 SymbolManager SymMgr;
794
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000795 /// StmtEntryNode - The immediate predecessor node.
796 NodeTy* StmtEntryNode;
797
798 /// CurrentStmt - The current block-level statement.
799 Stmt* CurrentStmt;
800
Ted Kremenekb38911f2008-01-30 23:03:39 +0000801 /// UninitBranches - Nodes in the ExplodedGraph that result from
802 /// taking a branch based on an uninitialized value.
803 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
804 UninitBranchesTy UninitBranches;
805
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000806 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000807
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000808 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000809
Ted Kremenekd27f8162008-01-15 23:55:06 +0000810public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000811 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
812 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
813 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000814
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000815 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000816 Liveness.runOnCFG(G.getCFG());
817 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000818 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000819
820 /// getCFG - Returns the CFG associated with this analysis.
821 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000822
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000823 /// getInitialState - Return the initial state used for the root vertex
824 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000826 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000827
828 // Iterate the parameters.
829 FunctionDecl& F = G.getFunctionDecl();
830
831 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
832 I!=E; ++I) {
833
834 // For now we only support symbolic values for non-pointer types.
835 if ((*I)->getType()->isPointerType() ||
836 (*I)->getType()->isReferenceType())
837 continue;
838
839 // FIXME: Set these values to a symbol, not Uninitialized.
Ted Kremenek68fd2572008-01-29 17:27:31 +0000840 St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000841 }
842
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000843 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000844 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000845
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000846 /// ProcessStmt - Called by GREngine. Used to generate new successor
847 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000848 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
849
850 /// ProcessBranch - Called by GREngine. Used to generate successor
851 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000852 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000853
854 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
855 /// that all subexpression mappings are removed and that any
856 /// block-level expressions that are not live at 'S' also have their
857 /// mappings removed.
858 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
859
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000860 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000862 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000863 return SetValue(St, const_cast<Stmt*>(S), V);
864 }
865
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000866 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000867
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000868 RValue GetValue(const StateTy& St, Stmt* S);
869 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000870 return GetValue(St, const_cast<Stmt*>(S));
871 }
872
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000873 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000874 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000875
876 /// Assume - Create new state by assuming that a given expression
877 /// is true or false.
878 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
879 bool& isFeasible) {
880 if (isa<LValue>(Cond))
881 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
882 else
883 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
884 }
885
886 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
887 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000888
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000889 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000890
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000891 /// Visit - Transfer function logic for all statements. Dispatches to
892 /// other functions that handle specific kinds of statements.
893 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000894
895 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
896 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000897
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000898 /// VisitUnaryOperator - Transfer function logic for unary operators.
899 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
900
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000901 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000902 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
903
904 /// VisitDeclStmt - Transfer function logic for DeclStmts.
905 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000906};
907} // end anonymous namespace
908
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000909
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000910void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
911 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000912
913 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000914
Ted Kremenekb38911f2008-01-30 23:03:39 +0000915 // Remove old bindings for subexpressions.
916 for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
917 if (I.getKey().isSubExpr())
918 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000919
Ted Kremenekb38911f2008-01-30 23:03:39 +0000920 RValue V = GetValue(PrevState, Condition);
921
922 switch (V.getBaseKind()) {
923 default:
924 break;
925
926 case RValue::InvalidKind:
927 builder.generateNode(PrevState, true);
928 builder.generateNode(PrevState, false);
929 return;
930
931 case RValue::UninitializedKind: {
932 NodeTy* N = builder.generateNode(PrevState, true);
933
934 if (N) {
935 N->markAsSink();
936 UninitBranches.insert(N);
937 }
938
939 builder.markInfeasible(false);
940 return;
941 }
942 }
943
944 // Process the true branch.
945 bool isFeasible = true;
946 StateTy St = Assume(PrevState, V, true, isFeasible);
947
948 if (isFeasible) builder.generateNode(St, true);
949 else {
950 builder.markInfeasible(true);
951 isFeasible = true;
952 }
953
954 // Process the false branch.
955 St = Assume(PrevState, V, false, isFeasible);
956
957 if (isFeasible) builder.generateNode(St, false);
958 else builder.markInfeasible(false);
959
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000960}
961
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000962void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000963 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000964
965 StmtEntryNode = builder.getLastNode();
966 CurrentStmt = S;
967 NodeSet Dst;
968 StateCleaned = false;
969
970 Visit(S, StmtEntryNode, Dst);
971
972 // If no nodes were generated, generate a new node that has all the
973 // dead mappings removed.
974 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
975 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
976 builder.generateNode(S, St, StmtEntryNode);
977 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000978
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000979 CurrentStmt = NULL;
980 StmtEntryNode = NULL;
981 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000982}
983
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000984
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000985RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000986 switch (LV.getSubKind()) {
987 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000988 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
989 return T ? T->getValue().second : InvalidValue();
990 }
991 default:
992 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000993 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000994 }
995
996 return InvalidValue();
997}
998
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000999RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +00001000 for (;;) {
1001 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001002
1003 // ParenExprs are no-ops.
1004
Ted Kremenek671c9e82008-01-24 00:50:08 +00001005 case Stmt::ParenExprClass:
1006 S = cast<ParenExpr>(S)->getSubExpr();
1007 continue;
1008
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001009 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
1010 // (assuming an implicit "load") depending on the context. In this
1011 // context we assume that we are retrieving the value contained
1012 // within the referenced variables.
1013
Ted Kremenek671c9e82008-01-24 00:50:08 +00001014 case Stmt::DeclRefExprClass:
1015 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +00001016
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001017 // Integer literals evaluate to an RValue. Simply retrieve the
1018 // RValue for the literal.
1019
Ted Kremenek671c9e82008-01-24 00:50:08 +00001020 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001021 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
1022
1023 // Casts where the source and target type are the same
1024 // are no-ops. We blast through these to get the descendant
1025 // subexpression that has a value.
1026
Ted Kremenek874d63f2008-01-24 02:02:54 +00001027 case Stmt::ImplicitCastExprClass: {
1028 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1029 if (C->getType() == C->getSubExpr()->getType()) {
1030 S = C->getSubExpr();
1031 continue;
1032 }
1033 break;
1034 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001035
Ted Kremenek874d63f2008-01-24 02:02:54 +00001036 case Stmt::CastExprClass: {
1037 CastExpr* C = cast<CastExpr>(S);
1038 if (C->getType() == C->getSubExpr()->getType()) {
1039 S = C->getSubExpr();
1040 continue;
1041 }
1042 break;
1043 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001044
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001045 // Handle all other Stmt* using a lookup.
1046
Ted Kremenek671c9e82008-01-24 00:50:08 +00001047 default:
1048 break;
1049 };
1050
1051 break;
1052 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001053
Ted Kremenek5c1b9962008-01-24 19:43:37 +00001054 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +00001055
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001056 return T ? T->getValue().second : InvalidValue();
1057}
1058
1059LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001060 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
1061 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001062
1063 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
1064 return LValueDecl(DR->getDecl());
1065
1066 return cast<LValue>(GetValue(St, S));
1067}
1068
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001069
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001070GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001071 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +00001072 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001073
1074 if (!StateCleaned) {
1075 St = RemoveDeadBindings(CurrentStmt, St);
1076 StateCleaned = true;
1077 }
1078
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001079 bool isBlkExpr = false;
1080
1081 if (S == CurrentStmt) {
1082 isBlkExpr = getCFG().isBlkExpr(S);
1083
1084 if (!isBlkExpr)
1085 return St;
1086 }
Ted Kremenekdaadf452008-01-24 19:28:01 +00001087
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001088 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1089 : St;
1090}
1091
1092GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001093 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001094 if (!LV.isValid())
1095 return St;
1096
1097 if (!StateCleaned) {
1098 St = RemoveDeadBindings(CurrentStmt, St);
1099 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001100 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001101
Ted Kremenekf13794e2008-01-24 23:19:54 +00001102 switch (LV.getSubKind()) {
1103 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001104 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1105 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1106
1107 default:
1108 assert ("SetValue for given LValue type not yet implemented.");
1109 return St;
1110 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001111}
1112
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001113GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001114 // Note: in the code below, we can assign a new map to M since the
1115 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001116 StateTy::iterator I = M.begin(), E = M.end();
1117
Ted Kremenekf84469b2008-01-18 00:41:32 +00001118
Ted Kremenek65cac132008-01-29 05:25:31 +00001119 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1120 // Remove old bindings for subexpressions and "dead"
1121 // block-level expressions.
1122 if (I.getKey().isSubExpr() ||
1123 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1124 M = StateMgr.Remove(M, I.getKey());
1125 }
1126 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1127 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1128 if (!Liveness.isLive(Loc, V))
1129 M = StateMgr.Remove(M, I.getKey());
1130 }
1131 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001132
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001133 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001134}
1135
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001136void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1137 GRConstants::StateTy St) {
1138
1139 // If the state hasn't changed, don't generate a new node.
1140 if (St == Pred->getState())
1141 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001142
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001143 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001144}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001145
Ted Kremenek874d63f2008-01-24 02:02:54 +00001146void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1147 GRConstants::NodeSet& Dst) {
1148
1149 QualType T = CastE->getType();
1150
1151 // Check for redundant casts.
1152 if (E->getType() == T) {
1153 Dst.Add(Pred);
1154 return;
1155 }
1156
1157 NodeSet S1;
1158 Visit(E, Pred, S1);
1159
1160 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1161 NodeTy* N = *I1;
1162 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001163 const RValue& V = GetValue(St, E);
1164 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001165 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001166}
1167
1168void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1169 GRConstants::NodeSet& Dst) {
1170
1171 StateTy St = Pred->getState();
1172
1173 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001174 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1175 const Expr* E = VD->getInit();
1176 St = SetValue(St, LValueDecl(VD),
1177 E ? GetValue(St, E) : UninitializedValue());
1178 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001179
1180 Nodify(Dst, DS, Pred, St);
1181
1182 if (Dst.empty())
1183 Dst.Add(Pred);
1184}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001185
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001186void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1187 GRConstants::NodeTy* Pred,
1188 GRConstants::NodeSet& Dst) {
1189 NodeSet S1;
1190 Visit(U->getSubExpr(), Pred, S1);
1191
1192 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1193 NodeTy* N1 = *I1;
1194 StateTy St = N1->getState();
1195
1196 switch (U->getOpcode()) {
1197 case UnaryOperator::PostInc: {
1198 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001199 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001200 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1201 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001202
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001203 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001204 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1205 break;
1206 }
1207
1208 case UnaryOperator::PostDec: {
1209 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001210 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001211 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1212 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001213
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001214 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001215 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1216 break;
1217 }
1218
1219 case UnaryOperator::PreInc: {
1220 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001221 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001222 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1223 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001224
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001225 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001226 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1227 break;
1228 }
1229
1230 case UnaryOperator::PreDec: {
1231 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001232 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001233 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1234 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001235
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001236 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001237 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1238 break;
1239 }
1240
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001241 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001242 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1243 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001244 break;
1245 }
1246
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001247 default: ;
1248 assert (false && "Not implemented.");
1249 }
1250 }
1251}
1252
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001253void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1254 GRConstants::NodeTy* Pred,
1255 GRConstants::NodeSet& Dst) {
1256 NodeSet S1;
1257 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001258
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001259 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1260 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001261
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001262 // When getting the value for the LHS, check if we are in an assignment.
1263 // In such cases, we want to (initially) treat the LHS as an LValue,
1264 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001265 // evaluated to LValueDecl's instead of to an NonLValue.
1266 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001267 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1268 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001269
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001270 NodeSet S2;
1271 Visit(B->getRHS(), N1, S2);
1272
1273 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1274 NodeTy* N2 = *I2;
1275 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001276 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001277
1278 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001279 default:
1280 Dst.Add(N2);
1281 break;
1282
1283 // Arithmetic opreators.
1284
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001285 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001286 const NonLValue& R1 = cast<NonLValue>(V1);
1287 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001288
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001289 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001290 break;
1291 }
1292
1293 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001294 const NonLValue& R1 = cast<NonLValue>(V1);
1295 const NonLValue& R2 = cast<NonLValue>(V2);
1296 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001297 break;
1298 }
1299
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001300 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001301 const NonLValue& R1 = cast<NonLValue>(V1);
1302 const NonLValue& R2 = cast<NonLValue>(V2);
1303 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001304 break;
1305 }
1306
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001307 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001308 const NonLValue& R1 = cast<NonLValue>(V1);
1309 const NonLValue& R2 = cast<NonLValue>(V2);
1310 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001311 break;
1312 }
1313
Ted Kremenekcce207d2008-01-28 22:26:15 +00001314 case BinaryOperator::Rem: {
1315 const NonLValue& R1 = cast<NonLValue>(V1);
1316 const NonLValue& R2 = cast<NonLValue>(V2);
1317 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1318 break;
1319 }
1320
Ted Kremenek687af802008-01-29 19:43:15 +00001321 // Assignment operators.
1322
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001323 case BinaryOperator::Assign: {
1324 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001325 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001326 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1327 break;
1328 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001329
1330 case BinaryOperator::AddAssign: {
1331 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001332 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1333 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001334 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1335 break;
1336 }
1337
1338 case BinaryOperator::SubAssign: {
1339 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001340 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1341 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001342 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1343 break;
1344 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001345
1346 case BinaryOperator::MulAssign: {
1347 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001348 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1349 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001350 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1351 break;
1352 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001353
1354 case BinaryOperator::DivAssign: {
1355 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001356 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1357 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001358 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1359 break;
1360 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001361
1362 case BinaryOperator::RemAssign: {
1363 const LValue& L1 = cast<LValue>(V1);
1364 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1365 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1366 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1367 break;
1368 }
Ted Kremenek687af802008-01-29 19:43:15 +00001369
1370 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001371
Ted Kremenek687af802008-01-29 19:43:15 +00001372 case BinaryOperator::EQ:
1373 // FIXME: should we allow XX.EQ() to return a set of values,
1374 // allowing state bifurcation? In such cases, they will also
1375 // modify the state (meaning that a new state will be returned
1376 // as well).
1377 assert (B->getType() == getContext().IntTy);
1378
1379 if (isa<LValue>(V1)) {
1380 const LValue& L1 = cast<LValue>(V1);
1381 const LValue& L2 = cast<LValue>(V2);
1382 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1383 }
1384 else {
1385 const NonLValue& R1 = cast<NonLValue>(V1);
1386 const NonLValue& R2 = cast<NonLValue>(V2);
1387 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1388 }
1389
1390 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001391 break;
1392 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001393 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001394 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001395}
Ted Kremenekee985462008-01-16 18:18:48 +00001396
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001397
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001398void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1399 GRConstants::NodeSet& Dst) {
1400
1401 // FIXME: add metadata to the CFG so that we can disable
1402 // this check when we KNOW that there is no block-level subexpression.
1403 // The motivation is that this check requires a hashtable lookup.
1404
1405 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1406 Dst.Add(Pred);
1407 return;
1408 }
1409
1410 switch (S->getStmtClass()) {
1411 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001412 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001413 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1414 break;
1415
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001416 case Stmt::UnaryOperatorClass:
1417 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1418 break;
1419
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001420 case Stmt::ParenExprClass:
1421 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1422 break;
1423
Ted Kremenek874d63f2008-01-24 02:02:54 +00001424 case Stmt::ImplicitCastExprClass: {
1425 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1426 VisitCast(C, C->getSubExpr(), Pred, Dst);
1427 break;
1428 }
1429
1430 case Stmt::CastExprClass: {
1431 CastExpr* C = cast<CastExpr>(S);
1432 VisitCast(C, C->getSubExpr(), Pred, Dst);
1433 break;
1434 }
1435
Ted Kremenek9de04c42008-01-24 20:55:43 +00001436 case Stmt::DeclStmtClass:
1437 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1438 break;
1439
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001440 default:
1441 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1442 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001443 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001444}
1445
Ted Kremenekee985462008-01-16 18:18:48 +00001446//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001447// "Assume" logic.
1448//===----------------------------------------------------------------------===//
1449
1450StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
1451 bool& isFeasible) {
1452 return St;
1453}
1454
1455StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
1456 bool& isFeasible) {
1457
1458 switch (Cond.getSubKind()) {
1459 default:
1460 assert (false && "'Assume' not implemented for this NonLValue.");
1461 return St;
1462
1463 case ConcreteIntKind: {
1464 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
1465 isFeasible = b ? Assumption : !Assumption;
1466 return St;
1467 }
1468 }
1469}
1470
1471
1472//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001473// Driver.
1474//===----------------------------------------------------------------------===//
1475
Ted Kremenekaa66a322008-01-16 21:46:15 +00001476#ifndef NDEBUG
1477namespace llvm {
1478template<>
1479struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1480 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001481
1482 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001483 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001484 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001485 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1486 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1487 default: assert (false && "Unknown ValueKey type.");
1488 }
1489 }
1490
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001491 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1492 ValueKey::Kind kind, bool isFirstGroup = false) {
1493 bool isFirst = true;
1494
1495 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1496 if (I.getKey().getKind() != kind)
1497 continue;
1498
1499 if (isFirst) {
1500 if (!isFirstGroup) Out << "\\l\\l";
1501 PrintKindLabel(Out, kind);
1502 isFirst = false;
1503 }
1504 else
1505 Out << "\\l";
1506
1507 Out << ' ';
1508
1509 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1510 Out << V->getName();
1511 else {
1512 Stmt* E = cast<Stmt>(I.getKey());
1513 Out << " (" << (void*) E << ") ";
1514 E->printPretty(Out);
1515 }
1516
1517 Out << " : ";
1518 I.getData().print(Out);
1519 }
1520 }
1521
Ted Kremenekaa66a322008-01-16 21:46:15 +00001522 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1523 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001524
1525 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001526 ProgramPoint Loc = N->getLocation();
1527
1528 switch (Loc.getKind()) {
1529 case ProgramPoint::BlockEntranceKind:
1530 Out << "Block Entrance: B"
1531 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1532 break;
1533
1534 case ProgramPoint::BlockExitKind:
1535 assert (false);
1536 break;
1537
1538 case ProgramPoint::PostStmtKind: {
1539 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001540 Out << L.getStmt()->getStmtClassName() << ':'
1541 << (void*) L.getStmt() << ' ';
1542
Ted Kremenekaa66a322008-01-16 21:46:15 +00001543 L.getStmt()->printPretty(Out);
1544 break;
1545 }
1546
1547 default: {
1548 const BlockEdge& E = cast<BlockEdge>(Loc);
1549 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1550 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001551
1552 if (Stmt* T = E.getSrc()->getTerminator()) {
1553 Out << "\\|Terminator: ";
1554 E.getSrc()->printTerminator(Out);
1555
1556 if (isa<SwitchStmt>(T)) {
1557 // FIXME
1558 }
1559 else {
1560 Out << "\\lCondition: ";
1561 if (*E.getSrc()->succ_begin() == E.getDst())
1562 Out << "true";
1563 else
1564 Out << "false";
1565 }
1566
1567 Out << "\\l";
1568 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001569 }
1570 }
1571
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001572 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001573
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001574 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1575 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001576 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001577
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001578 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001579 return Out.str();
1580 }
1581};
1582} // end llvm namespace
1583#endif
1584
Ted Kremenekee985462008-01-16 18:18:48 +00001585namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001586void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1587 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001588 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001589#ifndef NDEBUG
1590 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
1591#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001592}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001593} // end clang namespace