clang  6.0.0svn
CheckObjCInstMethSignature.cpp
Go to the documentation of this file.
1 //=- CheckObjCInstMethodRetTy.cpp - Check ObjC method signatures -*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a CheckObjCInstMethSignature, a flow-insenstive check
11 // that determines if an Objective-C class interface incorrectly redefines
12 // the method signature in a subclass.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "ClangSACheckers.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Type.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 using namespace clang;
27 using namespace ento;
28 
29 static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
30  ASTContext &C) {
31 
32  // Right now don't compare the compatibility of pointers. That involves
33  // looking at subtyping relationships. FIXME: Future patch.
34  if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
35  return true;
36 
37  return C.typesAreCompatible(Derived, Ancestor);
38 }
39 
40 static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
41  const ObjCMethodDecl *MethAncestor,
42  BugReporter &BR, ASTContext &Ctx,
44  const CheckerBase *Checker) {
45 
46  QualType ResDerived = MethDerived->getReturnType();
47  QualType ResAncestor = MethAncestor->getReturnType();
48 
49  if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
50  std::string sbuf;
51  llvm::raw_string_ostream os(sbuf);
52 
53  os << "The Objective-C class '"
54  << *MethDerived->getClassInterface()
55  << "', which is derived from class '"
56  << *MethAncestor->getClassInterface()
57  << "', defines the instance method '";
58  MethDerived->getSelector().print(os);
59  os << "' whose return type is '"
60  << ResDerived.getAsString()
61  << "'. A method with the same name (same selector) is also defined in "
62  "class '"
63  << *MethAncestor->getClassInterface()
64  << "' and has a return type of '"
65  << ResAncestor.getAsString()
66  << "'. These two types are incompatible, and may result in undefined "
67  "behavior for clients of these classes.";
68 
69  PathDiagnosticLocation MethDLoc =
71  BR.getSourceManager());
72 
73  BR.EmitBasicReport(
74  MethDerived, Checker, "Incompatible instance method return type",
75  categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
76  }
77 }
78 
80  BugReporter &BR,
81  const CheckerBase *Checker) {
82 
83  const ObjCInterfaceDecl *D = ID->getClassInterface();
84  const ObjCInterfaceDecl *C = D->getSuperClass();
85 
86  if (!C)
87  return;
88 
89  ASTContext &Ctx = BR.getContext();
90 
91  // Build a DenseMap of the methods for quick querying.
92  typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
93  MapTy IMeths;
94  unsigned NumMethods = 0;
95 
96  for (auto *M : ID->instance_methods()) {
97  IMeths[M->getSelector()] = M;
98  ++NumMethods;
99  }
100 
101  // Now recurse the class hierarchy chain looking for methods with the
102  // same signatures.
103  while (C && NumMethods) {
104  for (const auto *M : C->instance_methods()) {
105  Selector S = M->getSelector();
106 
107  MapTy::iterator MI = IMeths.find(S);
108 
109  if (MI == IMeths.end() || MI->second == nullptr)
110  continue;
111 
112  --NumMethods;
113  ObjCMethodDecl *MethDerived = MI->second;
114  MI->second = nullptr;
115 
116  CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
117  }
118 
119  C = C->getSuperClass();
120  }
121 }
122 
123 //===----------------------------------------------------------------------===//
124 // ObjCMethSigsChecker
125 //===----------------------------------------------------------------------===//
126 
127 namespace {
128 class ObjCMethSigsChecker : public Checker<
129  check::ASTDecl<ObjCImplementationDecl> > {
130 public:
131  void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
132  BugReporter &BR) const {
133  CheckObjCInstMethSignature(D, BR, this);
134  }
135 };
136 }
137 
138 void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
139  mgr.registerChecker<ObjCMethSigsChecker>();
140 }
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:614
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1089
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:113
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:128
instmeth_range instance_methods() const
Definition: DeclObjC.h:997
ASTContext & getContext()
Definition: BugReporter.h:461
std::string getAsString() const
Definition: Type.h:940
static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID, BugReporter &BR, const CheckerBase *Checker)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1108
QualType getReturnType() const
Definition: DeclObjC.h:330
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:314
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
Selector getSelector() const
Definition: DeclObjC.h:328
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:403
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
CHECKER * registerChecker()
Used to register checkers.
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=None)
bool isAnyPointerType() const
Definition: Type.h:5769
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2341
Dataflow Directional Tag Classes.
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2448
SourceManager & getSourceManager()
Definition: BugReporter.h:463
bool typesAreCompatible(QualType T1, QualType T2, bool CompareUnqualified=false)
Compatibility predicates used to check assignment expressions.