• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
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 
checkPreObjCMessage(const ObjCMethodCall & msg,CheckerContext & C) const44 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 
registerNSAutoreleasePoolChecker(CheckerManager & mgr)78 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
79   if (mgr.getLangOpts().getGC() != LangOptions::NonGC)
80     mgr.registerChecker<NSAutoreleasePoolChecker>();
81 }
82