clang 8.0.0

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 "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
22 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23 #include "clang/StaticAnalyzer/Core/Checker.h"
24 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.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
65 ExplodedNode *N = C.generateNonFatalErrorNode();
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 }
ento
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:673
clang::ObjCMethodCall::getSourceRange
SourceRange getSourceRange() const override
Definition: CallEvent.cpp:989
DeclObjC.h
BugType.h
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
ExprEngine.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:137
Checker.h
clang::ObjCMethodCall::getReceiverInterface
const ObjCInterfaceDecl * getReceiverInterface() const
Get the interface for the receiver.
Definition: CallEvent.h:1034
CheckerManager.h
clang::ObjCMethodCall
Represents any expression that calls an Objective-C method.
Definition: CallEvent.h:970
clang::GetNullarySelector
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
Definition: ASTContext.h:2953
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1172
BugReporter.h
BuiltinCheckerRegistration.h
clang::LangOptions::NonGC
Definition: LangOptions.h:54
clang::ObjCMethodCall::getSelector
Selector getSelector() const
Definition: CallEvent.h:1018
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::ObjCMethodCall::isInstanceMessage
bool isInstanceMessage() const
Definition: CallEvent.h:1010
CallEvent.h
CheckerContext.h