blob: 28814867bd589fd6f1d922a26e541614f2c6bb69 [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.
33 if ((Derived->isPointerType() || Derived->isObjCQualifiedIdType()) &&
34 (Ancestor->isPointerType() || Ancestor->isObjCQualifiedIdType()))
35 return true;
36
37 return C.typesAreCompatible(Derived, Ancestor);
38}
39
40static void CompareReturnTypes(ObjCMethodDecl* MethDerived,
41 ObjCMethodDecl* MethAncestor,
42 BugReporter& BR, ASTContext& Ctx,
43 ObjCImplementationDecl* ID) {
44
45 QualType ResDerived = MethDerived->getResultType();
46 QualType ResAncestor = MethAncestor->getResultType();
47
48 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
Ted Kremenek13493ea2009-04-02 02:44:03 +000049 std::string sbuf;
50 llvm::raw_string_ostream os(sbuf);
Ted Kremenek0d8019e2008-07-11 22:40:47 +000051
52 os << "The Objective-C class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000053 << MethDerived->getClassInterface()->getNameAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000054 << "', which is derived from class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000055 << MethAncestor->getClassInterface()->getNameAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000056 << "', defines the instance method '"
Chris Lattner077bf5e2008-11-24 03:33:13 +000057 << MethDerived->getSelector().getAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000058 << "' whose return type is '"
59 << ResDerived.getAsString()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000060 << "'. A method with the same name (same selector) is also defined in "
61 "class '"
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000062 << MethAncestor->getClassInterface()->getNameAsString()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000063 << "' and has a return type of '"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000064 << ResAncestor.getAsString()
65 << "'. These two types are incompatible, and may result in undefined "
66 "behavior for clients of these classes.";
67
Ted Kremenek13493ea2009-04-02 02:44:03 +000068 BR.EmitBasicReport("Incompatible instance method return type",
Ted Kremenek57202072008-07-14 17:40:50 +000069 os.str().c_str(), MethDerived->getLocStart());
Ted Kremenek0d8019e2008-07-11 22:40:47 +000070 }
71}
72
73void clang::CheckObjCInstMethSignature(ObjCImplementationDecl* ID,
74 BugReporter& BR) {
75
76 ObjCInterfaceDecl* D = ID->getClassInterface();
77 ObjCInterfaceDecl* C = D->getSuperClass();
78
79 if (!C)
80 return;
81
Douglas Gregor653f1b12009-04-23 01:02:12 +000082 ASTContext& Ctx = BR.getContext();
83
Ted Kremenek0d8019e2008-07-11 22:40:47 +000084 // Build a DenseMap of the methods for quick querying.
85 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
86 MapTy IMeths;
87 unsigned NumMethods = 0;
88
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000089 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
90 E=ID->instmeth_end(); I!=E; ++I) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +000091
92 ObjCMethodDecl* M = *I;
93 IMeths[M->getSelector()] = M;
94 ++NumMethods;
95 }
96
97 // Now recurse the class hierarchy chain looking for methods with the
98 // same signatures.
Ted Kremenek0d8019e2008-07-11 22:40:47 +000099 while (C && NumMethods) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000100 for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
101 E=C->instmeth_end(); I!=E; ++I) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000102
103 ObjCMethodDecl* M = *I;
104 Selector S = M->getSelector();
105
106 MapTy::iterator MI = IMeths.find(S);
107
108 if (MI == IMeths.end() || MI->second == 0)
109 continue;
110
111 --NumMethods;
112 ObjCMethodDecl* MethDerived = MI->second;
113 MI->second = 0;
114
115 CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
116 }
117
118 C = C->getSuperClass();
119 }
120}