2008-07-12 06:40:47 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CompareReturnTypes(ObjCMethodDecl* MethDerived,
|
|
|
|
ObjCMethodDecl* MethAncestor,
|
|
|
|
BugReporter& BR, ASTContext& Ctx,
|
|
|
|
ObjCImplementationDecl* ID) {
|
|
|
|
|
|
|
|
QualType ResDerived = MethDerived->getResultType();
|
|
|
|
QualType ResAncestor = MethAncestor->getResultType();
|
|
|
|
|
|
|
|
if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
|
2009-04-02 10:44:03 +08:00
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
os << "The Objective-C class '"
|
2008-11-24 13:29:24 +08:00
|
|
|
<< MethDerived->getClassInterface()->getNameAsString()
|
2008-07-12 06:40:47 +08:00
|
|
|
<< "', which is derived from class '"
|
2008-11-24 13:29:24 +08:00
|
|
|
<< MethAncestor->getClassInterface()->getNameAsString()
|
2008-07-12 06:40:47 +08:00
|
|
|
<< "', defines the instance method '"
|
2008-11-24 11:33:13 +08:00
|
|
|
<< MethDerived->getSelector().getAsString()
|
2008-07-12 06:40:47 +08:00
|
|
|
<< "' whose return type is '"
|
|
|
|
<< ResDerived.getAsString()
|
2008-07-12 07:17:01 +08:00
|
|
|
<< "'. A method with the same name (same selector) is also defined in "
|
|
|
|
"class '"
|
2008-11-24 13:29:24 +08:00
|
|
|
<< MethAncestor->getClassInterface()->getNameAsString()
|
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-04-02 10:44:03 +08:00
|
|
|
BR.EmitBasicReport("Incompatible instance method return type",
|
2008-07-15 01:40:50 +08:00
|
|
|
os.str().c_str(), MethDerived->getLocStart());
|
2008-07-12 06:40:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::CheckObjCInstMethSignature(ObjCImplementationDecl* ID,
|
|
|
|
BugReporter& BR) {
|
|
|
|
|
|
|
|
ObjCInterfaceDecl* D = ID->getClassInterface();
|
|
|
|
ObjCInterfaceDecl* C = D->getSuperClass();
|
|
|
|
|
|
|
|
if (!C)
|
|
|
|
return;
|
|
|
|
|
2009-04-23 09:02:12 +08:00
|
|
|
ASTContext& Ctx = BR.getContext();
|
|
|
|
|
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-06-30 10:36:12 +08:00
|
|
|
for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
|
|
|
|
E=ID->instmeth_end(); I!=E; ++I) {
|
2008-07-12 06:40:47 +08: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-06-30 10:36:12 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
|
|
|
|
E=C->instmeth_end(); I!=E; ++I) {
|
2008-07-12 06:40:47 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl* M = *I;
|
|
|
|
Selector S = M->getSelector();
|
|
|
|
|
|
|
|
MapTy::iterator MI = IMeths.find(S);
|
|
|
|
|
|
|
|
if (MI == IMeths.end() || MI->second == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
--NumMethods;
|
|
|
|
ObjCMethodDecl* MethDerived = MI->second;
|
|
|
|
MI->second = 0;
|
|
|
|
|
|
|
|
CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
C = C->getSuperClass();
|
|
|
|
}
|
|
|
|
}
|