Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1 | //== 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 Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 10 | // This files defines SymbolID, VarBindKey, and ValueState. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 11 | // |
| 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 | |
| 40 | namespace clang { |
| 41 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 42 | /// VarBindKey - A variant smart pointer that wraps either a ValueDecl* or a |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 43 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 44 | class VarBindKey { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 45 | uintptr_t Raw; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 46 | void operator=(const VarBindKey& RHS); // Do not implement. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
| 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 Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 67 | VarBindKey(const ValueDecl* VD) |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 68 | : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) { |
| 69 | assert(VD && "ValueDecl cannot be NULL."); |
| 70 | } |
| 71 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 72 | VarBindKey(Stmt* S, bool isBlkExpr = false) |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 73 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){ |
| 74 | assert(S && "Tracked statement cannot be NULL."); |
| 75 | } |
| 76 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 77 | VarBindKey(SymbolID V) |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 78 | : 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 Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 95 | inline bool operator==(const VarBindKey& X) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 96 | return isSymbol() ? getSymbolID() == X.getSymbolID() |
| 97 | : getPtr() == X.getPtr(); |
| 98 | } |
| 99 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 100 | inline bool operator!=(const VarBindKey& X) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 101 | return !operator==(X); |
| 102 | } |
| 103 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 104 | inline bool operator<(const VarBindKey& X) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 105 | 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 Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 116 | namespace vstate { |
| 117 | typedef llvm::ImmutableMap<VarBindKey,RValue> VariableBindingsTy; |
| 118 | } |
| 119 | |
| 120 | struct ValueStateImpl : public llvm::FoldingSetNode { |
| 121 | vstate::VariableBindingsTy VariableBindings; |
| 122 | |
| 123 | ValueStateImpl(vstate::VariableBindingsTy VB) |
| 124 | : VariableBindings(VB) {} |
| 125 | |
| 126 | ValueStateImpl(const ValueStateImpl& RHS) |
| 127 | : llvm::FoldingSetNode(), VariableBindings(RHS.VariableBindings) {} |
| 128 | |
| 129 | |
| 130 | static void Profile(llvm::FoldingSetNodeID& ID, const ValueStateImpl& V) { |
| 131 | V.VariableBindings.Profile(ID); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 132 | } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 133 | |
| 134 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 135 | Profile(ID, *this); |
| 136 | } |
| 137 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 140 | class ValueState : public llvm::FoldingSetNode { |
| 141 | ValueStateImpl* Data; |
| 142 | public: |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 143 | ValueState(ValueStateImpl* D) : Data(D) {} |
| 144 | ValueState() : Data(0) {} |
| 145 | void operator=(ValueStateImpl* D) { Data = D; } |
| 146 | |
| 147 | // Accessors. |
| 148 | |
| 149 | ValueStateImpl* getImpl() const { return Data; } |
| 150 | |
| 151 | // Iterators. |
| 152 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 153 | typedef vstate::VariableBindingsTy VariableBindingsTy; |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame^] | 154 | typedef VariableBindingsTy::iterator vb_iterator; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame^] | 156 | vb_iterator begin() { return Data->VariableBindings.begin(); } |
| 157 | vb_iterator end() { return Data->VariableBindings.end(); } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 159 | // Profiling and equality testing. |
| 160 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 161 | bool operator==(const ValueState& RHS) const { |
| 162 | return Data == RHS.Data; |
| 163 | } |
| 164 | |
| 165 | static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) { |
| 166 | ID.AddPointer(V.getImpl()); |
| 167 | } |
| 168 | |
| 169 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 170 | Profile(ID, *this); |
| 171 | } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | template<> struct GRTrait<ValueState> { |
| 175 | static inline void* toPtr(ValueState St) { |
| 176 | return reinterpret_cast<void*>(St.getImpl()); |
| 177 | } |
| 178 | static inline ValueState toState(void* P) { |
| 179 | return ValueState(static_cast<ValueStateImpl*>(P)); |
| 180 | } |
| 181 | }; |
| 182 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 183 | |
| 184 | class ValueStateManager { |
| 185 | public: |
| 186 | typedef ValueState StateTy; |
| 187 | |
| 188 | private: |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 189 | ValueState::VariableBindingsTy::Factory VBFactory; |
| 190 | llvm::FoldingSet<ValueStateImpl> StateSet; |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 191 | |
| 192 | /// ValueMgr - Object that manages the data for all created RValues. |
| 193 | ValueManager ValMgr; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 195 | /// SymMgr - Object that manages the symbol information. |
| 196 | SymbolManager SymMgr; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 197 | |
| 198 | /// Alloc - A BumpPtrAllocator to allocate states. |
| 199 | llvm::BumpPtrAllocator& Alloc; |
| 200 | |
| 201 | StateTy getPersistentState(const ValueState& St); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 202 | |
| 203 | public: |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 204 | ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc) |
| 205 | : ValMgr(Ctx, alloc), Alloc(alloc) {} |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 207 | StateTy getInitialState(); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 208 | |
| 209 | ValueManager& getValueManager() { return ValMgr; } |
| 210 | SymbolManager& getSymbolManager() { return SymMgr; } |
| 211 | |
| 212 | StateTy SetValue(StateTy St, Stmt* S, bool isBlkExpr, const RValue& V); |
| 213 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
| 214 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 215 | RValue GetValue(const StateTy& St, Stmt* S, bool* hasVal = NULL); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 216 | RValue GetValue(const StateTy& St, const LValue& LV); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 218 | LValue GetLValue(const StateTy& St, Stmt* S); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 219 | |
| 220 | StateTy Add(StateTy St, VarBindKey K, const RValue& V); |
| 221 | StateTy Remove(StateTy St, VarBindKey K); |
| 222 | StateTy getPersistentState(const ValueStateImpl& Impl); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 225 | } // end clang namespace |
| 226 | |
| 227 | //==------------------------------------------------------------------------==// |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 228 | // Casting machinery to get cast<> and dyn_cast<> working with VarBindKey. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 229 | //==------------------------------------------------------------------------==// |
| 230 | |
| 231 | namespace llvm { |
| 232 | |
| 233 | template<> inline bool |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 234 | isa<clang::ValueDecl,clang::VarBindKey>(const clang::VarBindKey& V) { |
| 235 | return V.getKind() == clang::VarBindKey::IsDecl; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | template<> inline bool |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 239 | isa<clang::Stmt,clang::VarBindKey>(const clang::VarBindKey& V) { |
| 240 | return ((unsigned) V.getKind()) < clang::VarBindKey::IsDecl; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 243 | template<> struct cast_retty_impl<clang::ValueDecl,clang::VarBindKey> { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 244 | typedef const clang::ValueDecl* ret_type; |
| 245 | }; |
| 246 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 247 | template<> struct cast_retty_impl<clang::Stmt,clang::VarBindKey> { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 248 | typedef const clang::Stmt* ret_type; |
| 249 | }; |
| 250 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 251 | template<> struct simplify_type<clang::VarBindKey> { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 252 | typedef void* SimpleType; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 253 | static inline SimpleType getSimplifiedValue(const clang::VarBindKey &V) { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 254 | return V.getPtr(); |
| 255 | } |
| 256 | }; |
| 257 | } // end llvm namespace |
| 258 | |
| 259 | #endif |