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