Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//=- 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"
#include <sstream>
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.
if ((Derived->isPointerType() || Derived->isObjCQualifiedIdType()) &&
(Ancestor->isPointerType() || Ancestor->isObjCQualifiedIdType()))
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)) {
std::ostringstream os;
os << "The Objective-C class '"
<< MethDerived->getClassInterface()->getName()
<< "', which is derived from class '"
<< MethAncestor->getClassInterface()->getName()
<< "', defines the instance method '"
<< MethDerived->getSelector().getName()
<< "' whose return type is '"
<< ResDerived.getAsString()
<< "'. A method with the same name (same selector) is also defined in "
"class '"
<< MethAncestor->getClassInterface()->getName()
<< "' and has a return type of '"
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<< ResAncestor.getAsString()
<< "'. These two types are incompatible, and may result in undefined "
"behavior for clients of these classes.";
// Refactor.
SimpleBugType BT("incompatible instance method return type");
DiagCollector C(BT);
Diagnostic& Diag = BR.getDiagnostic();
Diag.Report(&C, Ctx.getFullLoc(MethDerived->getLocStart()),
Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
NULL, 0, NULL, 0);
for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
BR.EmitWarning(*I);
}
}
void clang::CheckObjCInstMethSignature(ObjCImplementationDecl* ID,
BugReporter& BR) {
ObjCInterfaceDecl* D = ID->getClassInterface();
ObjCInterfaceDecl* C = D->getSuperClass();
if (!C)
return;
// Build a DenseMap of the methods for quick querying.
typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
MapTy IMeths;
unsigned NumMethods = 0;
for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
E=ID->instmeth_end(); I!=E; ++I) {
ObjCMethodDecl* M = *I;
IMeths[M->getSelector()] = M;
++NumMethods;
}
// Now recurse the class hierarchy chain looking for methods with the
// same signatures.
ASTContext& Ctx = BR.getContext();
while (C && NumMethods) {
for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
E=C->instmeth_end(); I!=E; ++I) {
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();
}
}