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