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