Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 1 | // GRSimpleVals.cpp - Transfer functions for tracking simple values -*- 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 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines GRSimpleVals, a sub-class of GRTransferFuncs that |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 11 | // provides transfer functions for performing simple value tracking with |
| 12 | // limited support for symbolics. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "GRSimpleVals.h" |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 17 | #include "BasicObjCFoundationChecks.h" |
Ted Kremenek | 87abc03 | 2008-04-02 22:03:53 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 5c06121 | 2008-02-27 17:56:16 +0000 | [diff] [blame] | 25 | #include <sstream> |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 30 | // Transfer Function creation for External clients. |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 33 | GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); } |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // Transfer function for Casts. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 39 | SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLoc X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 40 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 41 | if (!isa<nonloc::ConcreteInt>(X)) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 42 | return UnknownVal(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 43 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 44 | bool isLocType = Loc::IsLocType(T); |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | f496ee1 | 2008-07-15 23:17:54 +0000 | [diff] [blame] | 46 | // Only handle casts from integers to integers. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 47 | if (!isLocType && !T->isIntegerType()) |
Ted Kremenek | f496ee1 | 2008-07-15 23:17:54 +0000 | [diff] [blame] | 48 | return UnknownVal(); |
| 49 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 50 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 51 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 52 | llvm::APSInt V = cast<nonloc::ConcreteInt>(X).getValue(); |
| 53 | V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 54 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 55 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 56 | if (isLocType) |
| 57 | return loc::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 58 | else |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 59 | return nonloc::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Casts. |
| 63 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 64 | SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, Loc X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | e8c2bde | 2008-04-30 21:10:19 +0000 | [diff] [blame] | 66 | // Casts from pointers -> pointers, just return the lval. |
| 67 | // |
| 68 | // Casts from pointers -> references, just return the lval. These |
| 69 | // can be introduced by the frontend for corner cases, e.g |
| 70 | // casting from va_list* to __builtin_va_list&. |
| 71 | // |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 72 | assert (!X.isUnknownOrUndef()); |
| 73 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 74 | if (Loc::IsLocType(T) || T->isReferenceType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 75 | return X; |
| 76 | |
Ted Kremenek | 3f34d80 | 2009-02-10 05:42:58 +0000 | [diff] [blame^] | 77 | // FIXME: Handle transparent unions where a value can be "transparently" |
| 78 | // lifted into a union type. |
| 79 | if (T->isUnionType()) |
| 80 | return UnknownVal(); |
| 81 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 82 | assert (T->isIntegerType()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 83 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 84 | unsigned BitWidth = Eng.getContext().getTypeSize(T); |
| 85 | |
| 86 | if (!isa<loc::ConcreteInt>(X)) |
| 87 | return nonloc::LocAsInteger::Make(BasicVals, X, BitWidth); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 88 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 89 | llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue(); |
| 90 | V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T)); |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 91 | V.extOrTrunc(BitWidth); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 92 | return nonloc::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Unary operators. |
| 96 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 97 | SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 99 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 100 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 101 | case nonloc::ConcreteIntKind: |
| 102 | return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 104 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 105 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 109 | SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 111 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 112 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 113 | case nonloc::ConcreteIntKind: |
| 114 | return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 116 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 117 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 120 | |
| 121 | // Binary operators. |
| 122 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 123 | static unsigned char LNotOpMap[] = { |
| 124 | (unsigned char) BinaryOperator::GE, /* LT => GE */ |
| 125 | (unsigned char) BinaryOperator::LE, /* GT => LE */ |
| 126 | (unsigned char) BinaryOperator::GT, /* LE => GT */ |
| 127 | (unsigned char) BinaryOperator::LT, /* GE => LT */ |
| 128 | (unsigned char) BinaryOperator::NE, /* EQ => NE */ |
| 129 | (unsigned char) BinaryOperator::EQ /* NE => EQ */ |
| 130 | }; |
| 131 | |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 132 | SVal GRSimpleVals::DetermEvalBinOpNN(GRExprEngine& Eng, |
Ted Kremenek | ad8329e | 2008-07-18 15:27:58 +0000 | [diff] [blame] | 133 | BinaryOperator::Opcode Op, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 134 | NonLoc L, NonLoc R) { |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 136 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 137 | unsigned subkind = L.getSubKind(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 139 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 141 | switch (subkind) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 142 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 143 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 145 | case nonloc::LocAsIntegerKind: { |
| 146 | Loc LL = cast<nonloc::LocAsInteger>(L).getLoc(); |
| 147 | |
| 148 | switch (R.getSubKind()) { |
| 149 | case nonloc::LocAsIntegerKind: |
| 150 | return EvalBinOp(Eng, Op, LL, |
| 151 | cast<nonloc::LocAsInteger>(R).getLoc()); |
| 152 | |
| 153 | case nonloc::ConcreteIntKind: { |
| 154 | // Transform the integer into a location and compare. |
| 155 | ASTContext& Ctx = Eng.getContext(); |
| 156 | llvm::APSInt V = cast<nonloc::ConcreteInt>(R).getValue(); |
| 157 | V.setIsUnsigned(true); |
| 158 | V.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy)); |
| 159 | return EvalBinOp(Eng, Op, LL, |
| 160 | loc::ConcreteInt(BasicVals.getValue(V))); |
| 161 | } |
| 162 | |
| 163 | default: |
| 164 | switch (Op) { |
| 165 | case BinaryOperator::EQ: |
| 166 | return NonLoc::MakeIntTruthVal(BasicVals, false); |
| 167 | case BinaryOperator::NE: |
| 168 | return NonLoc::MakeIntTruthVal(BasicVals, true); |
| 169 | default: |
| 170 | // This case also handles pointer arithmetic. |
| 171 | return UnknownVal(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 176 | case nonloc::SymIntConstraintValKind: { |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 177 | |
| 178 | // Logical not? |
| 179 | if (!(Op == BinaryOperator::EQ && R.isZeroConstant())) |
| 180 | return UnknownVal(); |
| 181 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 182 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 183 | cast<nonloc::SymIntConstraintVal>(L).getConstraint(); |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 184 | |
| 185 | BinaryOperator::Opcode Opc = C.getOpcode(); |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 187 | if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE) |
| 188 | return UnknownVal(); |
| 189 | |
| 190 | // For comparison operators, translate the constraint by |
| 191 | // changing the opcode. |
| 192 | |
| 193 | int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT; |
| 194 | |
| 195 | assert (idx >= 0 && |
| 196 | (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char)); |
| 197 | |
| 198 | Opc = (BinaryOperator::Opcode) LNotOpMap[idx]; |
| 199 | |
| 200 | const SymIntConstraint& CNew = |
| 201 | BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt()); |
| 202 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 203 | return nonloc::SymIntConstraintVal(CNew); |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 206 | case nonloc::ConcreteIntKind: |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 207 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 208 | if (isa<nonloc::ConcreteInt>(R)) { |
| 209 | const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L); |
| 210 | const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 211 | return L_CI.EvalBinOp(BasicVals, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 212 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 213 | else { |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 214 | subkind = R.getSubKind(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 215 | NonLoc tmp = R; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 216 | R = L; |
| 217 | L = tmp; |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 218 | |
| 219 | // Swap the operators. |
| 220 | switch (Op) { |
| 221 | case BinaryOperator::LT: Op = BinaryOperator::GT; break; |
| 222 | case BinaryOperator::GT: Op = BinaryOperator::LT; break; |
| 223 | case BinaryOperator::LE: Op = BinaryOperator::GE; break; |
| 224 | case BinaryOperator::GE: Op = BinaryOperator::LE; break; |
| 225 | default: break; |
| 226 | } |
| 227 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 228 | continue; |
| 229 | } |
| 230 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 231 | case nonloc::SymbolValKind: |
| 232 | if (isa<nonloc::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 233 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 234 | BasicVals.getConstraint(cast<nonloc::SymbolVal>(L).getSymbol(), Op, |
| 235 | cast<nonloc::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 236 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 237 | return nonloc::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 238 | } |
| 239 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 240 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 246 | // Binary Operators (except assignments and comma). |
| 247 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 248 | SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
| 249 | Loc L, Loc R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 251 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 253 | default: |
| 254 | return UnknownVal(); |
| 255 | |
| 256 | case BinaryOperator::EQ: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 257 | return EvalEQ(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 258 | |
| 259 | case BinaryOperator::NE: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 260 | return EvalNE(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 264 | // Pointer arithmetic. |
| 265 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 266 | SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
| 267 | Loc L, NonLoc R) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 268 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 271 | // Equality operators for Locs. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 272 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 273 | SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 274 | |
| 275 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 276 | |
| 277 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 279 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 280 | assert(false && "EQ not implemented for this Loc."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 281 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 282 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 283 | case loc::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 284 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 285 | if (isa<loc::ConcreteInt>(R)) { |
| 286 | bool b = cast<loc::ConcreteInt>(L).getValue() == |
| 287 | cast<loc::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 288 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 289 | return NonLoc::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 290 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 291 | else if (isa<loc::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 292 | |
| 293 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 294 | BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 295 | BinaryOperator::EQ, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 296 | cast<loc::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 297 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 298 | return nonloc::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | break; |
| 302 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 303 | case loc::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 304 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 305 | if (isa<loc::ConcreteInt>(R)) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 306 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 307 | BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 308 | BinaryOperator::EQ, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 309 | cast<loc::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 310 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 311 | return nonloc::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 314 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 315 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 316 | // Since this is not useful for many checkers we'll punt on this for |
| 317 | // now. |
| 318 | |
| 319 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 320 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 321 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 322 | case loc::MemRegionKind: |
| 323 | case loc::FuncValKind: |
| 324 | case loc::GotoLabelKind: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 325 | return NonLoc::MakeIntTruthVal(BasicVals, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 328 | return NonLoc::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 331 | SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 333 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 334 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 335 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 337 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 338 | assert(false && "NE not implemented for this Loc."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 339 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 340 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 341 | case loc::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 342 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 343 | if (isa<loc::ConcreteInt>(R)) { |
| 344 | bool b = cast<loc::ConcreteInt>(L).getValue() != |
| 345 | cast<loc::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 346 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 347 | return NonLoc::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 348 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 349 | else if (isa<loc::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 350 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 351 | BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 352 | BinaryOperator::NE, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 353 | cast<loc::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 354 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 355 | return nonloc::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | break; |
| 359 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 360 | case loc::SymbolValKind: { |
| 361 | if (isa<loc::ConcreteInt>(R)) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 362 | const SymIntConstraint& C = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 363 | BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 364 | BinaryOperator::NE, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 365 | cast<loc::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 366 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 367 | return nonloc::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 370 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 371 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 372 | // Since this is not useful for many checkers we'll punt on this for |
| 373 | // now. |
| 374 | |
| 375 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 376 | |
| 377 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 380 | case loc::MemRegionKind: |
| 381 | case loc::FuncValKind: |
| 382 | case loc::GotoLabelKind: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 383 | return NonLoc::MakeIntTruthVal(BasicVals, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 386 | return NonLoc::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 388 | |
| 389 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 390 | // Transfer function for function calls. |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 391 | //===----------------------------------------------------------------------===// |
| 392 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 393 | void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 394 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 395 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 396 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 397 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 399 | GRStateManager& StateMgr = Eng.getStateManager(); |
| 400 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 401 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 402 | // Invalidate all arguments passed in by reference (Locs). |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 403 | |
| 404 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 405 | I != E; ++I) { |
| 406 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 407 | SVal V = StateMgr.GetSVal(St, *I); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 408 | |
Zhongxing Xu | d03eea0 | 2008-10-27 09:00:08 +0000 | [diff] [blame] | 409 | if (isa<loc::MemRegionVal>(V)) |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 410 | St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal()); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 411 | else if (isa<nonloc::LocAsInteger>(V)) |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 412 | St = StateMgr.BindLoc(St, cast<nonloc::LocAsInteger>(V).getLoc(), |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 413 | UnknownVal()); |
| 414 | |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 415 | } |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 417 | // Make up a symbol for the return value of this function. |
| 418 | // FIXME: We eventually should handle structs and other compound types |
| 419 | // that are returned by value. |
| 420 | QualType T = CE->getType(); |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 421 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 422 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 423 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 424 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 425 | SVal X = Loc::IsLocType(CE->getType()) |
| 426 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 427 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 428 | |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 429 | St = StateMgr.BindExpr(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 430 | } |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 432 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 433 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 434 | |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | // Transfer function for Objective-C message expressions. |
| 437 | //===----------------------------------------------------------------------===// |
| 438 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 439 | void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 440 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 441 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 442 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 443 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 444 | |
| 445 | |
| 446 | // The basic transfer function logic for message expressions does nothing. |
| 447 | // We just invalidate all arguments passed in by references. |
| 448 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 449 | GRStateManager& StateMgr = Eng.getStateManager(); |
| 450 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 451 | |
| 452 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 453 | I != E; ++I) { |
| 454 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 455 | SVal V = StateMgr.GetSVal(St, *I); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 456 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 457 | if (isa<Loc>(V)) |
Zhongxing Xu | 8cd5aae | 2008-10-30 05:33:54 +0000 | [diff] [blame] | 458 | St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal()); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | Builder.MakeNode(Dst, ME, Pred, St); |
| 462 | } |