NSAutoreleasePoolChecker.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  17. #include "clang/AST/Decl.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  20. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  21. #include "clang/StaticAnalyzer/Core/Checker.h"
  22. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  24. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  25. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  26. using namespace clang;
  27. using namespace ento;
  28. namespace {
  29. class NSAutoreleasePoolChecker
  30. : public Checker<check::PreObjCMessage> {
  31. mutable std::unique_ptr<BugType> BT;
  32. mutable Selector releaseS;
  33. public:
  34. void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
  35. };
  36. } // end anonymous namespace
  37. void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
  38. CheckerContext &C) const {
  39. if (!msg.isInstanceMessage())
  40. return;
  41. const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
  42. if (!OD)
  43. return;
  44. if (!OD->getIdentifier()->isStr("NSAutoreleasePool"))
  45. return;
  46. if (releaseS.isNull())
  47. releaseS = GetNullarySelector("release", C.getASTContext());
  48. // Sending 'release' message?
  49. if (msg.getSelector() != releaseS)
  50. return;
  51. if (!BT)
  52. BT.reset(new BugType(this, "Use -drain instead of -release",
  53. "API Upgrade (Apple)"));
  54. ExplodedNode *N = C.generateNonFatalErrorNode();
  55. if (!N) {
  56. assert(0);
  57. return;
  58. }
  59. auto Report = std::make_unique<PathSensitiveBugReport>(
  60. *BT,
  61. "Use -drain instead of -release when using NSAutoreleasePool and "
  62. "garbage collection",
  63. N);
  64. Report->addRange(msg.getSourceRange());
  65. C.emitReport(std::move(Report));
  66. }
  67. void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
  68. mgr.registerChecker<NSAutoreleasePoolChecker>();
  69. }
  70. bool ento::shouldRegisterNSAutoreleasePoolChecker(const CheckerManager &mgr) {
  71. const LangOptions &LO = mgr.getLangOpts();
  72. return LO.getGC() != LangOptions::NonGC;
  73. }