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 | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 109 | |
| 110 | #ifndef NDEBUG |
| 111 | if (Visualize) CheckerState->ViewGraph(); |
| 112 | #endif |
| 113 | |
| 114 | return Engine.getGraph().size(); |
| 115 | } |
| 116 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 117 | } // end clang namespace |
| 118 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
| 120 | // Transfer function for Casts. |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 123 | RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 125 | if (!isa<nonlval::ConcreteInt>(X)) |
| 126 | return UnknownVal(); |
| 127 | |
| 128 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 129 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 130 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation())); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 132 | if (T->isPointerType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 133 | return lval::ConcreteInt(ValMgr.getValue(V)); |
| 134 | else |
| 135 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
| 136 | } |
| 137 | |
| 138 | // Casts. |
| 139 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 140 | RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, LVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 142 | if (T->isPointerType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 143 | return X; |
| 144 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 145 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 146 | |
| 147 | if (!isa<lval::ConcreteInt>(X)) |
| 148 | return UnknownVal(); |
| 149 | |
| 150 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 151 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 152 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation())); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 153 | |
| 154 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Unary operators. |
| 158 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 159 | RVal GRSimpleVals::EvalMinus(ValueManager& ValMgr, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 161 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 163 | case nonlval::ConcreteIntKind: |
| 164 | return cast<nonlval::ConcreteInt>(X).EvalMinus(ValMgr, U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 166 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 167 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 171 | RVal GRSimpleVals::EvalComplement(ValueManager& ValMgr, NonLVal X) { |
| 172 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 173 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 175 | case nonlval::ConcreteIntKind: |
| 176 | return cast<nonlval::ConcreteInt>(X).EvalComplement(ValMgr); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 178 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 179 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 182 | |
| 183 | // Binary operators. |
| 184 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 185 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 186 | NonLVal L, NonLVal R) { |
| 187 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 189 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 190 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 191 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 192 | |
| 193 | case nonlval::ConcreteIntKind: |
| 194 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 195 | if (isa<nonlval::ConcreteInt>(R)) { |
| 196 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
| 197 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
| 198 | return L_CI.EvalBinOp(ValMgr, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 200 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 201 | NonLVal tmp = R; |
| 202 | R = L; |
| 203 | L = tmp; |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 204 | continue; |
| 205 | } |
| 206 | |
| 207 | case nonlval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 208 | |
| 209 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 210 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 211 | ValMgr.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
| 212 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 213 | |
| 214 | return nonlval::SymIntConstraintVal(C); |
| 215 | } |
| 216 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 217 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 224 | // Binary Operators (except assignments and comma). |
| 225 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 226 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 227 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 229 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 231 | default: |
| 232 | return UnknownVal(); |
| 233 | |
| 234 | case BinaryOperator::EQ: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 235 | return EvalEQ(ValMgr, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 236 | |
| 237 | case BinaryOperator::NE: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 238 | return EvalNE(ValMgr, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 242 | // Pointer arithmetic. |
| 243 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 244 | RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 245 | LVal L, NonLVal R) { |
| 246 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 249 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 251 | RVal GRSimpleVals::EvalEQ(ValueManager& ValMgr, LVal L, LVal R) { |
| 252 | |
| 253 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 255 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 256 | assert(false && "EQ not implemented for this LVal."); |
| 257 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 258 | |
| 259 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 260 | |
| 261 | if (isa<lval::ConcreteInt>(R)) { |
| 262 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 263 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 265 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 266 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 267 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 268 | |
| 269 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 270 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 271 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 272 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 273 | |
| 274 | return nonlval::SymIntConstraintVal(C); |
| 275 | } |
| 276 | |
| 277 | break; |
| 278 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 279 | case lval::SymbolValKind: { |
| 280 | |
| 281 | if (isa<lval::ConcreteInt>(R)) { |
| 282 | const SymIntConstraint& C = |
| 283 | ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
| 284 | BinaryOperator::EQ, |
| 285 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 287 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 290 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 291 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 292 | // Since this is not useful for many checkers we'll punt on this for |
| 293 | // now. |
| 294 | |
| 295 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 296 | } |
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 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 299 | case lval::FuncValKind: |
| 300 | case lval::GotoLabelKind: |
| 301 | return NonLVal::MakeIntTruthVal(ValMgr, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 304 | return NonLVal::MakeIntTruthVal(ValMgr, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 307 | RVal GRSimpleVals::EvalNE(ValueManager& ValMgr, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 309 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 311 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 312 | assert(false && "NE not implemented for this LVal."); |
| 313 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 314 | |
| 315 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 316 | |
| 317 | if (isa<lval::ConcreteInt>(R)) { |
| 318 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 319 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 321 | return NonLVal::MakeIntTruthVal(ValMgr, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 322 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 323 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 324 | const SymIntConstraint& C = |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 325 | ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 326 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 327 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 328 | |
| 329 | return nonlval::SymIntConstraintVal(C); |
| 330 | } |
| 331 | |
| 332 | break; |
| 333 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 334 | case lval::SymbolValKind: { |
| 335 | if (isa<lval::ConcreteInt>(R)) { |
| 336 | const SymIntConstraint& C = |
| 337 | ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
| 338 | BinaryOperator::NE, |
| 339 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 341 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 344 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 345 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 346 | // Since this is not useful for many checkers we'll punt on this for |
| 347 | // now. |
| 348 | |
| 349 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 350 | |
| 351 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 355 | case lval::FuncValKind: |
| 356 | case lval::GotoLabelKind: |
| 357 | return NonLVal::MakeIntTruthVal(ValMgr, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 360 | return NonLVal::MakeIntTruthVal(ValMgr, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 361 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Transfer function for Function Calls. |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 367 | ValueState* |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 368 | GRSimpleVals::EvalCall(ValueStateManager& StateMgr, ValueManager& ValMgr, |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 369 | CallExpr* CE, LVal L, ValueState* St) { |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 370 | |
| 371 | // Invalidate all arguments passed in by reference (LVals). |
| 372 | |
| 373 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 374 | I != E; ++I) { |
| 375 | |
| 376 | RVal V = StateMgr.GetRVal(St, *I); |
| 377 | |
| 378 | if (isa<LVal>(V)) |
| 379 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 380 | } |
| 381 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 382 | return St; |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 383 | } |