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