clang API Documentation

NSAutoreleasePoolChecker.cpp
Go to the documentation of this file.
00001 //=- NSAutoreleasePoolChecker.cpp --------------------------------*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines a NSAutoreleasePoolChecker, a small checker that warns
00011 //  about subpar uses of NSAutoreleasePool.  Note that while the check itself
00012 //  (in its current form) could be written as a flow-insensitive check, in
00013 //  can be potentially enhanced in the future with flow-sensitive information.
00014 //  It is also a good example of the CheckerVisitor interface. 
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "ClangSACheckers.h"
00019 #include "clang/AST/Decl.h"
00020 #include "clang/AST/DeclObjC.h"
00021 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
00022 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00023 #include "clang/StaticAnalyzer/Core/Checker.h"
00024 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00025 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
00026 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00027 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
00028 
00029 using namespace clang;
00030 using namespace ento;
00031 
00032 namespace {
00033 class NSAutoreleasePoolChecker
00034   : public Checker<check::PreObjCMessage> {
00035   mutable std::unique_ptr<BugType> BT;
00036   mutable Selector releaseS;
00037 
00038 public:
00039   void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
00040 };
00041 
00042 } // end anonymous namespace
00043 
00044 void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
00045                                                    CheckerContext &C) const {
00046   if (!msg.isInstanceMessage())
00047     return;
00048 
00049   const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
00050   if (!OD)
00051     return;  
00052   if (!OD->getIdentifier()->isStr("NSAutoreleasePool"))
00053     return;
00054 
00055   if (releaseS.isNull())
00056     releaseS = GetNullarySelector("release", C.getASTContext());
00057   // Sending 'release' message?
00058   if (msg.getSelector() != releaseS)
00059     return;
00060 
00061   if (!BT)
00062     BT.reset(new BugType(this, "Use -drain instead of -release",
00063                          "API Upgrade (Apple)"));
00064 
00065   ExplodedNode *N = C.addTransition();
00066   if (!N) {
00067     assert(0);
00068     return;
00069   }
00070 
00071   BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when "
00072     "using NSAutoreleasePool and garbage collection", N);
00073   Report->addRange(msg.getSourceRange());
00074   C.emitReport(Report);
00075 }
00076 
00077 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
00078   if (mgr.getLangOpts().getGC() != LangOptions::NonGC)
00079     mgr.registerChecker<NSAutoreleasePoolChecker>();
00080 }