Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp
Line
Count
Source
1
//===- OSObjectCStyleCast.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 OSObjectCStyleCast checker, which checks for C-style casts
10
// of OSObjects. Such casts almost always indicate a code smell,
11
// as an explicit static or dynamic cast should be used instead.
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/ASTMatchers/ASTMatchFinder.h"
16
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
17
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18
#include "clang/StaticAnalyzer/Core/Checker.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20
#include "llvm/Support/Debug.h"
21
22
using namespace clang;
23
using namespace ento;
24
using namespace ast_matchers;
25
26
namespace {
27
28
const char *WarnAtNode = "OSObjCast";
29
30
class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> {
31
public:
32
  void checkASTCodeBody(const Decl *D,
33
                        AnalysisManager &AM,
34
                        BugReporter &BR) const;
35
};
36
37
static void emitDiagnostics(const BoundNodes &Nodes,
38
                            BugReporter &BR,
39
                            AnalysisDeclContext *ADC,
40
1
                            const OSObjectCStyleCastChecker *Checker) {
41
1
  const auto *CE = Nodes.getNodeAs<CastExpr>(WarnAtNode);
42
1
  assert(CE);
43
1
44
1
  std::string Diagnostics;
45
1
  llvm::raw_string_ostream OS(Diagnostics);
46
1
  OS << "C-style cast of OSObject. Use OSDynamicCast instead.";
47
1
48
1
  BR.EmitBasicReport(
49
1
    ADC->getDecl(),
50
1
    Checker,
51
1
    /*Name=*/"OSObject C-Style Cast",
52
1
    /*BugCategory=*/"Security",
53
1
    OS.str(),
54
1
    PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC),
55
1
    CE->getSourceRange());
56
1
}
57
58
static auto hasTypePointingTo(DeclarationMatcher DeclM)
59
18
    -> decltype(hasType(pointerType())) {
60
18
  return hasType(pointerType(pointee(hasDeclaration(DeclM))));
61
18
}
62
63
void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM,
64
9
                                                 BugReporter &BR) const {
65
9
66
9
  AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
67
9
68
9
  auto DynamicCastM = callExpr(callee(functionDecl(hasName("safeMetaCast"))));
69
9
70
9
  auto OSObjTypeM = hasTypePointingTo(cxxRecordDecl(isDerivedFrom("OSMetaClassBase")));
71
9
  auto OSObjSubclassM = hasTypePointingTo(
72
9
    cxxRecordDecl(isDerivedFrom("OSObject")));
73
9
74
9
  auto CastM = cStyleCastExpr(
75
9
          allOf(hasSourceExpression(allOf(OSObjTypeM, unless(DynamicCastM))),
76
9
          OSObjSubclassM)).bind(WarnAtNode);
77
9
78
9
  auto Matches = match(stmt(forEachDescendant(CastM)), *D->getBody(), AM.getASTContext());
79
9
  for (BoundNodes Match : Matches)
80
1
    emitDiagnostics(Match, BR, ADC, this);
81
9
}
82
}
83
84
1
void ento::registerOSObjectCStyleCast(CheckerManager &Mgr) {
85
1
  Mgr.registerChecker<OSObjectCStyleCastChecker>();
86
1
}
87
88
1
bool ento::shouldRegisterOSObjectCStyleCast(const LangOptions &LO) {
89
1
  return true;
90
1
}