Ted Kremenek | 54cb7cc | 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 | d1e5a89 | 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 | 54cb7cc | 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 | 0b1ba62 | 2011-02-16 01:40:52 +0000 | [diff] [blame] | 18 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Anna Zaks | 48468df | 2011-10-26 21:06:39 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Jordy Rose | d1e5a89 | 2011-09-02 08:02:59 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Jordy Rose | d1e5a89 | 2011-09-02 08:02:59 +0000 | [diff] [blame] | 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" |
Ted Kremenek | 54cb7cc | 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 | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 30 | using namespace ento; |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 33 | class NSAutoreleasePoolChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 34 | : public Checker<check::PreObjCMessage> { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 35 | mutable OwningPtr<BugType> BT; |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 36 | mutable Selector releaseS; |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 39 | void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const; |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | } // end anonymous namespace |
| 43 | |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 44 | void NSAutoreleasePoolChecker::checkPreObjCMessage(ObjCMessage msg, |
| 45 | CheckerContext &C) const { |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 46 | |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 47 | const Expr *receiver = msg.getInstanceReceiver(); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 48 | if (!receiver) |
| 49 | return; |
| 50 | |
| 51 | // FIXME: Enhance with value-tracking information instead of consulting |
| 52 | // the type of the expression. |
| 53 | const ObjCObjectPointerType* PT = |
| 54 | receiver->getType()->getAs<ObjCObjectPointerType>(); |
Ted Kremenek | 71a5e28 | 2009-11-20 00:12:36 +0000 | [diff] [blame] | 55 | |
| 56 | if (!PT) |
| 57 | return; |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 58 | const ObjCInterfaceDecl *OD = PT->getInterfaceDecl(); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 59 | if (!OD) |
| 60 | return; |
| 61 | if (!OD->getIdentifier()->getName().equals("NSAutoreleasePool")) |
| 62 | return; |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 63 | |
| 64 | if (releaseS.isNull()) |
| 65 | releaseS = GetNullarySelector("release", C.getASTContext()); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 66 | // Sending 'release' message? |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 67 | if (msg.getSelector() != releaseS) |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 68 | return; |
Anna Zaks | 48468df | 2011-10-26 21:06:39 +0000 | [diff] [blame] | 69 | |
| 70 | if (!BT) |
| 71 | BT.reset(new BugType("Use -drain instead of -release", |
| 72 | "API Upgrade (Apple)")); |
| 73 | |
| 74 | ExplodedNode *N = C.addTransition(); |
| 75 | if (!N) { |
| 76 | assert(0); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when " |
| 81 | "using NSAutoreleasePool and garbage collection", N); |
| 82 | Report->addRange(msg.getSourceRange()); |
| 83 | C.EmitReport(Report); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 84 | } |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 85 | |
| 86 | void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 87 | if (mgr.getLangOptions().getGC() != LangOptions::NonGC) |
Argyrios Kyrtzidis | 45d9b4e | 2011-02-23 07:19:18 +0000 | [diff] [blame] | 88 | mgr.registerChecker<NSAutoreleasePoolChecker>(); |
| 89 | } |