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); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 33 | virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs, |
| 34 | QualType resultTy); |
| 35 | virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op, |
| 36 | Loc lhs, NonLoc rhs, QualType resultTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | }; |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 38 | } // end anonymous namespace |
| 39 | |
| 40 | SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) { |
| 41 | return new SimpleSValuator(valMgr); |
| 42 | } |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // Transfer function for Casts. |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Ted Kremenek | dd66114 | 2009-07-20 21:39:27 +0000 | [diff] [blame] | 48 | SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | dd66114 | 2009-07-20 21:39:27 +0000 | [diff] [blame] | 50 | bool isLocType = Loc::IsLocType(castTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 52 | if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) { |
| 53 | if (isLocType) |
Ted Kremenek | dd66114 | 2009-07-20 21:39:27 +0000 | [diff] [blame] | 54 | return LI->getLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 56 | // FIXME: Correctly support promotions/truncations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | ASTContext &Ctx = ValMgr.getContext(); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 58 | unsigned castSize = Ctx.getTypeSize(castTy); |
| 59 | if (castSize == LI->getNumBits()) |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 60 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 62 | return ValMgr.makeLocAsInteger(LI->getLoc(), castSize); |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (const SymExpr *se = val.getAsSymbolicExpression()) { |
| 66 | ASTContext &Ctx = ValMgr.getContext(); |
| 67 | QualType T = Ctx.getCanonicalType(se->getType(Ctx)); |
| 68 | if (T == Ctx.getCanonicalType(castTy)) |
| 69 | return val; |
Ted Kremenek | 8041747 | 2009-09-25 00:18:15 +0000 | [diff] [blame] | 70 | |
| 71 | // FIXME: Remove this hack when we support symbolic truncation/extension. |
| 72 | // HACK: If both castTy and T are integers, ignore the cast. This is |
| 73 | // not a permanent solution. Eventually we want to precisely handle |
| 74 | // extension/truncation of symbolic integers. This prevents us from losing |
| 75 | // precision when we assign 'x = y' and 'y' is symbolic and x and y are |
| 76 | // different integer types. |
| 77 | if (T->isIntegerType() && castTy->isIntegerType()) |
| 78 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 80 | return UnknownVal(); |
| 81 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 83 | if (!isa<nonloc::ConcreteInt>(val)) |
| 84 | return UnknownVal(); |
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 | // Only handle casts from integers to integers. |
| 87 | if (!isLocType && !castTy->isIntegerType()) |
| 88 | return UnknownVal(); |
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 | llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue(); |
| 91 | i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy)); |
| 92 | i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy)); |
| 93 | |
| 94 | if (isLocType) |
| 95 | return ValMgr.makeIntLocVal(i); |
| 96 | else |
| 97 | return ValMgr.makeIntVal(i); |
| 98 | } |
| 99 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 100 | SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 102 | // Casts from pointers -> pointers, just return the lval. |
| 103 | // |
| 104 | // Casts from pointers -> references, just return the lval. These |
| 105 | // can be introduced by the frontend for corner cases, e.g |
| 106 | // casting from va_list* to __builtin_va_list&. |
| 107 | // |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 108 | if (Loc::IsLocType(castTy) || castTy->isReferenceType()) |
| 109 | return val; |
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 | // FIXME: Handle transparent unions where a value can be "transparently" |
| 112 | // lifted into a union type. |
| 113 | if (castTy->isUnionType()) |
| 114 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame^] | 116 | if (castTy->isIntegerType()) { |
| 117 | unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame^] | 119 | if (!isa<loc::ConcreteInt>(val)) |
| 120 | return ValMgr.makeLocAsInteger(val, BitWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | d617b85 | 2010-04-16 17:54:33 +0000 | [diff] [blame^] | 122 | llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue(); |
| 123 | i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy)); |
| 124 | i.extOrTrunc(BitWidth); |
| 125 | return ValMgr.makeIntVal(i); |
| 126 | } |
| 127 | |
| 128 | // All other cases: return 'UnknownVal'. This includes casting pointers |
| 129 | // to floats, which is probably badness it itself, but this is a good |
| 130 | // intermediate solution until we do something better. |
| 131 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | // Transfer function for unary operators. |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | |
| 138 | SVal SimpleSValuator::EvalMinus(NonLoc val) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | switch (val.getSubKind()) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 140 | case nonloc::ConcreteIntKind: |
| 141 | return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr); |
| 142 | default: |
| 143 | return UnknownVal(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | SVal SimpleSValuator::EvalComplement(NonLoc X) { |
| 148 | switch (X.getSubKind()) { |
| 149 | case nonloc::ConcreteIntKind: |
| 150 | return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr); |
| 151 | default: |
| 152 | return UnknownVal(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | // Transfer function for binary operators. |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | |
| 160 | static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { |
| 161 | switch (op) { |
| 162 | default: |
| 163 | assert(false && "Invalid opcode."); |
| 164 | case BinaryOperator::LT: return BinaryOperator::GE; |
| 165 | case BinaryOperator::GT: return BinaryOperator::LE; |
| 166 | case BinaryOperator::LE: return BinaryOperator::GT; |
| 167 | case BinaryOperator::GE: return BinaryOperator::LT; |
| 168 | case BinaryOperator::EQ: return BinaryOperator::NE; |
| 169 | case BinaryOperator::NE: return BinaryOperator::EQ; |
| 170 | } |
| 171 | } |
| 172 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | // Equality operators for Locs. |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 174 | // FIXME: All this logic will be revamped when we have MemRegion::getLocation() |
| 175 | // implemented. |
| 176 | |
| 177 | static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual, |
| 178 | QualType resultTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 180 | switch (lhs.getSubKind()) { |
| 181 | default: |
| 182 | assert(false && "EQ/NE not implemented for this Loc."); |
| 183 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 185 | case loc::ConcreteIntKind: { |
| 186 | if (SymbolRef rSym = rhs.getAsSymbol()) |
| 187 | return ValMgr.makeNonLoc(rSym, |
| 188 | isEqual ? BinaryOperator::EQ |
| 189 | : BinaryOperator::NE, |
| 190 | cast<loc::ConcreteInt>(lhs).getValue(), |
| 191 | resultTy); |
| 192 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 194 | case loc::MemRegionKind: { |
| 195 | if (SymbolRef lSym = lhs.getAsLocSymbol()) { |
| 196 | if (isa<loc::ConcreteInt>(rhs)) { |
| 197 | return ValMgr.makeNonLoc(lSym, |
| 198 | isEqual ? BinaryOperator::EQ |
| 199 | : BinaryOperator::NE, |
| 200 | cast<loc::ConcreteInt>(rhs).getValue(), |
| 201 | resultTy); |
| 202 | } |
| 203 | } |
| 204 | break; |
| 205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 207 | case loc::GotoLabelKind: |
| 208 | break; |
| 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 211 | return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy); |
| 212 | } |
| 213 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 214 | SVal SimpleSValuator::EvalBinOpNN(const GRState *state, |
| 215 | BinaryOperator::Opcode op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 216 | NonLoc lhs, NonLoc rhs, |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 217 | QualType resultTy) { |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 218 | // Handle trivial case where left-side and right-side are the same. |
| 219 | if (lhs == rhs) |
| 220 | switch (op) { |
| 221 | default: |
| 222 | break; |
| 223 | case BinaryOperator::EQ: |
| 224 | case BinaryOperator::LE: |
| 225 | case BinaryOperator::GE: |
| 226 | return ValMgr.makeTruthVal(true, resultTy); |
| 227 | case BinaryOperator::LT: |
| 228 | case BinaryOperator::GT: |
| 229 | case BinaryOperator::NE: |
| 230 | return ValMgr.makeTruthVal(false, resultTy); |
| 231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 233 | while (1) { |
| 234 | switch (lhs.getSubKind()) { |
| 235 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 237 | case nonloc::LocAsIntegerKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 239 | switch (rhs.getSubKind()) { |
| 240 | case nonloc::LocAsIntegerKind: |
| 241 | return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 243 | case nonloc::ConcreteIntKind: { |
| 244 | // Transform the integer into a location and compare. |
| 245 | ASTContext& Ctx = ValMgr.getContext(); |
| 246 | llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue(); |
| 247 | i.setIsUnsigned(true); |
| 248 | i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy)); |
| 249 | return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy); |
| 250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | default: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 252 | switch (op) { |
| 253 | case BinaryOperator::EQ: |
| 254 | return ValMgr.makeTruthVal(false, resultTy); |
| 255 | case BinaryOperator::NE: |
| 256 | return ValMgr.makeTruthVal(true, resultTy); |
| 257 | default: |
| 258 | // This case also handles pointer arithmetic. |
| 259 | return UnknownVal(); |
| 260 | } |
| 261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 263 | case nonloc::SymExprValKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | // Logical not? |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 265 | if (!(op == BinaryOperator::EQ && rhs.isZeroConstant())) |
| 266 | return UnknownVal(); |
| 267 | |
| 268 | const SymExpr *symExpr = |
| 269 | cast<nonloc::SymExprVal>(lhs).getSymbolicExpression(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 271 | // Only handle ($sym op constant) for now. |
| 272 | if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) { |
| 273 | BinaryOperator::Opcode opc = symIntExpr->getOpcode(); |
| 274 | switch (opc) { |
| 275 | case BinaryOperator::LAnd: |
| 276 | case BinaryOperator::LOr: |
| 277 | assert(false && "Logical operators handled by branching logic."); |
| 278 | return UnknownVal(); |
| 279 | case BinaryOperator::Assign: |
| 280 | case BinaryOperator::MulAssign: |
| 281 | case BinaryOperator::DivAssign: |
| 282 | case BinaryOperator::RemAssign: |
| 283 | case BinaryOperator::AddAssign: |
| 284 | case BinaryOperator::SubAssign: |
| 285 | case BinaryOperator::ShlAssign: |
| 286 | case BinaryOperator::ShrAssign: |
| 287 | case BinaryOperator::AndAssign: |
| 288 | case BinaryOperator::XorAssign: |
| 289 | case BinaryOperator::OrAssign: |
| 290 | case BinaryOperator::Comma: |
| 291 | assert(false && "'=' and ',' operators handled by GRExprEngine."); |
| 292 | return UnknownVal(); |
| 293 | case BinaryOperator::PtrMemD: |
| 294 | case BinaryOperator::PtrMemI: |
| 295 | assert(false && "Pointer arithmetic not handled here."); |
| 296 | return UnknownVal(); |
| 297 | case BinaryOperator::Mul: |
| 298 | case BinaryOperator::Div: |
| 299 | case BinaryOperator::Rem: |
| 300 | case BinaryOperator::Add: |
| 301 | case BinaryOperator::Sub: |
| 302 | case BinaryOperator::Shl: |
| 303 | case BinaryOperator::Shr: |
| 304 | case BinaryOperator::And: |
| 305 | case BinaryOperator::Xor: |
| 306 | case BinaryOperator::Or: |
| 307 | // Not handled yet. |
| 308 | return UnknownVal(); |
| 309 | case BinaryOperator::LT: |
| 310 | case BinaryOperator::GT: |
| 311 | case BinaryOperator::LE: |
| 312 | case BinaryOperator::GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | case BinaryOperator::EQ: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 314 | case BinaryOperator::NE: |
| 315 | opc = NegateComparison(opc); |
| 316 | assert(symIntExpr->getType(ValMgr.getContext()) == resultTy); |
| 317 | return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc, |
| 318 | symIntExpr->getRHS(), resultTy); |
| 319 | } |
| 320 | } |
| 321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | case nonloc::ConcreteIntKind: { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 323 | if (isa<nonloc::ConcreteInt>(rhs)) { |
| 324 | const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs); |
| 325 | return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs)); |
| 326 | } |
| 327 | else { |
| 328 | // Swap the left and right sides and flip the operator if doing so |
| 329 | // allows us to better reason about the expression (this is a form |
| 330 | // of expression canonicalization). |
| 331 | NonLoc tmp = rhs; |
| 332 | rhs = lhs; |
| 333 | lhs = tmp; |
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 | switch (op) { |
| 336 | case BinaryOperator::LT: op = BinaryOperator::GT; continue; |
| 337 | case BinaryOperator::GT: op = BinaryOperator::LT; continue; |
| 338 | case BinaryOperator::LE: op = BinaryOperator::GE; continue; |
| 339 | case BinaryOperator::GE: op = BinaryOperator::LE; continue; |
| 340 | case BinaryOperator::EQ: |
| 341 | case BinaryOperator::NE: |
| 342 | case BinaryOperator::Add: |
| 343 | case BinaryOperator::Mul: |
| 344 | continue; |
| 345 | default: |
| 346 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | case nonloc::SymbolValKind: { |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 351 | nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs); |
| 352 | SymbolRef Sym = slhs->getSymbol(); |
| 353 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 354 | // Does the symbol simplify to a constant? If so, "fold" the constant |
| 355 | // by setting 'lhs' to a ConcreteInt and try again. |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 356 | if (Sym->getType(ValMgr.getContext())->isIntegerType()) |
| 357 | if (const llvm::APSInt *Constant = state->getSymVal(Sym)) { |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 358 | // The symbol evaluates to a constant. If necessary, promote the |
| 359 | // folded constant (LHS) to the result type. |
| 360 | BasicValueFactory &BVF = ValMgr.getBasicValueFactory(); |
| 361 | const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant); |
| 362 | lhs = nonloc::ConcreteInt(lhs_I); |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 364 | // Also promote the RHS (if necessary). |
| 365 | |
| 366 | // For shifts, it necessary promote the RHS to the result type. |
| 367 | if (BinaryOperator::isShiftOp(op)) |
| 368 | continue; |
| 369 | |
| 370 | // Other operators: do an implicit conversion. This shouldn't be |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 371 | // necessary once we support truncation/extension of symbolic values. |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 372 | if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){ |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 373 | rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue())); |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 374 | } |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 375 | |
| 376 | continue; |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 379 | if (isa<nonloc::ConcreteInt>(rhs)) { |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 380 | return ValMgr.makeNonLoc(slhs->getSymbol(), op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 381 | cast<nonloc::ConcreteInt>(rhs).getValue(), |
| 382 | resultTy); |
| 383 | } |
| 384 | |
| 385 | return UnknownVal(); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | QualType resultTy) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 393 | switch (op) { |
| 394 | default: |
| 395 | return UnknownVal(); |
| 396 | case BinaryOperator::EQ: |
| 397 | case BinaryOperator::NE: |
| 398 | return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy); |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 399 | case BinaryOperator::LT: |
| 400 | case BinaryOperator::GT: |
| 401 | // FIXME: Generalize. For now, just handle the trivial case where |
| 402 | // the two locations are identical. |
| 403 | if (lhs == rhs) |
| 404 | return ValMgr.makeTruthVal(false, resultTy); |
| 405 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | SVal SimpleSValuator::EvalBinOpLN(const GRState *state, |
| 410 | BinaryOperator::Opcode op, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | Loc lhs, NonLoc rhs, QualType resultTy) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 412 | // Special case: 'rhs' is an integer that has the same width as a pointer and |
| 413 | // we are using the integer location in a comparison. Normally this cannot be |
| 414 | // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32 |
| 415 | // can generate comparisons that trigger this code. |
| 416 | // FIXME: Are all locations guaranteed to have pointer width? |
| 417 | if (BinaryOperator::isEqualityOp(op)) { |
| 418 | if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { |
| 419 | const llvm::APSInt *x = &rhsInt->getValue(); |
| 420 | ASTContext &ctx = ValMgr.getContext(); |
| 421 | if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) { |
| 422 | // Convert the signedness of the integer (if necessary). |
| 423 | if (x->isSigned()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | x = &ValMgr.getBasicValueFactory().getValue(*x, true); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 425 | |
| 426 | return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy); |
| 427 | } |
| 428 | } |
| 429 | } |
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 | // Delegate pointer arithmetic to the StoreManager. |
Zhongxing Xu | 461147f | 2010-02-05 05:24:20 +0000 | [diff] [blame] | 432 | return state->getStateManager().getStoreManager().EvalBinOp(op, lhs, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 433 | rhs, resultTy); |
| 434 | } |