Zhongxing Xu | d19e21b | 2008-08-29 15:09:12 +0000 | [diff] [blame] | 1 | //== BasicConstraintManager.cpp - Manage basic constraints.------*- 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 | // |
| 10 | // This file defines BasicConstraintManager, a class that tracks simple |
| 11 | // equality and inequality constraints on symbolic values of GRState. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 15 | #include "SimpleConstraintManager.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { class VISIBILITY_HIDDEN ConstNotEq {}; } |
| 26 | namespace { class VISIBILITY_HIDDEN ConstEq {}; } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 28 | typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy; |
| 29 | typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy; |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 30 | |
| 31 | static int ConstEqIndex = 0; |
| 32 | static int ConstNotEqIndex = 0; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 34 | namespace clang { |
| 35 | template<> |
| 36 | struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> { |
| 37 | static inline void* GDMIndex() { return &ConstNotEqIndex; } |
| 38 | }; |
| 39 | |
| 40 | template<> |
| 41 | struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> { |
| 42 | static inline void* GDMIndex() { return &ConstEqIndex; } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | namespace { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 47 | // BasicConstraintManager only tracks equality and inequality constraints of |
| 48 | // constants and integer variables. |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 49 | class VISIBILITY_HIDDEN BasicConstraintManager |
| 50 | : public SimpleConstraintManager { |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 51 | GRState::IntSetTy::Factory ISetFactory; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 52 | public: |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 53 | BasicConstraintManager(GRStateManager& statemgr) |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 54 | : SimpleConstraintManager(statemgr), ISetFactory(statemgr.getAllocator()) {} |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 56 | const GRState* AssumeSymNE(const GRState* St, SymbolRef sym, |
Ted Kremenek | b2bf7cd | 2009-01-28 22:27:59 +0000 | [diff] [blame] | 57 | const llvm::APSInt& V, bool& isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 59 | const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 60 | const llvm::APSInt& V, bool& isFeasible); |
| 61 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 62 | const GRState* AssumeSymLT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 63 | const llvm::APSInt& V, bool& isFeasible); |
| 64 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 65 | const GRState* AssumeSymGT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 66 | const llvm::APSInt& V, bool& isFeasible); |
| 67 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 68 | const GRState* AssumeSymGE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 69 | const llvm::APSInt& V, bool& isFeasible); |
| 70 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 71 | const GRState* AssumeSymLE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 72 | const llvm::APSInt& V, bool& isFeasible); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 74 | const GRState* AddEQ(const GRState* St, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 76 | const GRState* AddNE(const GRState* St, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 78 | const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const; |
| 79 | bool isNotEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) |
| 80 | const; |
| 81 | bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) |
| 82 | const; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 84 | const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper); |
| 85 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 86 | void print(const GRState* St, std::ostream& Out, |
| 87 | const char* nl, const char *sep); |
| 88 | }; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 89 | |
| 90 | } // end anonymous namespace |
| 91 | |
| 92 | ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr) |
| 93 | { |
| 94 | return new BasicConstraintManager(StateMgr); |
| 95 | } |
| 96 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 97 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 98 | BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 99 | const llvm::APSInt& V, bool& isFeasible) { |
| 100 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 101 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 102 | isFeasible = (*X != V); |
| 103 | return St; |
| 104 | } |
| 105 | |
| 106 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 107 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 108 | isFeasible = true; |
| 109 | return St; |
| 110 | } |
| 111 | |
| 112 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 113 | // Make that assumption. |
| 114 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 115 | return AddNE(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 119 | BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 120 | const llvm::APSInt& V, bool& isFeasible) { |
| 121 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 122 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 123 | isFeasible = *X == V; |
| 124 | return St; |
| 125 | } |
| 126 | |
| 127 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 128 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 129 | isFeasible = false; |
| 130 | return St; |
| 131 | } |
| 132 | |
| 133 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 134 | // Make that assumption. |
| 135 | |
| 136 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 137 | return AddEQ(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // These logic will be handled in another ConstraintManager. |
| 141 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 142 | BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 143 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 144 | |
| 145 | // Is 'V' the smallest possible value? |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 146 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 147 | // sym cannot be any value less than 'V'. This path is infeasible. |
| 148 | isFeasible = false; |
| 149 | return St; |
| 150 | } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 151 | |
| 152 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
| 153 | return AssumeSymNE(St, sym, V, isFeasible); |
| 154 | } |
| 155 | |
| 156 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 157 | BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 158 | const llvm::APSInt& V, bool& isFeasible) { |
| 159 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 160 | // Is 'V' the largest possible value? |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 161 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 162 | // sym cannot be any value greater than 'V'. This path is infeasible. |
| 163 | isFeasible = false; |
| 164 | return St; |
| 165 | } |
| 166 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 167 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
| 168 | return AssumeSymNE(St, sym, V, isFeasible); |
| 169 | } |
| 170 | |
| 171 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 172 | BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 173 | const llvm::APSInt& V, bool& isFeasible) { |
| 174 | |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 175 | // Reject a path if the value of sym is a constant X and !(X >= V). |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 176 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 177 | isFeasible = *X >= V; |
| 178 | return St; |
| 179 | } |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 180 | |
| 181 | // Sym is not a constant, but it is worth looking to see if V is the |
| 182 | // maximum integer value. |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 183 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 184 | // If we know that sym != V, then this condition is infeasible since |
| 185 | // there is no other value greater than V. |
| 186 | isFeasible = !isNotEqual(St, sym, V); |
| 187 | |
| 188 | // If the path is still feasible then as a consequence we know that |
| 189 | // 'sym == V' because we cannot have 'sym > V' (no larger values). |
| 190 | // Add this constraint. |
| 191 | if (isFeasible) |
| 192 | return AddEQ(St, sym, V); |
| 193 | } |
| 194 | else |
| 195 | isFeasible = true; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 196 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 197 | return St; |
| 198 | } |
| 199 | |
| 200 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 201 | BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 202 | const llvm::APSInt& V, bool& isFeasible) { |
| 203 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 204 | // Reject a path if the value of sym is a constant X and !(X <= V). |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 205 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 206 | isFeasible = *X <= V; |
| 207 | return St; |
| 208 | } |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 210 | // Sym is not a constant, but it is worth looking to see if V is the |
| 211 | // minimum integer value. |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 212 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 213 | // If we know that sym != V, then this condition is infeasible since |
| 214 | // there is no other value less than V. |
| 215 | isFeasible = !isNotEqual(St, sym, V); |
| 216 | |
| 217 | // If the path is still feasible then as a consequence we know that |
| 218 | // 'sym == V' because we cannot have 'sym < V' (no smaller values). |
| 219 | // Add this constraint. |
| 220 | if (isFeasible) |
| 221 | return AddEQ(St, sym, V); |
| 222 | } |
| 223 | else |
| 224 | isFeasible = true; |
| 225 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 226 | return St; |
| 227 | } |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 229 | const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 230 | const llvm::APSInt& V) { |
| 231 | // Create a new state with the old binding replaced. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 232 | return state->set<ConstEq>(sym, &V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 235 | const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 236 | const llvm::APSInt& V) { |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 237 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 238 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 239 | ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 240 | GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 241 | |
| 242 | // Now add V to the NE set. |
| 243 | S = ISetFactory.Add(S, &V); |
| 244 | |
| 245 | // Create a new state with the old binding replaced. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 246 | return state->set<ConstNotEq>(sym, S); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St, |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 250 | SymbolRef sym) const { |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 251 | const ConstEqTy::data_type* T = St->get<ConstEq>(sym); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 252 | return T ? *T : NULL; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 255 | bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 256 | const llvm::APSInt& V) const { |
| 257 | |
| 258 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 259 | const ConstNotEqTy::data_type* T = St->get<ConstNotEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 260 | |
| 261 | // See if V is present in the NE-set. |
| 262 | return T ? T->contains(&V) : false; |
| 263 | } |
| 264 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 265 | bool BasicConstraintManager::isEqual(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 266 | const llvm::APSInt& V) const { |
| 267 | // Retrieve the EQ-set associated with the given symbol. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 268 | const ConstEqTy::data_type* T = St->get<ConstEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 269 | // See if V is present in the EQ-set. |
| 270 | return T ? **T == V : false; |
| 271 | } |
| 272 | |
Zhongxing Xu | 8fd9b35 | 2008-11-27 02:39:34 +0000 | [diff] [blame] | 273 | /// Scan all symbols referenced by the constraints. If the symbol is not alive |
| 274 | /// as marked in LSymbols, mark it as dead in DSymbols. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 275 | const GRState* |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 276 | BasicConstraintManager::RemoveDeadBindings(const GRState* state, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 277 | SymbolReaper& SymReaper) { |
| 278 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 279 | ConstEqTy CE = state->get<ConstEq>(); |
| 280 | ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 281 | |
| 282 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 283 | SymbolRef sym = I.getKey(); |
| 284 | if (SymReaper.maybeDead(sym)) CE = CEFactory.Remove(CE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 285 | } |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 286 | state = state->set<ConstEq>(CE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 288 | ConstNotEqTy CNE = state->get<ConstNotEq>(); |
| 289 | ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 290 | |
| 291 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 292 | SymbolRef sym = I.getKey(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 293 | if (SymReaper.maybeDead(sym)) CNE = CNEFactory.Remove(CNE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame^] | 296 | return state->set<ConstNotEq>(CNE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void BasicConstraintManager::print(const GRState* St, std::ostream& Out, |
| 300 | const char* nl, const char *sep) { |
| 301 | // Print equality constraints. |
| 302 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 303 | ConstEqTy CE = St->get<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 304 | |
| 305 | if (!CE.isEmpty()) { |
| 306 | Out << nl << sep << "'==' constraints:"; |
| 307 | |
| 308 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 309 | Out << nl << " $" << I.getKey(); |
| 310 | llvm::raw_os_ostream OS(Out); |
| 311 | OS << " : " << *I.getData(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Print != constraints. |
| 316 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 317 | ConstNotEqTy CNE = St->get<ConstNotEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 318 | |
| 319 | if (!CNE.isEmpty()) { |
| 320 | Out << nl << sep << "'!=' constraints:"; |
| 321 | |
| 322 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
| 323 | Out << nl << " $" << I.getKey() << " : "; |
| 324 | bool isFirst = true; |
| 325 | |
| 326 | GRState::IntSetTy::iterator J = I.getData().begin(), |
| 327 | EJ = I.getData().end(); |
| 328 | |
| 329 | for ( ; J != EJ; ++J) { |
| 330 | if (isFirst) isFirst = false; |
| 331 | else Out << ", "; |
| 332 | |
Zhongxing Xu | 7d94e26 | 2008-11-10 05:00:06 +0000 | [diff] [blame] | 333 | Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
Daniel Dunbar | 0e194dd | 2008-08-30 02:06:22 +0000 | [diff] [blame] | 337 | } |