blob: bc0da285818670fb3c9ee0663b3f55fc1c1d6de6 [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
Ted Kremenek21142582010-12-23 19:38:26 +000016#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Ted Kremenek0d8019e2008-07-11 22:40:47 +000019#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;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Ted Kremenek0d8019e2008-07-11 22:40:47 +000028
29static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
30 ASTContext& C) {
31
32 // Right now don't compare the compatibility of pointers. That involves
33 // looking at subtyping relationships. FIXME: Future patch.
Steve Naroff58f9f2c2009-07-14 18:25:06 +000034 if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
Ted Kremenek0d8019e2008-07-11 22:40:47 +000035 return true;
36
37 return C.typesAreCompatible(Derived, Ancestor);
38}
39
Ted Kremenek23760022009-08-21 23:58:43 +000040static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
41 const ObjCMethodDecl *MethAncestor,
42 BugReporter &BR, ASTContext &Ctx,
43 const ObjCImplementationDecl *ID) {
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenek0d8019e2008-07-11 22:40:47 +000045 QualType ResDerived = MethDerived->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +000046 QualType ResAncestor = MethAncestor->getResultType();
47
Ted Kremenek0d8019e2008-07-11 22:40:47 +000048 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
Ted Kremenek13493ea2009-04-02 02:44:03 +000049 std::string sbuf;
50 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek0d8019e2008-07-11 22:40:47 +000052 os << "The Objective-C class '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000053 << MethDerived->getClassInterface()
Ted Kremenek0d8019e2008-07-11 22:40:47 +000054 << "', which is derived from class '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000055 << MethAncestor->getClassInterface()
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 '"
Benjamin Kramer900fc632010-04-17 09:33:03 +000062 << MethAncestor->getClassInterface()
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.";
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenek13493ea2009-04-02 02:44:03 +000068 BR.EmitBasicReport("Incompatible instance method return type",
Benjamin Kramerf0171732009-11-29 18:27:55 +000069 os.str(), MethDerived->getLocStart());
Ted Kremenek0d8019e2008-07-11 22:40:47 +000070 }
71}
72
Ted Kremenek9ef65372010-12-23 07:20:52 +000073void ento::CheckObjCInstMethSignature(const ObjCImplementationDecl* ID,
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000074 BugReporter& BR) {
Mike Stump1eb44332009-09-09 15:08:12 +000075
Ted Kremenek23760022009-08-21 23:58:43 +000076 const ObjCInterfaceDecl* D = ID->getClassInterface();
77 const ObjCInterfaceDecl* C = D->getSuperClass();
Ted Kremenek0d8019e2008-07-11 22:40:47 +000078
79 if (!C)
80 return;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Douglas Gregor653f1b12009-04-23 01:02:12 +000082 ASTContext& Ctx = BR.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000083
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;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000089 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000090 E=ID->instmeth_end(); I!=E; ++I) {
91
Ted Kremenek0d8019e2008-07-11 22:40:47 +000092 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000106 MapTy::iterator MI = IMeths.find(S);
107
108 if (MI == IMeths.end() || MI->second == 0)
109 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000111 --NumMethods;
112 ObjCMethodDecl* MethDerived = MI->second;
113 MI->second = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000115 CompareReturnTypes(MethDerived, M, BR, Ctx, ID);
116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000118 C = C->getSuperClass();
119 }
120}