blob: b452979e2ee2ac3fb76554fa83b96cc97ed73eb5 [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//== ValueState.h - Path-Sens. "State" for tracking valuues -----*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek9153f732008-02-05 07:17:49 +000010// This files defines SymbolID, VarBindKey, and ValueState.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_VALUESTATE_H
15#define LLVM_CLANG_ANALYSIS_VALUESTATE_H
16
17// FIXME: Reduce the number of includes.
18
19#include "RValues.h"
20
21#include "clang/Analysis/PathSensitive/GREngine.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/ASTContext.h"
25#include "clang/Analysis/Analyses/LiveVariables.h"
26
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/DataTypes.h"
29#include "llvm/ADT/APSInt.h"
30#include "llvm/ADT/FoldingSet.h"
31#include "llvm/ADT/ImmutableMap.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/SmallPtrSet.h"
34#include "llvm/Support/Allocator.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Streams.h"
37
38#include <functional>
39
40namespace clang {
41
Ted Kremenek9153f732008-02-05 07:17:49 +000042/// VarBindKey - A variant smart pointer that wraps either a ValueDecl* or a
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000043/// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type
Ted Kremenek9153f732008-02-05 07:17:49 +000044class VarBindKey {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000045 uintptr_t Raw;
Ted Kremenek9153f732008-02-05 07:17:49 +000046 void operator=(const VarBindKey& RHS); // Do not implement.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000047
48public:
49 enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings.
50 IsSymbol=0x3, // Symbol Bindings.
51 Mask=0x3 };
52
53 inline Kind getKind() const {
54 return (Kind) (Raw & Mask);
55 }
56
57 inline void* getPtr() const {
58 assert (getKind() != IsSymbol);
59 return reinterpret_cast<void*>(Raw & ~Mask);
60 }
61
62 inline SymbolID getSymbolID() const {
63 assert (getKind() == IsSymbol);
64 return Raw >> 2;
65 }
66
Ted Kremenek9153f732008-02-05 07:17:49 +000067 VarBindKey(const ValueDecl* VD)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000068 : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {
69 assert(VD && "ValueDecl cannot be NULL.");
70 }
71
Ted Kremenek9153f732008-02-05 07:17:49 +000072 VarBindKey(Stmt* S, bool isBlkExpr = false)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000073 : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){
74 assert(S && "Tracked statement cannot be NULL.");
75 }
76
Ted Kremenek9153f732008-02-05 07:17:49 +000077 VarBindKey(SymbolID V)
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000078 : Raw((V << 2) | IsSymbol) {}
79
80 bool isSymbol() const { return getKind() == IsSymbol; }
81 bool isSubExpr() const { return getKind() == IsSubExpr; }
82 bool isBlkExpr() const { return getKind() == IsBlkExpr; }
83 bool isDecl() const { return getKind() == IsDecl; }
84 bool isStmt() const { return getKind() <= IsBlkExpr; }
85
86 inline void Profile(llvm::FoldingSetNodeID& ID) const {
87 ID.AddInteger(isSymbol() ? 1 : 0);
88
89 if (isSymbol())
90 ID.AddInteger(getSymbolID());
91 else
92 ID.AddPointer(getPtr());
93 }
94
Ted Kremenek9153f732008-02-05 07:17:49 +000095 inline bool operator==(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000096 return isSymbol() ? getSymbolID() == X.getSymbolID()
97 : getPtr() == X.getPtr();
98 }
99
Ted Kremenek9153f732008-02-05 07:17:49 +0000100 inline bool operator!=(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000101 return !operator==(X);
102 }
103
Ted Kremenek9153f732008-02-05 07:17:49 +0000104 inline bool operator<(const VarBindKey& X) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000105 if (isSymbol())
106 return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false;
107
108 return getPtr() < X.getPtr();
109 }
110};
111
112//===----------------------------------------------------------------------===//
113// ValueState - An ImmutableMap type Stmt*/Decl*/Symbols to RValues.
114//===----------------------------------------------------------------------===//
115
Ted Kremenek9153f732008-02-05 07:17:49 +0000116namespace vstate {
Ted Kremenek174aea42008-02-05 18:51:06 +0000117 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy;
118
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000119 typedef llvm::ImmutableMap<VarBindKey,RValue> VariableBindingsTy;
120 typedef llvm::ImmutableMap<SymbolID,IntSetTy> ConstantNotEqTy;
121 typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstantEqTy;
Ted Kremenek9153f732008-02-05 07:17:49 +0000122}
Ted Kremenek6f886bd2008-02-05 18:24:17 +0000123
124/// ValueStateImpl - This class encapsulates the actual data values for
125/// for a "state" in our symbolic value tracking. It is intended to be
126/// used as a functional object; that is once it is created and made
127/// "persistent" in a FoldingSet its values will never change.
Ted Kremenek9153f732008-02-05 07:17:49 +0000128struct ValueStateImpl : public llvm::FoldingSetNode {
129 vstate::VariableBindingsTy VariableBindings;
Ted Kremenek174aea42008-02-05 18:51:06 +0000130 vstate::ConstantNotEqTy ConstantNotEq;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000131 vstate::ConstantEqTy ConstantEq;
Ted Kremenek9153f732008-02-05 07:17:49 +0000132
Ted Kremenek174aea42008-02-05 18:51:06 +0000133 /// This ctor is used when creating the first ValueStateImpl object.
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000134 ValueStateImpl(vstate::VariableBindingsTy VB,
135 vstate::ConstantNotEqTy CNE,
136 vstate::ConstantEqTy CE)
137 : VariableBindings(VB), ConstantNotEq(CNE), ConstantEq(CE) {}
Ted Kremenek9153f732008-02-05 07:17:49 +0000138
Ted Kremenek174aea42008-02-05 18:51:06 +0000139 /// Copy ctor - We must explicitly define this or else the "Next" ptr
140 /// in FoldingSetNode will also get copied.
Ted Kremenek9153f732008-02-05 07:17:49 +0000141 ValueStateImpl(const ValueStateImpl& RHS)
Ted Kremenek6f886bd2008-02-05 18:24:17 +0000142 : llvm::FoldingSetNode(),
Ted Kremenek174aea42008-02-05 18:51:06 +0000143 VariableBindings(RHS.VariableBindings),
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000144 ConstantNotEq(RHS.ConstantNotEq),
145 ConstantEq(RHS.ConstantEq) {}
Ted Kremenek9153f732008-02-05 07:17:49 +0000146
Ted Kremenek174aea42008-02-05 18:51:06 +0000147 /// Profile - Profile the contents of a ValueStateImpl object for use
148 /// in a FoldingSet.
Ted Kremenek9153f732008-02-05 07:17:49 +0000149 static void Profile(llvm::FoldingSetNodeID& ID, const ValueStateImpl& V) {
150 V.VariableBindings.Profile(ID);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000151 V.ConstantNotEq.Profile(ID);
152 V.ConstantEq.Profile(ID);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000153 }
Ted Kremenek174aea42008-02-05 18:51:06 +0000154
155 /// Profile - Used to profile the contents of this object for inclusion
156 /// in a FoldingSet.
Ted Kremenek9153f732008-02-05 07:17:49 +0000157 void Profile(llvm::FoldingSetNodeID& ID) const {
158 Profile(ID, *this);
159 }
160
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000161};
162
Ted Kremenek6f886bd2008-02-05 18:24:17 +0000163/// ValueState - This class represents a "state" in our symbolic value
164/// tracking. It is really just a "smart pointer", wrapping a pointer
165/// to ValueStateImpl object. Making this class a smart pointer means that its
166/// size is always the size of a pointer, which allows easy conversion to
167/// void* when being handled by GREngine. It also forces us to unique states;
168/// consequently, a ValueStateImpl* with a specific address will always refer
169/// to the unique state with those values.
Ted Kremenek9153f732008-02-05 07:17:49 +0000170class ValueState : public llvm::FoldingSetNode {
171 ValueStateImpl* Data;
172public:
Ted Kremeneked900212008-02-05 18:17:58 +0000173 ValueState(ValueStateImpl* D) : Data(D) {}
174 ValueState() : Data(0) {}
175 void operator=(ValueStateImpl* D) { Data = D; }
176
Ted Kremenekcba2e432008-02-05 19:35:18 +0000177 // Accessors.
Ted Kremeneked900212008-02-05 18:17:58 +0000178 ValueStateImpl* getImpl() const { return Data; }
Ted Kremenek174aea42008-02-05 18:51:06 +0000179
Ted Kremenekcba2e432008-02-05 19:35:18 +0000180 // Typedefs.
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000181 typedef vstate::IntSetTy IntSetTy;
Ted Kremenekcba2e432008-02-05 19:35:18 +0000182 typedef vstate::VariableBindingsTy VariableBindingsTy;
183 typedef vstate::ConstantNotEqTy ConstantNotEqTy;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000184 typedef vstate::ConstantEqTy ConstantEqTy;
185
Ted Kremenekcba2e432008-02-05 19:35:18 +0000186 typedef llvm::SmallVector<ValueState,5> BufferTy;
Ted Kremenek174aea42008-02-05 18:51:06 +0000187
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000188 // Queries.
189
190 bool isNotEqual(SymbolID sym, const llvm::APSInt& V) const;
191 const llvm::APSInt* getSymVal(SymbolID sym) const;
192
Ted Kremenek174aea42008-02-05 18:51:06 +0000193 // Iterators.
194
195 typedef VariableBindingsTy::iterator vb_iterator;
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000196 vb_iterator begin() { return Data->VariableBindings.begin(); }
197 vb_iterator end() { return Data->VariableBindings.end(); }
Ted Kremenek9153f732008-02-05 07:17:49 +0000198
Ted Kremeneked900212008-02-05 18:17:58 +0000199 // Profiling and equality testing.
200
Ted Kremenek9153f732008-02-05 07:17:49 +0000201 bool operator==(const ValueState& RHS) const {
202 return Data == RHS.Data;
203 }
204
205 static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) {
206 ID.AddPointer(V.getImpl());
207 }
208
209 void Profile(llvm::FoldingSetNodeID& ID) const {
210 Profile(ID, *this);
211 }
Ted Kremenek9153f732008-02-05 07:17:49 +0000212};
213
214template<> struct GRTrait<ValueState> {
215 static inline void* toPtr(ValueState St) {
216 return reinterpret_cast<void*>(St.getImpl());
217 }
218 static inline ValueState toState(void* P) {
219 return ValueState(static_cast<ValueStateImpl*>(P));
220 }
221};
222
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000223
224class ValueStateManager {
225public:
226 typedef ValueState StateTy;
227
228private:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000229 ValueState::IntSetTy::Factory ISetFactory;
Ted Kremenek9153f732008-02-05 07:17:49 +0000230 ValueState::VariableBindingsTy::Factory VBFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000231 ValueState::ConstantNotEqTy::Factory CNEFactory;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000232 ValueState::ConstantEqTy::Factory CEFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000233
234 /// StateSet - FoldingSet containing all the states created for analyzing
235 /// a particular function. This is used to unique states.
Ted Kremenek9153f732008-02-05 07:17:49 +0000236 llvm::FoldingSet<ValueStateImpl> StateSet;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000237
238 /// ValueMgr - Object that manages the data for all created RValues.
239 ValueManager ValMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000240
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000241 /// SymMgr - Object that manages the symbol information.
242 SymbolManager SymMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000243
244 /// Alloc - A BumpPtrAllocator to allocate states.
245 llvm::BumpPtrAllocator& Alloc;
246
247 StateTy getPersistentState(const ValueState& St);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000248
249public:
Ted Kremenek9153f732008-02-05 07:17:49 +0000250 ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc)
251 : ValMgr(Ctx, alloc), Alloc(alloc) {}
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000252
Ted Kremenek9153f732008-02-05 07:17:49 +0000253 StateTy getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000254
255 ValueManager& getValueManager() { return ValMgr; }
256 SymbolManager& getSymbolManager() { return SymMgr; }
257
258 StateTy SetValue(StateTy St, Stmt* S, bool isBlkExpr, const RValue& V);
259 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
260
Ted Kremenekf233d482008-02-05 00:26:40 +0000261 RValue GetValue(const StateTy& St, Stmt* S, bool* hasVal = NULL);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000262 RValue GetValue(const StateTy& St, const LValue& LV);
Ted Kremenekf233d482008-02-05 00:26:40 +0000263
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000264 LValue GetLValue(const StateTy& St, Stmt* S);
Ted Kremenek9153f732008-02-05 07:17:49 +0000265
266 StateTy Add(StateTy St, VarBindKey K, const RValue& V);
267 StateTy Remove(StateTy St, VarBindKey K);
268 StateTy getPersistentState(const ValueStateImpl& Impl);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000269
270 StateTy AddEQ(StateTy St, SymbolID sym, const llvm::APSInt& V);
271 StateTy AddNE(StateTy St, SymbolID sym, const llvm::APSInt& V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000272};
273
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000274} // end clang namespace
275
276//==------------------------------------------------------------------------==//
Ted Kremenek9153f732008-02-05 07:17:49 +0000277// Casting machinery to get cast<> and dyn_cast<> working with VarBindKey.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000278//==------------------------------------------------------------------------==//
279
280namespace llvm {
281
282 template<> inline bool
Ted Kremenek9153f732008-02-05 07:17:49 +0000283 isa<clang::ValueDecl,clang::VarBindKey>(const clang::VarBindKey& V) {
284 return V.getKind() == clang::VarBindKey::IsDecl;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000285 }
286
287 template<> inline bool
Ted Kremenek9153f732008-02-05 07:17:49 +0000288 isa<clang::Stmt,clang::VarBindKey>(const clang::VarBindKey& V) {
289 return ((unsigned) V.getKind()) < clang::VarBindKey::IsDecl;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000290 }
291
Ted Kremenek9153f732008-02-05 07:17:49 +0000292 template<> struct cast_retty_impl<clang::ValueDecl,clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000293 typedef const clang::ValueDecl* ret_type;
294 };
295
Ted Kremenek9153f732008-02-05 07:17:49 +0000296 template<> struct cast_retty_impl<clang::Stmt,clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000297 typedef const clang::Stmt* ret_type;
298 };
299
Ted Kremenek9153f732008-02-05 07:17:49 +0000300 template<> struct simplify_type<clang::VarBindKey> {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000301 typedef void* SimpleType;
Ted Kremenek9153f732008-02-05 07:17:49 +0000302 static inline SimpleType getSimplifiedValue(const clang::VarBindKey &V) {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000303 return V.getPtr();
304 }
305 };
306} // end llvm namespace
307
308#endif