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" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame^] | 16 | #include "clang/GR/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/GR/PathSensitive/GRState.h" |
| 18 | #include "clang/GR/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. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 34 | case BO_And: |
| 35 | case BO_Or: |
| 36 | case BO_Xor: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 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. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 40 | case BO_Mul: |
| 41 | case BO_Div: |
| 42 | case BO_Rem: |
| 43 | case BO_Shl: |
| 44 | case BO_Shr: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 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 | 28f47b9 | 2010-12-01 22:16:56 +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 | 28f47b9 | 2010-12-01 22:16:56 +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 | 28f47b9 | 2010-12-01 22:16:56 +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 | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 67 | const GRState *SimpleConstraintManager::assume(const GRState *state, Loc cond, |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 68 | bool assumption) { |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 69 | state = assumeAux(state, cond, assumption); |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 70 | return SU.ProcessAssume(state, cond, assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 73 | const GRState *SimpleConstraintManager::assumeAux(const GRState *state, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 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) |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +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 |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +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 | 28f47b9 | 2010-12-01 22:16:56 +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) { |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 118 | state = assumeAux(state, cond, assumption); |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 124 | // the only place it's used. (This code was copied from SimpleSValBuilder.cpp.) |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 125 | switch (op) { |
| 126 | default: |
| 127 | assert(false && "Invalid opcode."); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 128 | case BO_LT: return BO_GE; |
| 129 | case BO_GT: return BO_LE; |
| 130 | case BO_LE: return BO_GT; |
| 131 | case BO_GE: return BO_LT; |
| 132 | case BO_EQ: return BO_NE; |
| 133 | case BO_NE: return BO_EQ; |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 137 | const GRState *SimpleConstraintManager::assumeAux(const GRState *state, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 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) |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 162 | return assumeSymNE(state, sym, zero, zero); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 163 | else |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 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 | BinaryOperator::Opcode op = SE->getOpcode(); |
Jordy Rose | 5ca129c | 2010-06-27 01:20:56 +0000 | [diff] [blame] | 177 | // Implicitly compare non-comparison expressions to 0. |
| 178 | if (!BinaryOperator::isComparisonOp(op)) { |
| 179 | QualType T = SymMgr.getType(SE); |
| 180 | const llvm::APSInt &zero = BasicVals.getValue(0, T); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 181 | op = (Assumption ? BO_NE : BO_EQ); |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 182 | return assumeSymRel(state, SE, op, zero); |
Jordy Rose | 5ca129c | 2010-06-27 01:20:56 +0000 | [diff] [blame] | 183 | } |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 184 | |
| 185 | // From here on out, op is the real comparison we'll be testing. |
| 186 | if (!Assumption) |
| 187 | op = NegateComparison(op); |
| 188 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 189 | return assumeSymRel(state, SE->getLHS(), op, SE->getRHS()); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 190 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 191 | |
| 192 | case nonloc::ConcreteIntKind: { |
| 193 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 194 | bool isFeasible = b ? Assumption : !Assumption; |
| 195 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | case nonloc::LocAsIntegerKind: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 199 | return assumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 200 | Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 201 | } // end switch |
| 202 | } |
| 203 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 204 | const GRState *SimpleConstraintManager::assumeSymRel(const GRState *state, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 205 | const SymExpr *LHS, |
| 206 | BinaryOperator::Opcode op, |
| 207 | const llvm::APSInt& Int) { |
| 208 | assert(BinaryOperator::isComparisonOp(op) && |
| 209 | "Non-comparison ops should be rewritten as comparisons to zero."); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 210 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 211 | // We only handle simple comparisons of the form "$sym == constant" |
| 212 | // or "($sym+constant1) == constant2". |
| 213 | // The adjustment is "constant1" in the above expression. It's used to |
| 214 | // "slide" the solution range around for modular arithmetic. For example, |
| 215 | // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which |
| 216 | // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to |
| 217 | // the subclasses of SimpleConstraintManager to handle the adjustment. |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 218 | llvm::APSInt Adjustment; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 219 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 220 | // First check if the LHS is a simple symbol reference. |
| 221 | SymbolRef Sym = dyn_cast<SymbolData>(LHS); |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 222 | if (Sym) { |
| 223 | Adjustment = 0; |
| 224 | } else { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 225 | // Next, see if it's a "($sym+constant1)" expression. |
| 226 | const SymIntExpr *SE = dyn_cast<SymIntExpr>(LHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 228 | // We don't handle "($sym1+$sym2)". |
| 229 | // Give up and assume the constraint is feasible. |
| 230 | if (!SE) |
| 231 | return state; |
| 232 | |
| 233 | // We don't handle "(<expr>+constant1)". |
| 234 | // Give up and assume the constraint is feasible. |
| 235 | Sym = dyn_cast<SymbolData>(SE->getLHS()); |
| 236 | if (!Sym) |
| 237 | return state; |
| 238 | |
| 239 | // Get the constant out of the expression "($sym+constant1)". |
| 240 | switch (SE->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 241 | case BO_Add: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 242 | Adjustment = SE->getRHS(); |
| 243 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 244 | case BO_Sub: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 245 | Adjustment = -SE->getRHS(); |
| 246 | break; |
| 247 | default: |
| 248 | // We don't handle non-additive operators. |
| 249 | // Give up and assume the constraint is feasible. |
| 250 | return state; |
| 251 | } |
| 252 | } |
| 253 | |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 254 | // FIXME: This next section is a hack. It silently converts the integers to |
| 255 | // be of the same type as the symbol, which is not always correct. Really the |
| 256 | // comparisons should be performed using the Int's type, then mapped back to |
| 257 | // the symbol's range of values. |
| 258 | GRStateManager &StateMgr = state->getStateManager(); |
| 259 | ASTContext &Ctx = StateMgr.getContext(); |
| 260 | |
| 261 | QualType T = Sym->getType(Ctx); |
| 262 | assert(T->isIntegerType() || Loc::IsLocType(T)); |
| 263 | unsigned bitwidth = Ctx.getTypeSize(T); |
| 264 | bool isSymUnsigned = T->isUnsignedIntegerType() || Loc::IsLocType(T); |
| 265 | |
| 266 | // Convert the adjustment. |
| 267 | Adjustment.setIsUnsigned(isSymUnsigned); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 268 | Adjustment = Adjustment.extOrTrunc(bitwidth); |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 269 | |
| 270 | // Convert the right-hand side integer. |
| 271 | llvm::APSInt ConvertedInt(Int, isSymUnsigned); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 272 | ConvertedInt = ConvertedInt.extOrTrunc(bitwidth); |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 273 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 274 | switch (op) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 275 | default: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 276 | // No logic yet for other operators. assume the constraint is feasible. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 277 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 278 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 279 | case BO_EQ: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 280 | return assumeSymEQ(state, Sym, ConvertedInt, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 281 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 282 | case BO_NE: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 283 | return assumeSymNE(state, Sym, ConvertedInt, Adjustment); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 284 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 285 | case BO_GT: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 286 | return assumeSymGT(state, Sym, ConvertedInt, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 287 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 288 | case BO_GE: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 289 | return assumeSymGE(state, Sym, ConvertedInt, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 290 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 291 | case BO_LT: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 292 | return assumeSymLT(state, Sym, ConvertedInt, Adjustment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 294 | case BO_LE: |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 295 | return assumeSymLE(state, Sym, ConvertedInt, Adjustment); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 296 | } // end switch |
| 297 | } |
| 298 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 299 | } // end of namespace clang |