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