Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 1 | // SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*- |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 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 | // |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 10 | // This file defines SValBuilder, the base class for all (complete) SValBuilder |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 11 | // implementations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/SValBuilder.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 21 | SVal SValBuilder::EvalBinOp(const GRState *ST, BinaryOperator::Opcode Op, |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 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)) |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 32 | return EvalBinOpLL(ST, Op, cast<Loc>(L), cast<Loc>(R), T); |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 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)) { |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 38 | // Support pointer arithmetic where the addend is on the left |
| 39 | // and the pointer on the right. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 40 | assert(Op == BO_Add); |
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 | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 49 | DefinedOrUnknownSVal SValBuilder::EvalEQ(const GRState *ST, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 50 | DefinedOrUnknownSVal L, |
| 51 | DefinedOrUnknownSVal R) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 52 | return cast<DefinedOrUnknownSVal>(EvalBinOp(ST, BO_EQ, L, R, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 53 | ValMgr.getContext().IntTy)); |
| 54 | } |
| 55 | |
Zhongxing Xu | dc1ad2c | 2010-11-26 07:15:40 +0000 | [diff] [blame] | 56 | // FIXME: should rewrite according to the cast kind. |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 57 | SVal SValBuilder::EvalCast(SVal val, QualType castTy, QualType originalTy) { |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 58 | if (val.isUnknownOrUndef() || castTy == originalTy) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 59 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 61 | ASTContext &C = ValMgr.getContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 63 | // For const casts, just propagate the value. |
Zhongxing Xu | 5ea95fc | 2010-01-05 09:27:03 +0000 | [diff] [blame] | 64 | if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) |
| 65 | if (C.hasSameUnqualifiedType(castTy, originalTy)) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 66 | return val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | f681704 | 2010-02-02 21:11:40 +0000 | [diff] [blame] | 68 | // Check for casts to real or complex numbers. We don't handle these at all |
| 69 | // right now. |
| 70 | if (castTy->isFloatingType() || castTy->isAnyComplexType()) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 71 | return UnknownVal(); |
Ted Kremenek | f681704 | 2010-02-02 21:11:40 +0000 | [diff] [blame] | 72 | |
| 73 | // Check for casts from integers to integers. |
Zhongxing Xu | 7b81e8f | 2010-01-14 03:45:06 +0000 | [diff] [blame] | 74 | if (castTy->isIntegerType() && originalTy->isIntegerType()) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 75 | return EvalCastNL(cast<NonLoc>(val), castTy); |
Zhongxing Xu | 7b81e8f | 2010-01-14 03:45:06 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 77 | // Check for casts from pointers to integers. |
| 78 | if (castTy->isIntegerType() && Loc::IsLocType(originalTy)) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 79 | return EvalCastL(cast<Loc>(val), castTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 81 | // Check for casts from integers to pointers. |
| 82 | if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) { |
| 83 | if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) { |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 84 | if (const MemRegion *R = LV->getLoc().getAsRegion()) { |
| 85 | StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager(); |
| 86 | R = storeMgr.CastRegion(R, castTy); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 87 | return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 88 | } |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 89 | return LV->getLoc(); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 90 | } |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 91 | goto DispatchCast; |
| 92 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 94 | // Just pass through function and block pointers. |
| 95 | if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { |
| 96 | assert(Loc::IsLocType(castTy)); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 97 | return val; |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 98 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 100 | // Check for casts from array type to another type. |
| 101 | if (originalTy->isArrayType()) { |
| 102 | // We will always decay to a pointer. |
| 103 | val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 105 | // Are we casting from an array to a pointer? If so just pass on |
| 106 | // the decayed value. |
| 107 | if (castTy->isPointerType()) |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 108 | return val; |
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 | // Are we casting from an array to an integer? If so, cast the decayed |
| 111 | // pointer value to an integer. |
| 112 | assert(castTy->isIntegerType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 114 | // FIXME: Keep these here for now in case we decide soon that we |
| 115 | // need the original decayed type. |
| 116 | // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); |
| 117 | // QualType pointerTy = C.getPointerType(elemTy); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 118 | return EvalCastL(cast<Loc>(val), castTy); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 121 | // Check for casts from a region to a specific type. |
| 122 | if (const MemRegion *R = val.getAsRegion()) { |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 123 | // FIXME: We should handle the case where we strip off view layers to get |
| 124 | // to a desugared type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 948163b | 2010-11-15 20:09:42 +0000 | [diff] [blame] | 126 | if (!Loc::IsLocType(castTy)) { |
| 127 | // FIXME: There can be gross cases where one casts the result of a function |
| 128 | // (that returns a pointer) to some other value that happens to fit |
| 129 | // within that pointer value. We currently have no good way to |
| 130 | // model such operations. When this happens, the underlying operation |
| 131 | // is that the caller is reasoning about bits. Conceptually we are |
| 132 | // layering a "view" of a location on top of those bits. Perhaps |
| 133 | // we need to be more lazy about mutual possible views, even on an |
| 134 | // SVal? This may be necessary for bit-level reasoning as well. |
| 135 | return UnknownVal(); |
| 136 | } |
| 137 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 138 | // We get a symbolic function pointer for a dereference of a function |
| 139 | // pointer, but it is of function type. Example: |
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 | // struct FPRec { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | // void (*my_func)(int * x); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 143 | // }; |
| 144 | // |
| 145 | // int bar(int x); |
| 146 | // |
| 147 | // int f1_a(struct FPRec* foo) { |
| 148 | // int x; |
| 149 | // (*foo->my_func)(&x); |
| 150 | // return bar(x)+1; // no-warning |
| 151 | // } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 153 | assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() || |
| 154 | originalTy->isBlockPointerType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 156 | StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 158 | // Delegate to store manager to get the result of casting a region to a |
| 159 | // different type. If the MemRegion* returned is NULL, this expression |
| 160 | // evaluates to UnknownVal. |
| 161 | R = storeMgr.CastRegion(R, castTy); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 162 | return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 165 | DispatchCast: |
Ted Kremenek | 5bbc8e7 | 2009-12-23 02:52:14 +0000 | [diff] [blame] | 166 | // All other cases. |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 167 | return isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy) |
| 168 | : EvalCastNL(cast<NonLoc>(val), castTy); |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 169 | } |