blob: 36ad5fe20a80858e0b0181efc49923b82b72e82a [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
2//
Ted Kremenekab2b8c52008-01-23 19:59:44 +00003// The LLValM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Constant Propagation via Graph Reachability
11//
12// This files defines a simple analysis that performs path-sensitive
13// constant propagation within a function. An example use of this analysis
14// is to perform simple checks for NULL dereferences.
15//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Analysis/PathSensitive/GREngine.h"
19#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000020#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000022
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/DataTypes.h"
25#include "llvm/ADT/APSInt.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000028#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000029#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000030#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000031#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000032#include "llvm/Support/Streams.h"
33
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000034#include <functional>
35
Ted Kremenekaa66a322008-01-16 21:46:15 +000036#ifndef NDEBUG
37#include "llvm/Support/GraphWriter.h"
38#include <sstream>
39#endif
40
Ted Kremenekd27f8162008-01-15 23:55:06 +000041using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000042using llvm::dyn_cast;
43using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000044using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000045
46//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +000047/// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a
Ted Kremenekd27f8162008-01-15 23:55:06 +000048/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
49//===----------------------------------------------------------------------===//
50namespace {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000051
Ted Kremenek68fd2572008-01-29 17:27:31 +000052class SymbolID {
53 unsigned Data;
54public:
55 SymbolID() : Data(~0) {}
56 SymbolID(unsigned x) : Data(x) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +000057
Ted Kremenek68fd2572008-01-29 17:27:31 +000058 bool isInitialized() const { return Data != (unsigned) ~0; }
59 operator unsigned() const { assert (isInitialized()); return Data; }
60};
61
Ted Kremenekab2b8c52008-01-23 19:59:44 +000062class VISIBILITY_HIDDEN ValueKey {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000063 uintptr_t Raw;
Ted Kremenekcc1c3652008-01-25 23:43:12 +000064 void operator=(const ValueKey& RHS); // Do not implement.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000065
Ted Kremenekd27f8162008-01-15 23:55:06 +000066public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000067 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
68 IsSymbol=0x3, // Symbol Bindings.
69 Flags=0x3 };
70
71 inline Kind getKind() const {
72 return (Kind) (Raw & Flags);
73 }
74
75 inline void* getPtr() const {
76 assert (getKind() != IsSymbol);
77 return reinterpret_cast<void*>(Raw & ~Flags);
78 }
79
80 inline SymbolID getSymbolID() const {
81 assert (getKind() == IsSymbol);
Ted Kremenek68fd2572008-01-29 17:27:31 +000082 return Raw >> 2;
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000083 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000084
Ted Kremenekab2b8c52008-01-23 19:59:44 +000085 ValueKey(const ValueDecl* VD)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000086 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
87 assert(VD && "ValueDecl cannot be NULL.");
88 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000089
Ted Kremenek5c1b9962008-01-24 19:43:37 +000090 ValueKey(Stmt* S, bool isBlkExpr = false)
Ted Kremenekcc1c3652008-01-25 23:43:12 +000091 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000092 assert(S && "Tracked statement cannot be NULL.");
Ted Kremenekcc1c3652008-01-25 23:43:12 +000093 }
Ted Kremenekd27f8162008-01-15 23:55:06 +000094
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000095 ValueKey(SymbolID V)
96 : Raw((V << 2) | IsSymbol) {}
97
98 bool isSymbol() const { return getKind() == IsSymbol; }
Ted Kremenek565256e2008-01-24 22:44:24 +000099 bool isSubExpr() const { return getKind() == IsSubExpr; }
Ted Kremenek65cac132008-01-29 05:25:31 +0000100 bool isBlkExpr() const { return getKind() == IsBlkExpr; }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000101 bool isDecl() const { return getKind() == IsDecl; }
Ted Kremenek65cac132008-01-29 05:25:31 +0000102 bool isStmt() const { return getKind() <= IsBlkExpr; }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000103
104 inline void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000105 ID.AddInteger(isSymbol() ? 1 : 0);
106
107 if (isSymbol())
Ted Kremenekf2645622008-01-28 22:25:21 +0000108 ID.AddInteger(getSymbolID());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000109 else
110 ID.AddPointer(getPtr());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000111 }
112
113 inline bool operator==(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000114 return isSymbol() ? getSymbolID() == X.getSymbolID()
115 : getPtr() == X.getPtr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000116 }
117
118 inline bool operator!=(const ValueKey& X) const {
119 return !operator==(X);
120 }
121
122 inline bool operator<(const ValueKey& X) const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000123 if (isSymbol())
124 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
Ted Kremenek5c1b9962008-01-24 19:43:37 +0000125
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000126 return getPtr() < X.getPtr();
Ted Kremenekb3d2dca2008-01-16 23:33:44 +0000127 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000128};
129} // end anonymous namespace
130
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000131// Machinery to get cast<> and dyn_cast<> working with ValueKey.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000132namespace llvm {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000133 template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) {
134 return V.getKind() == ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000135 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000136 template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000137 return ((unsigned) V.getKind()) < ValueKey::IsDecl;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000139 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> {
Ted Kremenekaa66a322008-01-16 21:46:15 +0000140 typedef const ValueDecl* ret_type;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000141 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000142 template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000143 typedef const Stmt* ret_type;
144 };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000145 template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146 typedef void* SimpleType;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000147 static inline SimpleType getSimplifiedValue(const ValueKey &V) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000148 return V.getPtr();
149 }
150 };
151} // end llvm namespace
152
Ted Kremenek68fd2572008-01-29 17:27:31 +0000153
154//===----------------------------------------------------------------------===//
155// SymbolManager.
156//===----------------------------------------------------------------------===//
157
158namespace {
159class VISIBILITY_HIDDEN SymbolData {
160 uintptr_t Data;
161public:
162 enum Kind { ParmKind = 0x0, Mask = 0x3 };
163
164 SymbolData(ParmVarDecl* D)
165 : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
166
167 inline Kind getKind() const { return (Kind) (Data & Mask); }
168 inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
169 inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
170};
171}
172
173// Machinery to get cast<> and dyn_cast<> working with SymbolData.
174namespace llvm {
175 template<> inline bool isa<ParmVarDecl,SymbolData>(const SymbolData& V) {
176 return V.getKind() == SymbolData::ParmKind;
177 }
178 template<> struct VISIBILITY_HIDDEN cast_retty_impl<ParmVarDecl,SymbolData> {
179 typedef const ParmVarDecl* ret_type;
180 };
181 template<> struct VISIBILITY_HIDDEN simplify_type<SymbolData> {
182 typedef void* SimpleType;
183 static inline SimpleType getSimplifiedValue(const SymbolData &V) {
184 return V.getPtr();
185 }
186 };
187} // end llvm namespace
188
189namespace {
190class VISIBILITY_HIDDEN SymbolManager {
191 std::vector<SymbolData> SymbolToData;
192
193 typedef llvm::DenseMap<void*,SymbolID> MapTy;
194 MapTy DataToSymbol;
195
196public:
197 SymbolData getSymbolData(SymbolID id) const {
198 assert (id < SymbolToData.size());
199 return SymbolToData[id];
200 }
201
202 SymbolID getSymbol(ParmVarDecl* D);
203};
204} // end anonymous namespace
205
206SymbolID SymbolManager::getSymbol(ParmVarDecl* D) {
207 SymbolID& X = DataToSymbol[D];
208
209 if (!X.isInitialized()) {
210 X = SymbolToData.size();
211 SymbolToData.push_back(D);
212 }
213
214 return X;
215}
216
Ted Kremenekd27f8162008-01-15 23:55:06 +0000217//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000218// ValueManager.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000219//===----------------------------------------------------------------------===//
220
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000221namespace {
222
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000223typedef llvm::ImmutableSet<APSInt > APSIntSetTy;
224
225
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000226class VISIBILITY_HIDDEN ValueManager {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000227 ASTContext& Ctx;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000228
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000229 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy;
230 APSIntSetTy APSIntSet;
231
232 llvm::BumpPtrAllocator BPAlloc;
233
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000234public:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000235 ValueManager(ASTContext& ctx) : Ctx(ctx) {}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000236 ~ValueManager();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000237
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000238 ASTContext& getContext() const { return Ctx; }
Ted Kremenek687af802008-01-29 19:43:15 +0000239 APSInt& getValue(const APSInt& X);
240 APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
241 APSInt& getValue(uint64_t X, QualType T,
242 SourceLocation Loc = SourceLocation());
Ted Kremenekd27f8162008-01-15 23:55:06 +0000243};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000244} // end anonymous namespace
245
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000246ValueManager::~ValueManager() {
247 // Note that the dstor for the contents of APSIntSet will never be called,
248 // so we iterate over the set and invoke the dstor for each APSInt. This
249 // frees an aux. memory allocated to represent very large constants.
250 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
251 I->getValue().~APSInt();
252}
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000253
254APSInt& ValueManager::getValue(const APSInt& X) {
255 llvm::FoldingSetNodeID ID;
256 void* InsertPos;
257 typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy;
Ted Kremenekcc1c3652008-01-25 23:43:12 +0000258
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000259 X.Profile(ID);
260 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000261
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000262 if (!P) {
263 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
264 new (P) FoldNodeTy(X);
265 APSIntSet.InsertNode(P, InsertPos);
266 }
267
268 return *P;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +0000269}
270
Ted Kremenek687af802008-01-29 19:43:15 +0000271APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) {
272 APSInt V(BitWidth, isUnsigned);
273 V = X;
274 return getValue(V);
275}
276
277APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) {
278 unsigned bits = Ctx.getTypeSize(T, Loc);
279 APSInt V(bits, T->isUnsignedIntegerType());
280 V = X;
281 return getValue(V);
282}
283
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000284//===----------------------------------------------------------------------===//
285// Expression Values.
286//===----------------------------------------------------------------------===//
Ted Kremenekf13794e2008-01-24 23:19:54 +0000287
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000288namespace {
289
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000290class VISIBILITY_HIDDEN RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000291public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000292 enum BaseKind { LValueKind=0x0, NonLValueKind=0x1,
Ted Kremenek6753fe32008-01-30 18:54:06 +0000293 UninitializedKind=0x2, InvalidKind=0x3 };
294
295 enum { BaseBits = 2, BaseMask = 0x3 };
Ted Kremenekf13794e2008-01-24 23:19:54 +0000296
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000297private:
Ted Kremenekf2645622008-01-28 22:25:21 +0000298 void* Data;
Ted Kremenekf13794e2008-01-24 23:19:54 +0000299 unsigned Kind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000300
301protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000302 RValue(const void* d, bool isLValue, unsigned ValKind)
Ted Kremenekf2645622008-01-28 22:25:21 +0000303 : Data(const_cast<void*>(d)),
Ted Kremenek6753fe32008-01-30 18:54:06 +0000304 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000305
Ted Kremenek403c1812008-01-28 22:51:57 +0000306 explicit RValue(BaseKind k)
307 : Data(0), Kind(k) {}
Ted Kremenekf2645622008-01-28 22:25:21 +0000308
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000309 void* getRawPtr() const {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000310 return reinterpret_cast<void*>(Data);
311 }
312
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000313public:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000314 ~RValue() {};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000315
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000316 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
Ted Kremenek874d63f2008-01-24 02:02:54 +0000317
Ted Kremenekf13794e2008-01-24 23:19:54 +0000318 unsigned getRawKind() const { return Kind; }
Ted Kremenek6753fe32008-01-30 18:54:06 +0000319 BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
320 unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
Ted Kremenekf13794e2008-01-24 23:19:54 +0000321
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000322 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000323 ID.AddInteger((unsigned) getRawKind());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000324 ID.AddPointer(reinterpret_cast<void*>(Data));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000325 }
326
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000327 bool operator==(const RValue& RHS) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000328 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000329 }
330
Ted Kremenekf13794e2008-01-24 23:19:54 +0000331 inline bool isValid() const { return getRawKind() != InvalidKind; }
332 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
333
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000334 void print(std::ostream& OS) const;
335 void print() const { print(*llvm::cerr.stream()); }
336
337 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000338 static inline bool classof(const RValue*) { return true; }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000339};
340
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000341class VISIBILITY_HIDDEN InvalidValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000342public:
Ted Kremenek403c1812008-01-28 22:51:57 +0000343 InvalidValue() : RValue(InvalidKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000344
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000345 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000346 return V->getBaseKind() == InvalidKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000347 }
348};
Ted Kremenek403c1812008-01-28 22:51:57 +0000349
350class VISIBILITY_HIDDEN UninitializedValue : public RValue {
351public:
352 UninitializedValue() : RValue(UninitializedKind) {}
353
354 static inline bool classof(const RValue* V) {
355 return V->getBaseKind() == UninitializedKind;
356 }
357};
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000358
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000359class VISIBILITY_HIDDEN NonLValue : public RValue {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000360protected:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000361 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000362
363public:
Ted Kremenekf13794e2008-01-24 23:19:54 +0000364 void print(std::ostream& Out) const;
365
Ted Kremenek687af802008-01-29 19:43:15 +0000366 // Arithmetic operators.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000367 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
368 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
369 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
370 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
371 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
372 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000373
Ted Kremenek687af802008-01-29 19:43:15 +0000374 // Equality operators.
375 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
376 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000377
Ted Kremenek687af802008-01-29 19:43:15 +0000378 // Utility methods to create NonLValues.
379 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
380 SourceLocation Loc = SourceLocation());
381
382 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
Ted Kremenek68fd2572008-01-29 17:27:31 +0000383 static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000384
Ted Kremenek687af802008-01-29 19:43:15 +0000385 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
386 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
387 }
388
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000389 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000390 static inline bool classof(const RValue* V) {
Ted Kremenek403c1812008-01-28 22:51:57 +0000391 return V->getBaseKind() >= NonLValueKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000392 }
393};
Ted Kremenek687af802008-01-29 19:43:15 +0000394
395class VISIBILITY_HIDDEN LValue : public RValue {
396protected:
397 LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {}
398
399public:
400
401 // Equality operators.
402 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
403 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
404
405 // Implement isa<T> support.
406 static inline bool classof(const RValue* V) {
407 return V->getBaseKind() == LValueKind;
408 }
409};
Ted Kremenekf13794e2008-01-24 23:19:54 +0000410
411} // end anonymous namespace
412
413//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000414// LValues.
Ted Kremenekf13794e2008-01-24 23:19:54 +0000415//===----------------------------------------------------------------------===//
416
417namespace {
418
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000419enum { LValueDeclKind, NumLValueKind };
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000420
421class VISIBILITY_HIDDEN LValueDecl : public LValue {
422public:
Ted Kremenek9de04c42008-01-24 20:55:43 +0000423 LValueDecl(const ValueDecl* vd)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000424 : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {}
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000425
426 ValueDecl* getDecl() const {
427 return static_cast<ValueDecl*>(getRawPtr());
428 }
429
Ted Kremenek687af802008-01-29 19:43:15 +0000430 inline bool operator==(const LValueDecl& R) const {
431 return getDecl() == R.getDecl();
432 }
433
434 inline bool operator!=(const LValueDecl& R) const {
435 return getDecl() != R.getDecl();
436 }
437
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000438 // Implement isa<T> support.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000439 static inline bool classof(const RValue* V) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000440 return V->getSubKind() == LValueDeclKind;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000441 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000442};
443
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000444} // end anonymous namespace
445
446//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000447// Non-LValues.
448//===----------------------------------------------------------------------===//
449
450namespace {
451
Ted Kremenekf2645622008-01-28 22:25:21 +0000452enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind,
453 NumNonLValueKind };
454
455class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue {
456public:
457 SymbolicNonLValue(unsigned SymID)
458 : NonLValue(SymbolicNonLValueKind,
459 reinterpret_cast<void*>((uintptr_t) SymID)) {}
460
461 SymbolID getSymbolID() const {
462 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
463 }
464
465 static inline bool classof(const RValue* V) {
466 return V->getSubKind() == SymbolicNonLValueKind;
467 }
468};
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000469
470class VISIBILITY_HIDDEN ConcreteInt : public NonLValue {
471public:
472 ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
473
474 const APSInt& getValue() const {
475 return *static_cast<APSInt*>(getRawPtr());
476 }
477
Ted Kremenek687af802008-01-29 19:43:15 +0000478 // Arithmetic operators.
479
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000480 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
481 return ValMgr.getValue(getValue() + V.getValue());
482 }
Ted Kremenek687af802008-01-29 19:43:15 +0000483
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000484 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
485 return ValMgr.getValue(getValue() - V.getValue());
486 }
487
488 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
489 return ValMgr.getValue(getValue() * V.getValue());
490 }
491
492 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
493 return ValMgr.getValue(getValue() / V.getValue());
494 }
495
496 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
497 return ValMgr.getValue(getValue() % V.getValue());
498 }
499
Ted Kremenek687af802008-01-29 19:43:15 +0000500 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
501 assert (U->getType() == U->getSubExpr()->getType());
502 assert (U->getType()->isIntegerType());
503 return ValMgr.getValue(-getValue());
504 }
505
506 // Casting.
507
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000508 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
509 assert (CastExpr->getType()->isIntegerType());
510
511 APSInt X(getValue());
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000512 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
513 CastExpr->getLocStart()));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000514 return ValMgr.getValue(X);
515 }
516
Ted Kremenek687af802008-01-29 19:43:15 +0000517 // Equality operators.
518
519 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
520 const APSInt& Val = getValue();
521 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
522 Val.getBitWidth(), Val.isUnsigned());
523 }
524
525 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
526 const APSInt& Val = getValue();
527 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
528 Val.getBitWidth(), Val.isUnsigned());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000529 }
530
531 // Implement isa<T> support.
532 static inline bool classof(const RValue* V) {
533 return V->getSubKind() == ConcreteIntKind;
534 }
535};
536
537} // end anonymous namespace
538
539//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000540// Transfer function dispatch for Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000541//===----------------------------------------------------------------------===//
542
543RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const {
544 switch (getSubKind()) {
545 case ConcreteIntKind:
546 return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr);
547 default:
548 return InvalidValue();
549 }
550}
551
552NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
553 switch (getSubKind()) {
554 case ConcreteIntKind:
555 return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U);
556 default:
557 return cast<NonLValue>(InvalidValue());
558 }
559}
560
Ted Kremenek687af802008-01-29 19:43:15 +0000561#define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000562case (k1##Kind*NumNonLValueKind+k2##Kind):\
563 return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS));
564
Ted Kremenek687af802008-01-29 19:43:15 +0000565#define NONLVALUE_DISPATCH(Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000566switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\
Ted Kremenek687af802008-01-29 19:43:15 +0000567 NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000568 default:\
Ted Kremenek403c1812008-01-28 22:51:57 +0000569 if (getBaseKind() == UninitializedKind ||\
570 RHS.getBaseKind() == UninitializedKind)\
571 return cast<NonLValue>(UninitializedValue());\
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000572 assert (!isValid() || !RHS.isValid() && "Missing case.");\
573 break;\
574}\
575return cast<NonLValue>(InvalidValue());
576
577NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000578 NONLVALUE_DISPATCH(Add)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000579}
580
581NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000582 NONLVALUE_DISPATCH(Sub)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000583}
584
585NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000586 NONLVALUE_DISPATCH(Mul)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000587}
588
589NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000590 NONLVALUE_DISPATCH(Div)
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000591}
592
593NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const {
Ted Kremenek687af802008-01-29 19:43:15 +0000594 NONLVALUE_DISPATCH(Rem)
595}
596
597NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const {
598 NONLVALUE_DISPATCH(EQ)
599}
600
601NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const {
602 NONLVALUE_DISPATCH(NE)
603}
604
605#undef NONLVALUE_DISPATCH_CASE
606#undef NONLVALUE_DISPATCH
607
608//===----------------------------------------------------------------------===//
609// Transfer function dispatch for LValues.
610//===----------------------------------------------------------------------===//
611
612
613NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
614 if (getSubKind() != RHS.getSubKind())
615 return NonLValue::GetIntTruthValue(ValMgr, false);
616
617 switch (getSubKind()) {
618 default:
619 assert(false && "EQ not implemented for this LValue.");
620 return cast<NonLValue>(InvalidValue());
621
622 case LValueDeclKind: {
623 bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS);
624 return NonLValue::GetIntTruthValue(ValMgr, b);
625 }
626 }
627}
628
629NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
630 if (getSubKind() != RHS.getSubKind())
Ted Kremenek03701672008-01-29 21:27:49 +0000631 return NonLValue::GetIntTruthValue(ValMgr, true);
Ted Kremenek687af802008-01-29 19:43:15 +0000632
633 switch (getSubKind()) {
634 default:
635 assert(false && "EQ not implemented for this LValue.");
636 return cast<NonLValue>(InvalidValue());
637
638 case LValueDeclKind: {
639 bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS);
640 return NonLValue::GetIntTruthValue(ValMgr, b);
641 }
642 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000643}
644
645
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000646//===----------------------------------------------------------------------===//
Ted Kremenek687af802008-01-29 19:43:15 +0000647// Utility methods for constructing Non-LValues.
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000648//===----------------------------------------------------------------------===//
649
Ted Kremenek687af802008-01-29 19:43:15 +0000650NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
651 SourceLocation Loc) {
652
653 return ConcreteInt(ValMgr.getValue(X, T, Loc));
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000654}
655
656NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
657 return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
658 I->getType()->isUnsignedIntegerType())));
659}
660
Ted Kremenek68fd2572008-01-29 17:27:31 +0000661NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
662 return SymbolicNonLValue(SymMgr.getSymbol(D));
663}
664
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000665//===----------------------------------------------------------------------===//
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000666// Pretty-Printing.
667//===----------------------------------------------------------------------===//
668
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000669void RValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000670 switch (getBaseKind()) {
671 case InvalidKind:
672 Out << "Invalid";
673 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000674
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000675 case NonLValueKind:
676 cast<NonLValue>(this)->print(Out);
Ted Kremenekf13794e2008-01-24 23:19:54 +0000677 break;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000678
Ted Kremenekf13794e2008-01-24 23:19:54 +0000679 case LValueKind:
680 assert (false && "FIXME: LValue printing not implemented.");
681 break;
682
Ted Kremenek403c1812008-01-28 22:51:57 +0000683 case UninitializedKind:
684 Out << "Uninitialized";
685 break;
686
Ted Kremenekf13794e2008-01-24 23:19:54 +0000687 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000688 assert (false && "Invalid RValue.");
Ted Kremenekf13794e2008-01-24 23:19:54 +0000689 }
690}
691
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000692void NonLValue::print(std::ostream& Out) const {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000693 switch (getSubKind()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000694 case ConcreteIntKind:
695 Out << cast<ConcreteInt>(this)->getValue().toString();
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000696 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000697
Ted Kremenek68fd2572008-01-29 17:27:31 +0000698 case SymbolicNonLValueKind:
Ted Kremenek6753fe32008-01-30 18:54:06 +0000699 Out << '$' << cast<SymbolicNonLValue>(this)->getSymbolID();
Ted Kremenek68fd2572008-01-29 17:27:31 +0000700 break;
701
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000702 default:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000703 assert (false && "Pretty-printed not implemented for this NonLValue.");
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000704 break;
705 }
706}
707
708//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709// ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000710//===----------------------------------------------------------------------===//
711
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000712typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000713
714namespace clang {
715 template<>
716 struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> {
717 static inline void* toPtr(ValueMapTy M) {
718 return reinterpret_cast<void*>(M.getRoot());
719 }
720 static inline ValueMapTy toState(void* P) {
721 return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P));
722 }
723 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000724}
725
Ted Kremenekb38911f2008-01-30 23:03:39 +0000726typedef ValueMapTy StateTy;
727
Ted Kremenekd27f8162008-01-15 23:55:06 +0000728//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000729// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000730//
731// FIXME: This checker logic should be eventually broken into two components.
732// The first is the "meta"-level checking logic; the code that
733// does the Stmt visitation, fetching values from the map, etc.
734// The second part does the actual state manipulation. This way we
735// get more of a separate of concerns of these two pieces, with the
736// latter potentially being refactored back into the main checking
737// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000738//===----------------------------------------------------------------------===//
739
740namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000741
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000742class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000743
744public:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000745 typedef ValueMapTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000746 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
747 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000748 typedef ExplodedGraph<GRConstants> GraphTy;
749 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000750
751 class NodeSet {
752 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
753 ImplTy Impl;
754 public:
755
756 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000757 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000758
Ted Kremenekb38911f2008-01-30 23:03:39 +0000759 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000760
761 typedef ImplTy::iterator iterator;
762 typedef ImplTy::const_iterator const_iterator;
763
764 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000765 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000766
767 iterator begin() { return Impl.begin(); }
768 iterator end() { return Impl.end(); }
769
770 const_iterator begin() const { return Impl.begin(); }
771 const_iterator end() const { return Impl.end(); }
772 };
Ted Kremenekd27f8162008-01-15 23:55:06 +0000773
774protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000775 /// G - the simulation graph.
776 GraphTy& G;
777
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000778 /// Liveness - live-variables information the ValueDecl* and block-level
779 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000780 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000781
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000782 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000783 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000784 StmtNodeBuilder* Builder;
785
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000786 /// StateMgr - Object that manages the data for all created states.
787 ValueMapTy::Factory StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000788
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000789 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000790 ValueManager ValMgr;
791
Ted Kremenek68fd2572008-01-29 17:27:31 +0000792 /// SymMgr - Object that manages the symbol information.
793 SymbolManager SymMgr;
794
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000795 /// StmtEntryNode - The immediate predecessor node.
796 NodeTy* StmtEntryNode;
797
798 /// CurrentStmt - The current block-level statement.
799 Stmt* CurrentStmt;
800
Ted Kremenekb38911f2008-01-30 23:03:39 +0000801 /// UninitBranches - Nodes in the ExplodedGraph that result from
802 /// taking a branch based on an uninitialized value.
803 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
804 UninitBranchesTy UninitBranches;
805
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000806 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000807
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000808 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000809
Ted Kremenekd27f8162008-01-15 23:55:06 +0000810public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000811 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
812 Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL),
813 CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000814
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000815 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000816 Liveness.runOnCFG(G.getCFG());
817 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000818 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000819
820 /// getCFG - Returns the CFG associated with this analysis.
821 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000822
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000823 /// getInitialState - Return the initial state used for the root vertex
824 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825 StateTy getInitialState() {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000826 StateTy St = StateMgr.GetEmptyMap();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000827
828 // Iterate the parameters.
829 FunctionDecl& F = G.getFunctionDecl();
830
831 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
832 I!=E; ++I) {
833
834 // For now we only support symbolic values for non-pointer types.
835 if ((*I)->getType()->isPointerType() ||
836 (*I)->getType()->isReferenceType())
837 continue;
838
839 // FIXME: Set these values to a symbol, not Uninitialized.
Ted Kremenek68fd2572008-01-29 17:27:31 +0000840 St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000841 }
842
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000843 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000844 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000845
846 bool isUninitControlFlow(const NodeTy* N) const {
847 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
848 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000849
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000850 /// ProcessStmt - Called by GREngine. Used to generate new successor
851 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000852 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
853
854 /// ProcessBranch - Called by GREngine. Used to generate successor
855 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000856 void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000857
858 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
859 /// that all subexpression mappings are removed and that any
860 /// block-level expressions that are not live at 'S' also have their
861 /// mappings removed.
862 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
863
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000864 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000865
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000866 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000867 return SetValue(St, const_cast<Stmt*>(S), V);
868 }
869
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000870 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000871
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000872 RValue GetValue(const StateTy& St, Stmt* S);
873 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000874 return GetValue(St, const_cast<Stmt*>(S));
875 }
876
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000877 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000878 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000879
880 /// Assume - Create new state by assuming that a given expression
881 /// is true or false.
882 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
883 bool& isFeasible) {
884 if (isa<LValue>(Cond))
885 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
886 else
887 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
888 }
889
890 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
891 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000892
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000893 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000894
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000895 /// Visit - Transfer function logic for all statements. Dispatches to
896 /// other functions that handle specific kinds of statements.
897 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000898
899 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
900 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000901
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000902 /// VisitUnaryOperator - Transfer function logic for unary operators.
903 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
904
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000905 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000906 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
907
908 /// VisitDeclStmt - Transfer function logic for DeclStmts.
909 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000910};
911} // end anonymous namespace
912
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000913
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000914void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term,
915 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000916
917 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000918
Ted Kremenekb38911f2008-01-30 23:03:39 +0000919 // Remove old bindings for subexpressions.
920 for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
921 if (I.getKey().isSubExpr())
922 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000923
Ted Kremenekb38911f2008-01-30 23:03:39 +0000924 RValue V = GetValue(PrevState, Condition);
925
926 switch (V.getBaseKind()) {
927 default:
928 break;
929
930 case RValue::InvalidKind:
931 builder.generateNode(PrevState, true);
932 builder.generateNode(PrevState, false);
933 return;
934
935 case RValue::UninitializedKind: {
936 NodeTy* N = builder.generateNode(PrevState, true);
937
938 if (N) {
939 N->markAsSink();
940 UninitBranches.insert(N);
941 }
942
943 builder.markInfeasible(false);
944 return;
945 }
946 }
947
948 // Process the true branch.
949 bool isFeasible = true;
950 StateTy St = Assume(PrevState, V, true, isFeasible);
951
952 if (isFeasible) builder.generateNode(St, true);
953 else {
954 builder.markInfeasible(true);
955 isFeasible = true;
956 }
957
958 // Process the false branch.
959 St = Assume(PrevState, V, false, isFeasible);
960
961 if (isFeasible) builder.generateNode(St, false);
962 else builder.markInfeasible(false);
963
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000964}
965
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000966void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000967 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000968
969 StmtEntryNode = builder.getLastNode();
970 CurrentStmt = S;
971 NodeSet Dst;
972 StateCleaned = false;
973
974 Visit(S, StmtEntryNode, Dst);
975
976 // If no nodes were generated, generate a new node that has all the
977 // dead mappings removed.
978 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
979 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
980 builder.generateNode(S, St, StmtEntryNode);
981 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000982
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000983 CurrentStmt = NULL;
984 StmtEntryNode = NULL;
985 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000986}
987
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000988
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000989RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) {
Ted Kremenekf13794e2008-01-24 23:19:54 +0000990 switch (LV.getSubKind()) {
991 case LValueDeclKind: {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000992 StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl());
993 return T ? T->getValue().second : InvalidValue();
994 }
995 default:
996 assert (false && "Invalid LValue.");
Ted Kremenekca3e8572008-01-16 22:28:08 +0000997 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000998 }
999
1000 return InvalidValue();
1001}
1002
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001003RValue GRConstants::GetValue(const StateTy& St, Stmt* S) {
Ted Kremenek671c9e82008-01-24 00:50:08 +00001004 for (;;) {
1005 switch (S->getStmtClass()) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001006
1007 // ParenExprs are no-ops.
1008
Ted Kremenek671c9e82008-01-24 00:50:08 +00001009 case Stmt::ParenExprClass:
1010 S = cast<ParenExpr>(S)->getSubExpr();
1011 continue;
1012
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001013 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
1014 // (assuming an implicit "load") depending on the context. In this
1015 // context we assume that we are retrieving the value contained
1016 // within the referenced variables.
1017
Ted Kremenek671c9e82008-01-24 00:50:08 +00001018 case Stmt::DeclRefExprClass:
1019 return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekca3e8572008-01-16 22:28:08 +00001020
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001021 // Integer literals evaluate to an RValue. Simply retrieve the
1022 // RValue for the literal.
1023
Ted Kremenek671c9e82008-01-24 00:50:08 +00001024 case Stmt::IntegerLiteralClass:
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001025 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
1026
1027 // Casts where the source and target type are the same
1028 // are no-ops. We blast through these to get the descendant
1029 // subexpression that has a value.
1030
Ted Kremenek874d63f2008-01-24 02:02:54 +00001031 case Stmt::ImplicitCastExprClass: {
1032 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1033 if (C->getType() == C->getSubExpr()->getType()) {
1034 S = C->getSubExpr();
1035 continue;
1036 }
1037 break;
1038 }
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001039
Ted Kremenek874d63f2008-01-24 02:02:54 +00001040 case Stmt::CastExprClass: {
1041 CastExpr* C = cast<CastExpr>(S);
1042 if (C->getType() == C->getSubExpr()->getType()) {
1043 S = C->getSubExpr();
1044 continue;
1045 }
1046 break;
1047 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001048
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001049 // Handle all other Stmt* using a lookup.
1050
Ted Kremenek671c9e82008-01-24 00:50:08 +00001051 default:
1052 break;
1053 };
1054
1055 break;
1056 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001057
Ted Kremenek5c1b9962008-01-24 19:43:37 +00001058 StateTy::TreeTy* T = St.SlimFind(S);
Ted Kremenek874d63f2008-01-24 02:02:54 +00001059
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001060 return T ? T->getValue().second : InvalidValue();
1061}
1062
1063LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001064 while (ParenExpr* P = dyn_cast<ParenExpr>(S))
1065 S = P->getSubExpr();
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001066
1067 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
1068 return LValueDecl(DR->getDecl());
1069
1070 return cast<LValue>(GetValue(St, S));
1071}
1072
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001073
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001074GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001075 const RValue& V) {
Ted Kremenekcc1c3652008-01-25 23:43:12 +00001076 assert (S);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001077
1078 if (!StateCleaned) {
1079 St = RemoveDeadBindings(CurrentStmt, St);
1080 StateCleaned = true;
1081 }
1082
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001083 bool isBlkExpr = false;
1084
1085 if (S == CurrentStmt) {
1086 isBlkExpr = getCFG().isBlkExpr(S);
1087
1088 if (!isBlkExpr)
1089 return St;
1090 }
Ted Kremenekdaadf452008-01-24 19:28:01 +00001091
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001092 return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V)
1093 : St;
1094}
1095
1096GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV,
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001097 const RValue& V) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001098 if (!LV.isValid())
1099 return St;
1100
1101 if (!StateCleaned) {
1102 St = RemoveDeadBindings(CurrentStmt, St);
1103 StateCleaned = true;
Ted Kremenekca3e8572008-01-16 22:28:08 +00001104 }
Ted Kremenek0525a4f2008-01-16 19:47:19 +00001105
Ted Kremenekf13794e2008-01-24 23:19:54 +00001106 switch (LV.getSubKind()) {
1107 case LValueDeclKind:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001108 return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V)
1109 : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl());
1110
1111 default:
1112 assert ("SetValue for given LValue type not yet implemented.");
1113 return St;
1114 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001115}
1116
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001117GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenekf84469b2008-01-18 00:41:32 +00001118 // Note: in the code below, we can assign a new map to M since the
1119 // iterators are iterating over the tree of the *original* map.
Ted Kremenekf84469b2008-01-18 00:41:32 +00001120 StateTy::iterator I = M.begin(), E = M.end();
1121
Ted Kremenekf84469b2008-01-18 00:41:32 +00001122
Ted Kremenek65cac132008-01-29 05:25:31 +00001123 for (; I!=E && !I.getKey().isSymbol(); ++I) {
1124 // Remove old bindings for subexpressions and "dead"
1125 // block-level expressions.
1126 if (I.getKey().isSubExpr() ||
1127 I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){
1128 M = StateMgr.Remove(M, I.getKey());
1129 }
1130 else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls.
1131 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
1132 if (!Liveness.isLive(Loc, V))
1133 M = StateMgr.Remove(M, I.getKey());
1134 }
1135 }
Ted Kremenek565256e2008-01-24 22:44:24 +00001136
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001137 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001138}
1139
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001140void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred,
1141 GRConstants::StateTy St) {
1142
1143 // If the state hasn't changed, don't generate a new node.
1144 if (St == Pred->getState())
1145 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001146
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001147 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001148}
Ted Kremenekd27f8162008-01-15 23:55:06 +00001149
Ted Kremenek874d63f2008-01-24 02:02:54 +00001150void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred,
1151 GRConstants::NodeSet& Dst) {
1152
1153 QualType T = CastE->getType();
1154
1155 // Check for redundant casts.
1156 if (E->getType() == T) {
1157 Dst.Add(Pred);
1158 return;
1159 }
1160
1161 NodeSet S1;
1162 Visit(E, Pred, S1);
1163
1164 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1165 NodeTy* N = *I1;
1166 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001167 const RValue& V = GetValue(St, E);
1168 Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001169 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001170}
1171
1172void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
1173 GRConstants::NodeSet& Dst) {
1174
1175 StateTy St = Pred->getState();
1176
1177 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001178 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1179 const Expr* E = VD->getInit();
1180 St = SetValue(St, LValueDecl(VD),
1181 E ? GetValue(St, E) : UninitializedValue());
1182 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001183
1184 Nodify(Dst, DS, Pred, St);
1185
1186 if (Dst.empty())
1187 Dst.Add(Pred);
1188}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001189
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001190void GRConstants::VisitUnaryOperator(UnaryOperator* U,
1191 GRConstants::NodeTy* Pred,
1192 GRConstants::NodeSet& Dst) {
1193 NodeSet S1;
1194 Visit(U->getSubExpr(), Pred, S1);
1195
1196 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1197 NodeTy* N1 = *I1;
1198 StateTy St = N1->getState();
1199
1200 switch (U->getOpcode()) {
1201 case UnaryOperator::PostInc: {
1202 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001203 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001204 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1205 U->getLocStart());
Ted Kremeneke0cf9c82008-01-24 19:00:57 +00001206
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001207 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001208 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1209 break;
1210 }
1211
1212 case UnaryOperator::PostDec: {
1213 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001214 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001215 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1216 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001217
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001218 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001219 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
1220 break;
1221 }
1222
1223 case UnaryOperator::PreInc: {
1224 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001225 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001226 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1227 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001228
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001229 NonLValue Result = R1.Add(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001230 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1231 break;
1232 }
1233
1234 case UnaryOperator::PreDec: {
1235 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001236 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenek687af802008-01-29 19:43:15 +00001237 NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(),
1238 U->getLocStart());
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001239
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001240 NonLValue Result = R1.Sub(ValMgr, R2);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001241 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
1242 break;
1243 }
1244
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001245 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001246 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
1247 Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001248 break;
1249 }
1250
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001251 default: ;
1252 assert (false && "Not implemented.");
1253 }
1254 }
1255}
1256
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001257void GRConstants::VisitBinaryOperator(BinaryOperator* B,
1258 GRConstants::NodeTy* Pred,
1259 GRConstants::NodeSet& Dst) {
1260 NodeSet S1;
1261 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001262
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001263 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
1264 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001265
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001266 // When getting the value for the LHS, check if we are in an assignment.
1267 // In such cases, we want to (initially) treat the LHS as an LValue,
1268 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001269 // evaluated to LValueDecl's instead of to an NonLValue.
1270 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001271 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
1272 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001273
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001274 NodeSet S2;
1275 Visit(B->getRHS(), N1, S2);
1276
1277 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
1278 NodeTy* N2 = *I2;
1279 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001280 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001281
1282 switch (B->getOpcode()) {
Ted Kremenek687af802008-01-29 19:43:15 +00001283 default:
1284 Dst.Add(N2);
1285 break;
1286
1287 // Arithmetic opreators.
1288
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001289 case BinaryOperator::Add: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001290 const NonLValue& R1 = cast<NonLValue>(V1);
1291 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001292
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001293 Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001294 break;
1295 }
1296
1297 case BinaryOperator::Sub: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001298 const NonLValue& R1 = cast<NonLValue>(V1);
1299 const NonLValue& R2 = cast<NonLValue>(V2);
1300 Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001301 break;
1302 }
1303
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001304 case BinaryOperator::Mul: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001305 const NonLValue& R1 = cast<NonLValue>(V1);
1306 const NonLValue& R2 = cast<NonLValue>(V2);
1307 Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2)));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001308 break;
1309 }
1310
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001311 case BinaryOperator::Div: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001312 const NonLValue& R1 = cast<NonLValue>(V1);
1313 const NonLValue& R2 = cast<NonLValue>(V2);
1314 Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2)));
Ted Kremenek5ee4ff82008-01-25 22:55:56 +00001315 break;
1316 }
1317
Ted Kremenekcce207d2008-01-28 22:26:15 +00001318 case BinaryOperator::Rem: {
1319 const NonLValue& R1 = cast<NonLValue>(V1);
1320 const NonLValue& R2 = cast<NonLValue>(V2);
1321 Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2)));
1322 break;
1323 }
1324
Ted Kremenek687af802008-01-29 19:43:15 +00001325 // Assignment operators.
1326
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001327 case BinaryOperator::Assign: {
1328 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001329 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001330 Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2));
1331 break;
1332 }
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001333
1334 case BinaryOperator::AddAssign: {
1335 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001336 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1337 NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001338 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1339 break;
1340 }
1341
1342 case BinaryOperator::SubAssign: {
1343 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001344 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1345 NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2));
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001346 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1347 break;
1348 }
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001349
1350 case BinaryOperator::MulAssign: {
1351 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001352 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1353 NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2));
Ted Kremenek2eafd0e2008-01-23 23:42:27 +00001354 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1355 break;
1356 }
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001357
1358 case BinaryOperator::DivAssign: {
1359 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbd03f1d2008-01-28 22:09:13 +00001360 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1361 NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2));
Ted Kremenek5c1e2622008-01-25 23:45:34 +00001362 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1363 break;
1364 }
Ted Kremenek10099a62008-01-28 22:28:54 +00001365
1366 case BinaryOperator::RemAssign: {
1367 const LValue& L1 = cast<LValue>(V1);
1368 NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
1369 NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2));
1370 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
1371 break;
1372 }
Ted Kremenek687af802008-01-29 19:43:15 +00001373
1374 // Equality operators.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001375
Ted Kremenek687af802008-01-29 19:43:15 +00001376 case BinaryOperator::EQ:
1377 // FIXME: should we allow XX.EQ() to return a set of values,
1378 // allowing state bifurcation? In such cases, they will also
1379 // modify the state (meaning that a new state will be returned
1380 // as well).
1381 assert (B->getType() == getContext().IntTy);
1382
1383 if (isa<LValue>(V1)) {
1384 const LValue& L1 = cast<LValue>(V1);
1385 const LValue& L2 = cast<LValue>(V2);
1386 St = SetValue(St, B, L1.EQ(ValMgr, L2));
1387 }
1388 else {
1389 const NonLValue& R1 = cast<NonLValue>(V1);
1390 const NonLValue& R2 = cast<NonLValue>(V2);
1391 St = SetValue(St, B, R1.EQ(ValMgr, R2));
1392 }
1393
1394 Nodify(Dst, B, N2, St);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001395 break;
1396 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001397 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001398 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001399}
Ted Kremenekee985462008-01-16 18:18:48 +00001400
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001401
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001402void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
1403 GRConstants::NodeSet& Dst) {
1404
1405 // FIXME: add metadata to the CFG so that we can disable
1406 // this check when we KNOW that there is no block-level subexpression.
1407 // The motivation is that this check requires a hashtable lookup.
1408
1409 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1410 Dst.Add(Pred);
1411 return;
1412 }
1413
1414 switch (S->getStmtClass()) {
1415 case Stmt::BinaryOperatorClass:
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001416 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001417 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1418 break;
1419
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001420 case Stmt::UnaryOperatorClass:
1421 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
1422 break;
1423
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001424 case Stmt::ParenExprClass:
1425 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1426 break;
1427
Ted Kremenek874d63f2008-01-24 02:02:54 +00001428 case Stmt::ImplicitCastExprClass: {
1429 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1430 VisitCast(C, C->getSubExpr(), Pred, Dst);
1431 break;
1432 }
1433
1434 case Stmt::CastExprClass: {
1435 CastExpr* C = cast<CastExpr>(S);
1436 VisitCast(C, C->getSubExpr(), Pred, Dst);
1437 break;
1438 }
1439
Ted Kremenek9de04c42008-01-24 20:55:43 +00001440 case Stmt::DeclStmtClass:
1441 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1442 break;
1443
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001444 default:
1445 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1446 break;
Ted Kremenek79649df2008-01-17 18:25:22 +00001447 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001448}
1449
Ted Kremenekee985462008-01-16 18:18:48 +00001450//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001451// "Assume" logic.
1452//===----------------------------------------------------------------------===//
1453
1454StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption,
1455 bool& isFeasible) {
1456 return St;
1457}
1458
1459StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption,
1460 bool& isFeasible) {
1461
1462 switch (Cond.getSubKind()) {
1463 default:
1464 assert (false && "'Assume' not implemented for this NonLValue.");
1465 return St;
1466
1467 case ConcreteIntKind: {
1468 bool b = cast<ConcreteInt>(Cond).getValue() != 0;
1469 isFeasible = b ? Assumption : !Assumption;
1470 return St;
1471 }
1472 }
1473}
1474
1475
1476//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001477// Driver.
1478//===----------------------------------------------------------------------===//
1479
Ted Kremenekaa66a322008-01-16 21:46:15 +00001480#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001481static GRConstants* GraphPrintCheckerState;
1482
Ted Kremenekaa66a322008-01-16 21:46:15 +00001483namespace llvm {
1484template<>
1485struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1486 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001487
1488 static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001489 switch (kind) {
Ted Kremenek565256e2008-01-24 22:44:24 +00001490 case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001491 case ValueKey::IsDecl: Out << "Variables:\\l"; break;
1492 case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1493 default: assert (false && "Unknown ValueKey type.");
1494 }
1495 }
1496
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001497 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
1498 ValueKey::Kind kind, bool isFirstGroup = false) {
1499 bool isFirst = true;
1500
1501 for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) {
1502 if (I.getKey().getKind() != kind)
1503 continue;
1504
1505 if (isFirst) {
1506 if (!isFirstGroup) Out << "\\l\\l";
1507 PrintKindLabel(Out, kind);
1508 isFirst = false;
1509 }
1510 else
1511 Out << "\\l";
1512
1513 Out << ' ';
1514
1515 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1516 Out << V->getName();
1517 else {
1518 Stmt* E = cast<Stmt>(I.getKey());
1519 Out << " (" << (void*) E << ") ";
1520 E->printPretty(Out);
1521 }
1522
1523 Out << " : ";
1524 I.getData().print(Out);
1525 }
1526 }
1527
Ted Kremenekaa66a322008-01-16 21:46:15 +00001528 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1529 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001530
1531 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001532 ProgramPoint Loc = N->getLocation();
1533
1534 switch (Loc.getKind()) {
1535 case ProgramPoint::BlockEntranceKind:
1536 Out << "Block Entrance: B"
1537 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1538 break;
1539
1540 case ProgramPoint::BlockExitKind:
1541 assert (false);
1542 break;
1543
1544 case ProgramPoint::PostStmtKind: {
1545 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001546 Out << L.getStmt()->getStmtClassName() << ':'
1547 << (void*) L.getStmt() << ' ';
1548
Ted Kremenekaa66a322008-01-16 21:46:15 +00001549 L.getStmt()->printPretty(Out);
1550 break;
1551 }
1552
1553 default: {
1554 const BlockEdge& E = cast<BlockEdge>(Loc);
1555 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1556 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001557
1558 if (Stmt* T = E.getSrc()->getTerminator()) {
1559 Out << "\\|Terminator: ";
1560 E.getSrc()->printTerminator(Out);
1561
1562 if (isa<SwitchStmt>(T)) {
1563 // FIXME
1564 }
1565 else {
1566 Out << "\\lCondition: ";
1567 if (*E.getSrc()->succ_begin() == E.getDst())
1568 Out << "true";
1569 else
1570 Out << "false";
1571 }
1572
1573 Out << "\\l";
1574 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001575
1576 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1577 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1578 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001579 }
1580 }
1581
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001582 Out << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001583
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001584 PrintKind(Out, N->getState(), ValueKey::IsDecl, true);
1585 PrintKind(Out, N->getState(), ValueKey::IsBlkExpr);
Ted Kremenek565256e2008-01-24 22:44:24 +00001586 PrintKind(Out, N->getState(), ValueKey::IsSubExpr);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001587
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001588 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001589 return Out.str();
1590 }
1591};
1592} // end llvm namespace
1593#endif
1594
Ted Kremenekee985462008-01-16 18:18:48 +00001595namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001596void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1597 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001598 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001599#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001600 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001601 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001602 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001603#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001604}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001605} // end clang namespace