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