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