[analyzer] Refactoring: include/clang/Checker -> include/clang/GR
[clang.git] / lib / Checker / CheckObjCInstMethSignature.cpp
blob2b83d13e75973a6eb0ffe215d8abbb4eb32c65aa
1 //=- CheckObjCInstMethodRetTy.cpp - Check ObjC method signatures -*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/GR/Checkers/LocalCheckers.h"
17 #include "clang/GR/BugReporter/PathDiagnostic.h"
18 #include "clang/GR/BugReporter/BugReporter.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/Type.h"
21 #include "clang/AST/ASTContext.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
28 static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
29 ASTContext& C) {
31 // Right now don't compare the compatibility of pointers. That involves
32 // looking at subtyping relationships. FIXME: Future patch.
33 if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
34 return true;
36 return C.typesAreCompatible(Derived, Ancestor);
39 static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
40 const ObjCMethodDecl *MethAncestor,
41 BugReporter &BR, ASTContext &Ctx,
42 const ObjCImplementationDecl *ID) {
44 QualType ResDerived = MethDerived->getResultType();
45 QualType ResAncestor = MethAncestor->getResultType();
47 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
48 std::string sbuf;
49 llvm::raw_string_ostream os(sbuf);
51 os << "The Objective-C class '"
52 << MethDerived->getClassInterface()
53 << "', which is derived from class '"
54 << MethAncestor->getClassInterface()
55 << "', defines the instance method '"
56 << MethDerived->getSelector().getAsString()
57 << "' whose return type is '"
58 << ResDerived.getAsString()
59 << "'. A method with the same name (same selector) is also defined in "
60 "class '"
61 << MethAncestor->getClassInterface()
62 << "' and has a return type of '"
63 << ResAncestor.getAsString()
64 << "'. These two types are incompatible, and may result in undefined "
65 "behavior for clients of these classes.";
67 BR.EmitBasicReport("Incompatible instance method return type",
68 os.str(), MethDerived->getLocStart());
72 void clang::CheckObjCInstMethSignature(const ObjCImplementationDecl* ID,
73 BugReporter& BR) {
75 const ObjCInterfaceDecl* D = ID->getClassInterface();
76 const ObjCInterfaceDecl* C = D->getSuperClass();
78 if (!C)
79 return;
81 ASTContext& Ctx = BR.getContext();
83 // Build a DenseMap of the methods for quick querying.
84 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
85 MapTy IMeths;
86 unsigned NumMethods = 0;
88 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
89 E=ID->instmeth_end(); I!=E; ++I) {
91 ObjCMethodDecl* M = *I;
92 IMeths[M->getSelector()] = M;
93 ++NumMethods;
96 // Now recurse the class hierarchy chain looking for methods with the
97 // same signatures.
98 while (C && NumMethods) {
99 for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
100 E=C->instmeth_end(); I!=E; ++I) {
102 ObjCMethodDecl* M = *I;
103 Selector S = M->getSelector();
105 MapTy::iterator MI = IMeths.find(S);
107 if (MI == IMeths.end() || MI->second == 0)
108 continue;
110 --NumMethods;
111 ObjCMethodDecl* MethDerived = MI->second;
112 MI->second = 0;
114 CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
117 C = C->getSuperClass();