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