clang  15.0.0git
CheckObjCInstMethSignature.cpp
Go to the documentation of this file.
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-insensitive check
10 // that determines if an Objective-C class interface incorrectly redefines
11 // the method signature in a subclass.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Type.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  ASTContext &C) {
30 
31  // Right now don't compare the compatibility of pointers. That involves
32  // looking at subtyping relationships. FIXME: Future patch.
33  if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
34  return true;
35 
36  return C.typesAreCompatible(Derived, Ancestor);
37 }
38 
39 static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
40  const ObjCMethodDecl *MethAncestor,
41  BugReporter &BR, ASTContext &Ctx,
43  const CheckerBase *Checker) {
44 
45  QualType ResDerived = MethDerived->getReturnType();
46  QualType ResAncestor = MethAncestor->getReturnType();
47 
48  if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
49  std::string sbuf;
50  llvm::raw_string_ostream os(sbuf);
51 
52  os << "The Objective-C class '"
53  << *MethDerived->getClassInterface()
54  << "', which is derived from class '"
55  << *MethAncestor->getClassInterface()
56  << "', defines the instance method '";
57  MethDerived->getSelector().print(os);
58  os << "' whose return type is '" << ResDerived
59  << "'. A method with the same name (same selector) is also defined in "
60  "class '"
61  << *MethAncestor->getClassInterface() << "' and has a return type of '"
62  << ResAncestor
63  << "'. These two types are incompatible, and may result in undefined "
64  "behavior for clients of these classes.";
65 
66  PathDiagnosticLocation MethDLoc =
68  BR.getSourceManager());
69 
70  BR.EmitBasicReport(
71  MethDerived, Checker, "Incompatible instance method return type",
72  categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
73  }
74 }
75 
77  BugReporter &BR,
78  const CheckerBase *Checker) {
79 
80  const ObjCInterfaceDecl *D = ID->getClassInterface();
81  const ObjCInterfaceDecl *C = D->getSuperClass();
82 
83  if (!C)
84  return;
85 
86  ASTContext &Ctx = BR.getContext();
87 
88  // Build a DenseMap of the methods for quick querying.
89  typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
90  MapTy IMeths;
91  unsigned NumMethods = 0;
92 
93  for (auto *M : ID->instance_methods()) {
94  IMeths[M->getSelector()] = M;
95  ++NumMethods;
96  }
97 
98  // Now recurse the class hierarchy chain looking for methods with the
99  // same signatures.
100  while (C && NumMethods) {
101  for (const auto *M : C->instance_methods()) {
102  Selector S = M->getSelector();
103 
104  MapTy::iterator MI = IMeths.find(S);
105 
106  if (MI == IMeths.end() || MI->second == nullptr)
107  continue;
108 
109  --NumMethods;
110  ObjCMethodDecl *MethDerived = MI->second;
111  MI->second = nullptr;
112 
113  CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
114  }
115 
116  C = C->getSuperClass();
117  }
118 }
119 
120 //===----------------------------------------------------------------------===//
121 // ObjCMethSigsChecker
122 //===----------------------------------------------------------------------===//
123 
124 namespace {
125 class ObjCMethSigsChecker : public Checker<
126  check::ASTDecl<ObjCImplementationDecl> > {
127 public:
128  void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
129  BugReporter &BR) const {
130  CheckObjCInstMethSignature(D, BR, this);
131  }
132 };
133 }
134 
135 void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
136  mgr.registerChecker<ObjCMethSigsChecker>();
137 }
138 
139 bool ento::shouldRegisterObjCMethSigsChecker(const CheckerManager &mgr) {
140  return true;
141 }
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1150
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ObjCImplementationDecl
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2548
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
clang::ObjCMethodDecl::getClassInterface
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1176
DeclObjC.h
CompareReturnTypes
static void CompareReturnTypes(const ObjCMethodDecl *MethDerived, const ObjCMethodDecl *MethAncestor, BugReporter &BR, ASTContext &Ctx, const ObjCImplementationDecl *ID, const CheckerBase *Checker)
Definition: CheckObjCInstMethSignature.cpp:39
BuiltinCheckerRegistration.h
BugReporter.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:208
clang::ObjCMethodDecl::getSelector
Selector getSelector() const
Definition: DeclObjC.h:330
clang::Selector::print
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
Definition: IdentifierTable.cpp:554
Type.h
ASTContext.h
clang::ObjCMethodDecl::getReturnType
QualType getReturnType() const
Definition: DeclObjC.h:332
MapTy
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:22
PathDiagnostic.h
clang::ento::categories::CoreFoundationObjectiveC
const char *const CoreFoundationObjectiveC
Definition: CommonBugCategories.cpp:16
clang::ObjCInterfaceDecl::getSuperClass
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:351
Checker.h
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang::Builtin::ID
ID
Definition: Builtins.h:51
clang
Definition: CalledOnceCheck.h:17
clang::Type::isAnyPointerType
bool isAnyPointerType() const
Definition: Type.h:6752
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:752
CheckObjCInstMethSignature
static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID, BugReporter &BR, const CheckerBase *Checker)
Definition: CheckObjCInstMethSignature.cpp:76
clang::ento::PathDiagnosticLocation::createBegin
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
Definition: PathDiagnostic.cpp:580
AreTypesCompatible
static bool AreTypesCompatible(QualType Derived, QualType Ancestor, ASTContext &C)
Definition: CheckObjCInstMethSignature.cpp:28