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 | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 116 | assert(castTy->isIntegerType()); |
| 117 | unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy); |
| 118 | |
| 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 | e839172 | 2009-06-26 00:25:05 +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 | //===----------------------------------------------------------------------===// |
| 129 | // Transfer function for unary operators. |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
| 132 | SVal SimpleSValuator::EvalMinus(NonLoc val) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | switch (val.getSubKind()) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 134 | case nonloc::ConcreteIntKind: |
| 135 | return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr); |
| 136 | default: |
| 137 | return UnknownVal(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | SVal SimpleSValuator::EvalComplement(NonLoc X) { |
| 142 | switch (X.getSubKind()) { |
| 143 | case nonloc::ConcreteIntKind: |
| 144 | return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr); |
| 145 | default: |
| 146 | return UnknownVal(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | // Transfer function for binary operators. |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
| 154 | static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { |
| 155 | switch (op) { |
| 156 | default: |
| 157 | assert(false && "Invalid opcode."); |
| 158 | case BinaryOperator::LT: return BinaryOperator::GE; |
| 159 | case BinaryOperator::GT: return BinaryOperator::LE; |
| 160 | case BinaryOperator::LE: return BinaryOperator::GT; |
| 161 | case BinaryOperator::GE: return BinaryOperator::LT; |
| 162 | case BinaryOperator::EQ: return BinaryOperator::NE; |
| 163 | case BinaryOperator::NE: return BinaryOperator::EQ; |
| 164 | } |
| 165 | } |
| 166 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | // Equality operators for Locs. |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 168 | // FIXME: All this logic will be revamped when we have MemRegion::getLocation() |
| 169 | // implemented. |
| 170 | |
| 171 | static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual, |
| 172 | QualType resultTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 174 | switch (lhs.getSubKind()) { |
| 175 | default: |
| 176 | assert(false && "EQ/NE not implemented for this Loc."); |
| 177 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 179 | case loc::ConcreteIntKind: { |
| 180 | if (SymbolRef rSym = rhs.getAsSymbol()) |
| 181 | return ValMgr.makeNonLoc(rSym, |
| 182 | isEqual ? BinaryOperator::EQ |
| 183 | : BinaryOperator::NE, |
| 184 | cast<loc::ConcreteInt>(lhs).getValue(), |
| 185 | resultTy); |
| 186 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 188 | case loc::MemRegionKind: { |
| 189 | if (SymbolRef lSym = lhs.getAsLocSymbol()) { |
| 190 | if (isa<loc::ConcreteInt>(rhs)) { |
| 191 | return ValMgr.makeNonLoc(lSym, |
| 192 | isEqual ? BinaryOperator::EQ |
| 193 | : BinaryOperator::NE, |
| 194 | cast<loc::ConcreteInt>(rhs).getValue(), |
| 195 | resultTy); |
| 196 | } |
| 197 | } |
| 198 | break; |
| 199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 201 | case loc::GotoLabelKind: |
| 202 | break; |
| 203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 205 | return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy); |
| 206 | } |
| 207 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 208 | SVal SimpleSValuator::EvalBinOpNN(const GRState *state, |
| 209 | BinaryOperator::Opcode op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 210 | NonLoc lhs, NonLoc rhs, |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 211 | QualType resultTy) { |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 212 | // Handle trivial case where left-side and right-side are the same. |
| 213 | if (lhs == rhs) |
| 214 | switch (op) { |
| 215 | default: |
| 216 | break; |
| 217 | case BinaryOperator::EQ: |
| 218 | case BinaryOperator::LE: |
| 219 | case BinaryOperator::GE: |
| 220 | return ValMgr.makeTruthVal(true, resultTy); |
| 221 | case BinaryOperator::LT: |
| 222 | case BinaryOperator::GT: |
| 223 | case BinaryOperator::NE: |
| 224 | return ValMgr.makeTruthVal(false, resultTy); |
| 225 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 227 | while (1) { |
| 228 | switch (lhs.getSubKind()) { |
| 229 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 231 | case nonloc::LocAsIntegerKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 233 | switch (rhs.getSubKind()) { |
| 234 | case nonloc::LocAsIntegerKind: |
| 235 | return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | resultTy); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 237 | case nonloc::ConcreteIntKind: { |
| 238 | // Transform the integer into a location and compare. |
| 239 | ASTContext& Ctx = ValMgr.getContext(); |
| 240 | llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue(); |
| 241 | i.setIsUnsigned(true); |
| 242 | i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy)); |
| 243 | return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy); |
| 244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | default: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 246 | switch (op) { |
| 247 | case BinaryOperator::EQ: |
| 248 | return ValMgr.makeTruthVal(false, resultTy); |
| 249 | case BinaryOperator::NE: |
| 250 | return ValMgr.makeTruthVal(true, resultTy); |
| 251 | default: |
| 252 | // This case also handles pointer arithmetic. |
| 253 | return UnknownVal(); |
| 254 | } |
| 255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 257 | case nonloc::SymExprValKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | // Logical not? |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 259 | if (!(op == BinaryOperator::EQ && rhs.isZeroConstant())) |
| 260 | return UnknownVal(); |
| 261 | |
| 262 | const SymExpr *symExpr = |
| 263 | cast<nonloc::SymExprVal>(lhs).getSymbolicExpression(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 265 | // Only handle ($sym op constant) for now. |
| 266 | if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) { |
| 267 | BinaryOperator::Opcode opc = symIntExpr->getOpcode(); |
| 268 | switch (opc) { |
| 269 | case BinaryOperator::LAnd: |
| 270 | case BinaryOperator::LOr: |
| 271 | assert(false && "Logical operators handled by branching logic."); |
| 272 | return UnknownVal(); |
| 273 | case BinaryOperator::Assign: |
| 274 | case BinaryOperator::MulAssign: |
| 275 | case BinaryOperator::DivAssign: |
| 276 | case BinaryOperator::RemAssign: |
| 277 | case BinaryOperator::AddAssign: |
| 278 | case BinaryOperator::SubAssign: |
| 279 | case BinaryOperator::ShlAssign: |
| 280 | case BinaryOperator::ShrAssign: |
| 281 | case BinaryOperator::AndAssign: |
| 282 | case BinaryOperator::XorAssign: |
| 283 | case BinaryOperator::OrAssign: |
| 284 | case BinaryOperator::Comma: |
| 285 | assert(false && "'=' and ',' operators handled by GRExprEngine."); |
| 286 | return UnknownVal(); |
| 287 | case BinaryOperator::PtrMemD: |
| 288 | case BinaryOperator::PtrMemI: |
| 289 | assert(false && "Pointer arithmetic not handled here."); |
| 290 | return UnknownVal(); |
| 291 | case BinaryOperator::Mul: |
| 292 | case BinaryOperator::Div: |
| 293 | case BinaryOperator::Rem: |
| 294 | case BinaryOperator::Add: |
| 295 | case BinaryOperator::Sub: |
| 296 | case BinaryOperator::Shl: |
| 297 | case BinaryOperator::Shr: |
| 298 | case BinaryOperator::And: |
| 299 | case BinaryOperator::Xor: |
| 300 | case BinaryOperator::Or: |
| 301 | // Not handled yet. |
| 302 | return UnknownVal(); |
| 303 | case BinaryOperator::LT: |
| 304 | case BinaryOperator::GT: |
| 305 | case BinaryOperator::LE: |
| 306 | case BinaryOperator::GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | case BinaryOperator::EQ: |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 308 | case BinaryOperator::NE: |
| 309 | opc = NegateComparison(opc); |
| 310 | assert(symIntExpr->getType(ValMgr.getContext()) == resultTy); |
| 311 | return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc, |
| 312 | symIntExpr->getRHS(), resultTy); |
| 313 | } |
| 314 | } |
| 315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | case nonloc::ConcreteIntKind: { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 317 | if (isa<nonloc::ConcreteInt>(rhs)) { |
| 318 | const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs); |
| 319 | return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs)); |
| 320 | } |
| 321 | else { |
| 322 | // Swap the left and right sides and flip the operator if doing so |
| 323 | // allows us to better reason about the expression (this is a form |
| 324 | // of expression canonicalization). |
| 325 | NonLoc tmp = rhs; |
| 326 | rhs = lhs; |
| 327 | lhs = tmp; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 329 | switch (op) { |
| 330 | case BinaryOperator::LT: op = BinaryOperator::GT; continue; |
| 331 | case BinaryOperator::GT: op = BinaryOperator::LT; continue; |
| 332 | case BinaryOperator::LE: op = BinaryOperator::GE; continue; |
| 333 | case BinaryOperator::GE: op = BinaryOperator::LE; continue; |
| 334 | case BinaryOperator::EQ: |
| 335 | case BinaryOperator::NE: |
| 336 | case BinaryOperator::Add: |
| 337 | case BinaryOperator::Mul: |
| 338 | continue; |
| 339 | default: |
| 340 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | } |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | case nonloc::SymbolValKind: { |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 345 | nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs); |
| 346 | SymbolRef Sym = slhs->getSymbol(); |
| 347 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 348 | // Does the symbol simplify to a constant? If so, "fold" the constant |
| 349 | // by setting 'lhs' to a ConcreteInt and try again. |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 350 | if (Sym->getType(ValMgr.getContext())->isIntegerType()) |
| 351 | if (const llvm::APSInt *Constant = state->getSymVal(Sym)) { |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 352 | // The symbol evaluates to a constant. If necessary, promote the |
| 353 | // folded constant (LHS) to the result type. |
| 354 | BasicValueFactory &BVF = ValMgr.getBasicValueFactory(); |
| 355 | const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant); |
| 356 | lhs = nonloc::ConcreteInt(lhs_I); |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 358 | // Also promote the RHS (if necessary). |
| 359 | |
| 360 | // For shifts, it necessary promote the RHS to the result type. |
| 361 | if (BinaryOperator::isShiftOp(op)) |
| 362 | continue; |
| 363 | |
| 364 | // Other operators: do an implicit conversion. This shouldn't be |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 365 | // necessary once we support truncation/extension of symbolic values. |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 366 | if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){ |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 367 | rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue())); |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 368 | } |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 369 | |
| 370 | continue; |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 373 | if (isa<nonloc::ConcreteInt>(rhs)) { |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 374 | return ValMgr.makeNonLoc(slhs->getSymbol(), op, |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 375 | cast<nonloc::ConcreteInt>(rhs).getValue(), |
| 376 | resultTy); |
| 377 | } |
| 378 | |
| 379 | return UnknownVal(); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | QualType resultTy) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 387 | switch (op) { |
| 388 | default: |
| 389 | return UnknownVal(); |
| 390 | case BinaryOperator::EQ: |
| 391 | case BinaryOperator::NE: |
| 392 | return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy); |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 393 | case BinaryOperator::LT: |
| 394 | case BinaryOperator::GT: |
| 395 | // FIXME: Generalize. For now, just handle the trivial case where |
| 396 | // the two locations are identical. |
| 397 | if (lhs == rhs) |
| 398 | return ValMgr.makeTruthVal(false, resultTy); |
| 399 | return UnknownVal(); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | SVal SimpleSValuator::EvalBinOpLN(const GRState *state, |
| 404 | BinaryOperator::Opcode op, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | Loc lhs, NonLoc rhs, QualType resultTy) { |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 406 | // Special case: 'rhs' is an integer that has the same width as a pointer and |
| 407 | // we are using the integer location in a comparison. Normally this cannot be |
| 408 | // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32 |
| 409 | // can generate comparisons that trigger this code. |
| 410 | // FIXME: Are all locations guaranteed to have pointer width? |
| 411 | if (BinaryOperator::isEqualityOp(op)) { |
| 412 | if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { |
| 413 | const llvm::APSInt *x = &rhsInt->getValue(); |
| 414 | ASTContext &ctx = ValMgr.getContext(); |
| 415 | if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) { |
| 416 | // Convert the signedness of the integer (if necessary). |
| 417 | if (x->isSigned()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | x = &ValMgr.getBasicValueFactory().getValue(*x, true); |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 419 | |
| 420 | return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy); |
| 421 | } |
| 422 | } |
| 423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Ted Kremenek | e839172 | 2009-06-26 00:25:05 +0000 | [diff] [blame] | 425 | // Delegate pointer arithmetic to the StoreManager. |
| 426 | return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs, |
| 427 | rhs, resultTy); |
| 428 | } |