Home | History | Annotate | Download | only in Checkers
      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/StaticAnalyzer/Core/Checker.h"
     20 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
     21 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
     22 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
     23 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
     24 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
     25 #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
     26 #include "clang/AST/DeclObjC.h"
     27 #include "clang/AST/Decl.h"
     28 
     29 using namespace clang;
     30 using namespace ento;
     31 
     32 namespace {
     33 class NSAutoreleasePoolChecker
     34   : public Checker<check::PreObjCMessage> {
     35   mutable OwningPtr<BugType> BT;
     36   mutable Selector releaseS;
     37 
     38 public:
     39   void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const;
     40 };
     41 
     42 } // end anonymous namespace
     43 
     44 void NSAutoreleasePoolChecker::checkPreObjCMessage(ObjCMessage msg,
     45                                                    CheckerContext &C) const {
     46 
     47   const Expr *receiver = msg.getInstanceReceiver();
     48   if (!receiver)
     49     return;
     50 
     51   // FIXME: Enhance with value-tracking information instead of consulting
     52   // the type of the expression.
     53   const ObjCObjectPointerType* PT =
     54     receiver->getType()->getAs<ObjCObjectPointerType>();
     55 
     56   if (!PT)
     57     return;
     58   const ObjCInterfaceDecl *OD = PT->getInterfaceDecl();
     59   if (!OD)
     60     return;
     61   if (!OD->getIdentifier()->getName().equals("NSAutoreleasePool"))
     62     return;
     63 
     64   if (releaseS.isNull())
     65     releaseS = GetNullarySelector("release", C.getASTContext());
     66   // Sending 'release' message?
     67   if (msg.getSelector() != releaseS)
     68     return;
     69 
     70   if (!BT)
     71     BT.reset(new BugType("Use -drain instead of -release",
     72                          "API Upgrade (Apple)"));
     73 
     74   ExplodedNode *N = C.addTransition();
     75   if (!N) {
     76     assert(0);
     77     return;
     78   }
     79 
     80   BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when "
     81     "using NSAutoreleasePool and garbage collection", N);
     82   Report->addRange(msg.getSourceRange());
     83   C.EmitReport(Report);
     84 }
     85 
     86 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
     87   if (mgr.getLangOpts().getGC() != LangOptions::NonGC)
     88     mgr.registerChecker<NSAutoreleasePoolChecker>();
     89 }
     90