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 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/ConstraintManager.h" |
| 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" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.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 | |
| 23 | namespace { |
| 24 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 25 | typedef llvm::ImmutableMap<SymbolID,GRState::IntSetTy> ConstNotEqTy; |
| 26 | typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy; |
| 27 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 28 | // BasicConstraintManager only tracks equality and inequality constraints of |
| 29 | // constants and integer variables. |
| 30 | class VISIBILITY_HIDDEN BasicConstraintManager : public ConstraintManager { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 31 | GRStateManager& StateMgr; |
| 32 | |
| 33 | public: |
| 34 | BasicConstraintManager(GRStateManager& statemgr) : StateMgr(statemgr) {} |
| 35 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 36 | virtual const GRState* Assume(const GRState* St, SVal Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 37 | bool Assumption, bool& isFeasible); |
| 38 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 39 | const GRState* Assume(const GRState* St, Loc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 40 | bool& isFeasible); |
| 41 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 42 | const GRState* AssumeAux(const GRState* St, Loc Cond,bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 43 | bool& isFeasible); |
| 44 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 45 | const GRState* Assume(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 46 | bool& isFeasible); |
| 47 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 48 | const GRState* AssumeAux(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 49 | bool& isFeasible); |
| 50 | |
| 51 | const GRState* AssumeSymInt(const GRState* St, bool Assumption, |
| 52 | const SymIntConstraint& C, bool& isFeasible); |
| 53 | |
| 54 | const GRState* AssumeSymNE(const GRState* St, SymbolID sym, |
| 55 | const llvm::APSInt& V, bool& isFeasible); |
| 56 | |
| 57 | const GRState* AssumeSymEQ(const GRState* St, SymbolID sym, |
| 58 | const llvm::APSInt& V, bool& isFeasible); |
| 59 | |
| 60 | const GRState* AssumeSymLT(const GRState* St, SymbolID sym, |
| 61 | const llvm::APSInt& V, bool& isFeasible); |
| 62 | |
| 63 | const GRState* AssumeSymGT(const GRState* St, SymbolID sym, |
| 64 | const llvm::APSInt& V, bool& isFeasible); |
| 65 | |
| 66 | const GRState* AssumeSymGE(const GRState* St, SymbolID sym, |
| 67 | const llvm::APSInt& V, bool& isFeasible); |
| 68 | |
| 69 | const GRState* AssumeSymLE(const GRState* St, SymbolID sym, |
| 70 | const llvm::APSInt& V, bool& isFeasible); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 71 | |
| 72 | const GRState* AddEQ(const GRState* St, SymbolID sym, const llvm::APSInt& V); |
| 73 | |
| 74 | const GRState* AddNE(const GRState* St, SymbolID sym, const llvm::APSInt& V); |
| 75 | |
| 76 | const llvm::APSInt* getSymVal(const GRState* St, SymbolID sym); |
| 77 | bool isNotEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const; |
| 78 | bool isEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const; |
| 79 | |
| 80 | const GRState* RemoveDeadBindings(const GRState* St, |
| 81 | StoreManager::LiveSymbolsTy& LSymbols, |
| 82 | StoreManager::DeadSymbolsTy& DSymbols); |
| 83 | |
| 84 | void print(const GRState* St, std::ostream& Out, |
| 85 | const char* nl, const char *sep); |
| 86 | }; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 87 | |
| 88 | } // end anonymous namespace |
| 89 | |
| 90 | ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr) |
| 91 | { |
| 92 | return new BasicConstraintManager(StateMgr); |
| 93 | } |
| 94 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 95 | const GRState* BasicConstraintManager::Assume(const GRState* St, SVal Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 96 | bool Assumption, bool& isFeasible) { |
| 97 | if (Cond.isUnknown()) { |
| 98 | isFeasible = true; |
| 99 | return St; |
| 100 | } |
| 101 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 102 | if (isa<NonLoc>(Cond)) |
| 103 | return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 104 | else |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 105 | return Assume(St, cast<Loc>(Cond), Assumption, isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 108 | const GRState* BasicConstraintManager::Assume(const GRState* St, Loc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 109 | bool Assumption, bool& isFeasible) { |
| 110 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 111 | // TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 112 | return St; |
| 113 | } |
| 114 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 115 | const GRState* BasicConstraintManager::AssumeAux(const GRState* St, Loc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 116 | bool Assumption, bool& isFeasible) { |
| 117 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 118 | |
| 119 | switch (Cond.getSubKind()) { |
| 120 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 121 | assert (false && "'Assume' not implemented for this Loc."); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 122 | return St; |
| 123 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 124 | case loc::SymbolValKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 125 | if (Assumption) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 126 | return AssumeSymNE(St, cast<loc::SymbolVal>(Cond).getSymbol(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 127 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 128 | else |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 129 | return AssumeSymEQ(St, cast<loc::SymbolVal>(Cond).getSymbol(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 130 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 131 | |
Ted Kremenek | c523471 | 2008-10-17 21:22:20 +0000 | [diff] [blame] | 132 | case loc::MemRegionKind: { |
| 133 | // FIXME: Should this go into the storemanager? |
| 134 | |
| 135 | const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion(); |
| 136 | |
| 137 | while (R) { |
| 138 | if (const SubRegion* SubR = dyn_cast<SubRegion>(R)) { |
| 139 | R = SubR->getSuperRegion(); |
| 140 | continue; |
| 141 | } |
| 142 | else if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 143 | return AssumeAux(St, loc::SymbolVal(SymR->getSymbol()), Assumption, |
| 144 | isFeasible); |
| 145 | |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | // FALL-THROUGH. |
| 150 | } |
| 151 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 152 | case loc::FuncValKind: |
| 153 | case loc::GotoLabelKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 154 | isFeasible = Assumption; |
| 155 | return St; |
| 156 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 157 | case loc::ConcreteIntKind: { |
| 158 | bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 159 | isFeasible = b ? Assumption : !Assumption; |
| 160 | return St; |
| 161 | } |
| 162 | } // end switch |
| 163 | } |
| 164 | |
| 165 | const GRState* |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 166 | BasicConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 167 | bool& isFeasible) { |
| 168 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 169 | // TF->EvalAssume() does nothing now. |
| 170 | return St; |
| 171 | } |
| 172 | |
| 173 | const GRState* |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 174 | BasicConstraintManager::AssumeAux(const GRState* St,NonLoc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 175 | bool Assumption, bool& isFeasible) { |
| 176 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 177 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 178 | |
| 179 | switch (Cond.getSubKind()) { |
| 180 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 181 | assert(false && "'Assume' not implemented for this NonLoc"); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 182 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 183 | case nonloc::SymbolValKind: { |
| 184 | nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 185 | SymbolID sym = SV.getSymbol(); |
| 186 | |
| 187 | if (Assumption) |
| 188 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 189 | isFeasible); |
| 190 | else |
| 191 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 192 | isFeasible); |
| 193 | } |
| 194 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 195 | case nonloc::SymIntConstraintValKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 196 | return |
| 197 | AssumeSymInt(St, Assumption, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 198 | cast<nonloc::SymIntConstraintVal>(Cond).getConstraint(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 199 | isFeasible); |
| 200 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 201 | case nonloc::ConcreteIntKind: { |
| 202 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 203 | isFeasible = b ? Assumption : !Assumption; |
| 204 | return St; |
| 205 | } |
| 206 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 207 | case nonloc::LocAsIntegerKind: |
| 208 | return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 209 | Assumption, isFeasible); |
| 210 | } // end switch |
| 211 | } |
| 212 | |
| 213 | const GRState* |
| 214 | BasicConstraintManager::AssumeSymInt(const GRState* St, bool Assumption, |
| 215 | const SymIntConstraint& C, bool& isFeasible) { |
| 216 | |
| 217 | switch (C.getOpcode()) { |
| 218 | default: |
| 219 | // No logic yet for other operators. |
| 220 | isFeasible = true; |
| 221 | return St; |
| 222 | |
| 223 | case BinaryOperator::EQ: |
| 224 | if (Assumption) |
| 225 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 226 | else |
| 227 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 228 | |
| 229 | case BinaryOperator::NE: |
| 230 | if (Assumption) |
| 231 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 232 | else |
| 233 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 234 | |
Zhongxing Xu | 94b8312 | 2008-09-19 06:07:59 +0000 | [diff] [blame] | 235 | case BinaryOperator::GT: |
| 236 | if (Assumption) |
| 237 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 238 | else |
| 239 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 240 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 241 | case BinaryOperator::GE: |
| 242 | if (Assumption) |
| 243 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 244 | else |
| 245 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 246 | |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 247 | case BinaryOperator::LT: |
| 248 | if (Assumption) |
| 249 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 250 | else |
| 251 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 252 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 253 | case BinaryOperator::LE: |
| 254 | if (Assumption) |
| 255 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 256 | else |
| 257 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 258 | } // end switch |
| 259 | } |
| 260 | |
| 261 | const GRState* |
| 262 | BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolID sym, |
| 263 | const llvm::APSInt& V, bool& isFeasible) { |
| 264 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 265 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 266 | isFeasible = (*X != V); |
| 267 | return St; |
| 268 | } |
| 269 | |
| 270 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 271 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 272 | isFeasible = true; |
| 273 | return St; |
| 274 | } |
| 275 | |
| 276 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 277 | // Make that assumption. |
| 278 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 279 | return AddNE(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | const GRState* |
| 283 | BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolID sym, |
| 284 | const llvm::APSInt& V, bool& isFeasible) { |
| 285 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 286 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 287 | isFeasible = *X == V; |
| 288 | return St; |
| 289 | } |
| 290 | |
| 291 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 292 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 293 | isFeasible = false; |
| 294 | return St; |
| 295 | } |
| 296 | |
| 297 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 298 | // Make that assumption. |
| 299 | |
| 300 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 301 | return AddEQ(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // These logic will be handled in another ConstraintManager. |
| 305 | const GRState* |
| 306 | BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolID sym, |
| 307 | const llvm::APSInt& V, bool& isFeasible) { |
| 308 | |
| 309 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
| 310 | return AssumeSymNE(St, sym, V, isFeasible); |
| 311 | } |
| 312 | |
| 313 | const GRState* |
| 314 | BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolID sym, |
| 315 | const llvm::APSInt& V, bool& isFeasible) { |
| 316 | |
| 317 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
| 318 | return AssumeSymNE(St, sym, V, isFeasible); |
| 319 | } |
| 320 | |
| 321 | const GRState* |
| 322 | BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolID sym, |
| 323 | const llvm::APSInt& V, bool& isFeasible) { |
| 324 | |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 325 | // 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] | 326 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 327 | isFeasible = *X >= V; |
| 328 | return St; |
| 329 | } |
| 330 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 331 | isFeasible = !isNotEqual(St, sym, V) || |
| 332 | (V != llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned())); |
| 333 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 334 | return St; |
| 335 | } |
| 336 | |
| 337 | const GRState* |
| 338 | BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym, |
| 339 | const llvm::APSInt& V, bool& isFeasible) { |
| 340 | |
| 341 | // FIXME: Primitive logic for now. Only reject a path if the value of |
| 342 | // sym is a constant X and !(X <= V). |
| 343 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 344 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 345 | isFeasible = *X <= V; |
| 346 | return St; |
| 347 | } |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 348 | |
| 349 | isFeasible = !isNotEqual(St, sym, V) || |
| 350 | (V != llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned())); |
| 351 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 352 | return St; |
| 353 | } |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 354 | |
| 355 | static int ConstEqTyIndex = 0; |
| 356 | static int ConstNotEqTyIndex = 0; |
| 357 | |
| 358 | namespace clang { |
| 359 | template<> |
| 360 | struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> { |
| 361 | static inline void* GDMIndex() { return &ConstNotEqTyIndex; } |
| 362 | }; |
| 363 | |
| 364 | template<> |
| 365 | struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> { |
| 366 | static inline void* GDMIndex() { return &ConstEqTyIndex; } |
| 367 | }; |
| 368 | } |
| 369 | |
| 370 | const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym, |
| 371 | const llvm::APSInt& V) { |
| 372 | // Create a new state with the old binding replaced. |
| 373 | GRStateRef state(St, StateMgr); |
| 374 | return state.set<ConstEqTy>(sym, &V); |
| 375 | } |
| 376 | |
| 377 | const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym, |
| 378 | const llvm::APSInt& V) { |
| 379 | GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator()); |
| 380 | GRStateRef state(St, StateMgr); |
| 381 | |
| 382 | // First, retrieve the NE-set associated with the given symbol. |
| 383 | ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym); |
| 384 | GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
| 385 | |
| 386 | |
| 387 | // Now add V to the NE set. |
| 388 | S = ISetFactory.Add(S, &V); |
| 389 | |
| 390 | // Create a new state with the old binding replaced. |
| 391 | return state.set<ConstNotEqTy>(sym, S); |
| 392 | } |
| 393 | |
| 394 | const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St, |
| 395 | SymbolID sym) { |
| 396 | const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym); |
| 397 | return T ? *T : NULL; |
| 398 | } |
| 399 | |
| 400 | bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym, |
| 401 | const llvm::APSInt& V) const { |
| 402 | |
| 403 | // Retrieve the NE-set associated with the given symbol. |
| 404 | const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym); |
| 405 | |
| 406 | // See if V is present in the NE-set. |
| 407 | return T ? T->contains(&V) : false; |
| 408 | } |
| 409 | |
| 410 | bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym, |
| 411 | const llvm::APSInt& V) const { |
| 412 | // Retrieve the EQ-set associated with the given symbol. |
| 413 | const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym); |
| 414 | // See if V is present in the EQ-set. |
| 415 | return T ? **T == V : false; |
| 416 | } |
| 417 | |
| 418 | const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St, |
| 419 | StoreManager::LiveSymbolsTy& LSymbols, |
| 420 | StoreManager::DeadSymbolsTy& DSymbols) { |
| 421 | GRStateRef state(St, StateMgr); |
| 422 | ConstEqTy CE = state.get<ConstEqTy>(); |
| 423 | ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>(); |
| 424 | |
| 425 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 426 | SymbolID sym = I.getKey(); |
| 427 | if (!LSymbols.count(sym)) { |
| 428 | DSymbols.insert(sym); |
| 429 | CE = CEFactory.Remove(CE, sym); |
| 430 | } |
| 431 | } |
| 432 | state = state.set<ConstEqTy>(CE); |
| 433 | |
| 434 | ConstNotEqTy CNE = state.get<ConstNotEqTy>(); |
| 435 | ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>(); |
| 436 | |
| 437 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
| 438 | SymbolID sym = I.getKey(); |
| 439 | if (!LSymbols.count(sym)) { |
| 440 | DSymbols.insert(sym); |
| 441 | CNE = CNEFactory.Remove(CNE, sym); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return state.set<ConstNotEqTy>(CNE); |
| 446 | } |
| 447 | |
| 448 | void BasicConstraintManager::print(const GRState* St, std::ostream& Out, |
| 449 | const char* nl, const char *sep) { |
| 450 | // Print equality constraints. |
| 451 | |
| 452 | ConstEqTy CE = St->get<ConstEqTy>(); |
| 453 | |
| 454 | if (!CE.isEmpty()) { |
| 455 | Out << nl << sep << "'==' constraints:"; |
| 456 | |
| 457 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 458 | Out << nl << " $" << I.getKey(); |
| 459 | llvm::raw_os_ostream OS(Out); |
| 460 | OS << " : " << *I.getData(); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Print != constraints. |
| 465 | |
| 466 | ConstNotEqTy CNE = St->get<ConstNotEqTy>(); |
| 467 | |
| 468 | if (!CNE.isEmpty()) { |
| 469 | Out << nl << sep << "'!=' constraints:"; |
| 470 | |
| 471 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
| 472 | Out << nl << " $" << I.getKey() << " : "; |
| 473 | bool isFirst = true; |
| 474 | |
| 475 | GRState::IntSetTy::iterator J = I.getData().begin(), |
| 476 | EJ = I.getData().end(); |
| 477 | |
| 478 | for ( ; J != EJ; ++J) { |
| 479 | if (isFirst) isFirst = false; |
| 480 | else Out << ", "; |
| 481 | |
Zhongxing Xu | 7d94e26 | 2008-11-10 05:00:06 +0000 | [diff] [blame^] | 482 | Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | } |
Daniel Dunbar | 0e194dd | 2008-08-30 02:06:22 +0000 | [diff] [blame] | 486 | } |