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