clang API Documentation
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/StaticAnalyzer/Core/Checker.h" 00020 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00021 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 00022 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 00023 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 00024 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 00025 #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" 00026 #include "clang/AST/DeclObjC.h" 00027 #include "clang/AST/Decl.h" 00028 00029 using namespace clang; 00030 using namespace ento; 00031 00032 namespace { 00033 class NSAutoreleasePoolChecker 00034 : public Checker<check::PreObjCMessage> { 00035 mutable OwningPtr<BugType> BT; 00036 mutable Selector releaseS; 00037 00038 public: 00039 void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const; 00040 }; 00041 00042 } // end anonymous namespace 00043 00044 void NSAutoreleasePoolChecker::checkPreObjCMessage(ObjCMessage msg, 00045 CheckerContext &C) const { 00046 00047 const Expr *receiver = msg.getInstanceReceiver(); 00048 if (!receiver) 00049 return; 00050 00051 // FIXME: Enhance with value-tracking information instead of consulting 00052 // the type of the expression. 00053 const ObjCObjectPointerType* PT = 00054 receiver->getType()->getAs<ObjCObjectPointerType>(); 00055 00056 if (!PT) 00057 return; 00058 const ObjCInterfaceDecl *OD = PT->getInterfaceDecl(); 00059 if (!OD) 00060 return; 00061 if (!OD->getIdentifier()->getName().equals("NSAutoreleasePool")) 00062 return; 00063 00064 if (releaseS.isNull()) 00065 releaseS = GetNullarySelector("release", C.getASTContext()); 00066 // Sending 'release' message? 00067 if (msg.getSelector() != releaseS) 00068 return; 00069 00070 if (!BT) 00071 BT.reset(new BugType("Use -drain instead of -release", 00072 "API Upgrade (Apple)")); 00073 00074 ExplodedNode *N = C.addTransition(); 00075 if (!N) { 00076 assert(0); 00077 return; 00078 } 00079 00080 BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when " 00081 "using NSAutoreleasePool and garbage collection", N); 00082 Report->addRange(msg.getSourceRange()); 00083 C.EmitReport(Report); 00084 } 00085 00086 void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) { 00087 if (mgr.getLangOpts().getGC() != LangOptions::NonGC) 00088 mgr.registerChecker<NSAutoreleasePoolChecker>(); 00089 }