clang  9.0.0svn
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-insenstive check
10 // that determines if an Objective-C class interface incorrectly redefines
11 // the method signature in a subclass.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclObjC.h"
18 #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 '"
59  << ResDerived.getAsString()
60  << "'. A method with the same name (same selector) is also defined in "
61  "class '"
62  << *MethAncestor->getClassInterface()
63  << "' and has a return type of '"
64  << ResAncestor.getAsString()
65  << "'. These two types are incompatible, and may result in undefined "
66  "behavior for clients of these classes.";
67 
68  PathDiagnosticLocation MethDLoc =
70  BR.getSourceManager());
71 
72  BR.EmitBasicReport(
73  MethDerived, Checker, "Incompatible instance method return type",
74  categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
75  }
76 }
77 
79  BugReporter &BR,
80  const CheckerBase *Checker) {
81 
82  const ObjCInterfaceDecl *D = ID->getClassInterface();
83  const ObjCInterfaceDecl *C = D->getSuperClass();
84 
85  if (!C)
86  return;
87 
88  ASTContext &Ctx = BR.getContext();
89 
90  // Build a DenseMap of the methods for quick querying.
91  typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
92  MapTy IMeths;
93  unsigned NumMethods = 0;
94 
95  for (auto *M : ID->instance_methods()) {
96  IMeths[M->getSelector()] = M;
97  ++NumMethods;
98  }
99 
100  // Now recurse the class hierarchy chain looking for methods with the
101  // same signatures.
102  while (C && NumMethods) {
103  for (const auto *M : C->instance_methods()) {
104  Selector S = M->getSelector();
105 
106  MapTy::iterator MI = IMeths.find(S);
107 
108  if (MI == IMeths.end() || MI->second == nullptr)
109  continue;
110 
111  --NumMethods;
112  ObjCMethodDecl *MethDerived = MI->second;
113  MI->second = nullptr;
114 
115  CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
116  }
117 
118  C = C->getSuperClass();
119  }
120 }
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  BugReporter &BR) const {
132  CheckObjCInstMethSignature(D, BR, this);
133  }
134 };
135 }
136 
137 void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
138  mgr.registerChecker<ObjCMethSigsChecker>();
139 }
140 
141 bool ento::shouldRegisterObjCMethSigsChecker(const LangOptions &LO) {
142  return true;
143 }
Defines the clang::ASTContext interface.
const char *const CoreFoundationObjectiveC
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:639
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1143
C Language Family Type Representation.
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:22
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
static bool AreTypesCompatible(QualType Derived, QualType Ancestor, ASTContext &C)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
instmeth_range instance_methods() const
Definition: DeclObjC.h:1053
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID, BugReporter &BR, const CheckerBase *Checker)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
QualType getReturnType() const
Definition: DeclObjC.h:322
static void CompareReturnTypes(const ObjCMethodDecl *MethDerived, const ObjCMethodDecl *MethAncestor, BugReporter &BR, ASTContext &Ctx, const ObjCImplementationDecl *ID, const CheckerBase *Checker)
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:337
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
Selector getSelector() const
Definition: DeclObjC.h:320
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
bool isAnyPointerType() const
Definition: Type.h:6312
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2439
Dataflow Directional Tag Classes.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:972
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2551
bool typesAreCompatible(QualType T1, QualType T2, bool CompareUnqualified=false)
Compatibility predicates used to check assignment expressions.