opnsense-src/lib/Checker/CheckObjCInstMethSignature.cpp

120 lines
3.9 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04:00
//=- CheckObjCInstMethodRetTy.cpp - Check ObjC method signatures -*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
2010-02-16 04:31:36 -05:00
#include "clang/Checker/Checkers/LocalCheckers.h"
#include "clang/Checker/BugReporter/PathDiagnostic.h"
#include "clang/Checker/BugReporter/BugReporter.h"
2009-06-02 13:58:47 -04:00
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Type.h"
#include "clang/AST/ASTContext.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
ASTContext& C) {
// Right now don't compare the compatibility of pointers. That involves
// looking at subtyping relationships. FIXME: Future patch.
2009-10-14 14:03:49 -04:00
if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
2009-06-02 13:58:47 -04:00
return true;
return C.typesAreCompatible(Derived, Ancestor);
}
2009-10-14 14:03:49 -04:00
static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
const ObjCMethodDecl *MethAncestor,
BugReporter &BR, ASTContext &Ctx,
const ObjCImplementationDecl *ID) {
2009-06-02 13:58:47 -04:00
QualType ResDerived = MethDerived->getResultType();
2009-10-14 14:03:49 -04:00
QualType ResAncestor = MethAncestor->getResultType();
2009-06-02 13:58:47 -04:00
if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
std::string sbuf;
llvm::raw_string_ostream os(sbuf);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
os << "The Objective-C class '"
<< MethDerived->getClassInterface()->getNameAsString()
<< "', which is derived from class '"
<< MethAncestor->getClassInterface()->getNameAsString()
<< "', defines the instance method '"
<< MethDerived->getSelector().getAsString()
<< "' whose return type is '"
<< ResDerived.getAsString()
<< "'. A method with the same name (same selector) is also defined in "
"class '"
<< MethAncestor->getClassInterface()->getNameAsString()
<< "' and has a return type of '"
<< ResAncestor.getAsString()
<< "'. These two types are incompatible, and may result in undefined "
"behavior for clients of these classes.";
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
BR.EmitBasicReport("Incompatible instance method return type",
2009-12-01 06:08:04 -05:00
os.str(), MethDerived->getLocStart());
2009-06-02 13:58:47 -04:00
}
}
2009-10-14 14:03:49 -04:00
void clang::CheckObjCInstMethSignature(const ObjCImplementationDecl* ID,
2009-06-02 13:58:47 -04:00
BugReporter& BR) {
2009-10-14 14:03:49 -04:00
const ObjCInterfaceDecl* D = ID->getClassInterface();
const ObjCInterfaceDecl* C = D->getSuperClass();
2009-06-02 13:58:47 -04:00
if (!C)
return;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
ASTContext& Ctx = BR.getContext();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Build a DenseMap of the methods for quick querying.
typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
MapTy IMeths;
unsigned NumMethods = 0;
2009-10-14 14:03:49 -04:00
2009-07-04 09:58:54 -04:00
for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
2009-10-14 14:03:49 -04:00
E=ID->instmeth_end(); I!=E; ++I) {
2009-06-02 13:58:47 -04:00
ObjCMethodDecl* M = *I;
IMeths[M->getSelector()] = M;
++NumMethods;
}
// Now recurse the class hierarchy chain looking for methods with the
// same signatures.
while (C && NumMethods) {
2009-07-04 09:58:54 -04:00
for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
E=C->instmeth_end(); I!=E; ++I) {
2009-06-02 13:58:47 -04:00
ObjCMethodDecl* M = *I;
Selector S = M->getSelector();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
MapTy::iterator MI = IMeths.find(S);
if (MI == IMeths.end() || MI->second == 0)
continue;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
--NumMethods;
ObjCMethodDecl* MethDerived = MI->second;
MI->second = 0;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
C = C->getSuperClass();
}
}