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