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 | // |
| 10 | // This files defines GRSimpleVals, a sub-class of GRTransferFuncs that |
| 11 | // provides transfer functions for performing simple value tracking with |
| 12 | // limited support for symbolics. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "GRSimpleVals.h" |
Ted Kremenek | 0f5f059 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5c06121 | 2008-02-27 17:56:16 +0000 | [diff] [blame] | 19 | #include <sstream> |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 23 | namespace clang { |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 24 | |
| 25 | template <typename ITERATOR> |
| 26 | static void EmitWarning(Diagnostic& Diag, SourceManager& SrcMgr, |
| 27 | ITERATOR I, ITERATOR E, const char* msg) { |
| 28 | |
Ted Kremenek | 5c06121 | 2008-02-27 17:56:16 +0000 | [diff] [blame] | 29 | std::ostringstream Out; |
| 30 | Out << "[CHECKER] " << msg; |
| 31 | msg = Out.str().c_str(); |
| 32 | |
Ted Kremenek | 2bebc40 | 2008-02-27 20:47:56 +0000 | [diff] [blame] | 33 | bool isFirst = true; |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 34 | unsigned ErrorDiag; |
| 35 | |
| 36 | for (; I != E; ++I) { |
| 37 | |
| 38 | if (isFirst) { |
| 39 | isFirst = false; |
| 40 | ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, msg); |
| 41 | } |
| 42 | |
| 43 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
| 44 | Expr* Exp = cast<Expr>(L.getStmt()); |
| 45 | |
| 46 | Diag.Report(FullSourceLoc(Exp->getExprLoc(), SrcMgr), ErrorDiag); |
| 47 | } |
| 48 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 49 | |
| 50 | unsigned RunGRSimpleVals(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
| 51 | Diagnostic& Diag, bool Visualize) { |
| 52 | |
| 53 | if (Diag.hasErrorOccurred()) |
| 54 | return 0; |
| 55 | |
| 56 | GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx); |
| 57 | GRExprEngine* CheckerState = &Engine.getCheckerState(); |
| 58 | GRSimpleVals GRSV; |
| 59 | CheckerState->setTransferFunctions(GRSV); |
| 60 | |
| 61 | // Execute the worklist algorithm. |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 62 | Engine.ExecuteWorkList(20000); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 64 | SourceManager& SrcMgr = Ctx.getSourceManager(); |
| 65 | |
| 66 | EmitWarning(Diag, SrcMgr, |
| 67 | CheckerState->null_derefs_begin(), |
| 68 | CheckerState->null_derefs_end(), |
| 69 | "NULL pointer is dereferenced after it is checked for NULL."); |
| 70 | |
| 71 | EmitWarning(Diag, SrcMgr, |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 72 | CheckerState->undef_derefs_begin(), |
| 73 | CheckerState->undef_derefs_end(), |
| 74 | "Dereference of undefined value."); |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 75 | |
| 76 | EmitWarning(Diag, SrcMgr, |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 77 | CheckerState->undef_derefs_begin(), |
| 78 | CheckerState->undef_derefs_end(), |
| 79 | "Dereference of undefined value."); |
Ted Kremenek | d87a321 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 80 | |
| 81 | EmitWarning(Diag, SrcMgr, |
| 82 | CheckerState->bad_divides_begin(), |
| 83 | CheckerState->bad_divides_end(), |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 84 | "Division by zero/undefined value."); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame^] | 85 | |
| 86 | EmitWarning(Diag, SrcMgr, |
| 87 | CheckerState->undef_results_begin(), |
| 88 | CheckerState->undef_results_end(), |
| 89 | "Result of operation is undefined."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 90 | |
| 91 | #ifndef NDEBUG |
| 92 | if (Visualize) CheckerState->ViewGraph(); |
| 93 | #endif |
| 94 | |
| 95 | return Engine.getGraph().size(); |
| 96 | } |
| 97 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 98 | } // end clang namespace |
| 99 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 100 | //===----------------------------------------------------------------------===// |
| 101 | // Transfer function for Casts. |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 104 | RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 106 | if (!isa<nonlval::ConcreteInt>(X)) |
| 107 | return UnknownVal(); |
| 108 | |
| 109 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 110 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 111 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation())); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 113 | if (T->isPointerType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 114 | return lval::ConcreteInt(ValMgr.getValue(V)); |
| 115 | else |
| 116 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
| 117 | } |
| 118 | |
| 119 | // Casts. |
| 120 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 121 | RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, LVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 123 | if (T->isPointerType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 124 | return X; |
| 125 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 126 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 127 | |
| 128 | if (!isa<lval::ConcreteInt>(X)) |
| 129 | return UnknownVal(); |
| 130 | |
| 131 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 132 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 133 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation())); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 134 | |
| 135 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Unary operators. |
| 139 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 140 | RVal GRSimpleVals::EvalMinus(ValueManager& ValMgr, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 142 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 144 | case nonlval::ConcreteIntKind: |
| 145 | return cast<nonlval::ConcreteInt>(X).EvalMinus(ValMgr, U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 147 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 148 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 152 | RVal GRSimpleVals::EvalComplement(ValueManager& ValMgr, NonLVal X) { |
| 153 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 154 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 156 | case nonlval::ConcreteIntKind: |
| 157 | return cast<nonlval::ConcreteInt>(X).EvalComplement(ValMgr); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 159 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 160 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 163 | |
| 164 | // Binary operators. |
| 165 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 166 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 167 | NonLVal L, NonLVal R) { |
| 168 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 170 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 171 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 172 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 173 | |
| 174 | case nonlval::ConcreteIntKind: |
| 175 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 176 | if (isa<nonlval::ConcreteInt>(R)) { |
| 177 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
| 178 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
| 179 | return L_CI.EvalBinOp(ValMgr, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 180 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 181 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 182 | NonLVal tmp = R; |
| 183 | R = L; |
| 184 | L = tmp; |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 185 | continue; |
| 186 | } |
| 187 | |
| 188 | case nonlval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 189 | |
| 190 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 191 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 192 | ValMgr.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
| 193 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 194 | |
| 195 | return nonlval::SymIntConstraintVal(C); |
| 196 | } |
| 197 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 198 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 205 | // Binary Operators (except assignments and comma). |
| 206 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 207 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 208 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 210 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 212 | default: |
| 213 | return UnknownVal(); |
| 214 | |
| 215 | case BinaryOperator::EQ: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 216 | return EvalEQ(ValMgr, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 217 | |
| 218 | case BinaryOperator::NE: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 219 | return EvalNE(ValMgr, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 223 | // Pointer arithmetic. |
| 224 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 225 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 226 | LVal L, NonLVal R) { |
| 227 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 230 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 232 | RVal GRSimpleVals::EvalEQ(ValueManager& ValMgr, LVal L, LVal R) { |
| 233 | |
| 234 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 236 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 237 | assert(false && "EQ not implemented for this LVal."); |
| 238 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 239 | |
| 240 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 241 | |
| 242 | if (isa<lval::ConcreteInt>(R)) { |
| 243 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 244 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 246 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 247 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 248 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 249 | |
| 250 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 251 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 252 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 253 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 254 | |
| 255 | return nonlval::SymIntConstraintVal(C); |
| 256 | } |
| 257 | |
| 258 | break; |
| 259 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 260 | case lval::SymbolValKind: { |
| 261 | |
| 262 | if (isa<lval::ConcreteInt>(R)) { |
| 263 | const SymIntConstraint& C = |
| 264 | ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
| 265 | BinaryOperator::EQ, |
| 266 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 268 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 271 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 272 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 273 | // Since this is not useful for many checkers we'll punt on this for |
| 274 | // now. |
| 275 | |
| 276 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 277 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 279 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 280 | case lval::FuncValKind: |
| 281 | case lval::GotoLabelKind: |
| 282 | return NonLVal::MakeIntTruthVal(ValMgr, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 285 | return NonLVal::MakeIntTruthVal(ValMgr, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 288 | RVal GRSimpleVals::EvalNE(ValueManager& ValMgr, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 290 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 292 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 293 | assert(false && "NE not implemented for this LVal."); |
| 294 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 295 | |
| 296 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 297 | |
| 298 | if (isa<lval::ConcreteInt>(R)) { |
| 299 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 300 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 302 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 303 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 304 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 305 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 306 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 307 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 308 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 309 | |
| 310 | return nonlval::SymIntConstraintVal(C); |
| 311 | } |
| 312 | |
| 313 | break; |
| 314 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 315 | case lval::SymbolValKind: { |
| 316 | if (isa<lval::ConcreteInt>(R)) { |
| 317 | const SymIntConstraint& C = |
| 318 | ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
| 319 | BinaryOperator::NE, |
| 320 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 322 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 325 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 326 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 327 | // Since this is not useful for many checkers we'll punt on this for |
| 328 | // now. |
| 329 | |
| 330 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 331 | |
| 332 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 336 | case lval::FuncValKind: |
| 337 | case lval::GotoLabelKind: |
| 338 | return NonLVal::MakeIntTruthVal(ValMgr, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 341 | return NonLVal::MakeIntTruthVal(ValMgr, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 342 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 343 | |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | // Transfer function for Function Calls. |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 348 | ValueState* |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 349 | GRSimpleVals::EvalCall(ValueStateManager& StateMgr, ValueManager& ValMgr, |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 350 | CallExpr* CE, LVal L, ValueState* St) { |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 351 | |
| 352 | // Invalidate all arguments passed in by reference (LVals). |
| 353 | |
| 354 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 355 | I != E; ++I) { |
| 356 | |
| 357 | RVal V = StateMgr.GetRVal(St, *I); |
| 358 | |
| 359 | if (isa<LVal>(V)) |
| 360 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 361 | } |
| 362 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 363 | return St; |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 364 | } |