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