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