Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/CheckObjCInstMethSignature.cpp
Line
Count
Source
1
//===-- CheckObjCInstMethSignature.cpp - Check ObjC method signatures -----===//
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 CheckObjCInstMethSignature, a flow-insenstive check
10
//  that determines if an Objective-C class interface incorrectly redefines
11
//  the method signature in a subclass.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/DeclObjC.h"
18
#include "clang/AST/Type.h"
19
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
20
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
21
#include "clang/StaticAnalyzer/Core/Checker.h"
22
#include "llvm/ADT/DenseMap.h"
23
#include "llvm/Support/raw_ostream.h"
24
25
using namespace clang;
26
using namespace ento;
27
28
static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
29
6
                               ASTContext &C) {
30
6
31
6
  // Right now don't compare the compatibility of pointers.  That involves
32
6
  // looking at subtyping relationships.  FIXME: Future patch.
33
6
  if (Derived->isAnyPointerType() &&  
Ancestor->isAnyPointerType()3
)
34
2
    return true;
35
4
36
4
  return C.typesAreCompatible(Derived, Ancestor);
37
4
}
38
39
static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
40
                               const ObjCMethodDecl *MethAncestor,
41
                               BugReporter &BR, ASTContext &Ctx,
42
                               const ObjCImplementationDecl *ID,
43
6
                               const CheckerBase *Checker) {
44
6
45
6
  QualType ResDerived = MethDerived->getReturnType();
46
6
  QualType ResAncestor = MethAncestor->getReturnType();
47
6
48
6
  if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
49
2
    std::string sbuf;
50
2
    llvm::raw_string_ostream os(sbuf);
51
2
52
2
    os << "The Objective-C class '"
53
2
       << *MethDerived->getClassInterface()
54
2
       << "', which is derived from class '"
55
2
       << *MethAncestor->getClassInterface()
56
2
       << "', defines the instance method '";
57
2
    MethDerived->getSelector().print(os);
58
2
    os << "' whose return type is '"
59
2
       << ResDerived.getAsString()
60
2
       << "'.  A method with the same name (same selector) is also defined in "
61
2
          "class '"
62
2
       << *MethAncestor->getClassInterface()
63
2
       << "' and has a return type of '"
64
2
       << ResAncestor.getAsString()
65
2
       << "'.  These two types are incompatible, and may result in undefined "
66
2
          "behavior for clients of these classes.";
67
2
68
2
    PathDiagnosticLocation MethDLoc =
69
2
      PathDiagnosticLocation::createBegin(MethDerived,
70
2
                                          BR.getSourceManager());
71
2
72
2
    BR.EmitBasicReport(
73
2
        MethDerived, Checker, "Incompatible instance method return type",
74
2
        categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
75
2
  }
76
6
}
77
78
static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID,
79
                                       BugReporter &BR,
80
14
                                       const CheckerBase *Checker) {
81
14
82
14
  const ObjCInterfaceDecl *D = ID->getClassInterface();
83
14
  const ObjCInterfaceDecl *C = D->getSuperClass();
84
14
85
14
  if (!C)
86
1
    return;
87
13
88
13
  ASTContext &Ctx = BR.getContext();
89
13
90
13
  // Build a DenseMap of the methods for quick querying.
91
13
  typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
92
13
  MapTy IMeths;
93
13
  unsigned NumMethods = 0;
94
13
95
27
  for (auto *M : ID->instance_methods()) {
96
27
    IMeths[M->getSelector()] = M;
97
27
    ++NumMethods;
98
27
  }
99
13
100
13
  // Now recurse the class hierarchy chain looking for methods with the
101
13
  // same signatures.
102
28
  while (C && 
NumMethods17
) {
103
62
    for (const auto *M : C->instance_methods()) {
104
62
      Selector S = M->getSelector();
105
62
106
62
      MapTy::iterator MI = IMeths.find(S);
107
62
108
62
      if (MI == IMeths.end() || 
MI->second == nullptr6
)
109
56
        continue;
110
6
111
6
      --NumMethods;
112
6
      ObjCMethodDecl *MethDerived = MI->second;
113
6
      MI->second = nullptr;
114
6
115
6
      CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
116
6
    }
117
15
118
15
    C = C->getSuperClass();
119
15
  }
120
13
}
121
122
//===----------------------------------------------------------------------===//
123
// ObjCMethSigsChecker
124
//===----------------------------------------------------------------------===//
125
126
namespace {
127
class ObjCMethSigsChecker : public Checker<
128
                                      check::ASTDecl<ObjCImplementationDecl> > {
129
public:
130
  void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
131
14
                    BugReporter &BR) const {
132
14
    CheckObjCInstMethSignature(D, BR, this);
133
14
  }
134
};
135
}
136
137
25
void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
138
25
  mgr.registerChecker<ObjCMethSigsChecker>();
139
25
}
140
141
25
bool ento::shouldRegisterObjCMethSigsChecker(const LangOptions &LO) {
142
25
  return true;
143
25
}