Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1 | // SValuator.cpp - Basic class for all SValuator implementations --*- 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 SValuator, the base class for all (complete) SValuator |
| 11 | // implementations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/SValuator.h" |
| 16 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 20 | |
| 21 | SVal SValuator::EvalBinOp(const GRState *ST, BinaryOperator::Opcode Op, |
| 22 | SVal L, SVal R, QualType T) { |
| 23 | |
| 24 | if (L.isUndef() || R.isUndef()) |
| 25 | return UndefinedVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 27 | if (L.isUnknown() || R.isUnknown()) |
| 28 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 30 | if (isa<Loc>(L)) { |
| 31 | if (isa<Loc>(R)) |
| 32 | return EvalBinOpLL(Op, cast<Loc>(L), cast<Loc>(R), T); |
| 33 | |
| 34 | return EvalBinOpLN(ST, Op, cast<Loc>(L), cast<NonLoc>(R), T); |
| 35 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 37 | if (isa<Loc>(R)) { |
| 38 | // Support pointer arithmetic where the increment/decrement operand |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | // is on the left and the pointer on the right. |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 40 | assert(Op == BinaryOperator::Add || Op == BinaryOperator::Sub); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 42 | // Commute the operands. |
| 43 | return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T); |
| 44 | } |
| 45 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 46 | return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T); |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 49 | DefinedOrUnknownSVal SValuator::EvalEQ(const GRState *ST, |
| 50 | DefinedOrUnknownSVal L, |
| 51 | DefinedOrUnknownSVal R) { |
| 52 | return cast<DefinedOrUnknownSVal>(EvalBinOp(ST, BinaryOperator::EQ, L, R, |
| 53 | ValMgr.getContext().IntTy)); |
| 54 | } |
| 55 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | SValuator::CastResult SValuator::EvalCast(SVal val, const GRState *state, |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 57 | QualType castTy, QualType originalTy){ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 59 | if (val.isUnknownOrUndef() || castTy == originalTy) |
| 60 | return CastResult(state, val); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 62 | ASTContext &C = ValMgr.getContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 64 | // For const casts, just propagate the value. |
Zhongxing Xu | 5ea95fc | 2010-01-05 09:27:03 +0000 | [diff] [blame] | 65 | if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) |
| 66 | if (C.hasSameUnqualifiedType(castTy, originalTy)) |
| 67 | return CastResult(state, val); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Zhongxing Xu | 7b81e8f | 2010-01-14 03:45:06 +0000 | [diff] [blame] | 69 | if (castTy->isIntegerType() && originalTy->isIntegerType()) |
| 70 | return CastResult(state, EvalCastNL(cast<NonLoc>(val), castTy)); |
| 71 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 72 | // Check for casts from pointers to integers. |
| 73 | if (castTy->isIntegerType() && Loc::IsLocType(originalTy)) |
| 74 | return CastResult(state, EvalCastL(cast<Loc>(val), castTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 76 | // Check for casts from integers to pointers. |
| 77 | if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) { |
| 78 | if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) { |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 79 | if (const MemRegion *R = LV->getLoc().getAsRegion()) { |
| 80 | StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager(); |
| 81 | R = storeMgr.CastRegion(R, castTy); |
| 82 | return R ? CastResult(state, loc::MemRegionVal(R)) |
| 83 | : CastResult(state, UnknownVal()); |
| 84 | } |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 85 | return CastResult(state, LV->getLoc()); |
| 86 | } |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 87 | goto DispatchCast; |
| 88 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 90 | // Just pass through function and block pointers. |
| 91 | if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { |
| 92 | assert(Loc::IsLocType(castTy)); |
| 93 | return CastResult(state, val); |
| 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 96 | // Check for casts from array type to another type. |
| 97 | if (originalTy->isArrayType()) { |
| 98 | // We will always decay to a pointer. |
| 99 | val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 101 | // Are we casting from an array to a pointer? If so just pass on |
| 102 | // the decayed value. |
| 103 | if (castTy->isPointerType()) |
| 104 | return CastResult(state, val); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 106 | // Are we casting from an array to an integer? If so, cast the decayed |
| 107 | // pointer value to an integer. |
| 108 | assert(castTy->isIntegerType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 110 | // FIXME: Keep these here for now in case we decide soon that we |
| 111 | // need the original decayed type. |
| 112 | // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); |
| 113 | // QualType pointerTy = C.getPointerType(elemTy); |
| 114 | return CastResult(state, EvalCastL(cast<Loc>(val), castTy)); |
| 115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 117 | // Check for casts from a region to a specific type. |
| 118 | if (const MemRegion *R = val.getAsRegion()) { |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 119 | // FIXME: We should handle the case where we strip off view layers to get |
| 120 | // to a desugared type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 122 | assert(Loc::IsLocType(castTy)); |
| 123 | // We get a symbolic function pointer for a dereference of a function |
| 124 | // pointer, but it is of function type. Example: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 126 | // struct FPRec { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | // void (*my_func)(int * x); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 128 | // }; |
| 129 | // |
| 130 | // int bar(int x); |
| 131 | // |
| 132 | // int f1_a(struct FPRec* foo) { |
| 133 | // int x; |
| 134 | // (*foo->my_func)(&x); |
| 135 | // return bar(x)+1; // no-warning |
| 136 | // } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 138 | assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() || |
| 139 | originalTy->isBlockPointerType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 141 | StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 143 | // Delegate to store manager to get the result of casting a region to a |
| 144 | // different type. If the MemRegion* returned is NULL, this expression |
| 145 | // evaluates to UnknownVal. |
| 146 | R = storeMgr.CastRegion(R, castTy); |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 147 | return R ? CastResult(state, loc::MemRegionVal(R)) |
| 148 | : CastResult(state, UnknownVal()); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 151 | DispatchCast: |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 152 | // All other cases. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 153 | return CastResult(state, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy) |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 155 | : EvalCastNL(cast<NonLoc>(val), castTy)); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 156 | } |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 157 | |
| 158 | SValuator::DefinedOrUnknownCastResult |
| 159 | SValuator::EvalCast(DefinedOrUnknownSVal V, const GRState *ST, |
| 160 | QualType castTy, QualType originalType) { |
| 161 | SValuator::CastResult X = EvalCast((SVal) V, ST, castTy, originalType); |
| 162 | return DefinedOrUnknownCastResult(X.getState(), |
| 163 | cast<DefinedOrUnknownSVal>(X.getSVal())); |
| 164 | } |