Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/NSAutoreleasePoolChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=- NSAutoreleasePoolChecker.cpp --------------------------------*- C++ -*-==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines a NSAutoreleasePoolChecker, a small checker that warns
10
//  about subpar uses of NSAutoreleasePool.  Note that while the check itself
11
//  (in its current form) could be written as a flow-insensitive check, in
12
//  can be potentially enhanced in the future with flow-sensitive information.
13
//  It is also a good example of the CheckerVisitor interface.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclObjC.h"
20
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22
#include "clang/StaticAnalyzer/Core/Checker.h"
23
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
25
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
26
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
27
28
using namespace clang;
29
using namespace ento;
30
31
namespace {
32
class NSAutoreleasePoolChecker
33
  : public Checker<check::PreObjCMessage> {
34
  mutable std::unique_ptr<BugType> BT;
35
  mutable Selector releaseS;
36
37
public:
38
  void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
39
};
40
41
} // end anonymous namespace
42
43
void NSAutoreleasePoolChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
44
0
                                                   CheckerContext &C) const {
45
0
  if (!msg.isInstanceMessage())
46
0
    return;
47
0
48
0
  const ObjCInterfaceDecl *OD = msg.getReceiverInterface();
49
0
  if (!OD)
50
0
    return;
51
0
  if (!OD->getIdentifier()->isStr("NSAutoreleasePool"))
52
0
    return;
53
0
54
0
  if (releaseS.isNull())
55
0
    releaseS = GetNullarySelector("release", C.getASTContext());
56
0
  // Sending 'release' message?
57
0
  if (msg.getSelector() != releaseS)
58
0
    return;
59
0
60
0
  if (!BT)
61
0
    BT.reset(new BugType(this, "Use -drain instead of -release",
62
0
                         "API Upgrade (Apple)"));
63
0
64
0
  ExplodedNode *N = C.generateNonFatalErrorNode();
65
0
  if (!N) {
66
0
    assert(0);
67
0
    return;
68
0
  }
69
0
70
0
  auto Report = llvm::make_unique<BugReport>(
71
0
      *BT, "Use -drain instead of -release when using NSAutoreleasePool and "
72
0
           "garbage collection", N);
73
0
  Report->addRange(msg.getSourceRange());
74
0
  C.emitReport(std::move(Report));
75
0
}
76
77
0
void ento::registerNSAutoreleasePoolChecker(CheckerManager &mgr) {
78
0
  mgr.registerChecker<NSAutoreleasePoolChecker>();
79
0
}
80
81
23
bool ento::shouldRegisterNSAutoreleasePoolChecker(const LangOptions &LO) {
82
23
  return LO.getGC() != LangOptions::NonGC;
83
23
}