clang  15.0.0git
NSAutoreleasePoolChecker.cpp
Go to the documentation of this file.
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 
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclObjC.h"
27 
28 using namespace clang;
29 using namespace ento;
30 
31 namespace {
32 class NSAutoreleasePoolChecker
33  : public Checker<check::PreObjCMessage> {
34  mutable std::unique_ptr<BugType> BT;
35  mutable Selector releaseS;
36 
37 public:
38  void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
39 };
40 
41 } // end anonymous namespace
42 
43 void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
44  CheckerContext &C) const {
45  if (!msg.isInstanceMessage())
46  return;
47 
48  const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
49  if (!OD)
50  return;
51  if (!OD->getIdentifier()->isStr("NSAutoreleasePool"))
52  return;
53 
54  if (releaseS.isNull())
55  releaseS = GetNullarySelector("release", C.getASTContext());
56  // Sending 'release' message?
57  if (msg.getSelector() != releaseS)
58  return;
59 
60  if (!BT)
61  BT.reset(new BugType(this, "Use -drain instead of -release",
62  "API Upgrade (Apple)"));
63 
64  ExplodedNode *N = C.generateNonFatalErrorNode();
65  if (!N) {
66  assert(0);
67  return;
68  }
69 
70  auto Report = std::make_unique<PathSensitiveBugReport>(
71  *BT,
72  "Use -drain instead of -release when using NSAutoreleasePool and "
73  "garbage collection",
74  N);
75  Report->addRange(msg.getSourceRange());
76  C.emitReport(std::move(Report));
77 }
78 
79 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
80  mgr.registerChecker<NSAutoreleasePoolChecker>();
81 }
82 
83 bool ento::shouldRegisterNSAutoreleasePoolChecker(const CheckerManager &mgr) {
84  const LangOptions &LO = mgr.getLangOpts();
85  return LO.getGC() != LangOptions::NonGC;
86 }
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1150
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:55
Decl.h
CallEvent.h
DeclObjC.h
clang::IdentifierInfo::isStr
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Definition: IdentifierTable.h:176
BuiltinCheckerRegistration.h
CheckerManager.h
BugReporter.h
BugType.h
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:268
clang::LangOptions::NonGC
@ NonGC
Definition: LangOptions.h:83
CheckerContext.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:78
Checker.h
ExprEngine.h
clang
Definition: CalledOnceCheck.h:17
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:752
clang::GetNullarySelector
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
Definition: ASTContext.h:3316