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 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 39 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 41 | if (!isa<nonlval::ConcreteInt>(X)) |
| 42 | return UnknownVal(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 44 | bool isLValType = LVal::IsLValType(T); |
| 45 | |
Ted Kremenek | f496ee1 | 2008-07-15 23:17:54 +0000 | [diff] [blame] | 46 | // Only handle casts from integers to integers. |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 47 | if (!isLValType && !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 | |
| 52 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 53 | V.setIsUnsigned(T->isUnsignedIntegerType() || LVal::IsLValType(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 | |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 56 | if (isLValType) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 57 | return lval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 58 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 59 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Casts. |
| 63 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 64 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal 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 | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 72 | if (LVal::IsLValType(T) || T->isReferenceType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 73 | return X; |
| 74 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 75 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 76 | |
| 77 | if (!isa<lval::ConcreteInt>(X)) |
| 78 | return UnknownVal(); |
| 79 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 80 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 81 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 82 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 83 | V.setIsUnsigned(T->isUnsignedIntegerType() || LVal::IsLValType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 84 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 86 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Unary operators. |
| 90 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 91 | RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 93 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 95 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 96 | return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 98 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 99 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 103 | RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 105 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 107 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 108 | return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 110 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 111 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 114 | |
| 115 | // Binary operators. |
| 116 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 117 | static unsigned char LNotOpMap[] = { |
| 118 | (unsigned char) BinaryOperator::GE, /* LT => GE */ |
| 119 | (unsigned char) BinaryOperator::LE, /* GT => LE */ |
| 120 | (unsigned char) BinaryOperator::GT, /* LE => GT */ |
| 121 | (unsigned char) BinaryOperator::LT, /* GE => LT */ |
| 122 | (unsigned char) BinaryOperator::NE, /* EQ => NE */ |
| 123 | (unsigned char) BinaryOperator::EQ /* NE => EQ */ |
| 124 | }; |
| 125 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 126 | RVal GRSimpleVals::DetermEvalBinOpNN(GRStateManager& StateMgr, |
Ted Kremenek | ad8329e | 2008-07-18 15:27:58 +0000 | [diff] [blame] | 127 | BinaryOperator::Opcode Op, |
| 128 | NonLVal L, NonLVal R) { |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 6297a8e | 2008-07-18 05:53:58 +0000 | [diff] [blame] | 130 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 131 | unsigned subkind = L.getSubKind(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 133 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 135 | switch (subkind) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 136 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 137 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 139 | case nonlval::SymIntConstraintValKind: { |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 140 | |
| 141 | // Logical not? |
| 142 | if (!(Op == BinaryOperator::EQ && R.isZeroConstant())) |
| 143 | return UnknownVal(); |
| 144 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 145 | const SymIntConstraint& C = |
| 146 | cast<nonlval::SymIntConstraintVal>(L).getConstraint(); |
| 147 | |
| 148 | BinaryOperator::Opcode Opc = C.getOpcode(); |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 1e38f85 | 2008-07-18 15:46:06 +0000 | [diff] [blame] | 150 | if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE) |
| 151 | return UnknownVal(); |
| 152 | |
| 153 | // For comparison operators, translate the constraint by |
| 154 | // changing the opcode. |
| 155 | |
| 156 | int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT; |
| 157 | |
| 158 | assert (idx >= 0 && |
| 159 | (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char)); |
| 160 | |
| 161 | Opc = (BinaryOperator::Opcode) LNotOpMap[idx]; |
| 162 | |
| 163 | const SymIntConstraint& CNew = |
| 164 | BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt()); |
| 165 | |
| 166 | return nonlval::SymIntConstraintVal(CNew); |
| 167 | } |
| 168 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 169 | case nonlval::ConcreteIntKind: |
| 170 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 171 | if (isa<nonlval::ConcreteInt>(R)) { |
| 172 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 173 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 174 | return L_CI.EvalBinOp(BasicVals, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 175 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 176 | else { |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 177 | subkind = R.getSubKind(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 178 | NonLVal tmp = R; |
| 179 | R = L; |
| 180 | L = tmp; |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 181 | |
| 182 | // Swap the operators. |
| 183 | switch (Op) { |
| 184 | case BinaryOperator::LT: Op = BinaryOperator::GT; break; |
| 185 | case BinaryOperator::GT: Op = BinaryOperator::LT; break; |
| 186 | case BinaryOperator::LE: Op = BinaryOperator::GE; break; |
| 187 | case BinaryOperator::GE: Op = BinaryOperator::LE; break; |
| 188 | default: break; |
| 189 | } |
| 190 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
Ted Kremenek | 2675875 | 2008-09-19 17:31:13 +0000 | [diff] [blame] | 194 | case nonlval::SymbolValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 195 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 196 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 197 | BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 198 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 199 | |
| 200 | return nonlval::SymIntConstraintVal(C); |
| 201 | } |
| 202 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 203 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 209 | // Binary Operators (except assignments and comma). |
| 210 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 211 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 212 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 214 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 216 | default: |
| 217 | return UnknownVal(); |
| 218 | |
| 219 | case BinaryOperator::EQ: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 220 | return EvalEQ(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 221 | |
| 222 | case BinaryOperator::NE: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 223 | return EvalNE(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 227 | // Pointer arithmetic. |
| 228 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 229 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 230 | LVal L, NonLVal R) { |
| 231 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 234 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 236 | RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) { |
| 237 | |
| 238 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 239 | |
| 240 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 241 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 242 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 243 | assert(false && "EQ not implemented for this LVal."); |
| 244 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 245 | |
| 246 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 247 | |
| 248 | if (isa<lval::ConcreteInt>(R)) { |
| 249 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 250 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 252 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 253 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 254 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 255 | |
| 256 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 257 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 258 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 259 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 260 | |
| 261 | return nonlval::SymIntConstraintVal(C); |
| 262 | } |
| 263 | |
| 264 | break; |
| 265 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 266 | case lval::SymbolValKind: { |
| 267 | |
| 268 | if (isa<lval::ConcreteInt>(R)) { |
| 269 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 270 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 271 | BinaryOperator::EQ, |
| 272 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 274 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 277 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 278 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 279 | // Since this is not useful for many checkers we'll punt on this for |
| 280 | // now. |
| 281 | |
| 282 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 283 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 285 | // FIXME: Different offsets can map to the same memory cell. |
| 286 | case lval::ArrayOffsetKind: |
| 287 | case lval::FieldOffsetKind: |
| 288 | // Fall-through. |
| 289 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 290 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 291 | case lval::FuncValKind: |
| 292 | case lval::GotoLabelKind: |
Ted Kremenek | 666de3b | 2008-05-12 17:41:30 +0000 | [diff] [blame] | 293 | case lval::StringLiteralValKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 294 | return NonLVal::MakeIntTruthVal(BasicVals, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 297 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 300 | RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 302 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 303 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 304 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 306 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 307 | assert(false && "NE not implemented for this LVal."); |
| 308 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 309 | |
| 310 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 311 | |
| 312 | if (isa<lval::ConcreteInt>(R)) { |
| 313 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 314 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 316 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 317 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 318 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 319 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 320 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 321 | BinaryOperator::NE, |
| 322 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 323 | |
| 324 | return nonlval::SymIntConstraintVal(C); |
| 325 | } |
| 326 | |
| 327 | break; |
| 328 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 329 | case lval::SymbolValKind: { |
| 330 | if (isa<lval::ConcreteInt>(R)) { |
| 331 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 332 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 333 | BinaryOperator::NE, |
| 334 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 336 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 339 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 340 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 341 | // Since this is not useful for many checkers we'll punt on this for |
| 342 | // now. |
| 343 | |
| 344 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 345 | |
| 346 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 349 | // FIXME: Different offsets can map to the same memory cell. |
| 350 | case lval::ArrayOffsetKind: |
| 351 | case lval::FieldOffsetKind: |
| 352 | // Fall-through. |
| 353 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 354 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 355 | case lval::FuncValKind: |
| 356 | case lval::GotoLabelKind: |
Ted Kremenek | 666de3b | 2008-05-12 17:41:30 +0000 | [diff] [blame] | 357 | case lval::StringLiteralValKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 358 | return NonLVal::MakeIntTruthVal(BasicVals, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 361 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 362 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 363 | |
| 364 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 365 | // Transfer function for function calls. |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 368 | void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 369 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 370 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 371 | CallExpr* CE, RVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 372 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 374 | GRStateManager& StateMgr = Eng.getStateManager(); |
| 375 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 376 | |
| 377 | // Invalidate all arguments passed in by reference (LVals). |
| 378 | |
| 379 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 380 | I != E; ++I) { |
| 381 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 382 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 383 | |
| 384 | if (isa<LVal>(V)) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 385 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 386 | else if (isa<nonlval::LValAsInteger>(V)) |
| 387 | St = StateMgr.SetRVal(St, cast<nonlval::LValAsInteger>(V).getLVal(), |
| 388 | UnknownVal()); |
| 389 | |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 390 | } |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 391 | |
| 392 | // Make up a symbol for the return value of this function. |
| 393 | |
| 394 | if (CE->getType() != Eng.getContext().VoidTy) { |
| 395 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 396 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 398 | RVal X = LVal::IsLValType(CE->getType()) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 399 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 400 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 401 | |
| 402 | St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
| 403 | } |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 405 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 406 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 407 | |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | // Transfer function for Objective-C message expressions. |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 412 | void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 413 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 414 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 415 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 416 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 417 | |
| 418 | |
| 419 | // The basic transfer function logic for message expressions does nothing. |
| 420 | // We just invalidate all arguments passed in by references. |
| 421 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 422 | GRStateManager& StateMgr = Eng.getStateManager(); |
| 423 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 424 | |
| 425 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 426 | I != E; ++I) { |
| 427 | |
| 428 | RVal V = StateMgr.GetRVal(St, *I); |
| 429 | |
| 430 | if (isa<LVal>(V)) |
| 431 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 432 | } |
| 433 | |
| 434 | Builder.MakeNode(Dst, ME, Pred, St); |
| 435 | } |