blob: aae1e1da3b83bfbbb4e9973980aeda0062f0b57e [file] [log] [blame]
Ted Kremenek0d8019e2008-07-11 22:40:47 +00001//=- 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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "clang/Analysis/LocalCheckers.h"
17#include "clang/Analysis/PathDiagnostic.h"
18#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Type.h"
21#include "clang/AST/ASTContext.h"
22
23#include "llvm/ADT/DenseMap.h"
Ted Kremenek13493ea2009-04-02 02:44:03 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000025
26using namespace clang;
27
28static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
29 ASTContext& C) {
30
31 // Right now don't compare the compatibility of pointers. That involves
32 // looking at subtyping relationships. FIXME: Future patch.
Steve Naroff58f9f2c2009-07-14 18:25:06 +000033 if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
Ted Kremenek0d8019e2008-07-11 22:40:47 +000034 return true;
35
36 return C.typesAreCompatible(Derived, Ancestor);
37}
38
Ted Kremenek23760022009-08-21 23:58:43 +000039static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
40 const ObjCMethodDecl *MethAncestor,
41 BugReporter &BR, ASTContext &Ctx,
42 const ObjCImplementationDecl *ID) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +000043
44 QualType ResDerived = MethDerived->getResultType();
45 QualType ResAncestor = MethAncestor->getResultType();
46
47 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
Ted Kremenek13493ea2009-04-02 02:44:03 +000048 std::string sbuf;
49 llvm::raw_string_ostream os(sbuf);
Ted Kremenek0d8019e2008-07-11 22:40:47 +000050
51 os << "The Objective-C class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000052 << MethDerived->getClassInterface()->getNameAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000053 << "', which is derived from class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000054 << MethAncestor->getClassInterface()->getNameAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000055 << "', defines the instance method '"
Chris Lattner077bf5e2008-11-24 03:33:13 +000056 << MethDerived->getSelector().getAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000057 << "' whose return type is '"
58 << ResDerived.getAsString()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000059 << "'. A method with the same name (same selector) is also defined in "
60 "class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000061 << MethAncestor->getClassInterface()->getNameAsString()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000062 << "' and has a return type of '"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000063 << ResAncestor.getAsString()
64 << "'. These two types are incompatible, and may result in undefined "
65 "behavior for clients of these classes.";
66
Ted Kremenek13493ea2009-04-02 02:44:03 +000067 BR.EmitBasicReport("Incompatible instance method return type",
Ted Kremenek57202072008-07-14 17:40:50 +000068 os.str().c_str(), MethDerived->getLocStart());
Ted Kremenek0d8019e2008-07-11 22:40:47 +000069 }
70}
71
Ted Kremenek23760022009-08-21 23:58:43 +000072void clang::CheckObjCInstMethSignature(const ObjCImplementationDecl* ID,
Ted Kremenek0d8019e2008-07-11 22:40:47 +000073 BugReporter& BR) {
74
Ted Kremenek23760022009-08-21 23:58:43 +000075 const ObjCInterfaceDecl* D = ID->getClassInterface();
76 const ObjCInterfaceDecl* C = D->getSuperClass();
Ted Kremenek0d8019e2008-07-11 22:40:47 +000077
78 if (!C)
79 return;
80
Douglas Gregor653f1b12009-04-23 01:02:12 +000081 ASTContext& Ctx = BR.getContext();
82
Ted Kremenek0d8019e2008-07-11 22:40:47 +000083 // Build a DenseMap of the methods for quick querying.
84 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
85 MapTy IMeths;
86 unsigned NumMethods = 0;
87
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000088 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
89 E=ID->instmeth_end(); I!=E; ++I) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +000090
91 ObjCMethodDecl* M = *I;
92 IMeths[M->getSelector()] = M;
93 ++NumMethods;
94 }
95
96 // Now recurse the class hierarchy chain looking for methods with the
97 // same signatures.
Ted Kremenek0d8019e2008-07-11 22:40:47 +000098 while (C && NumMethods) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000099 for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
100 E=C->instmeth_end(); I!=E; ++I) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000101
102 ObjCMethodDecl* M = *I;
103 Selector S = M->getSelector();
104
105 MapTy::iterator MI = IMeths.find(S);
106
107 if (MI == IMeths.end() || MI->second == 0)
108 continue;
109
110 --NumMethods;
111 ObjCMethodDecl* MethDerived = MI->second;
112 MI->second = 0;
113
114 CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
115 }
116
117 C = C->getSuperClass();
118 }
119}