1218887Sdim//=- NSAutoreleasePoolChecker.cpp --------------------------------*- C++ -*-==//
2218887Sdim//
3218887Sdim//                     The LLVM Compiler Infrastructure
4218887Sdim//
5218887Sdim// This file is distributed under the University of Illinois Open Source
6218887Sdim// License. See LICENSE.TXT for details.
7218887Sdim//
8218887Sdim//===----------------------------------------------------------------------===//
9218887Sdim//
10218887Sdim//  This file defines a NSAutoreleasePoolChecker, a small checker that warns
11218887Sdim//  about subpar uses of NSAutoreleasePool.  Note that while the check itself
12226633Sdim//  (in its current form) could be written as a flow-insensitive check, in
13218887Sdim//  can be potentially enhanced in the future with flow-sensitive information.
14218887Sdim//  It is also a good example of the CheckerVisitor interface.
15218887Sdim//
16218887Sdim//===----------------------------------------------------------------------===//
17218887Sdim
18218887Sdim#include "ClangSACheckers.h"
19249423Sdim#include "clang/AST/Decl.h"
20249423Sdim#include "clang/AST/DeclObjC.h"
21249423Sdim#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
22249423Sdim#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23221345Sdim#include "clang/StaticAnalyzer/Core/Checker.h"
24218887Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h"
25239462Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
26219077Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
27218887Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
28218887Sdim
29218887Sdimusing namespace clang;
30218887Sdimusing namespace ento;
31218887Sdim
32218887Sdimnamespace {
33218887Sdimclass NSAutoreleasePoolChecker
34221345Sdim  : public Checker<check::PreObjCMessage> {
35234353Sdim  mutable OwningPtr<BugType> BT;
36219077Sdim  mutable Selector releaseS;
37218887Sdim
38218887Sdimpublic:
39239462Sdim  void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
40218887Sdim};
41218887Sdim
42218887Sdim} // end anonymous namespace
43218887Sdim
44239462Sdimvoid NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
45219077Sdim                                                   CheckerContext &C) const {
46239462Sdim  if (!msg.isInstanceMessage())
47218887Sdim    return;
48239462Sdim
49239462Sdim  const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
50218887Sdim  if (!OD)
51218887Sdim    return;
52239462Sdim  if (!OD->getIdentifier()->isStr("NSAutoreleasePool"))
53218887Sdim    return;
54219077Sdim
55219077Sdim  if (releaseS.isNull())
56219077Sdim    releaseS = GetNullarySelector("release", C.getASTContext());
57218887Sdim  // Sending 'release' message?
58218887Sdim  if (msg.getSelector() != releaseS)
59218887Sdim    return;
60234353Sdim
61234353Sdim  if (!BT)
62234353Sdim    BT.reset(new BugType("Use -drain instead of -release",
63234353Sdim                         "API Upgrade (Apple)"));
64234353Sdim
65234353Sdim  ExplodedNode *N = C.addTransition();
66234353Sdim  if (!N) {
67234353Sdim    assert(0);
68234353Sdim    return;
69234353Sdim  }
70234353Sdim
71234353Sdim  BugReport *Report = new BugReport(*BT, "Use -drain instead of -release when "
72234353Sdim    "using NSAutoreleasePool and garbage collection", N);
73234353Sdim  Report->addRange(msg.getSourceRange());
74243830Sdim  C.emitReport(Report);
75218887Sdim}
76219077Sdim
77219077Sdimvoid ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
78234353Sdim  if (mgr.getLangOpts().getGC() != LangOptions::NonGC)
79219077Sdim    mgr.registerChecker<NSAutoreleasePoolChecker>();
80219077Sdim}
81