blob: 369ba0bbb2acf9f0b4f0bc1966119abf16cd9c78 [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
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000016#include "ClangSACheckers.h"
17#include "clang/StaticAnalyzer/Core/CheckerV2.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Type.h"
22#include "clang/AST/ASTContext.h"
23
24#include "llvm/ADT/DenseMap.h"
Ted Kremenek13493ea2009-04-02 02:44:03 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000026
27using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000028using namespace ento;
Ted Kremenek0d8019e2008-07-11 22:40:47 +000029
30static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
31 ASTContext& C) {
32
33 // Right now don't compare the compatibility of pointers. That involves
34 // looking at subtyping relationships. FIXME: Future patch.
Steve Naroff58f9f2c2009-07-14 18:25:06 +000035 if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
Ted Kremenek0d8019e2008-07-11 22:40:47 +000036 return true;
37
38 return C.typesAreCompatible(Derived, Ancestor);
39}
40
Ted Kremenek23760022009-08-21 23:58:43 +000041static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
42 const ObjCMethodDecl *MethAncestor,
43 BugReporter &BR, ASTContext &Ctx,
44 const ObjCImplementationDecl *ID) {
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenek0d8019e2008-07-11 22:40:47 +000046 QualType ResDerived = MethDerived->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +000047 QualType ResAncestor = MethAncestor->getResultType();
48
Ted Kremenek0d8019e2008-07-11 22:40:47 +000049 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
Ted Kremenek13493ea2009-04-02 02:44:03 +000050 std::string sbuf;
51 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek0d8019e2008-07-11 22:40:47 +000053 os << "The Objective-C class '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000054 << MethDerived->getClassInterface()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000055 << "', which is derived from class '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000056 << MethAncestor->getClassInterface()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000057 << "', defines the instance method '"
Chris Lattner077bf5e2008-11-24 03:33:13 +000058 << MethDerived->getSelector().getAsString()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000059 << "' whose return type is '"
60 << ResDerived.getAsString()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000061 << "'. A method with the same name (same selector) is also defined in "
62 "class '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000063 << MethAncestor->getClassInterface()
Ted Kremeneke4773eb2008-07-11 23:17:01 +000064 << "' and has a return type of '"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000065 << ResAncestor.getAsString()
66 << "'. These two types are incompatible, and may result in undefined "
67 "behavior for clients of these classes.";
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek13493ea2009-04-02 02:44:03 +000069 BR.EmitBasicReport("Incompatible instance method return type",
Benjamin Kramerf0171732009-11-29 18:27:55 +000070 os.str(), MethDerived->getLocStart());
Ted Kremenek0d8019e2008-07-11 22:40:47 +000071 }
72}
73
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000074static void CheckObjCInstMethSignature(const ObjCImplementationDecl* ID,
75 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +000076
Ted Kremenek23760022009-08-21 23:58:43 +000077 const ObjCInterfaceDecl* D = ID->getClassInterface();
78 const ObjCInterfaceDecl* C = D->getSuperClass();
Ted Kremenek0d8019e2008-07-11 22:40:47 +000079
80 if (!C)
81 return;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Douglas Gregor653f1b12009-04-23 01:02:12 +000083 ASTContext& Ctx = BR.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000084
Ted Kremenek0d8019e2008-07-11 22:40:47 +000085 // Build a DenseMap of the methods for quick querying.
86 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
87 MapTy IMeths;
88 unsigned NumMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000090 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000091 E=ID->instmeth_end(); I!=E; ++I) {
92
Ted Kremenek0d8019e2008-07-11 22:40:47 +000093 ObjCMethodDecl* M = *I;
94 IMeths[M->getSelector()] = M;
95 ++NumMethods;
96 }
97
98 // Now recurse the class hierarchy chain looking for methods with the
99 // same signatures.
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000100 while (C && NumMethods) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000101 for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
102 E=C->instmeth_end(); I!=E; ++I) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000103
104 ObjCMethodDecl* M = *I;
105 Selector S = M->getSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000107 MapTy::iterator MI = IMeths.find(S);
108
109 if (MI == IMeths.end() || MI->second == 0)
110 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000112 --NumMethods;
113 ObjCMethodDecl* MethDerived = MI->second;
114 MI->second = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000116 CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000119 C = C->getSuperClass();
120 }
121}
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000122
123//===----------------------------------------------------------------------===//
124// ObjCMethSigsChecker
125//===----------------------------------------------------------------------===//
126
127namespace {
128class ObjCMethSigsChecker : public CheckerV2<
129 check::ASTDecl<ObjCImplementationDecl> > {
130public:
131 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
132 BugReporter &BR) const {
133 CheckObjCInstMethSignature(D, BR);
134 }
135};
136}
137
138void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
139 mgr.registerChecker<ObjCMethSigsChecker>();
140}