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