blob: b7d068a118719a6497e19b04da383305fb3d27ef [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 }
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:
736 Out << cast<LValueDecl>(this)->getDecl()->getIdentifier();
737 break;
738
739 default:
740 assert (false && "Pretty-printed not implemented for this LValue.");
741 break;
742 }
743}
744
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000745//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000746// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000747//===----------------------------------------------------------------------===//
748
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000749typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000750
751namespace clang {
752 template<>
753 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
754 static inline void* toPtr(ValueMapTy M) {
755 return reinterpret_cast<void*>(M.getRoot());
756 }
757 static inline ValueMapTy toState(void* P) {
758 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
759 }
760 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000761}
762
Ted Kremenekb38911f2008-01-30 23:03:39 +0000763typedef ValueMapTy StateTy;
764
Ted Kremenekd27f8162008-01-15 23:55:06 +0000765//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000766// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000767//
768// FIXME: This checker logic should be eventually broken into two components.
769// The first is the "meta"-level checking logic; the code that
770// does the Stmt visitation, fetching values from the map, etc.
771// The second part does the actual state manipulation. This way we
772// get more of a separate of concerns of these two pieces, with the
773// latter potentially being refactored back into the main checking
774// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000775//===----------------------------------------------------------------------===//
776
777namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000778
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000779class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000780
781public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000782 typedef ValueMapTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000783 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
784 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000785 typedef ExplodedGraph<GRConstants> GraphTy;
786 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000787
788 class NodeSet {
789 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
790 ImplTy Impl;
791 public:
792
793 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000794 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000795
Ted Kremenekb38911f2008-01-30 23:03:39 +0000796 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000797
798 typedef ImplTy::iterator iterator;
799 typedef ImplTy::const_iterator const_iterator;
800
801 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000802 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000803
804 iterator begin() { return Impl.begin(); }
805 iterator end() { return Impl.end(); }
806
807 const_iterator begin() const { return Impl.begin(); }
808 const_iterator end() const { return Impl.end(); }
809 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000810
811protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000812 /// G - the simulation graph.
813 GraphTy& G;
814
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000815 /// Liveness - live-variables information the ValueDecl* and block-level
816 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000817 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000818
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000819 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000820 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000821 StmtNodeBuilder* Builder;
822
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000823 /// StateMgr - Object that manages the data for all created states.
824 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000826 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000827 ValueManager ValMgr;
828
Ted Kremenek68fd2572008-01-29 17:27:31 +0000829 /// SymMgr - Object that manages the symbol information.
830 SymbolManager SymMgr;
831
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000832 /// StmtEntryNode - The immediate predecessor node.
833 NodeTy* StmtEntryNode;
834
835 /// CurrentStmt - The current block-level statement.
836 Stmt* CurrentStmt;
837
Ted Kremenekb38911f2008-01-30 23:03:39 +0000838 /// UninitBranches - Nodes in the ExplodedGraph that result from
839 /// taking a branch based on an uninitialized value.
840 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
841 UninitBranchesTy UninitBranches;
842
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000843 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000844
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000845 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000846
Ted Kremenekd27f8162008-01-15 23:55:06 +0000847public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000848 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
849 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
850 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000851
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000852 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000853 Liveness.runOnCFG(G.getCFG());
854 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000855 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000856
857 /// getCFG - Returns the CFG associated with this analysis.
858 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000859
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000860 /// getInitialState - Return the initial state used for the root vertex
861 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000862 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000863 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000864
865 // Iterate the parameters.
866 FunctionDecl& F = G.getFunctionDecl();
867
868 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000869 I!=E; ++I)
870 St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000871
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000872 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000873 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000874
875 bool isUninitControlFlow(const NodeTy* N) const {
876 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
877 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000878
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000879 /// ProcessStmt - Called by GREngine. Used to generate new successor
880 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000881 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
882
883 /// ProcessBranch - Called by GREngine. Used to generate successor
884 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000885 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000886
887 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
888 /// that all subexpression mappings are removed and that any
889 /// block-level expressions that are not live at 'S' also have their
890 /// mappings removed.
891 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
892
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000893 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000894
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000895 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000896 return SetValue(St, const_cast<Stmt*>(S), V);
897 }
898
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000899 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000900
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000901 RValue GetValue(const StateTy& St, Stmt* S);
902 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000903 return GetValue(St, const_cast<Stmt*>(S));
904 }
905
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000906 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000907 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000908
909 /// Assume - Create new state by assuming that a given expression
910 /// is true or false.
911 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
912 bool& isFeasible) {
913 if (isa<LValue>(Cond))
914 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
915 else
916 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
917 }
918
919 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
920 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000921
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000922 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000923
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000924 /// Visit - Transfer function logic for all statements. Dispatches to
925 /// other functions that handle specific kinds of statements.
926 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000927
928 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
929 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000930
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000931 /// VisitUnaryOperator - Transfer function logic for unary operators.
932 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
933
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000934 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000935 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
936
937 /// VisitDeclStmt - Transfer function logic for DeclStmts.
938 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000939};
940} // end anonymous namespace
941
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000942
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000943void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
944 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000945
946 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000947
Ted Kremenekb38911f2008-01-30 23:03:39 +0000948 // Remove old bindings for subexpressions.
949 for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
950 if (I.getKey().isSubExpr())
951 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000952
Ted Kremenekb38911f2008-01-30 23:03:39 +0000953 RValue V = GetValue(PrevState, Condition);
954
955 switch (V.getBaseKind()) {
956 default:
957 break;
958
959 case RValue::InvalidKind:
960 builder.generateNode(PrevState, true);
961 builder.generateNode(PrevState, false);
962 return;
963
964 case RValue::UninitializedKind: {
965 NodeTy* N = builder.generateNode(PrevState, true);
966
967 if (N) {
968 N->markAsSink();
969 UninitBranches.insert(N);
970 }
971
972 builder.markInfeasible(false);
973 return;
974 }
975 }
976
977 // Process the true branch.
978 bool isFeasible = true;
979 StateTy St = Assume(PrevState, V, true, isFeasible);
980
981 if (isFeasible) builder.generateNode(St, true);
982 else {
983 builder.markInfeasible(true);
984 isFeasible = true;
985 }
986
987 // Process the false branch.
988 St = Assume(PrevState, V, false, isFeasible);
989
990 if (isFeasible) builder.generateNode(St, false);
991 else builder.markInfeasible(false);
992
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000993}
994
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000995void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000996 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000997
998 StmtEntryNode = builder.getLastNode();
999 CurrentStmt = S;
1000 NodeSet Dst;
1001 StateCleaned = false;
1002
1003 Visit(S, StmtEntryNode, Dst);
1004
1005 // If no nodes were generated, generate a new node that has all the
1006 // dead mappings removed.
1007 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
1008 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
1009 builder.generateNode(S, St, StmtEntryNode);
1010 }
Ted Kremenekf84469b2008-01-18 00:41:32 +00001011
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001012 CurrentStmt = NULL;
1013 StmtEntryNode = NULL;
1014 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +00001015}
1016
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001017
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001018RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +00001019 switch (LV.getSubKind()) {
1020 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001021 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
1022 return T ? T->getValue().second : InvalidValue();
1023 }
1024 default:
1025 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +00001026 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001027 }
1028
1029 return InvalidValue();
1030}
1031
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001032RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +00001033 for (;;) {
1034 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001035
1036 // ParenExprs are no-ops.
1037
Ted Kremenek671c9e82008-01-24 00:50:08 +00001038 case Stmt::ParenExprClass:
1039 S = cast<ParenExpr>(S)->getSubExpr();
1040 continue;
1041
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001042 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
1043 // (assuming an implicit "load") depending on the context. In this
1044 // context we assume that we are retrieving the value contained
1045 // within the referenced variables.
1046
Ted Kremenek671c9e82008-01-24 00:50:08 +00001047 case Stmt::DeclRefExprClass:
1048 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +00001049
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001050 // Integer literals evaluate to an RValue. Simply retrieve the
1051 // RValue for the literal.
1052
Ted Kremenek671c9e82008-01-24 00:50:08 +00001053 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001054 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
1055
1056 // Casts where the source and target type are the same
1057 // are no-ops. We blast through these to get the descendant
1058 // subexpression that has a value.
1059
Ted Kremenek874d63f2008-01-24 02:02:54 +00001060 case Stmt::ImplicitCastExprClass: {
1061 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1062 if (C->getType() == C->getSubExpr()->getType()) {
1063 S = C->getSubExpr();
1064 continue;
1065 }
1066 break;
1067 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001068
Ted Kremenek874d63f2008-01-24 02:02:54 +00001069 case Stmt::CastExprClass: {
1070 CastExpr* C = cast<CastExpr>(S);
1071 if (C->getType() == C->getSubExpr()->getType()) {
1072 S = C->getSubExpr();
1073 continue;
1074 }
1075 break;
1076 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001077
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001078 // Handle all other Stmt* using a lookup.
1079
Ted Kremenek671c9e82008-01-24 00:50:08 +00001080 default:
1081 break;
1082 };
1083
1084 break;
1085 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001086
Ted Kremenek5c1b9962008-01-24 19:43:37 +00001087 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +00001088
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001089 return T ? T->getValue().second : InvalidValue();
1090}
1091
1092LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001093 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
1094 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001095
1096 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
1097 return LValueDecl(DR->getDecl());
1098
1099 return cast<LValue>(GetValue(St, S));
1100}
1101
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001102
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001103GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001104 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +00001105 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001106
1107 if (!StateCleaned) {
1108 St = RemoveDeadBindings(CurrentStmt, St);
1109 StateCleaned = true;
1110 }
1111
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001112 bool isBlkExpr = false;
1113
1114 if (S == CurrentStmt) {
1115 isBlkExpr = getCFG().isBlkExpr(S);
1116
1117 if (!isBlkExpr)
1118 return St;
1119 }
Ted Kremenekdaadf452008-01-24 19:28:01 +00001120
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001121 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1122 : St;
1123}
1124
1125GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001126 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001127 if (!LV.isValid())
1128 return St;
1129
1130 if (!StateCleaned) {
1131 St = RemoveDeadBindings(CurrentStmt, St);
1132 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001133 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001134
Ted Kremenekf13794e2008-01-24 23:19:54 +00001135 switch (LV.getSubKind()) {
1136 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001137 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1138 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1139
1140 default:
1141 assert ("SetValue for given LValue type not yet implemented.");
1142 return St;
1143 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001144}
1145
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001146GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001147 // Note: in the code below, we can assign a new map to M since the
1148 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001149 StateTy::iterator I = M.begin(), E = M.end();
1150
Ted Kremenekf84469b2008-01-18 00:41:32 +00001151
Ted Kremenek65cac132008-01-29 05:25:31 +00001152 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1153 // Remove old bindings for subexpressions and "dead"
1154 // block-level expressions.
1155 if (I.getKey().isSubExpr() ||
1156 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1157 M = StateMgr.Remove(M, I.getKey());
1158 }
1159 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1160 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1161 if (!Liveness.isLive(Loc, V))
1162 M = StateMgr.Remove(M, I.getKey());
1163 }
1164 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001165
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001166 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001167}
1168
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001169void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1170 GRConstants::StateTy St) {
1171
1172 // If the state hasn't changed, don't generate a new node.
1173 if (St == Pred->getState())
1174 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001175
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001176 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001177}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001178
Ted Kremenek874d63f2008-01-24 02:02:54 +00001179void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1180 GRConstants::NodeSet& Dst) {
1181
1182 QualType T = CastE->getType();
1183
1184 // Check for redundant casts.
1185 if (E->getType() == T) {
1186 Dst.Add(Pred);
1187 return;
1188 }
1189
1190 NodeSet S1;
1191 Visit(E, Pred, S1);
1192
1193 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1194 NodeTy* N = *I1;
1195 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001196 const RValue& V = GetValue(St, E);
1197 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001198 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001199}
1200
1201void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1202 GRConstants::NodeSet& Dst) {
1203
1204 StateTy St = Pred->getState();
1205
1206 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001207 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1208 const Expr* E = VD->getInit();
1209 St = SetValue(St, LValueDecl(VD),
1210 E ? GetValue(St, E) : UninitializedValue());
1211 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001212
1213 Nodify(Dst, DS, Pred, St);
1214
1215 if (Dst.empty())
1216 Dst.Add(Pred);
1217}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001218
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001219void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1220 GRConstants::NodeTy* Pred,
1221 GRConstants::NodeSet& Dst) {
1222 NodeSet S1;
1223 Visit(U->getSubExpr(), Pred, S1);
1224
1225 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1226 NodeTy* N1 = *I1;
1227 StateTy St = N1->getState();
1228
1229 switch (U->getOpcode()) {
1230 case UnaryOperator::PostInc: {
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 Kremeneke0cf9c82008-01-24 19:00:57 +00001235
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001236 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001237 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1238 break;
1239 }
1240
1241 case UnaryOperator::PostDec: {
1242 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001243 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001244 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1245 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001246
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001247 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001248 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1249 break;
1250 }
1251
1252 case UnaryOperator::PreInc: {
1253 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001254 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001255 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1256 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001257
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001258 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001259 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1260 break;
1261 }
1262
1263 case UnaryOperator::PreDec: {
1264 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001265 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001266 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1267 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001268
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001269 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001270 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1271 break;
1272 }
1273
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001274 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001275 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1276 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001277 break;
1278 }
1279
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001280 default: ;
1281 assert (false && "Not implemented.");
1282 }
1283 }
1284}
1285
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001286void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1287 GRConstants::NodeTy* Pred,
1288 GRConstants::NodeSet& Dst) {
1289 NodeSet S1;
1290 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001291
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001292 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1293 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001294
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001295 // When getting the value for the LHS, check if we are in an assignment.
1296 // In such cases, we want to (initially) treat the LHS as an LValue,
1297 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001298 // evaluated to LValueDecl's instead of to an NonLValue.
1299 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001300 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1301 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001302
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001303 NodeSet S2;
1304 Visit(B->getRHS(), N1, S2);
1305
1306 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1307 NodeTy* N2 = *I2;
1308 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001309 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001310
1311 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001312 default:
1313 Dst.Add(N2);
1314 break;
1315
1316 // Arithmetic opreators.
1317
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001318 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001319 const NonLValue& R1 = cast<NonLValue>(V1);
1320 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001321
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001322 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001323 break;
1324 }
1325
1326 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001327 const NonLValue& R1 = cast<NonLValue>(V1);
1328 const NonLValue& R2 = cast<NonLValue>(V2);
1329 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001330 break;
1331 }
1332
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001333 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001334 const NonLValue& R1 = cast<NonLValue>(V1);
1335 const NonLValue& R2 = cast<NonLValue>(V2);
1336 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001337 break;
1338 }
1339
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001340 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001341 const NonLValue& R1 = cast<NonLValue>(V1);
1342 const NonLValue& R2 = cast<NonLValue>(V2);
1343 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001344 break;
1345 }
1346
Ted Kremenekcce207d2008-01-28 22:26:15 +00001347 case BinaryOperator::Rem: {
1348 const NonLValue& R1 = cast<NonLValue>(V1);
1349 const NonLValue& R2 = cast<NonLValue>(V2);
1350 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1351 break;
1352 }
1353
Ted Kremenek687af802008-01-29 19:43:15 +00001354 // Assignment operators.
1355
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001356 case BinaryOperator::Assign: {
1357 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001358 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001359 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1360 break;
1361 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001362
1363 case BinaryOperator::AddAssign: {
1364 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001365 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1366 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001367 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1368 break;
1369 }
1370
1371 case BinaryOperator::SubAssign: {
1372 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001373 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1374 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001375 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1376 break;
1377 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001378
1379 case BinaryOperator::MulAssign: {
1380 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001381 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1382 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001383 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1384 break;
1385 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001386
1387 case BinaryOperator::DivAssign: {
1388 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001389 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1390 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001391 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1392 break;
1393 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001394
1395 case BinaryOperator::RemAssign: {
1396 const LValue& L1 = cast<LValue>(V1);
1397 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1398 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1399 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1400 break;
1401 }
Ted Kremenek687af802008-01-29 19:43:15 +00001402
1403 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001404
Ted Kremenek687af802008-01-29 19:43:15 +00001405 case BinaryOperator::EQ:
1406 // FIXME: should we allow XX.EQ() to return a set of values,
1407 // allowing state bifurcation? In such cases, they will also
1408 // modify the state (meaning that a new state will be returned
1409 // as well).
1410 assert (B->getType() == getContext().IntTy);
1411
1412 if (isa<LValue>(V1)) {
1413 const LValue& L1 = cast<LValue>(V1);
1414 const LValue& L2 = cast<LValue>(V2);
1415 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1416 }
1417 else {
1418 const NonLValue& R1 = cast<NonLValue>(V1);
1419 const NonLValue& R2 = cast<NonLValue>(V2);
1420 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1421 }
1422
1423 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001424 break;
1425 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001426 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001427 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001428}
Ted Kremenekee985462008-01-16 18:18:48 +00001429
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001430
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001431void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1432 GRConstants::NodeSet& Dst) {
1433
1434 // FIXME: add metadata to the CFG so that we can disable
1435 // this check when we KNOW that there is no block-level subexpression.
1436 // The motivation is that this check requires a hashtable lookup.
1437
1438 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1439 Dst.Add(Pred);
1440 return;
1441 }
1442
1443 switch (S->getStmtClass()) {
1444 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001445 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001446 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1447 break;
1448
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001449 case Stmt::UnaryOperatorClass:
1450 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1451 break;
1452
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001453 case Stmt::ParenExprClass:
1454 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1455 break;
1456
Ted Kremenek874d63f2008-01-24 02:02:54 +00001457 case Stmt::ImplicitCastExprClass: {
1458 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1459 VisitCast(C, C->getSubExpr(), Pred, Dst);
1460 break;
1461 }
1462
1463 case Stmt::CastExprClass: {
1464 CastExpr* C = cast<CastExpr>(S);
1465 VisitCast(C, C->getSubExpr(), Pred, Dst);
1466 break;
1467 }
1468
Ted Kremenek9de04c42008-01-24 20:55:43 +00001469 case Stmt::DeclStmtClass:
1470 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1471 break;
1472
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001473 default:
1474 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1475 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001476 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001477}
1478
Ted Kremenekee985462008-01-16 18:18:48 +00001479//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001480// "Assume" logic.
1481//===----------------------------------------------------------------------===//
1482
1483StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
1484 bool& isFeasible) {
1485 return St;
1486}
1487
1488StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
1489 bool& isFeasible) {
1490
1491 switch (Cond.getSubKind()) {
1492 default:
1493 assert (false && "'Assume' not implemented for this NonLValue.");
1494 return St;
1495
1496 case ConcreteIntKind: {
1497 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
1498 isFeasible = b ? Assumption : !Assumption;
1499 return St;
1500 }
1501 }
1502}
1503
1504
1505//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001506// Driver.
1507//===----------------------------------------------------------------------===//
1508
Ted Kremenekaa66a322008-01-16 21:46:15 +00001509#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001510static GRConstants* GraphPrintCheckerState;
1511
Ted Kremenekaa66a322008-01-16 21:46:15 +00001512namespace llvm {
1513template<>
1514struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1515 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001516
1517 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001518 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001519 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001520 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1521 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1522 default: assert (false && "Unknown ValueKey type.");
1523 }
1524 }
1525
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001526 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1527 ValueKey::Kind kind, bool isFirstGroup = false) {
1528 bool isFirst = true;
1529
1530 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1531 if (I.getKey().getKind() != kind)
1532 continue;
1533
1534 if (isFirst) {
1535 if (!isFirstGroup) Out << "\\l\\l";
1536 PrintKindLabel(Out, kind);
1537 isFirst = false;
1538 }
1539 else
1540 Out << "\\l";
1541
1542 Out << ' ';
1543
1544 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1545 Out << V->getName();
1546 else {
1547 Stmt* E = cast<Stmt>(I.getKey());
1548 Out << " (" << (void*) E << ") ";
1549 E->printPretty(Out);
1550 }
1551
1552 Out << " : ";
1553 I.getData().print(Out);
1554 }
1555 }
1556
Ted Kremenekaa66a322008-01-16 21:46:15 +00001557 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1558 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001559
1560 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001561 ProgramPoint Loc = N->getLocation();
1562
1563 switch (Loc.getKind()) {
1564 case ProgramPoint::BlockEntranceKind:
1565 Out << "Block Entrance: B"
1566 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1567 break;
1568
1569 case ProgramPoint::BlockExitKind:
1570 assert (false);
1571 break;
1572
1573 case ProgramPoint::PostStmtKind: {
1574 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001575 Out << L.getStmt()->getStmtClassName() << ':'
1576 << (void*) L.getStmt() << ' ';
1577
Ted Kremenekaa66a322008-01-16 21:46:15 +00001578 L.getStmt()->printPretty(Out);
1579 break;
1580 }
1581
1582 default: {
1583 const BlockEdge& E = cast<BlockEdge>(Loc);
1584 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1585 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001586
1587 if (Stmt* T = E.getSrc()->getTerminator()) {
1588 Out << "\\|Terminator: ";
1589 E.getSrc()->printTerminator(Out);
1590
1591 if (isa<SwitchStmt>(T)) {
1592 // FIXME
1593 }
1594 else {
1595 Out << "\\lCondition: ";
1596 if (*E.getSrc()->succ_begin() == E.getDst())
1597 Out << "true";
1598 else
1599 Out << "false";
1600 }
1601
1602 Out << "\\l";
1603 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001604
1605 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1606 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1607 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001608 }
1609 }
1610
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001611 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001612
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001613 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1614 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001615 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001616
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001617 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001618 return Out.str();
1619 }
1620};
1621} // end llvm namespace
1622#endif
1623
Ted Kremenekee985462008-01-16 18:18:48 +00001624namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001625void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1626 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001627 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001628#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001629 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001630 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001631 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001632#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001633}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001634} // end clang namespace