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