1 //=- CheckObjCInstMethodRetTy.cpp - Check ObjC method signatures -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Type.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
29 static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
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())
37 return C.typesAreCompatible(Derived, Ancestor);
40 static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
41 const ObjCMethodDecl *MethAncestor,
42 BugReporter &BR, ASTContext &Ctx,
43 const ObjCImplementationDecl *ID,
44 const CheckerBase *Checker) {
46 QualType ResDerived = MethDerived->getReturnType();
47 QualType ResAncestor = MethAncestor->getReturnType();
49 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
51 llvm::raw_string_ostream os(sbuf);
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 "
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.";
69 PathDiagnosticLocation MethDLoc =
70 PathDiagnosticLocation::createBegin(MethDerived,
71 BR.getSourceManager());
74 MethDerived, Checker, "Incompatible instance method return type",
75 categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
79 static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID,
81 const CheckerBase *Checker) {
83 const ObjCInterfaceDecl *D = ID->getClassInterface();
84 const ObjCInterfaceDecl *C = D->getSuperClass();
89 ASTContext &Ctx = BR.getContext();
91 // Build a DenseMap of the methods for quick querying.
92 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
94 unsigned NumMethods = 0;
96 for (auto *M : ID->instance_methods()) {
97 IMeths[M->getSelector()] = M;
101 // Now recurse the class hierarchy chain looking for methods with the
103 while (C && NumMethods) {
104 for (const auto *M : C->instance_methods()) {
105 Selector S = M->getSelector();
107 MapTy::iterator MI = IMeths.find(S);
109 if (MI == IMeths.end() || MI->second == nullptr)
113 ObjCMethodDecl *MethDerived = MI->second;
114 MI->second = nullptr;
116 CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
119 C = C->getSuperClass();
123 //===----------------------------------------------------------------------===//
124 // ObjCMethSigsChecker
125 //===----------------------------------------------------------------------===//
128 class ObjCMethSigsChecker : public Checker<
129 check::ASTDecl<ObjCImplementationDecl> > {
131 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
132 BugReporter &BR) const {
133 CheckObjCInstMethSignature(D, BR, this);
138 void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
139 mgr.registerChecker<ObjCMethSigsChecker>();