| 1 | //=- NSAutoreleasePoolChecker.cpp --------------------------------*- C++ -*-==// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a NSAutoreleasePoolChecker, a small checker that warns |
| 10 | // about subpar uses of NSAutoreleasePool. Note that while the check itself |
| 11 | // (in its current form) could be written as a flow-insensitive check, in |
| 12 | // can be potentially enhanced in the future with flow-sensitive information. |
| 13 | // It is also a good example of the CheckerVisitor interface. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 21 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 22 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 23 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 26 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | using namespace ento; |
| 30 | |
| 31 | namespace { |
| 32 | class NSAutoreleasePoolChecker |
| 33 | : public Checker<check::PreObjCMessage> { |
| 34 | const BugType BT{this, "Use -drain instead of -release" , |
| 35 | "API Upgrade (Apple)" }; |
| 36 | mutable Selector releaseS; |
| 37 | |
| 38 | public: |
| 39 | void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; |
| 40 | }; |
| 41 | |
| 42 | } // end anonymous namespace |
| 43 | |
| 44 | void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg, |
| 45 | CheckerContext &C) const { |
| 46 | if (!msg.isInstanceMessage()) |
| 47 | return; |
| 48 | |
| 49 | const ObjCInterfaceDecl *OD = msg.getReceiverInterface(); |
| 50 | if (!OD) |
| 51 | return; |
| 52 | if (!OD->getIdentifier()->isStr(Str: "NSAutoreleasePool" )) |
| 53 | return; |
| 54 | |
| 55 | if (releaseS.isNull()) |
| 56 | releaseS = GetNullarySelector(name: "release" , Ctx&: C.getASTContext()); |
| 57 | // Sending 'release' message? |
| 58 | if (msg.getSelector() != releaseS) |
| 59 | return; |
| 60 | |
| 61 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 62 | if (!N) { |
| 63 | assert(0); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | auto Report = std::make_unique<PathSensitiveBugReport>( |
| 68 | args: BT, |
| 69 | args: "Use -drain instead of -release when using NSAutoreleasePool and " |
| 70 | "garbage collection" , |
| 71 | args&: N); |
| 72 | Report->addRange(R: msg.getSourceRange()); |
| 73 | C.emitReport(R: std::move(Report)); |
| 74 | } |
| 75 | |
| 76 | void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) { |
| 77 | mgr.registerChecker<NSAutoreleasePoolChecker>(); |
| 78 | } |
| 79 | |
| 80 | bool ento::shouldRegisterNSAutoreleasePoolChecker(const CheckerManager &mgr) { |
| 81 | const LangOptions &LO = mgr.getLangOpts(); |
| 82 | return LO.getGC() != LangOptions::NonGC; |
| 83 | } |
| 84 | |