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