clang  6.0.0svn
NSAutoreleasePoolChecker.cpp
Go to the documentation of this file.
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
12 // (in its current form) could be written as a flow-insensitive check, in
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 
18 #include "ClangSACheckers.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
28 
29 using namespace clang;
30 using namespace ento;
31 
32 namespace {
33 class NSAutoreleasePoolChecker
34  : public Checker<check::PreObjCMessage> {
35  mutable std::unique_ptr<BugType> BT;
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("NSAutoreleasePool"))
53  return;
54 
55  if (releaseS.isNull())
56  releaseS = GetNullarySelector("release", C.getASTContext());
57  // Sending 'release' message?
58  if (msg.getSelector() != releaseS)
59  return;
60 
61  if (!BT)
62  BT.reset(new BugType(this, "Use -drain instead of -release",
63  "API Upgrade (Apple)"));
64 
66  if (!N) {
67  assert(0);
68  return;
69  }
70 
71  auto Report = llvm::make_unique<BugReport>(
72  *BT, "Use -drain instead of -release when using NSAutoreleasePool and "
73  "garbage collection", N);
74  Report->addRange(msg.getSourceRange());
75  C.emitReport(std::move(Report));
76 }
77 
78 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
79  if (mgr.getLangOpts().getGC() != LangOptions::NonGC)
80  mgr.registerChecker<NSAutoreleasePoolChecker>();
81 }
Smart pointer class that efficiently represents Objective-C method names.
SourceRange getSourceRange() const override
Definition: CallEvent.cpp:777
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:265
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
const ObjCInterfaceDecl * getReceiverInterface() const
Get the interface for the receiver.
Definition: CallEvent.h:928
Represents any expression that calls an Objective-C method.
Definition: CallEvent.h:870
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
Definition: ASTContext.h:2845
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
ExplodedNode * generateNonFatalErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
Selector getSelector() const
Definition: CallEvent.h:912
Dataflow Directional Tag Classes.
bool isInstanceMessage() const
Definition: CallEvent.h:906
const LangOptions & getLangOpts() const