blob: ae6cdf7bf9ba4a2664dd20870092d6719b1acd37 [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM 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 }
Ted Kremenek4150abf2008-01-31 00:09:56 +0000330
331 static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000332
Ted Kremenekf13794e2008-01-24 23:19:54 +0000333 inline bool isValid() const { return getRawKind() != InvalidKind; }
334 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
335
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000336 void print(std::ostream& OS) const;
337 void print() const { print(*llvm::cerr.stream()); }
338
339 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000340 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000341};
342
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000343class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000344public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000345 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000346
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000347 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000348 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000349 }
350};
Ted Kremenek403c1812008-01-28 22:51:57 +0000351
352class VISIBILITY_HIDDEN UninitializedValue : public RValue {
353public:
354 UninitializedValue() : RValue(UninitializedKind) {}
355
356 static inline bool classof(const RValue* V) {
357 return V->getBaseKind() == UninitializedKind;
358 }
359};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000360
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000361class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000362protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000363 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000364
365public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000366 void print(std::ostream& Out) const;
367
Ted Kremenek687af802008-01-29 19:43:15 +0000368 // Arithmetic operators.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000369 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
370 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
371 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
372 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
373 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
374 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000375
Ted Kremenek687af802008-01-29 19:43:15 +0000376 // Equality operators.
377 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
378 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000379
Ted Kremenek687af802008-01-29 19:43:15 +0000380 // Utility methods to create NonLValues.
381 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
382 SourceLocation Loc = SourceLocation());
383
384 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000385
Ted Kremenek687af802008-01-29 19:43:15 +0000386 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
387 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
388 }
389
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000390 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000391 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000392 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000393 }
394};
Ted Kremenek687af802008-01-29 19:43:15 +0000395
396class VISIBILITY_HIDDEN LValue : public RValue {
397protected:
398 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
399
400public:
Ted Kremenek4150abf2008-01-31 00:09:56 +0000401 void print(std::ostream& Out) const;
Ted Kremenek687af802008-01-29 19:43:15 +0000402
403 // Equality operators.
404 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
405 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
406
407 // Implement isa<T> support.
408 static inline bool classof(const RValue* V) {
409 return V->getBaseKind() == LValueKind;
410 }
411};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000412
413} // end anonymous namespace
414
415//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000416// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000417//===----------------------------------------------------------------------===//
418
419namespace {
420
Ted Kremenek4150abf2008-01-31 00:09:56 +0000421enum { SymbolicLValueKind, LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000422
Ted Kremenek4150abf2008-01-31 00:09:56 +0000423class VISIBILITY_HIDDEN SymbolicLValue : public LValue {
424public:
425 SymbolicLValue(unsigned SymID)
426 : LValue(SymbolicLValueKind, reinterpret_cast<void*>((uintptr_t) SymID)) {}
427
428 SymbolID getSymbolID() const {
429 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
430 }
431
432 static inline bool classof(const RValue* V) {
433 return V->getSubKind() == SymbolicLValueKind;
434 }
435};
436
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000437class VISIBILITY_HIDDEN LValueDecl : public LValue {
438public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000439 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000440 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000441
442 ValueDecl* getDecl() const {
443 return static_cast<ValueDecl*>(getRawPtr());
444 }
445
Ted Kremenek687af802008-01-29 19:43:15 +0000446 inline bool operator==(const LValueDecl& R) const {
447 return getDecl() == R.getDecl();
448 }
449
450 inline bool operator!=(const LValueDecl& R) const {
451 return getDecl() != R.getDecl();
452 }
453
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000454 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000455 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000456 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000457 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000458};
459
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000460} // end anonymous namespace
461
462//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000463// Non-LValues.
464//===----------------------------------------------------------------------===//
465
466namespace {
467
Ted Kremenekf2645622008-01-28 22:25:21 +0000468enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
469 NumNonLValueKind };
470
471class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
472public:
473 SymbolicNonLValue(unsigned SymID)
474 : NonLValue(SymbolicNonLValueKind,
475 reinterpret_cast<void*>((uintptr_t) SymID)) {}
476
477 SymbolID getSymbolID() const {
478 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
479 }
480
481 static inline bool classof(const RValue* V) {
482 return V->getSubKind() == SymbolicNonLValueKind;
483 }
484};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000485
486class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
487public:
488 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
489
490 const APSInt& getValue() const {
491 return *static_cast<APSInt*>(getRawPtr());
492 }
493
Ted Kremenek687af802008-01-29 19:43:15 +0000494 // Arithmetic operators.
495
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000496 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
497 return ValMgr.getValue(getValue() + V.getValue());
498 }
Ted Kremenek687af802008-01-29 19:43:15 +0000499
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000500 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
501 return ValMgr.getValue(getValue() - V.getValue());
502 }
503
504 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
505 return ValMgr.getValue(getValue() * V.getValue());
506 }
507
508 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
509 return ValMgr.getValue(getValue() / V.getValue());
510 }
511
512 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
513 return ValMgr.getValue(getValue() % V.getValue());
514 }
515
Ted Kremenek687af802008-01-29 19:43:15 +0000516 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
517 assert (U->getType() == U->getSubExpr()->getType());
518 assert (U->getType()->isIntegerType());
519 return ValMgr.getValue(-getValue());
520 }
521
522 // Casting.
523
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000524 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
525 assert (CastExpr->getType()->isIntegerType());
526
527 APSInt X(getValue());
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000528 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
529 CastExpr->getLocStart()));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000530 return ValMgr.getValue(X);
531 }
532
Ted Kremenek687af802008-01-29 19:43:15 +0000533 // Equality operators.
534
535 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
536 const APSInt& Val = getValue();
537 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
538 Val.getBitWidth(), Val.isUnsigned());
539 }
540
541 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
542 const APSInt& Val = getValue();
543 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
544 Val.getBitWidth(), Val.isUnsigned());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000545 }
546
547 // Implement isa<T> support.
548 static inline bool classof(const RValue* V) {
549 return V->getSubKind() == ConcreteIntKind;
550 }
551};
552
553} // end anonymous namespace
554
555//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000556// Transfer function dispatch for Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000557//===----------------------------------------------------------------------===//
558
559RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
560 switch (getSubKind()) {
561 case ConcreteIntKind:
562 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
563 default:
564 return InvalidValue();
565 }
566}
567
568NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
569 switch (getSubKind()) {
570 case ConcreteIntKind:
571 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
572 default:
573 return cast<NonLValue>(InvalidValue());
574 }
575}
576
Ted Kremenek687af802008-01-29 19:43:15 +0000577#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000578case (k1##Kind*NumNonLValueKind+k2##Kind):\
579 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
580
Ted Kremenek687af802008-01-29 19:43:15 +0000581#define NONLVALUE_DISPATCH(Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000582switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
Ted Kremenek687af802008-01-29 19:43:15 +0000583 NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000584 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000585 if (getBaseKind() == UninitializedKind ||\
586 RHS.getBaseKind() == UninitializedKind)\
587 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000588 assert (!isValid() || !RHS.isValid() && "Missing case.");\
589 break;\
590}\
591return cast<NonLValue>(InvalidValue());
592
593NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000594 NONLVALUE_DISPATCH(Add)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000595}
596
597NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000598 NONLVALUE_DISPATCH(Sub)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000599}
600
601NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000602 NONLVALUE_DISPATCH(Mul)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000603}
604
605NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000606 NONLVALUE_DISPATCH(Div)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000607}
608
609NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000610 NONLVALUE_DISPATCH(Rem)
611}
612
613NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
614 NONLVALUE_DISPATCH(EQ)
615}
616
617NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
618 NONLVALUE_DISPATCH(NE)
619}
620
621#undef NONLVALUE_DISPATCH_CASE
622#undef NONLVALUE_DISPATCH
623
624//===----------------------------------------------------------------------===//
625// Transfer function dispatch for LValues.
626//===----------------------------------------------------------------------===//
627
628
629NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
630 if (getSubKind() != RHS.getSubKind())
631 return NonLValue::GetIntTruthValue(ValMgr, false);
632
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 }
643}
644
645NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
646 if (getSubKind() != RHS.getSubKind())
Ted Kremenek03701672008-01-29 21:27:49 +0000647 return NonLValue::GetIntTruthValue(ValMgr, true);
Ted Kremenek687af802008-01-29 19:43:15 +0000648
649 switch (getSubKind()) {
650 default:
651 assert(false && "EQ not implemented for this LValue.");
652 return cast<NonLValue>(InvalidValue());
653
654 case LValueDeclKind: {
655 bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
656 return NonLValue::GetIntTruthValue(ValMgr, b);
657 }
658 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000659}
660
661
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000662//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000663// Utility methods for constructing Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000664//===----------------------------------------------------------------------===//
665
Ted Kremenek687af802008-01-29 19:43:15 +0000666NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
667 SourceLocation Loc) {
668
669 return ConcreteInt(ValMgr.getValue(X, T, Loc));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000670}
671
672NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
673 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
674 I->getType()->isUnsignedIntegerType())));
675}
676
Ted Kremenek4150abf2008-01-31 00:09:56 +0000677RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
678 QualType T = D->getType();
679
680 if (T->isPointerType() || T->isReferenceType())
681 return SymbolicLValue(SymMgr.getSymbol(D));
682 else
683 return SymbolicNonLValue(SymMgr.getSymbol(D));
Ted Kremenek68fd2572008-01-29 17:27:31 +0000684}
685
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000686//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000687// Pretty-Printing.
688//===----------------------------------------------------------------------===//
689
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000690void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000691 switch (getBaseKind()) {
692 case InvalidKind:
693 Out << "Invalid";
694 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000695
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000696 case NonLValueKind:
697 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000698 break;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000699
Ted Kremenekf13794e2008-01-24 23:19:54 +0000700 case LValueKind:
Ted Kremenek4150abf2008-01-31 00:09:56 +0000701 cast<LValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000702 break;
703
Ted Kremenek403c1812008-01-28 22:51:57 +0000704 case UninitializedKind:
705 Out << "Uninitialized";
706 break;
707
Ted Kremenekf13794e2008-01-24 23:19:54 +0000708 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000710 }
711}
712
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000713void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000714 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000715 case ConcreteIntKind:
716 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000717 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000718
Ted Kremenek68fd2572008-01-29 17:27:31 +0000719 case SymbolicNonLValueKind:
Ted Kremenek6753fe32008-01-30 18:54:06 +0000720 Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID();
Ted Kremenek68fd2572008-01-29 17:27:31 +0000721 break;
722
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000723 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000724 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000725 break;
726 }
727}
728
Ted Kremenek4150abf2008-01-31 00:09:56 +0000729void LValue::print(std::ostream& Out) const {
730 switch (getSubKind()) {
731 case SymbolicLValueKind:
732 Out << '$' << cast<SymbolicLValue>(this)->getSymbolID();
733 break;
734
735 case LValueDeclKind:
Ted Kremenek64924852008-01-31 02:35:41 +0000736 Out << '&'
737 << cast<LValueDecl>(this)->getDecl()->getIdentifier()->getName();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000738 break;
739
740 default:
741 assert (false && "Pretty-printed not implemented for this LValue.");
742 break;
743 }
744}
745
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000746//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000747// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000748//===----------------------------------------------------------------------===//
749
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000750typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000751
752namespace clang {
753 template<>
754 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
755 static inline void* toPtr(ValueMapTy M) {
756 return reinterpret_cast<void*>(M.getRoot());
757 }
758 static inline ValueMapTy toState(void* P) {
759 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
760 }
761 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000762}
763
Ted Kremenekb38911f2008-01-30 23:03:39 +0000764typedef ValueMapTy StateTy;
765
Ted Kremenekd27f8162008-01-15 23:55:06 +0000766//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000767// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000768//
769// FIXME: This checker logic should be eventually broken into two components.
770// The first is the "meta"-level checking logic; the code that
771// does the Stmt visitation, fetching values from the map, etc.
772// The second part does the actual state manipulation. This way we
773// get more of a separate of concerns of these two pieces, with the
774// latter potentially being refactored back into the main checking
775// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000776//===----------------------------------------------------------------------===//
777
778namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000779
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000780class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000781
782public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000783 typedef ValueMapTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000784 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
785 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000786 typedef ExplodedGraph<GRConstants> GraphTy;
787 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000788
789 class NodeSet {
790 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
791 ImplTy Impl;
792 public:
793
794 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000795 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000796
Ted Kremenekb38911f2008-01-30 23:03:39 +0000797 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000798
799 typedef ImplTy::iterator iterator;
800 typedef ImplTy::const_iterator const_iterator;
801
802 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000803 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000804
805 iterator begin() { return Impl.begin(); }
806 iterator end() { return Impl.end(); }
807
808 const_iterator begin() const { return Impl.begin(); }
809 const_iterator end() const { return Impl.end(); }
810 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000811
812protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000813 /// G - the simulation graph.
814 GraphTy& G;
815
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000816 /// Liveness - live-variables information the ValueDecl* and block-level
817 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000818 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000819
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000820 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000821 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000822 StmtNodeBuilder* Builder;
823
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000824 /// StateMgr - Object that manages the data for all created states.
825 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000826
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000827 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000828 ValueManager ValMgr;
829
Ted Kremenek68fd2572008-01-29 17:27:31 +0000830 /// SymMgr - Object that manages the symbol information.
831 SymbolManager SymMgr;
832
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000833 /// StmtEntryNode - The immediate predecessor node.
834 NodeTy* StmtEntryNode;
835
836 /// CurrentStmt - The current block-level statement.
837 Stmt* CurrentStmt;
838
Ted Kremenekb38911f2008-01-30 23:03:39 +0000839 /// UninitBranches - Nodes in the ExplodedGraph that result from
840 /// taking a branch based on an uninitialized value.
841 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
842 UninitBranchesTy UninitBranches;
843
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000844 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000845
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000846 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000847
Ted Kremenekd27f8162008-01-15 23:55:06 +0000848public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000849 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
850 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
851 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000852
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000853 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000854 Liveness.runOnCFG(G.getCFG());
855 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000856 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000857
858 /// getCFG - Returns the CFG associated with this analysis.
859 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000860
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861 /// getInitialState - Return the initial state used for the root vertex
862 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000863 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000864 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000865
866 // Iterate the parameters.
867 FunctionDecl& F = G.getFunctionDecl();
868
869 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000870 I!=E; ++I)
871 St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000872
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000873 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000874 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000875
876 bool isUninitControlFlow(const NodeTy* N) const {
877 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
878 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000879
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000880 /// ProcessStmt - Called by GREngine. Used to generate new successor
881 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000882 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
883
884 /// ProcessBranch - Called by GREngine. Used to generate successor
885 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000886 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000887
888 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
889 /// that all subexpression mappings are removed and that any
890 /// block-level expressions that are not live at 'S' also have their
891 /// mappings removed.
892 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
893
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000894 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000895
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000896 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000897 return SetValue(St, const_cast<Stmt*>(S), V);
898 }
899
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000900 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000901
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000902 RValue GetValue(const StateTy& St, Stmt* S);
903 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000904 return GetValue(St, const_cast<Stmt*>(S));
905 }
906
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000907 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000908 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000909
910 /// Assume - Create new state by assuming that a given expression
911 /// is true or false.
912 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
913 bool& isFeasible) {
914 if (isa<LValue>(Cond))
915 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
916 else
917 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
918 }
919
920 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
921 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000922
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000923 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000924
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000925 /// Visit - Transfer function logic for all statements. Dispatches to
926 /// other functions that handle specific kinds of statements.
927 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000928
929 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
930 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000931
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000932 /// VisitUnaryOperator - Transfer function logic for unary operators.
933 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
934
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000935 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000936 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
937
938 /// VisitDeclStmt - Transfer function logic for DeclStmts.
939 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000940};
941} // end anonymous namespace
942
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000943
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000944void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
945 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000946
947 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000948
Ted Kremenekb38911f2008-01-30 23:03:39 +0000949 // Remove old bindings for subexpressions.
950 for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
951 if (I.getKey().isSubExpr())
952 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000953
Ted Kremenekb38911f2008-01-30 23:03:39 +0000954 RValue V = GetValue(PrevState, Condition);
955
956 switch (V.getBaseKind()) {
957 default:
958 break;
959
960 case RValue::InvalidKind:
961 builder.generateNode(PrevState, true);
962 builder.generateNode(PrevState, false);
963 return;
964
965 case RValue::UninitializedKind: {
966 NodeTy* N = builder.generateNode(PrevState, true);
967
968 if (N) {
969 N->markAsSink();
970 UninitBranches.insert(N);
971 }
972
973 builder.markInfeasible(false);
974 return;
975 }
976 }
977
978 // Process the true branch.
979 bool isFeasible = true;
980 StateTy St = Assume(PrevState, V, true, isFeasible);
981
982 if (isFeasible) builder.generateNode(St, true);
983 else {
984 builder.markInfeasible(true);
985 isFeasible = true;
986 }
987
988 // Process the false branch.
989 St = Assume(PrevState, V, false, isFeasible);
990
991 if (isFeasible) builder.generateNode(St, false);
992 else builder.markInfeasible(false);
993
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000994}
995
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000996void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000997 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000998
999 StmtEntryNode = builder.getLastNode();
1000 CurrentStmt = S;
1001 NodeSet Dst;
1002 StateCleaned = false;
1003
1004 Visit(S, StmtEntryNode, Dst);
1005
1006 // If no nodes were generated, generate a new node that has all the
1007 // dead mappings removed.
1008 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
1009 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
1010 builder.generateNode(S, St, StmtEntryNode);
1011 }
Ted Kremenekf84469b2008-01-18 00:41:32 +00001012
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001013 CurrentStmt = NULL;
1014 StmtEntryNode = NULL;
1015 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +00001016}
1017
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001018
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001019RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +00001020 switch (LV.getSubKind()) {
1021 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001022 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
1023 return T ? T->getValue().second : InvalidValue();
1024 }
1025 default:
1026 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +00001027 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001028 }
1029
1030 return InvalidValue();
1031}
1032
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001033RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +00001034 for (;;) {
1035 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001036
1037 // ParenExprs are no-ops.
1038
Ted Kremenek671c9e82008-01-24 00:50:08 +00001039 case Stmt::ParenExprClass:
1040 S = cast<ParenExpr>(S)->getSubExpr();
1041 continue;
1042
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001043 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
1044 // (assuming an implicit "load") depending on the context. In this
1045 // context we assume that we are retrieving the value contained
1046 // within the referenced variables.
1047
Ted Kremenek671c9e82008-01-24 00:50:08 +00001048 case Stmt::DeclRefExprClass:
1049 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +00001050
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001051 // Integer literals evaluate to an RValue. Simply retrieve the
1052 // RValue for the literal.
1053
Ted Kremenek671c9e82008-01-24 00:50:08 +00001054 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001055 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
1056
1057 // Casts where the source and target type are the same
1058 // are no-ops. We blast through these to get the descendant
1059 // subexpression that has a value.
1060
Ted Kremenek874d63f2008-01-24 02:02:54 +00001061 case Stmt::ImplicitCastExprClass: {
1062 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1063 if (C->getType() == C->getSubExpr()->getType()) {
1064 S = C->getSubExpr();
1065 continue;
1066 }
1067 break;
1068 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001069
Ted Kremenek874d63f2008-01-24 02:02:54 +00001070 case Stmt::CastExprClass: {
1071 CastExpr* C = cast<CastExpr>(S);
1072 if (C->getType() == C->getSubExpr()->getType()) {
1073 S = C->getSubExpr();
1074 continue;
1075 }
1076 break;
1077 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001078
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001079 // Handle all other Stmt* using a lookup.
1080
Ted Kremenek671c9e82008-01-24 00:50:08 +00001081 default:
1082 break;
1083 };
1084
1085 break;
1086 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001087
Ted Kremenek5c1b9962008-01-24 19:43:37 +00001088 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +00001089
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001090 return T ? T->getValue().second : InvalidValue();
1091}
1092
1093LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001094 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
1095 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001096
1097 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
1098 return LValueDecl(DR->getDecl());
1099
1100 return cast<LValue>(GetValue(St, S));
1101}
1102
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001103
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001104GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001105 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +00001106 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001107
1108 if (!StateCleaned) {
1109 St = RemoveDeadBindings(CurrentStmt, St);
1110 StateCleaned = true;
1111 }
1112
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001113 bool isBlkExpr = false;
1114
1115 if (S == CurrentStmt) {
1116 isBlkExpr = getCFG().isBlkExpr(S);
1117
1118 if (!isBlkExpr)
1119 return St;
1120 }
Ted Kremenekdaadf452008-01-24 19:28:01 +00001121
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001122 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1123 : St;
1124}
1125
1126GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001127 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001128 if (!LV.isValid())
1129 return St;
1130
1131 if (!StateCleaned) {
1132 St = RemoveDeadBindings(CurrentStmt, St);
1133 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001134 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001135
Ted Kremenekf13794e2008-01-24 23:19:54 +00001136 switch (LV.getSubKind()) {
1137 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001138 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1139 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1140
1141 default:
1142 assert ("SetValue for given LValue type not yet implemented.");
1143 return St;
1144 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001145}
1146
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001147GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001148 // Note: in the code below, we can assign a new map to M since the
1149 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001150 StateTy::iterator I = M.begin(), E = M.end();
1151
Ted Kremenekf84469b2008-01-18 00:41:32 +00001152
Ted Kremenek65cac132008-01-29 05:25:31 +00001153 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1154 // Remove old bindings for subexpressions and "dead"
1155 // block-level expressions.
1156 if (I.getKey().isSubExpr() ||
1157 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1158 M = StateMgr.Remove(M, I.getKey());
1159 }
1160 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1161 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1162 if (!Liveness.isLive(Loc, V))
1163 M = StateMgr.Remove(M, I.getKey());
1164 }
1165 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001166
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001167 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001168}
1169
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001170void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1171 GRConstants::StateTy St) {
1172
1173 // If the state hasn't changed, don't generate a new node.
1174 if (St == Pred->getState())
1175 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001176
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001177 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001178}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001179
Ted Kremenek874d63f2008-01-24 02:02:54 +00001180void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1181 GRConstants::NodeSet& Dst) {
1182
1183 QualType T = CastE->getType();
1184
1185 // Check for redundant casts.
1186 if (E->getType() == T) {
1187 Dst.Add(Pred);
1188 return;
1189 }
1190
1191 NodeSet S1;
1192 Visit(E, Pred, S1);
1193
1194 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1195 NodeTy* N = *I1;
1196 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001197 const RValue& V = GetValue(St, E);
1198 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001199 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001200}
1201
1202void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1203 GRConstants::NodeSet& Dst) {
1204
1205 StateTy St = Pred->getState();
1206
1207 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001208 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1209 const Expr* E = VD->getInit();
1210 St = SetValue(St, LValueDecl(VD),
1211 E ? GetValue(St, E) : UninitializedValue());
1212 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001213
1214 Nodify(Dst, DS, Pred, St);
1215
1216 if (Dst.empty())
1217 Dst.Add(Pred);
1218}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001219
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001220void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1221 GRConstants::NodeTy* Pred,
1222 GRConstants::NodeSet& Dst) {
1223 NodeSet S1;
1224 Visit(U->getSubExpr(), Pred, S1);
1225
1226 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1227 NodeTy* N1 = *I1;
1228 StateTy St = N1->getState();
1229
1230 switch (U->getOpcode()) {
1231 case UnaryOperator::PostInc: {
1232 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001233 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001234 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1235 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001236
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001237 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001238 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1239 break;
1240 }
1241
1242 case UnaryOperator::PostDec: {
1243 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001244 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001245 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1246 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001247
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001248 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001249 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1250 break;
1251 }
1252
1253 case UnaryOperator::PreInc: {
1254 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001255 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001256 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1257 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001258
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001259 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001260 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1261 break;
1262 }
1263
1264 case UnaryOperator::PreDec: {
1265 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001266 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001267 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1268 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001269
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001270 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001271 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1272 break;
1273 }
1274
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001275 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001276 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1277 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001278 break;
1279 }
1280
Ted Kremenek64924852008-01-31 02:35:41 +00001281 case UnaryOperator::AddrOf: {
1282 const LValue& L1 = GetLValue(St, U->getSubExpr());
1283 Nodify(Dst, U, N1, SetValue(St, U, L1));
1284 break;
1285 }
1286
1287 case UnaryOperator::Deref: {
1288 const LValue& L1 = GetLValue(St, U->getSubExpr());
1289 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
1290 break;
1291 }
1292
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001293 default: ;
1294 assert (false && "Not implemented.");
1295 }
1296 }
1297}
1298
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001299void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1300 GRConstants::NodeTy* Pred,
1301 GRConstants::NodeSet& Dst) {
1302 NodeSet S1;
1303 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001304
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001305 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1306 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001307
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001308 // When getting the value for the LHS, check if we are in an assignment.
1309 // In such cases, we want to (initially) treat the LHS as an LValue,
1310 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001311 // evaluated to LValueDecl's instead of to an NonLValue.
1312 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001313 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1314 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001315
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001316 NodeSet S2;
1317 Visit(B->getRHS(), N1, S2);
1318
1319 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1320 NodeTy* N2 = *I2;
1321 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001322 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001323
1324 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001325 default:
1326 Dst.Add(N2);
1327 break;
1328
1329 // Arithmetic opreators.
1330
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001331 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001332 const NonLValue& R1 = cast<NonLValue>(V1);
1333 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001334
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001335 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001336 break;
1337 }
1338
1339 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001340 const NonLValue& R1 = cast<NonLValue>(V1);
1341 const NonLValue& R2 = cast<NonLValue>(V2);
1342 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001343 break;
1344 }
1345
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001346 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001347 const NonLValue& R1 = cast<NonLValue>(V1);
1348 const NonLValue& R2 = cast<NonLValue>(V2);
1349 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001350 break;
1351 }
1352
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001353 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001354 const NonLValue& R1 = cast<NonLValue>(V1);
1355 const NonLValue& R2 = cast<NonLValue>(V2);
1356 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001357 break;
1358 }
1359
Ted Kremenekcce207d2008-01-28 22:26:15 +00001360 case BinaryOperator::Rem: {
1361 const NonLValue& R1 = cast<NonLValue>(V1);
1362 const NonLValue& R2 = cast<NonLValue>(V2);
1363 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1364 break;
1365 }
1366
Ted Kremenek687af802008-01-29 19:43:15 +00001367 // Assignment operators.
1368
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001369 case BinaryOperator::Assign: {
1370 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001371 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001372 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1373 break;
1374 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001375
1376 case BinaryOperator::AddAssign: {
1377 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001378 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1379 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001380 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1381 break;
1382 }
1383
1384 case BinaryOperator::SubAssign: {
1385 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001386 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1387 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001388 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1389 break;
1390 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001391
1392 case BinaryOperator::MulAssign: {
1393 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001394 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1395 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001396 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1397 break;
1398 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001399
1400 case BinaryOperator::DivAssign: {
1401 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001402 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1403 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001404 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1405 break;
1406 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001407
1408 case BinaryOperator::RemAssign: {
1409 const LValue& L1 = cast<LValue>(V1);
1410 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1411 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1412 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1413 break;
1414 }
Ted Kremenek687af802008-01-29 19:43:15 +00001415
1416 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001417
Ted Kremenek687af802008-01-29 19:43:15 +00001418 case BinaryOperator::EQ:
1419 // FIXME: should we allow XX.EQ() to return a set of values,
1420 // allowing state bifurcation? In such cases, they will also
1421 // modify the state (meaning that a new state will be returned
1422 // as well).
1423 assert (B->getType() == getContext().IntTy);
1424
1425 if (isa<LValue>(V1)) {
1426 const LValue& L1 = cast<LValue>(V1);
1427 const LValue& L2 = cast<LValue>(V2);
1428 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1429 }
1430 else {
1431 const NonLValue& R1 = cast<NonLValue>(V1);
1432 const NonLValue& R2 = cast<NonLValue>(V2);
1433 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1434 }
1435
1436 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001437 break;
1438 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001439 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001440 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001441}
Ted Kremenekee985462008-01-16 18:18:48 +00001442
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001443
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001444void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1445 GRConstants::NodeSet& Dst) {
1446
1447 // FIXME: add metadata to the CFG so that we can disable
1448 // this check when we KNOW that there is no block-level subexpression.
1449 // The motivation is that this check requires a hashtable lookup.
1450
1451 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1452 Dst.Add(Pred);
1453 return;
1454 }
1455
1456 switch (S->getStmtClass()) {
1457 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001458 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001459 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1460 break;
1461
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001462 case Stmt::UnaryOperatorClass:
1463 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1464 break;
1465
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001466 case Stmt::ParenExprClass:
1467 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1468 break;
1469
Ted Kremenek874d63f2008-01-24 02:02:54 +00001470 case Stmt::ImplicitCastExprClass: {
1471 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1472 VisitCast(C, C->getSubExpr(), Pred, Dst);
1473 break;
1474 }
1475
1476 case Stmt::CastExprClass: {
1477 CastExpr* C = cast<CastExpr>(S);
1478 VisitCast(C, C->getSubExpr(), Pred, Dst);
1479 break;
1480 }
1481
Ted Kremenek9de04c42008-01-24 20:55:43 +00001482 case Stmt::DeclStmtClass:
1483 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1484 break;
1485
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001486 default:
1487 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1488 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001489 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001490}
1491
Ted Kremenekee985462008-01-16 18:18:48 +00001492//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001493// "Assume" logic.
1494//===----------------------------------------------------------------------===//
1495
1496StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
1497 bool& isFeasible) {
1498 return St;
1499}
1500
1501StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
1502 bool& isFeasible) {
1503
1504 switch (Cond.getSubKind()) {
1505 default:
1506 assert (false && "'Assume' not implemented for this NonLValue.");
1507 return St;
1508
1509 case ConcreteIntKind: {
1510 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
1511 isFeasible = b ? Assumption : !Assumption;
1512 return St;
1513 }
1514 }
1515}
1516
1517
1518//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001519// Driver.
1520//===----------------------------------------------------------------------===//
1521
Ted Kremenekaa66a322008-01-16 21:46:15 +00001522#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001523static GRConstants* GraphPrintCheckerState;
1524
Ted Kremenekaa66a322008-01-16 21:46:15 +00001525namespace llvm {
1526template<>
1527struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1528 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001529
1530 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001531 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001532 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001533 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1534 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1535 default: assert (false && "Unknown ValueKey type.");
1536 }
1537 }
1538
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001539 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1540 ValueKey::Kind kind, bool isFirstGroup = false) {
1541 bool isFirst = true;
1542
1543 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1544 if (I.getKey().getKind() != kind)
1545 continue;
1546
1547 if (isFirst) {
1548 if (!isFirstGroup) Out << "\\l\\l";
1549 PrintKindLabel(Out, kind);
1550 isFirst = false;
1551 }
1552 else
1553 Out << "\\l";
1554
1555 Out << ' ';
1556
1557 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1558 Out << V->getName();
1559 else {
1560 Stmt* E = cast<Stmt>(I.getKey());
1561 Out << " (" << (void*) E << ") ";
1562 E->printPretty(Out);
1563 }
1564
1565 Out << " : ";
1566 I.getData().print(Out);
1567 }
1568 }
1569
Ted Kremenekaa66a322008-01-16 21:46:15 +00001570 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1571 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001572
1573 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001574 ProgramPoint Loc = N->getLocation();
1575
1576 switch (Loc.getKind()) {
1577 case ProgramPoint::BlockEntranceKind:
1578 Out << "Block Entrance: B"
1579 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1580 break;
1581
1582 case ProgramPoint::BlockExitKind:
1583 assert (false);
1584 break;
1585
1586 case ProgramPoint::PostStmtKind: {
1587 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001588 Out << L.getStmt()->getStmtClassName() << ':'
1589 << (void*) L.getStmt() << ' ';
1590
Ted Kremenekaa66a322008-01-16 21:46:15 +00001591 L.getStmt()->printPretty(Out);
1592 break;
1593 }
1594
1595 default: {
1596 const BlockEdge& E = cast<BlockEdge>(Loc);
1597 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1598 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001599
1600 if (Stmt* T = E.getSrc()->getTerminator()) {
1601 Out << "\\|Terminator: ";
1602 E.getSrc()->printTerminator(Out);
1603
1604 if (isa<SwitchStmt>(T)) {
1605 // FIXME
1606 }
1607 else {
1608 Out << "\\lCondition: ";
1609 if (*E.getSrc()->succ_begin() == E.getDst())
1610 Out << "true";
1611 else
1612 Out << "false";
1613 }
1614
1615 Out << "\\l";
1616 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001617
1618 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1619 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1620 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001621 }
1622 }
1623
Ted Kremenek64924852008-01-31 02:35:41 +00001624 Out << "\\|StateID: " << (void*) N->getState().getRoot() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001625
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001626 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1627 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001628 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001629
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001630 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001631 return Out.str();
1632 }
1633};
1634} // end llvm namespace
1635#endif
1636
Ted Kremenekee985462008-01-16 18:18:48 +00001637namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001638void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1639 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001640 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001641#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001642 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001643 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001644 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001645#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001646}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001647} // end clang namespace