Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 1 | //== SimpleConstraintManager.cpp --------------------------------*- 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 SimpleConstraintManager, a class that holds code shared |
| 11 | // between BasicConstraintManager and RangeConstraintManager. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SimpleConstraintManager.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Checker/PathSensitive/GRState.h" |
| 18 | #include "clang/Checker/PathSensitive/Checker.h" |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 19 | |
| 20 | namespace clang { |
| 21 | |
| 22 | SimpleConstraintManager::~SimpleConstraintManager() {} |
| 23 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 24 | bool SimpleConstraintManager::canReasonAbout(SVal X) const { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 25 | if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) { |
| 26 | const SymExpr *SE = SymVal->getSymbolicExpression(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 28 | if (isa<SymbolData>(SE)) |
| 29 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 31 | if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { |
| 32 | switch (SIE->getOpcode()) { |
| 33 | // We don't reason yet about bitwise-constraints on symbolic values. |
| 34 | case BinaryOperator::And: |
| 35 | case BinaryOperator::Or: |
| 36 | case BinaryOperator::Xor: |
| 37 | return false; |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 38 | // We don't reason yet about these arithmetic constraints on |
| 39 | // symbolic values. |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 40 | case BinaryOperator::Mul: |
| 41 | case BinaryOperator::Div: |
| 42 | case BinaryOperator::Rem: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 43 | case BinaryOperator::Shl: |
| 44 | case BinaryOperator::Shr: |
| 45 | return false; |
| 46 | // All other cases. |
| 47 | default: |
| 48 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | } |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return false; |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 58 | const GRState *SimpleConstraintManager::Assume(const GRState *state, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 59 | DefinedSVal Cond, |
| 60 | bool Assumption) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 61 | if (isa<NonLoc>(Cond)) |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 62 | return Assume(state, cast<NonLoc>(Cond), Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 63 | else |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 64 | return Assume(state, cast<Loc>(Cond), Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 67 | const GRState *SimpleConstraintManager::Assume(const GRState *state, Loc cond, |
| 68 | bool assumption) { |
| 69 | state = AssumeAux(state, cond, assumption); |
| 70 | return SU.ProcessAssume(state, cond, assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 73 | const GRState *SimpleConstraintManager::AssumeAux(const GRState *state, |
| 74 | Loc Cond, bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 76 | BasicValueFactory &BasicVals = state->getBasicVals(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 77 | |
| 78 | switch (Cond.getSubKind()) { |
| 79 | default: |
| 80 | assert (false && "'Assume' not implemented for this Loc."); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 81 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 83 | case loc::MemRegionKind: { |
| 84 | // FIXME: Should this go into the storemanager? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 86 | const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion(); |
| 87 | const SubRegion *SubR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 88 | |
| 89 | while (SubR) { |
| 90 | // FIXME: now we only find the first symbolic region. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 91 | if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 92 | const llvm::APSInt &zero = BasicVals.getZeroWithPtrWidth(); |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 93 | if (Assumption) |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 94 | return AssumeSymNE(state, SymR->getSymbol(), zero, zero); |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 95 | else |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 96 | return AssumeSymEQ(state, SymR->getSymbol(), zero, zero); |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 98 | SubR = dyn_cast<SubRegion>(SubR->getSuperRegion()); |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 101 | // FALL-THROUGH. |
| 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 104 | case loc::GotoLabelKind: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 105 | return Assumption ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 106 | |
| 107 | case loc::ConcreteIntKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 109 | bool isFeasible = b ? Assumption : !Assumption; |
| 110 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 111 | } |
| 112 | } // end switch |
| 113 | } |
| 114 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 115 | const GRState *SimpleConstraintManager::Assume(const GRState *state, |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 116 | NonLoc cond, |
| 117 | bool assumption) { |
| 118 | state = AssumeAux(state, cond, assumption); |
| 119 | return SU.ProcessAssume(state, cond, assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 122 | static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { |
| 123 | // FIXME: This should probably be part of BinaryOperator, since this isn't |
| 124 | // the only place it's used. (This code was copied from SimpleSValuator.cpp.) |
| 125 | switch (op) { |
| 126 | default: |
| 127 | assert(false && "Invalid opcode."); |
| 128 | case BinaryOperator::LT: return BinaryOperator::GE; |
| 129 | case BinaryOperator::GT: return BinaryOperator::LE; |
| 130 | case BinaryOperator::LE: return BinaryOperator::GT; |
| 131 | case BinaryOperator::GE: return BinaryOperator::LT; |
| 132 | case BinaryOperator::EQ: return BinaryOperator::NE; |
| 133 | case BinaryOperator::NE: return BinaryOperator::EQ; |
| 134 | } |
| 135 | } |
| 136 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 137 | const GRState *SimpleConstraintManager::AssumeAux(const GRState *state, |
| 138 | NonLoc Cond, |
| 139 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 141 | // We cannot reason about SymSymExprs, |
| 142 | // and can only reason about some SymIntExprs. |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 143 | if (!canReasonAbout(Cond)) { |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 144 | // Just return the current state indicating that the path is feasible. |
| 145 | // This may be an over-approximation of what is possible. |
| 146 | return state; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | } |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 149 | BasicValueFactory &BasicVals = state->getBasicVals(); |
| 150 | SymbolManager &SymMgr = state->getSymbolManager(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 151 | |
| 152 | switch (Cond.getSubKind()) { |
| 153 | default: |
| 154 | assert(false && "'Assume' not implemented for this NonLoc"); |
| 155 | |
| 156 | case nonloc::SymbolValKind: { |
| 157 | nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond); |
| 158 | SymbolRef sym = SV.getSymbol(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | QualType T = SymMgr.getType(sym); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 160 | const llvm::APSInt &zero = BasicVals.getValue(0, T); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 161 | if (Assumption) |
| 162 | return AssumeSymNE(state, sym, zero, zero); |
| 163 | else |
| 164 | return AssumeSymEQ(state, sym, zero, zero); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 167 | case nonloc::SymExprValKind: { |
| 168 | nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond); |
Ted Kremenek | 8041747 | 2009-09-25 00:18:15 +0000 | [diff] [blame] | 169 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 170 | // For now, we only handle expressions whose RHS is an integer. |
| 171 | // All other expressions are assumed to be feasible. |
| 172 | const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()); |
| 173 | if (!SE) |
| 174 | return state; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 176 | GRStateManager &StateMgr = state->getStateManager(); |
| 177 | ASTContext &Ctx = StateMgr.getContext(); |
| 178 | BasicValueFactory &BasicVals = StateMgr.getBasicVals(); |
| 179 | |
| 180 | // FIXME: This is a hack. It silently converts the RHS integer to be |
| 181 | // of the same type as on the left side. This should be removed once |
| 182 | // we support truncation/extension of symbolic values. |
| 183 | const SymExpr *LHS = SE->getLHS(); |
| 184 | QualType LHSType = LHS->getType(Ctx); |
| 185 | const llvm::APSInt &RHS = BasicVals.Convert(LHSType, SE->getRHS()); |
| 186 | |
| 187 | BinaryOperator::Opcode op = SE->getOpcode(); |
| 188 | // FIXME: We should implicitly compare non-comparison expressions to 0. |
| 189 | if (!BinaryOperator::isComparisonOp(op)) |
| 190 | return state; |
| 191 | |
| 192 | // From here on out, op is the real comparison we'll be testing. |
| 193 | if (!Assumption) |
| 194 | op = NegateComparison(op); |
| 195 | |
| 196 | return AssumeSymRel(state, LHS, op, RHS); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 197 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 198 | |
| 199 | case nonloc::ConcreteIntKind: { |
| 200 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 201 | bool isFeasible = b ? Assumption : !Assumption; |
| 202 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | case nonloc::LocAsIntegerKind: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 206 | return AssumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
| 207 | Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 208 | } // end switch |
| 209 | } |
| 210 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 211 | const GRState *SimpleConstraintManager::AssumeSymRel(const GRState *state, |
| 212 | const SymExpr *LHS, |
| 213 | BinaryOperator::Opcode op, |
| 214 | const llvm::APSInt& Int) { |
| 215 | assert(BinaryOperator::isComparisonOp(op) && |
| 216 | "Non-comparison ops should be rewritten as comparisons to zero."); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 217 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 218 | // We only handle simple comparisons of the form "$sym == constant" |
| 219 | // or "($sym+constant1) == constant2". |
| 220 | // The adjustment is "constant1" in the above expression. It's used to |
| 221 | // "slide" the solution range around for modular arithmetic. For example, |
| 222 | // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which |
| 223 | // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to |
| 224 | // the subclasses of SimpleConstraintManager to handle the adjustment. |
| 225 | llvm::APSInt Adjustment(Int.getBitWidth(), Int.isUnsigned()); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 226 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 227 | // First check if the LHS is a simple symbol reference. |
| 228 | SymbolRef Sym = dyn_cast<SymbolData>(LHS); |
| 229 | if (!Sym) { |
| 230 | // Next, see if it's a "($sym+constant1)" expression. |
| 231 | const SymIntExpr *SE = dyn_cast<SymIntExpr>(LHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 233 | // We don't handle "($sym1+$sym2)". |
| 234 | // Give up and assume the constraint is feasible. |
| 235 | if (!SE) |
| 236 | return state; |
| 237 | |
| 238 | // We don't handle "(<expr>+constant1)". |
| 239 | // Give up and assume the constraint is feasible. |
| 240 | Sym = dyn_cast<SymbolData>(SE->getLHS()); |
| 241 | if (!Sym) |
| 242 | return state; |
| 243 | |
| 244 | // Get the constant out of the expression "($sym+constant1)". |
| 245 | switch (SE->getOpcode()) { |
| 246 | case BinaryOperator::Add: |
| 247 | Adjustment = SE->getRHS(); |
| 248 | break; |
| 249 | case BinaryOperator::Sub: |
| 250 | Adjustment = -SE->getRHS(); |
| 251 | break; |
| 252 | default: |
| 253 | // We don't handle non-additive operators. |
| 254 | // Give up and assume the constraint is feasible. |
| 255 | return state; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | switch (op) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 260 | default: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 261 | // No logic yet for other operators. Assume the constraint is feasible. |
| 262 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 263 | |
| 264 | case BinaryOperator::EQ: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 265 | return AssumeSymEQ(state, Sym, Int, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 266 | |
| 267 | case BinaryOperator::NE: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 268 | return AssumeSymNE(state, Sym, Int, Adjustment); |
| 269 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 270 | case BinaryOperator::GT: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 271 | return AssumeSymGT(state, Sym, Int, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 272 | |
| 273 | case BinaryOperator::GE: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 274 | return AssumeSymGE(state, Sym, Int, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 275 | |
| 276 | case BinaryOperator::LT: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 277 | return AssumeSymLT(state, Sym, Int, Adjustment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 279 | case BinaryOperator::LE: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 280 | return AssumeSymLE(state, Sym, Int, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 281 | } // end switch |
| 282 | } |
| 283 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 284 | const GRState *SimpleConstraintManager::AssumeInBound(const GRState *state, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 285 | DefinedSVal Idx, |
| 286 | DefinedSVal UpperBound, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | bool Assumption) { |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 289 | // Only support ConcreteInt for now. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 290 | if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))) |
| 291 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | f1b8227 | 2009-06-18 23:20:05 +0000 | [diff] [blame] | 293 | const llvm::APSInt& Zero = state->getBasicVals().getZeroWithPtrWidth(false); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 294 | llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue(); |
| 295 | // IdxV might be too narrow. |
| 296 | if (IdxV.getBitWidth() < Zero.getBitWidth()) |
| 297 | IdxV.extend(Zero.getBitWidth()); |
| 298 | // UBV might be too narrow, too. |
| 299 | llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue(); |
| 300 | if (UBV.getBitWidth() < Zero.getBitWidth()) |
| 301 | UBV.extend(Zero.getBitWidth()); |
| 302 | |
| 303 | bool InBound = (Zero <= IdxV) && (IdxV < UBV); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 304 | bool isFeasible = Assumption ? InBound : !InBound; |
| 305 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | } // end of namespace clang |