Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 1 | // SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*- |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 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 | // |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 10 | // This file defines SimpleSValBuilder, a basic implementation of SValBuilder. |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 14 | #include "clang/Checker/PathSensitive/SValBuilder.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
| 19 | namespace { |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 20 | class SimpleSValBuilder : public SValBuilder { |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 21 | protected: |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 22 | virtual SVal evalCastNL(NonLoc val, QualType castTy); |
| 23 | virtual SVal evalCastL(Loc val, QualType castTy); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 24 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 25 | public: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 26 | SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, |
| 27 | GRStateManager &stateMgr) |
| 28 | : SValBuilder(alloc, context, stateMgr) {} |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 29 | virtual ~SimpleSValBuilder() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 31 | virtual SVal evalMinus(NonLoc val); |
| 32 | virtual SVal evalComplement(NonLoc val); |
| 33 | virtual SVal evalBinOpNN(const GRState *state, BinaryOperator::Opcode op, |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 34 | NonLoc lhs, NonLoc rhs, QualType resultTy); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 35 | virtual SVal evalBinOpLL(const GRState *state, BinaryOperator::Opcode op, |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 36 | Loc lhs, Loc rhs, QualType resultTy); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 37 | virtual SVal evalBinOpLN(const GRState *state, BinaryOperator::Opcode op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 38 | Loc lhs, NonLoc rhs, QualType resultTy); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 40 | /// getKnownValue - evaluates a given SVal. If the SVal has only one possible |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 41 | /// (integer) value, that value is returned. Otherwise, returns NULL. |
| 42 | virtual const llvm::APSInt *getKnownValue(const GRState *state, SVal V); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 43 | |
| 44 | SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op, |
| 45 | const llvm::APSInt &RHS, QualType resultTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | }; |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 47 | } // end anonymous namespace |
| 48 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 49 | SValBuilder *clang::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, |
| 50 | ASTContext &context, |
| 51 | GRStateManager &stateMgr) { |
| 52 | return new SimpleSValBuilder(alloc, context, stateMgr); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // Transfer function for Casts. |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 59 | SVal SimpleSValBuilder::evalCastNL(NonLoc val, QualType castTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | dd66114 | 2009-07-20 21:39:27 +0000 | [diff] [blame] | 61 | bool isLocType = Loc::IsLocType(castTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 63 | if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) { |
| 64 | if (isLocType) |
Ted Kremenek | dd66114 | 2009-07-20 21:39:27 +0000 | [diff] [blame] | 65 | return LI->getLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 67 | // FIXME: Correctly support promotions/truncations. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 68 | unsigned castSize = Context.getTypeSize(castTy); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 69 | if (castSize == LI->getNumBits()) |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 70 | return val; |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 71 | return makeLocAsInteger(LI->getLoc(), castSize); |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (const SymExpr *se = val.getAsSymbolicExpression()) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 75 | QualType T = Context.getCanonicalType(se->getType(Context)); |
| 76 | if (T == Context.getCanonicalType(castTy)) |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 77 | return val; |
Ted Kremenek | 8041747 | 2009-09-25 00:18:15 +0000 | [diff] [blame] | 78 | |
| 79 | // FIXME: Remove this hack when we support symbolic truncation/extension. |
| 80 | // HACK: If both castTy and T are integers, ignore the cast. This is |
| 81 | // not a permanent solution. Eventually we want to precisely handle |
| 82 | // extension/truncation of symbolic integers. This prevents us from losing |
| 83 | // precision when we assign 'x = y' and 'y' is symbolic and x and y are |
| 84 | // different integer types. |
| 85 | if (T->isIntegerType() && castTy->isIntegerType()) |
| 86 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 88 | return UnknownVal(); |
| 89 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 91 | if (!isa<nonloc::ConcreteInt>(val)) |
| 92 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 94 | // Only handle casts from integers to integers. |
| 95 | if (!isLocType && !castTy->isIntegerType()) |
| 96 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 98 | llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue(); |
| 99 | i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy)); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 100 | i = i.extOrTrunc(Context.getTypeSize(castTy)); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 101 | |
| 102 | if (isLocType) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 103 | return makeIntLocVal(i); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 104 | else |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 105 | return makeIntVal(i); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 108 | SVal SimpleSValBuilder::evalCastL(Loc val, QualType castTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 110 | // Casts from pointers -> pointers, just return the lval. |
| 111 | // |
| 112 | // Casts from pointers -> references, just return the lval. These |
| 113 | // can be introduced by the frontend for corner cases, e.g |
| 114 | // casting from va_list* to __builtin_va_list&. |
| 115 | // |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 116 | if (Loc::IsLocType(castTy) || castTy->isReferenceType()) |
| 117 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 119 | // FIXME: Handle transparent unions where a value can be "transparently" |
| 120 | // lifted into a union type. |
| 121 | if (castTy->isUnionType()) |
| 122 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame] | 124 | if (castTy->isIntegerType()) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 125 | unsigned BitWidth = Context.getTypeSize(castTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame] | 127 | if (!isa<loc::ConcreteInt>(val)) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 128 | return makeLocAsInteger(val, BitWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame] | 130 | llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue(); |
| 131 | i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy)); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 132 | i = i.extOrTrunc(BitWidth); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 133 | return makeIntVal(i); |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // All other cases: return 'UnknownVal'. This includes casting pointers |
| 137 | // to floats, which is probably badness it itself, but this is a good |
| 138 | // intermediate solution until we do something better. |
| 139 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | // Transfer function for unary operators. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 146 | SVal SimpleSValBuilder::evalMinus(NonLoc val) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | switch (val.getSubKind()) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 148 | case nonloc::ConcreteIntKind: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 149 | return cast<nonloc::ConcreteInt>(val).evalMinus(*this); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 150 | default: |
| 151 | return UnknownVal(); |
| 152 | } |
| 153 | } |
| 154 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 155 | SVal SimpleSValBuilder::evalComplement(NonLoc X) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 156 | switch (X.getSubKind()) { |
| 157 | case nonloc::ConcreteIntKind: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 158 | return cast<nonloc::ConcreteInt>(X).evalComplement(*this); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 159 | default: |
| 160 | return UnknownVal(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | // Transfer function for binary operators. |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { |
| 169 | switch (op) { |
| 170 | default: |
| 171 | assert(false && "Invalid opcode."); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 172 | case BO_LT: return BO_GE; |
| 173 | case BO_GT: return BO_LE; |
| 174 | case BO_LE: return BO_GT; |
| 175 | case BO_GE: return BO_LT; |
| 176 | case BO_EQ: return BO_NE; |
| 177 | case BO_NE: return BO_EQ; |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 181 | static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) { |
| 182 | switch (op) { |
| 183 | default: |
| 184 | assert(false && "Invalid opcode."); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 185 | case BO_LT: return BO_GT; |
| 186 | case BO_GT: return BO_LT; |
| 187 | case BO_LE: return BO_GE; |
| 188 | case BO_GE: return BO_LE; |
| 189 | case BO_EQ: |
| 190 | case BO_NE: |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 191 | return op; |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 192 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 195 | SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS, |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 196 | BinaryOperator::Opcode op, |
| 197 | const llvm::APSInt &RHS, |
| 198 | QualType resultTy) { |
| 199 | bool isIdempotent = false; |
| 200 | |
| 201 | // Check for a few special cases with known reductions first. |
| 202 | switch (op) { |
| 203 | default: |
| 204 | // We can't reduce this case; just treat it normally. |
| 205 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 206 | case BO_Mul: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 207 | // a*0 and a*1 |
| 208 | if (RHS == 0) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 209 | return makeIntVal(0, resultTy); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 210 | else if (RHS == 1) |
| 211 | isIdempotent = true; |
| 212 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 213 | case BO_Div: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 214 | // a/0 and a/1 |
| 215 | if (RHS == 0) |
| 216 | // This is also handled elsewhere. |
| 217 | return UndefinedVal(); |
| 218 | else if (RHS == 1) |
| 219 | isIdempotent = true; |
| 220 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 221 | case BO_Rem: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 222 | // a%0 and a%1 |
| 223 | if (RHS == 0) |
| 224 | // This is also handled elsewhere. |
| 225 | return UndefinedVal(); |
| 226 | else if (RHS == 1) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 227 | return makeIntVal(0, resultTy); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 228 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 229 | case BO_Add: |
| 230 | case BO_Sub: |
| 231 | case BO_Shl: |
| 232 | case BO_Shr: |
| 233 | case BO_Xor: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 234 | // a+0, a-0, a<<0, a>>0, a^0 |
| 235 | if (RHS == 0) |
| 236 | isIdempotent = true; |
| 237 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 238 | case BO_And: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 239 | // a&0 and a&(~0) |
| 240 | if (RHS == 0) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 241 | return makeIntVal(0, resultTy); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 242 | else if (RHS.isAllOnesValue()) |
| 243 | isIdempotent = true; |
| 244 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 245 | case BO_Or: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 246 | // a|0 and a|(~0) |
| 247 | if (RHS == 0) |
| 248 | isIdempotent = true; |
| 249 | else if (RHS.isAllOnesValue()) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 250 | const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 251 | return nonloc::ConcreteInt(Result); |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | // Idempotent ops (like a*1) can still change the type of an expression. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 257 | // Wrap the LHS up in a NonLoc again and let evalCastNL do the dirty work. |
Benjamin Kramer | 24ada87 | 2010-06-20 10:20:36 +0000 | [diff] [blame] | 258 | if (isIdempotent) { |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 259 | if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS)) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 260 | return evalCastNL(nonloc::SymbolVal(LHSSym), resultTy); |
| 261 | return evalCastNL(nonloc::SymExprVal(LHS), resultTy); |
Benjamin Kramer | 24ada87 | 2010-06-20 10:20:36 +0000 | [diff] [blame] | 262 | } |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 263 | |
| 264 | // If we reach this point, the expression cannot be simplified. |
| 265 | // Make a SymExprVal for the entire thing. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 266 | return makeNonLoc(LHS, op, RHS, resultTy); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 269 | SVal SimpleSValBuilder::evalBinOpNN(const GRState *state, |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 270 | BinaryOperator::Opcode op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 271 | NonLoc lhs, NonLoc rhs, |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 272 | QualType resultTy) { |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 273 | // Handle trivial case where left-side and right-side are the same. |
| 274 | if (lhs == rhs) |
| 275 | switch (op) { |
| 276 | default: |
| 277 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 278 | case BO_EQ: |
| 279 | case BO_LE: |
| 280 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 281 | return makeTruthVal(true, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 282 | case BO_LT: |
| 283 | case BO_GT: |
| 284 | case BO_NE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 285 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 286 | case BO_Xor: |
| 287 | case BO_Sub: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 288 | return makeIntVal(0, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 289 | case BO_Or: |
| 290 | case BO_And: |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 291 | return evalCastNL(lhs, resultTy); |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 294 | while (1) { |
| 295 | switch (lhs.getSubKind()) { |
| 296 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 298 | case nonloc::LocAsIntegerKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 300 | switch (rhs.getSubKind()) { |
| 301 | case nonloc::LocAsIntegerKind: |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 302 | return evalBinOpLL(state, op, lhsL, |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 303 | cast<nonloc::LocAsInteger>(rhs).getLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 305 | case nonloc::ConcreteIntKind: { |
| 306 | // Transform the integer into a location and compare. |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 307 | llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue(); |
| 308 | i.setIsUnsigned(true); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 309 | i = i.extOrTrunc(Context.getTypeSize(Context.VoidPtrTy)); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 310 | return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | default: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 313 | switch (op) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 314 | case BO_EQ: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 315 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 316 | case BO_NE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 317 | return makeTruthVal(true, resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 318 | default: |
| 319 | // This case also handles pointer arithmetic. |
| 320 | return UnknownVal(); |
| 321 | } |
| 322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 324 | case nonloc::SymExprValKind: { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 325 | nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs); |
| 326 | |
| 327 | // Only handle LHS of the form "$sym op constant", at least for now. |
| 328 | const SymIntExpr *symIntExpr = |
| 329 | dyn_cast<SymIntExpr>(selhs->getSymbolicExpression()); |
| 330 | |
| 331 | if (!symIntExpr) |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 332 | return UnknownVal(); |
| 333 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 334 | // Is this a logical not? (!x is represented as x == 0.) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 335 | if (op == BO_EQ && rhs.isZeroConstant()) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 336 | // We know how to negate certain expressions. Simplify them here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 338 | BinaryOperator::Opcode opc = symIntExpr->getOpcode(); |
| 339 | switch (opc) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 340 | default: |
| 341 | // We don't know how to negate this operation. |
| 342 | // Just handle it as if it were a normal comparison to 0. |
| 343 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 344 | case BO_LAnd: |
| 345 | case BO_LOr: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 346 | assert(false && "Logical operators handled by branching logic."); |
| 347 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 348 | case BO_Assign: |
| 349 | case BO_MulAssign: |
| 350 | case BO_DivAssign: |
| 351 | case BO_RemAssign: |
| 352 | case BO_AddAssign: |
| 353 | case BO_SubAssign: |
| 354 | case BO_ShlAssign: |
| 355 | case BO_ShrAssign: |
| 356 | case BO_AndAssign: |
| 357 | case BO_XorAssign: |
| 358 | case BO_OrAssign: |
| 359 | case BO_Comma: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 360 | assert(false && "'=' and ',' operators handled by GRExprEngine."); |
| 361 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 362 | case BO_PtrMemD: |
| 363 | case BO_PtrMemI: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 364 | assert(false && "Pointer arithmetic not handled here."); |
| 365 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 366 | case BO_LT: |
| 367 | case BO_GT: |
| 368 | case BO_LE: |
| 369 | case BO_GE: |
| 370 | case BO_EQ: |
| 371 | case BO_NE: |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 372 | // Negate the comparison and make a value. |
| 373 | opc = NegateComparison(opc); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 374 | assert(symIntExpr->getType(Context) == resultTy); |
| 375 | return makeNonLoc(symIntExpr->getLHS(), opc, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 376 | symIntExpr->getRHS(), resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 379 | |
| 380 | // For now, only handle expressions whose RHS is a constant. |
| 381 | const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs); |
| 382 | if (!rhsInt) |
| 383 | return UnknownVal(); |
| 384 | |
| 385 | // If both the LHS and the current expression are additive, |
| 386 | // fold their constants. |
| 387 | if (BinaryOperator::isAdditiveOp(op)) { |
| 388 | BinaryOperator::Opcode lop = symIntExpr->getOpcode(); |
| 389 | if (BinaryOperator::isAdditiveOp(lop)) { |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 390 | // resultTy may not be the best type to convert to, but it's |
| 391 | // probably the best choice in expressions with mixed type |
| 392 | // (such as x+1U+2LL). The rules for implicit conversions should |
| 393 | // choose a reasonable type to preserve the expression, and will |
| 394 | // at least match how the value is going to be used. |
| 395 | const llvm::APSInt &first = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 396 | BasicVals.Convert(resultTy, symIntExpr->getRHS()); |
Jordy Rose | b4954a4 | 2010-06-21 20:15:15 +0000 | [diff] [blame] | 397 | const llvm::APSInt &second = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 398 | BasicVals.Convert(resultTy, rhsInt->getValue()); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 399 | const llvm::APSInt *newRHS; |
| 400 | if (lop == op) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 401 | newRHS = BasicVals.evalAPSInt(BO_Add, first, second); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 402 | else |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 403 | newRHS = BasicVals.evalAPSInt(BO_Sub, first, second); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 404 | return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy); |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | // Otherwise, make a SymExprVal out of the expression. |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 409 | return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | case nonloc::ConcreteIntKind: { |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 412 | const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs); |
| 413 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 414 | if (isa<nonloc::ConcreteInt>(rhs)) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 415 | return lhsInt.evalBinOp(*this, op, cast<nonloc::ConcreteInt>(rhs)); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 416 | } else { |
| 417 | const llvm::APSInt& lhsValue = lhsInt.getValue(); |
| 418 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 419 | // Swap the left and right sides and flip the operator if doing so |
| 420 | // allows us to better reason about the expression (this is a form |
| 421 | // of expression canonicalization). |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 422 | // While we're at it, catch some special cases for non-commutative ops. |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 423 | NonLoc tmp = rhs; |
| 424 | rhs = lhs; |
| 425 | lhs = tmp; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 427 | switch (op) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 428 | case BO_LT: |
| 429 | case BO_GT: |
| 430 | case BO_LE: |
| 431 | case BO_GE: |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 432 | op = ReverseComparison(op); |
| 433 | continue; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 434 | case BO_EQ: |
| 435 | case BO_NE: |
| 436 | case BO_Add: |
| 437 | case BO_Mul: |
| 438 | case BO_And: |
| 439 | case BO_Xor: |
| 440 | case BO_Or: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 441 | continue; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 442 | case BO_Shr: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 443 | if (lhsValue.isAllOnesValue() && lhsValue.isSigned()) |
| 444 | // At this point lhs and rhs have been swapped. |
| 445 | return rhs; |
| 446 | // FALL-THROUGH |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 447 | case BO_Shl: |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 448 | if (lhsValue == 0) |
| 449 | // At this point lhs and rhs have been swapped. |
| 450 | return rhs; |
| 451 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 452 | default: |
| 453 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | case nonloc::SymbolValKind: { |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 458 | nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs); |
| 459 | SymbolRef Sym = slhs->getSymbol(); |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 460 | // Does the symbol simplify to a constant? If so, "fold" the constant |
| 461 | // by setting 'lhs' to a ConcreteInt and try again. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 462 | if (Sym->getType(Context)->isIntegerType()) |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 463 | if (const llvm::APSInt *Constant = state->getSymVal(Sym)) { |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 464 | // The symbol evaluates to a constant. If necessary, promote the |
| 465 | // folded constant (LHS) to the result type. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 466 | const llvm::APSInt &lhs_I = BasicVals.Convert(resultTy, *Constant); |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 467 | lhs = nonloc::ConcreteInt(lhs_I); |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 469 | // Also promote the RHS (if necessary). |
| 470 | |
Jordy Rose | a277e77 | 2010-08-09 20:31:57 +0000 | [diff] [blame] | 471 | // For shifts, it is not necessary to promote the RHS. |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 472 | if (BinaryOperator::isShiftOp(op)) |
| 473 | continue; |
| 474 | |
| 475 | // Other operators: do an implicit conversion. This shouldn't be |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 476 | // necessary once we support truncation/extension of symbolic values. |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 477 | if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){ |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 478 | rhs = nonloc::ConcreteInt(BasicVals.Convert(resultTy, |
| 479 | rhs_I->getValue())); |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 480 | } |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 481 | |
| 482 | continue; |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 483 | } |
Jordy Rose | a277e77 | 2010-08-09 20:31:57 +0000 | [diff] [blame] | 484 | |
| 485 | // Is the RHS a symbol we can simplify? |
| 486 | if (const nonloc::SymbolVal *srhs = dyn_cast<nonloc::SymbolVal>(&rhs)) { |
| 487 | SymbolRef RSym = srhs->getSymbol(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 488 | if (RSym->getType(Context)->isIntegerType()) { |
Jordy Rose | a277e77 | 2010-08-09 20:31:57 +0000 | [diff] [blame] | 489 | if (const llvm::APSInt *Constant = state->getSymVal(RSym)) { |
| 490 | // The symbol evaluates to a constant. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 491 | const llvm::APSInt &rhs_I = BasicVals.Convert(resultTy, *Constant); |
Jordy Rose | a277e77 | 2010-08-09 20:31:57 +0000 | [diff] [blame] | 492 | rhs = nonloc::ConcreteInt(rhs_I); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 497 | if (isa<nonloc::ConcreteInt>(rhs)) { |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 498 | return MakeSymIntVal(slhs->getSymbol(), op, |
| 499 | cast<nonloc::ConcreteInt>(rhs).getValue(), |
| 500 | resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return UnknownVal(); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 509 | // FIXME: all this logic will change if/when we have MemRegion::getLocation(). |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 510 | SVal SimpleSValBuilder::evalBinOpLL(const GRState *state, |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 511 | BinaryOperator::Opcode op, |
| 512 | Loc lhs, Loc rhs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | QualType resultTy) { |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 514 | // Only comparisons and subtractions are valid operations on two pointers. |
| 515 | // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15]. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 516 | // However, if a pointer is casted to an integer, evalBinOpNN may end up |
Jordy Rose | a274148 | 2010-06-30 01:35:20 +0000 | [diff] [blame] | 517 | // calling this function with another operation (PR7527). We don't attempt to |
| 518 | // model this for now, but it could be useful, particularly when the |
| 519 | // "location" is actually an integer value that's been passed through a void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 520 | if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub)) |
Jordy Rose | a274148 | 2010-06-30 01:35:20 +0000 | [diff] [blame] | 521 | return UnknownVal(); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 522 | |
| 523 | // Special cases for when both sides are identical. |
| 524 | if (lhs == rhs) { |
| 525 | switch (op) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 526 | default: |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 527 | assert(false && "Unimplemented operation for two identical values"); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 528 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 529 | case BO_Sub: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 530 | return makeZeroVal(resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 531 | case BO_EQ: |
| 532 | case BO_LE: |
| 533 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 534 | return makeTruthVal(true, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 535 | case BO_NE: |
| 536 | case BO_LT: |
| 537 | case BO_GT: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 538 | return makeTruthVal(false, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | switch (lhs.getSubKind()) { |
| 543 | default: |
| 544 | assert(false && "Ordering not implemented for this Loc."); |
| 545 | return UnknownVal(); |
| 546 | |
| 547 | case loc::GotoLabelKind: |
| 548 | // The only thing we know about labels is that they're non-null. |
| 549 | if (rhs.isZeroConstant()) { |
| 550 | switch (op) { |
| 551 | default: |
| 552 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 553 | case BO_Sub: |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 554 | return evalCastL(lhs, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 555 | case BO_EQ: |
| 556 | case BO_LE: |
| 557 | case BO_LT: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 558 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 559 | case BO_NE: |
| 560 | case BO_GT: |
| 561 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 562 | return makeTruthVal(true, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | // There may be two labels for the same location, and a function region may |
| 566 | // have the same address as a label at the start of the function (depending |
| 567 | // on the ABI). |
| 568 | // FIXME: we can probably do a comparison against other MemRegions, though. |
| 569 | // FIXME: is there a way to tell if two labels refer to the same location? |
| 570 | return UnknownVal(); |
| 571 | |
| 572 | case loc::ConcreteIntKind: { |
| 573 | // If one of the operands is a symbol and the other is a constant, |
| 574 | // build an expression for use by the constraint manager. |
| 575 | if (SymbolRef rSym = rhs.getAsLocSymbol()) { |
| 576 | // We can only build expressions with symbols on the left, |
| 577 | // so we need a reversible operator. |
| 578 | if (!BinaryOperator::isComparisonOp(op)) |
| 579 | return UnknownVal(); |
| 580 | |
| 581 | const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 582 | return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // If both operands are constants, just perform the operation. |
| 586 | if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 587 | SVal ResultVal = cast<loc::ConcreteInt>(lhs).evalBinOp(BasicVals, op, |
| 588 | *rInt); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 589 | if (Loc *Result = dyn_cast<Loc>(&ResultVal)) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 590 | return evalCastL(*Result, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 591 | else |
| 592 | return UnknownVal(); |
| 593 | } |
| 594 | |
| 595 | // Special case comparisons against NULL. |
| 596 | // This must come after the test if the RHS is a symbol, which is used to |
| 597 | // build constraints. The address of any non-symbolic region is guaranteed |
| 598 | // to be non-NULL, as is any label. |
| 599 | assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs)); |
| 600 | if (lhs.isZeroConstant()) { |
| 601 | switch (op) { |
| 602 | default: |
| 603 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 604 | case BO_EQ: |
| 605 | case BO_GT: |
| 606 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 607 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 608 | case BO_NE: |
| 609 | case BO_LT: |
| 610 | case BO_LE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 611 | return makeTruthVal(true, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | // Comparing an arbitrary integer to a region or label address is |
| 616 | // completely unknowable. |
| 617 | return UnknownVal(); |
| 618 | } |
| 619 | case loc::MemRegionKind: { |
| 620 | if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { |
| 621 | // If one of the operands is a symbol and the other is a constant, |
| 622 | // build an expression for use by the constraint manager. |
| 623 | if (SymbolRef lSym = lhs.getAsLocSymbol()) |
| 624 | return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy); |
| 625 | |
| 626 | // Special case comparisons to NULL. |
| 627 | // This must come after the test if the LHS is a symbol, which is used to |
| 628 | // build constraints. The address of any non-symbolic region is guaranteed |
| 629 | // to be non-NULL. |
| 630 | if (rInt->isZeroConstant()) { |
| 631 | switch (op) { |
| 632 | default: |
| 633 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 634 | case BO_Sub: |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 635 | return evalCastL(lhs, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 636 | case BO_EQ: |
| 637 | case BO_LT: |
| 638 | case BO_LE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 639 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 640 | case BO_NE: |
| 641 | case BO_GT: |
| 642 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 643 | return makeTruthVal(true, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | // Comparing a region to an arbitrary integer is completely unknowable. |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 648 | return UnknownVal(); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // Get both values as regions, if possible. |
| 652 | const MemRegion *LeftMR = lhs.getAsRegion(); |
| 653 | assert(LeftMR && "MemRegionKind SVal doesn't have a region!"); |
| 654 | |
| 655 | const MemRegion *RightMR = rhs.getAsRegion(); |
| 656 | if (!RightMR) |
| 657 | // The RHS is probably a label, which in theory could address a region. |
| 658 | // FIXME: we can probably make a more useful statement about non-code |
| 659 | // regions, though. |
| 660 | return UnknownVal(); |
| 661 | |
| 662 | // If both values wrap regions, see if they're from different base regions. |
| 663 | const MemRegion *LeftBase = LeftMR->getBaseRegion(); |
| 664 | const MemRegion *RightBase = RightMR->getBaseRegion(); |
| 665 | if (LeftBase != RightBase && |
| 666 | !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) { |
| 667 | switch (op) { |
| 668 | default: |
| 669 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 670 | case BO_EQ: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 671 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 672 | case BO_NE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 673 | return makeTruthVal(true, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | // The two regions are from the same base region. See if they're both a |
| 678 | // type of region we know how to compare. |
| 679 | |
| 680 | // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this |
| 681 | // ElementRegion path and the FieldRegion path below should be unified. |
| 682 | if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) { |
| 683 | // First see if the right region is also an ElementRegion. |
| 684 | const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR); |
| 685 | if (!RightER) |
| 686 | return UnknownVal(); |
| 687 | |
| 688 | // Next, see if the two ERs have the same super-region and matching types. |
| 689 | // FIXME: This should do something useful even if the types don't match, |
| 690 | // though if both indexes are constant the RegionRawOffset path will |
| 691 | // give the correct answer. |
| 692 | if (LeftER->getSuperRegion() == RightER->getSuperRegion() && |
| 693 | LeftER->getElementType() == RightER->getElementType()) { |
| 694 | // Get the left index and cast it to the correct type. |
| 695 | // If the index is unknown or undefined, bail out here. |
| 696 | SVal LeftIndexVal = LeftER->getIndex(); |
| 697 | NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); |
| 698 | if (!LeftIndex) |
| 699 | return UnknownVal(); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 700 | LeftIndexVal = evalCastNL(*LeftIndex, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 701 | LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); |
| 702 | if (!LeftIndex) |
| 703 | return UnknownVal(); |
| 704 | |
| 705 | // Do the same for the right index. |
| 706 | SVal RightIndexVal = RightER->getIndex(); |
| 707 | NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal); |
| 708 | if (!RightIndex) |
| 709 | return UnknownVal(); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 710 | RightIndexVal = evalCastNL(*RightIndex, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 711 | RightIndex = dyn_cast<NonLoc>(&RightIndexVal); |
| 712 | if (!RightIndex) |
| 713 | return UnknownVal(); |
| 714 | |
| 715 | // Actually perform the operation. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 716 | // evalBinOpNN expects the two indexes to already be the right type. |
| 717 | return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | // If the element indexes aren't comparable, see if the raw offsets are. |
Zhongxing Xu | 7caf9b3 | 2010-08-02 04:56:14 +0000 | [diff] [blame] | 721 | RegionRawOffset LeftOffset = LeftER->getAsArrayOffset(); |
| 722 | RegionRawOffset RightOffset = RightER->getAsArrayOffset(); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 723 | |
| 724 | if (LeftOffset.getRegion() != NULL && |
| 725 | LeftOffset.getRegion() == RightOffset.getRegion()) { |
| 726 | int64_t left = LeftOffset.getByteOffset(); |
| 727 | int64_t right = RightOffset.getByteOffset(); |
| 728 | |
| 729 | switch (op) { |
| 730 | default: |
| 731 | return UnknownVal(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 732 | case BO_LT: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 733 | return makeTruthVal(left < right, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 734 | case BO_GT: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 735 | return makeTruthVal(left > right, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 736 | case BO_LE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 737 | return makeTruthVal(left <= right, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 738 | case BO_GE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 739 | return makeTruthVal(left >= right, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 740 | case BO_EQ: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 741 | return makeTruthVal(left == right, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 742 | case BO_NE: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 743 | return makeTruthVal(left != right, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | // If we get here, we have no way of comparing the ElementRegions. |
| 748 | return UnknownVal(); |
| 749 | } |
| 750 | |
| 751 | // See if both regions are fields of the same structure. |
| 752 | // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars. |
| 753 | if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) { |
| 754 | // Only comparisons are meaningful here! |
| 755 | if (!BinaryOperator::isComparisonOp(op)) |
| 756 | return UnknownVal(); |
| 757 | |
| 758 | // First see if the right region is also a FieldRegion. |
| 759 | const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR); |
| 760 | if (!RightFR) |
| 761 | return UnknownVal(); |
| 762 | |
| 763 | // Next, see if the two FRs have the same super-region. |
| 764 | // FIXME: This doesn't handle casts yet, and simply stripping the casts |
| 765 | // doesn't help. |
| 766 | if (LeftFR->getSuperRegion() != RightFR->getSuperRegion()) |
| 767 | return UnknownVal(); |
| 768 | |
| 769 | const FieldDecl *LeftFD = LeftFR->getDecl(); |
| 770 | const FieldDecl *RightFD = RightFR->getDecl(); |
| 771 | const RecordDecl *RD = LeftFD->getParent(); |
| 772 | |
| 773 | // Make sure the two FRs are from the same kind of record. Just in case! |
| 774 | // FIXME: This is probably where inheritance would be a problem. |
| 775 | if (RD != RightFD->getParent()) |
| 776 | return UnknownVal(); |
| 777 | |
| 778 | // We know for sure that the two fields are not the same, since that |
| 779 | // would have given us the same SVal. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 780 | if (op == BO_EQ) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 781 | return makeTruthVal(false, resultTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 782 | if (op == BO_NE) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 783 | return makeTruthVal(true, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 784 | |
| 785 | // Iterate through the fields and see which one comes first. |
| 786 | // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field |
| 787 | // members and the units in which bit-fields reside have addresses that |
| 788 | // increase in the order in which they are declared." |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 789 | bool leftFirst = (op == BO_LT || op == BO_LE); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 790 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 791 | E = RD->field_end(); I!=E; ++I) { |
| 792 | if (*I == LeftFD) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 793 | return makeTruthVal(leftFirst, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 794 | if (*I == RightFD) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 795 | return makeTruthVal(!leftFirst, resultTy); |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | assert(false && "Fields not found in parent record's definition"); |
| 799 | } |
| 800 | |
| 801 | // If we get here, we have no way of comparing the regions. |
| 802 | return UnknownVal(); |
| 803 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 807 | SVal SimpleSValBuilder::evalBinOpLN(const GRState *state, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 808 | BinaryOperator::Opcode op, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | Loc lhs, NonLoc rhs, QualType resultTy) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 810 | // Special case: 'rhs' is an integer that has the same width as a pointer and |
| 811 | // we are using the integer location in a comparison. Normally this cannot be |
| 812 | // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32 |
| 813 | // can generate comparisons that trigger this code. |
| 814 | // FIXME: Are all locations guaranteed to have pointer width? |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 815 | if (BinaryOperator::isComparisonOp(op)) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 816 | if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { |
| 817 | const llvm::APSInt *x = &rhsInt->getValue(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 818 | ASTContext &ctx = Context; |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 819 | if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) { |
| 820 | // Convert the signedness of the integer (if necessary). |
| 821 | if (x->isSigned()) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 822 | x = &getBasicValueFactory().getValue(*x, true); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 824 | return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
Ted Kremenek | 56af446 | 2010-09-03 01:07:06 +0000 | [diff] [blame] | 828 | |
| 829 | // We are dealing with pointer arithmetic. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | 56af446 | 2010-09-03 01:07:06 +0000 | [diff] [blame] | 831 | // Handle pointer arithmetic on constant values. |
| 832 | if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { |
| 833 | if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) { |
| 834 | const llvm::APSInt &leftI = lhsInt->getValue(); |
| 835 | assert(leftI.isUnsigned()); |
| 836 | llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true); |
| 837 | |
| 838 | // Convert the bitwidth of rightI. This should deal with overflow |
| 839 | // since we are dealing with concrete values. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 840 | rightI = rightI.extOrTrunc(leftI.getBitWidth()); |
Ted Kremenek | 56af446 | 2010-09-03 01:07:06 +0000 | [diff] [blame] | 841 | |
| 842 | // Offset the increment by the pointer size. |
Ted Kremenek | 56af446 | 2010-09-03 01:07:06 +0000 | [diff] [blame] | 843 | llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true); |
| 844 | rightI *= Multiplicand; |
| 845 | |
| 846 | // Compute the adjusted pointer. |
| 847 | switch (op) { |
| 848 | case BO_Add: |
| 849 | rightI = leftI + rightI; |
| 850 | break; |
| 851 | case BO_Sub: |
| 852 | rightI = leftI - rightI; |
| 853 | break; |
| 854 | default: |
| 855 | llvm_unreachable("Invalid pointer arithmetic operation"); |
| 856 | } |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 857 | return loc::ConcreteInt(getBasicValueFactory().getValue(rightI)); |
Ted Kremenek | 56af446 | 2010-09-03 01:07:06 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
| 861 | |
| 862 | // Delegate remaining pointer arithmetic to the StoreManager. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 863 | return state->getStateManager().getStoreManager().evalBinOp(op, lhs, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 864 | rhs, resultTy); |
| 865 | } |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 866 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 867 | const llvm::APSInt *SimpleSValBuilder::getKnownValue(const GRState *state, |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 868 | SVal V) { |
| 869 | if (V.isUnknownOrUndef()) |
| 870 | return NULL; |
| 871 | |
| 872 | if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V)) |
| 873 | return &X->getValue(); |
| 874 | |
| 875 | if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V)) |
| 876 | return &X->getValue(); |
| 877 | |
| 878 | if (SymbolRef Sym = V.getAsSymbol()) |
| 879 | return state->getSymVal(Sym); |
| 880 | |
| 881 | // FIXME: Add support for SymExprs. |
| 882 | return NULL; |
| 883 | } |