Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 1 | //=- NSAutoreleasePoolChecker.cpp --------------------------------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a NSAutoreleasePoolChecker, a small checker that warns |
| 11 | // about subpar uses of NSAutoreleasePool. Note that while the check itself |
Jordy Rose | 087611e | 2011-09-02 08:02:59 +0000 | [diff] [blame] | 12 | // (in its current form) could be written as a flow-insensitive check, in |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 13 | // can be potentially enhanced in the future with flow-sensitive information. |
| 14 | // It is also a good example of the CheckerVisitor interface. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Argyrios Kyrtzidis | 9d4d4f9 | 2011-02-16 01:40:52 +0000 | [diff] [blame] | 18 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Anna Zaks | 6b1c212 | 2011-10-26 21:06:39 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Jordan Rose | 4f7df9b | 2012-07-26 21:39:41 +0000 | [diff] [blame^] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Jordy Rose | 087611e | 2011-09-02 08:02:59 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
| 27 | #include "clang/AST/Decl.h" |
| 28 | |
| 29 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 30 | using namespace ento; |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 33 | class NSAutoreleasePoolChecker |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 34 | : public Checker<check::PreObjCMessage> { |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 35 | mutable OwningPtr<BugType> BT; |
Argyrios Kyrtzidis | aad8372 | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 36 | mutable Selector releaseS; |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 39 | void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | } // end anonymous namespace |
| 43 | |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 44 | void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg, |
Argyrios Kyrtzidis | aad8372 | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 45 | CheckerContext &C) const { |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 46 | if (!msg.isInstanceMessage()) |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 47 | return; |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 48 | |
| 49 | const ObjCInterfaceDecl *OD = msg.getReceiverInterface(); |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 50 | if (!OD) |
| 51 | return; |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 52 | if (!OD->getIdentifier()->isStr("NSAutoreleasePool")) |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 53 | return; |
Argyrios Kyrtzidis | aad8372 | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 54 | |
| 55 | if (releaseS.isNull()) |
| 56 | releaseS = GetNullarySelector("release", C.getASTContext()); |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 57 | // Sending 'release' message? |
Argyrios Kyrtzidis | 37ab726 | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 58 | if (msg.getSelector() != releaseS) |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 59 | return; |
Anna Zaks | 6b1c212 | 2011-10-26 21:06:39 +0000 | [diff] [blame] | 60 | |
| 61 | if (!BT) |
| 62 | BT.reset(new BugType("Use -drain instead of -release", |
| 63 | "API Upgrade (Apple)")); |
| 64 | |
| 65 | ExplodedNode *N = C.addTransition(); |
| 66 | if (!N) { |
| 67 | assert(0); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when " |
| 72 | "using NSAutoreleasePool and garbage collection", N); |
| 73 | Report->addRange(msg.getSourceRange()); |
| 74 | C.EmitReport(Report); |
Ted Kremenek | 18c7cee | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 75 | } |
Argyrios Kyrtzidis | aad8372 | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 76 | |
| 77 | void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 78 | if (mgr.getLangOpts().getGC() != LangOptions::NonGC) |
Argyrios Kyrtzidis | aad8372 | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 79 | mgr.registerChecker<NSAutoreleasePoolChecker>(); |
| 80 | } |