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 | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 10 | // This files defines SymbolID, ExprBindKey, 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 | |
Ted Kremenek | cc409b7 | 2008-02-14 17:30:51 +0000 | [diff] [blame^] | 19 | #include "clang/Analysis/PathSensitive/RValues.h" |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/GRCoreEngine.h" |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/ASTContext.h" |
| 24 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 25 | |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/DataTypes.h" |
| 28 | #include "llvm/ADT/APSInt.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/Support/Allocator.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | #include "llvm/Support/Streams.h" |
| 36 | |
| 37 | #include <functional> |
| 38 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 39 | namespace clang { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // ValueState - An ImmutableMap type Stmt*/Decl*/Symbols to RValues. |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 45 | namespace vstate { |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 46 | typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy; |
| 47 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 48 | typedef llvm::ImmutableMap<Expr*,RValue> ExprBindingsTy; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 49 | typedef llvm::ImmutableMap<VarDecl*,RValue> VarBindingsTy; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 50 | typedef llvm::ImmutableMap<SymbolID,IntSetTy> ConstantNotEqTy; |
| 51 | typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstantEqTy; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 52 | } |
Ted Kremenek | 6f886bd | 2008-02-05 18:24:17 +0000 | [diff] [blame] | 53 | |
| 54 | /// ValueStateImpl - This class encapsulates the actual data values for |
| 55 | /// for a "state" in our symbolic value tracking. It is intended to be |
| 56 | /// used as a functional object; that is once it is created and made |
| 57 | /// "persistent" in a FoldingSet its values will never change. |
Ted Kremenek | a40ba02 | 2008-02-06 02:50:36 +0000 | [diff] [blame] | 58 | class ValueStateImpl : public llvm::FoldingSetNode { |
| 59 | private: |
| 60 | void operator=(const ValueStateImpl& R) const; |
| 61 | |
| 62 | public: |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 63 | vstate::ExprBindingsTy SubExprBindings; |
| 64 | vstate::ExprBindingsTy BlockExprBindings; |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 65 | vstate::VarBindingsTy VarBindings; |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 66 | vstate::ConstantNotEqTy ConstantNotEq; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 67 | vstate::ConstantEqTy ConstantEq; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 69 | /// This ctor is used when creating the first ValueStateImpl object. |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 70 | ValueStateImpl(vstate::ExprBindingsTy EB, |
| 71 | vstate::VarBindingsTy VB, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 72 | vstate::ConstantNotEqTy CNE, |
| 73 | vstate::ConstantEqTy CE) |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 74 | : SubExprBindings(EB), |
| 75 | BlockExprBindings(EB), |
| 76 | VarBindings(VB), |
| 77 | ConstantNotEq(CNE), |
| 78 | ConstantEq(CE) {} |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 80 | /// Copy ctor - We must explicitly define this or else the "Next" ptr |
| 81 | /// in FoldingSetNode will also get copied. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 82 | ValueStateImpl(const ValueStateImpl& RHS) |
Ted Kremenek | 6f886bd | 2008-02-05 18:24:17 +0000 | [diff] [blame] | 83 | : llvm::FoldingSetNode(), |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 84 | SubExprBindings(RHS.SubExprBindings), |
| 85 | BlockExprBindings(RHS.BlockExprBindings), |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 86 | VarBindings(RHS.VarBindings), |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 87 | ConstantNotEq(RHS.ConstantNotEq), |
| 88 | ConstantEq(RHS.ConstantEq) {} |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | a40ba02 | 2008-02-06 02:50:36 +0000 | [diff] [blame] | 90 | |
| 91 | |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 92 | /// Profile - Profile the contents of a ValueStateImpl object for use |
| 93 | /// in a FoldingSet. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 94 | static void Profile(llvm::FoldingSetNodeID& ID, const ValueStateImpl& V) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 95 | V.SubExprBindings.Profile(ID); |
| 96 | V.BlockExprBindings.Profile(ID); |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 97 | V.VarBindings.Profile(ID); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 98 | V.ConstantNotEq.Profile(ID); |
| 99 | V.ConstantEq.Profile(ID); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 101 | |
| 102 | /// Profile - Used to profile the contents of this object for inclusion |
| 103 | /// in a FoldingSet. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 104 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 105 | Profile(ID, *this); |
| 106 | } |
| 107 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Ted Kremenek | 6f886bd | 2008-02-05 18:24:17 +0000 | [diff] [blame] | 110 | /// ValueState - This class represents a "state" in our symbolic value |
| 111 | /// tracking. It is really just a "smart pointer", wrapping a pointer |
| 112 | /// to ValueStateImpl object. Making this class a smart pointer means that its |
| 113 | /// size is always the size of a pointer, which allows easy conversion to |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 114 | /// void* when being handled by GRCoreEngine. It also forces us to unique states; |
Ted Kremenek | 6f886bd | 2008-02-05 18:24:17 +0000 | [diff] [blame] | 115 | /// consequently, a ValueStateImpl* with a specific address will always refer |
| 116 | /// to the unique state with those values. |
Ted Kremenek | a40ba02 | 2008-02-06 02:50:36 +0000 | [diff] [blame] | 117 | class ValueState { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 118 | ValueStateImpl* Data; |
| 119 | public: |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 120 | ValueState(ValueStateImpl* D) : Data(D) {} |
Ted Kremenek | a40ba02 | 2008-02-06 02:50:36 +0000 | [diff] [blame] | 121 | ValueState() : Data(0) {} |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 123 | // Accessors. |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 124 | ValueStateImpl* getImpl() const { return Data; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 125 | ValueStateImpl& operator*() { return *Data; } |
| 126 | ValueStateImpl* operator->() { return Data; } |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 128 | // Typedefs. |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 129 | typedef vstate::IntSetTy IntSetTy; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 130 | typedef vstate::ExprBindingsTy ExprBindingsTy; |
| 131 | typedef vstate::VarBindingsTy VarBindingsTy; |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 132 | typedef vstate::ConstantNotEqTy ConstantNotEqTy; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 133 | typedef vstate::ConstantEqTy ConstantEqTy; |
| 134 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 135 | typedef llvm::SmallVector<ValueState,5> BufferTy; |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 137 | // Queries. |
| 138 | |
| 139 | bool isNotEqual(SymbolID sym, const llvm::APSInt& V) const; |
| 140 | const llvm::APSInt* getSymVal(SymbolID sym) const; |
| 141 | |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 142 | // Iterators. |
| 143 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 144 | typedef VarBindingsTy::iterator vb_iterator; |
| 145 | vb_iterator vb_begin() const { return Data->VarBindings.begin(); } |
| 146 | vb_iterator vb_end() const { return Data->VarBindings.end(); } |
| 147 | |
| 148 | typedef ExprBindingsTy::iterator seb_iterator; |
| 149 | seb_iterator seb_begin() const { return Data->SubExprBindings.begin(); } |
| 150 | seb_iterator seb_end() const { return Data->SubExprBindings.end(); } |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 152 | typedef ExprBindingsTy::iterator beb_iterator; |
| 153 | beb_iterator beb_begin() const { return Data->BlockExprBindings.begin(); } |
| 154 | beb_iterator beb_end() const { return Data->BlockExprBindings.end(); } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | ed90021 | 2008-02-05 18:17:58 +0000 | [diff] [blame] | 156 | // Profiling and equality testing. |
| 157 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 158 | bool operator==(const ValueState& RHS) const { |
| 159 | return Data == RHS.Data; |
| 160 | } |
| 161 | |
| 162 | static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) { |
| 163 | ID.AddPointer(V.getImpl()); |
| 164 | } |
| 165 | |
| 166 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 167 | Profile(ID, *this); |
| 168 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 169 | |
| 170 | void printDOT(std::ostream& Out) const; |
| 171 | void print(std::ostream& Out) const; |
| 172 | void print() const { print(*llvm::cerr); } |
| 173 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | template<> struct GRTrait<ValueState> { |
| 177 | static inline void* toPtr(ValueState St) { |
| 178 | return reinterpret_cast<void*>(St.getImpl()); |
| 179 | } |
| 180 | static inline ValueState toState(void* P) { |
| 181 | return ValueState(static_cast<ValueStateImpl*>(P)); |
| 182 | } |
| 183 | }; |
| 184 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 185 | |
| 186 | class ValueStateManager { |
| 187 | public: |
| 188 | typedef ValueState StateTy; |
| 189 | |
| 190 | private: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 191 | ValueState::IntSetTy::Factory ISetFactory; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 192 | ValueState::ExprBindingsTy::Factory EXFactory; |
| 193 | ValueState::VarBindingsTy::Factory VBFactory; |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 194 | ValueState::ConstantNotEqTy::Factory CNEFactory; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 195 | ValueState::ConstantEqTy::Factory CEFactory; |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 196 | |
| 197 | /// StateSet - FoldingSet containing all the states created for analyzing |
| 198 | /// a particular function. This is used to unique states. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 199 | llvm::FoldingSet<ValueStateImpl> StateSet; |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 200 | |
| 201 | /// ValueMgr - Object that manages the data for all created RValues. |
| 202 | ValueManager ValMgr; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 204 | /// SymMgr - Object that manages the symbol information. |
| 205 | SymbolManager SymMgr; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 206 | |
| 207 | /// Alloc - A BumpPtrAllocator to allocate states. |
| 208 | llvm::BumpPtrAllocator& Alloc; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | |
| 212 | ValueState::ExprBindingsTy Remove(ValueState::ExprBindingsTy B, Expr* E) { |
| 213 | return EXFactory.Remove(B, E); |
| 214 | } |
| 215 | |
| 216 | ValueState::VarBindingsTy Remove(ValueState::VarBindingsTy B, VarDecl* V) { |
| 217 | return VBFactory.Remove(B, V); |
| 218 | } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 220 | inline ValueState::ExprBindingsTy Remove(const ValueStateImpl& V, Expr* E) { |
| 221 | return Remove(V.BlockExprBindings, E); |
| 222 | } |
| 223 | |
| 224 | inline ValueState::VarBindingsTy Remove(const ValueStateImpl& V, VarDecl* D) { |
| 225 | return Remove(V.VarBindings, D); |
| 226 | } |
| 227 | |
| 228 | ValueState BindVar(ValueState St, VarDecl* D, const RValue& V); |
| 229 | ValueState UnbindVar(ValueState St, VarDecl* D); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 230 | |
| 231 | public: |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 232 | ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc) |
Ted Kremenek | 8158a0e | 2008-02-11 23:12:59 +0000 | [diff] [blame] | 233 | : ISetFactory(alloc), |
| 234 | EXFactory(alloc), |
| 235 | VBFactory(alloc), |
| 236 | CNEFactory(alloc), |
| 237 | CEFactory(alloc), |
| 238 | ValMgr(Ctx, alloc), Alloc(alloc) {} |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 240 | ValueState getInitialState(); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 241 | |
| 242 | ValueManager& getValueManager() { return ValMgr; } |
| 243 | SymbolManager& getSymbolManager() { return SymMgr; } |
| 244 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 245 | ValueState RemoveDeadBindings(ValueState St, Stmt* Loc, |
| 246 | const LiveVariables& Liveness); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 248 | ValueState RemoveSubExprBindings(ValueState St) { |
| 249 | ValueStateImpl NewSt = *St; |
| 250 | NewSt.SubExprBindings = EXFactory.GetEmptyMap(); |
| 251 | return getPersistentState(NewSt); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | ValueState SetValue(ValueState St, Expr* S, bool isBlkExpr, const RValue& V); |
| 256 | ValueState SetValue(ValueState St, const LValue& LV, const RValue& V); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 258 | RValue GetValue(ValueState St, Expr* S, bool* hasVal = NULL); |
| 259 | RValue GetValue(ValueState St, const LValue& LV, QualType* T = NULL); |
| 260 | LValue GetLValue(ValueState St, Expr* S); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 262 | ValueState getPersistentState(const ValueStateImpl& Impl); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 264 | ValueState AddEQ(ValueState St, SymbolID sym, const llvm::APSInt& V); |
| 265 | ValueState AddNE(ValueState St, SymbolID sym, const llvm::APSInt& V); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 268 | } // end clang namespace |
| 269 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 270 | #endif |