2019-03-01 14:49:51 +08:00
|
|
|
//===-- CheckObjCInstMethSignature.cpp - Check ObjC method signatures -----===//
|
2008-07-12 06:40:47 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-07-12 06:40:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a CheckObjCInstMethSignature, a flow-insenstive check
|
|
|
|
// that determines if an Objective-C class interface incorrectly redefines
|
|
|
|
// the method signature in a subclass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2019-09-12 04:54:27 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-07-12 06:40:47 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Type.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2008-07-12 06:40:47 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-04-02 10:44:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &C) {
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
// Right now don't compare the compatibility of pointers. That involves
|
|
|
|
// looking at subtyping relationships. FIXME: Future patch.
|
2009-07-15 02:25:06 +08:00
|
|
|
if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
|
2008-07-12 06:40:47 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return C.typesAreCompatible(Derived, Ancestor);
|
|
|
|
}
|
|
|
|
|
2009-08-22 07:58:43 +08:00
|
|
|
static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
|
|
|
|
const ObjCMethodDecl *MethAncestor,
|
|
|
|
BugReporter &BR, ASTContext &Ctx,
|
2014-02-12 05:49:21 +08:00
|
|
|
const ObjCImplementationDecl *ID,
|
|
|
|
const CheckerBase *Checker) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResDerived = MethDerived->getReturnType();
|
|
|
|
QualType ResAncestor = MethAncestor->getReturnType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
|
2009-04-02 10:44:03 +08:00
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
os << "The Objective-C class '"
|
2011-10-15 02:45:37 +08:00
|
|
|
<< *MethDerived->getClassInterface()
|
2008-07-12 06:40:47 +08:00
|
|
|
<< "', which is derived from class '"
|
2011-10-15 02:45:37 +08:00
|
|
|
<< *MethAncestor->getClassInterface()
|
2014-01-04 01:59:55 +08:00
|
|
|
<< "', defines the instance method '";
|
|
|
|
MethDerived->getSelector().print(os);
|
|
|
|
os << "' whose return type is '"
|
2008-07-12 06:40:47 +08:00
|
|
|
<< ResDerived.getAsString()
|
2008-07-12 07:17:01 +08:00
|
|
|
<< "'. A method with the same name (same selector) is also defined in "
|
|
|
|
"class '"
|
2011-10-15 02:45:37 +08:00
|
|
|
<< *MethAncestor->getClassInterface()
|
2008-07-12 07:17:01 +08:00
|
|
|
<< "' and has a return type of '"
|
2008-07-12 06:40:47 +08:00
|
|
|
<< ResAncestor.getAsString()
|
|
|
|
<< "'. These two types are incompatible, and may result in undefined "
|
|
|
|
"behavior for clients of these classes.";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation MethDLoc =
|
|
|
|
PathDiagnosticLocation::createBegin(MethDerived,
|
|
|
|
BR.getSourceManager());
|
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(
|
|
|
|
MethDerived, Checker, "Incompatible instance method return type",
|
|
|
|
categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
|
2008-07-12 06:40:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID,
|
2014-02-12 05:49:21 +08:00
|
|
|
BugReporter &BR,
|
|
|
|
const CheckerBase *Checker) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
const ObjCInterfaceDecl *D = ID->getClassInterface();
|
|
|
|
const ObjCInterfaceDecl *C = D->getSuperClass();
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
if (!C)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx = BR.getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
// Build a DenseMap of the methods for quick querying.
|
|
|
|
typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
|
|
|
|
MapTy IMeths;
|
|
|
|
unsigned NumMethods = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-14 03:50:17 +08:00
|
|
|
for (auto *M : ID->instance_methods()) {
|
2008-07-12 06:40:47 +08:00
|
|
|
IMeths[M->getSelector()] = M;
|
|
|
|
++NumMethods;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now recurse the class hierarchy chain looking for methods with the
|
|
|
|
// same signatures.
|
|
|
|
while (C && NumMethods) {
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *M : C->instance_methods()) {
|
2008-07-12 06:40:47 +08:00
|
|
|
Selector S = M->getSelector();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
MapTy::iterator MI = IMeths.find(S);
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
if (MI == IMeths.end() || MI->second == nullptr)
|
2008-07-12 06:40:47 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
--NumMethods;
|
2011-08-13 07:37:29 +08:00
|
|
|
ObjCMethodDecl *MethDerived = MI->second;
|
2014-05-27 10:45:47 +08:00
|
|
|
MI->second = nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
|
2008-07-12 06:40:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 06:40:47 +08:00
|
|
|
C = C->getSuperClass();
|
|
|
|
}
|
|
|
|
}
|
2011-02-18 05:39:33 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ObjCMethSigsChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class ObjCMethSigsChecker : public Checker<
|
2011-02-18 05:39:33 +08:00
|
|
|
check::ASTDecl<ObjCImplementationDecl> > {
|
|
|
|
public:
|
|
|
|
void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2014-02-12 05:49:21 +08:00
|
|
|
CheckObjCInstMethSignature(D, BR, this);
|
2011-02-18 05:39:33 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCMethSigsChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterObjCMethSigsChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|