blob: fbda45e802a9ae2d79a64cc703bb498e5c023ee1 [file] [log] [blame]
Ted Kremenek3bfb3142008-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 Kyrtzidisaf45aca2011-02-17 21:39:33 +000016#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Ted Kremenek3bfb3142008-07-11 22:40:47 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Type.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
22#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek3bfb3142008-07-11 22:40:47 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek6c3413c2009-04-02 02:44:03 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek3bfb3142008-07-11 22:40:47 +000025
26using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000027using namespace ento;
Ted Kremenek3bfb3142008-07-11 22:40:47 +000028
29static bool AreTypesCompatible(QualType Derived, QualType Ancestor,
Ted Kremenek5ef32db2011-08-12 23:37:29 +000030 ASTContext &C) {
Ted Kremenek3bfb3142008-07-11 22:40:47 +000031
32 // Right now don't compare the compatibility of pointers. That involves
33 // looking at subtyping relationships. FIXME: Future patch.
Steve Naroff6b712a72009-07-14 18:25:06 +000034 if (Derived->isAnyPointerType() && Ancestor->isAnyPointerType())
Ted Kremenek3bfb3142008-07-11 22:40:47 +000035 return true;
36
37 return C.typesAreCompatible(Derived, Ancestor);
38}
39
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +000040static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
41 const ObjCMethodDecl *MethAncestor,
42 BugReporter &BR, ASTContext &Ctx,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000043 const ObjCImplementationDecl *ID,
44 const CheckerBase *Checker) {
Mike Stump11289f42009-09-09 15:08:12 +000045
Alp Toker314cc812014-01-25 16:55:45 +000046 QualType ResDerived = MethDerived->getReturnType();
47 QualType ResAncestor = MethAncestor->getReturnType();
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenek3bfb3142008-07-11 22:40:47 +000049 if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) {
Ted Kremenek6c3413c2009-04-02 02:44:03 +000050 std::string sbuf;
51 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +000052
Ted Kremenek3bfb3142008-07-11 22:40:47 +000053 os << "The Objective-C class '"
Benjamin Kramerb89514a2011-10-14 18:45:37 +000054 << *MethDerived->getClassInterface()
Ted Kremenek3bfb3142008-07-11 22:40:47 +000055 << "', which is derived from class '"
Benjamin Kramerb89514a2011-10-14 18:45:37 +000056 << *MethAncestor->getClassInterface()
Aaron Ballmanb190f972014-01-03 17:59:55 +000057 << "', defines the instance method '";
58 MethDerived->getSelector().print(os);
59 os << "' whose return type is '"
Ted Kremenek3bfb3142008-07-11 22:40:47 +000060 << ResDerived.getAsString()
Ted Kremeneke5b59532008-07-11 23:17:01 +000061 << "'. A method with the same name (same selector) is also defined in "
62 "class '"
Benjamin Kramerb89514a2011-10-14 18:45:37 +000063 << *MethAncestor->getClassInterface()
Ted Kremeneke5b59532008-07-11 23:17:01 +000064 << "' and has a return type of '"
Ted Kremenek3bfb3142008-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 Stump11289f42009-09-09 15:08:12 +000068
Anna Zaksc29bed32011-09-20 21:38:35 +000069 PathDiagnosticLocation MethDLoc =
70 PathDiagnosticLocation::createBegin(MethDerived,
71 BR.getSourceManager());
72
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000073 BR.EmitBasicReport(
74 MethDerived, Checker, "Incompatible instance method return type",
75 categories::CoreFoundationObjectiveC, os.str(), MethDLoc);
Ted Kremenek3bfb3142008-07-11 22:40:47 +000076 }
77}
78
Ted Kremenek5ef32db2011-08-12 23:37:29 +000079static void CheckObjCInstMethSignature(const ObjCImplementationDecl *ID,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000080 BugReporter &BR,
81 const CheckerBase *Checker) {
Mike Stump11289f42009-09-09 15:08:12 +000082
Ted Kremenek5ef32db2011-08-12 23:37:29 +000083 const ObjCInterfaceDecl *D = ID->getClassInterface();
84 const ObjCInterfaceDecl *C = D->getSuperClass();
Ted Kremenek3bfb3142008-07-11 22:40:47 +000085
86 if (!C)
87 return;
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremenek5ef32db2011-08-12 23:37:29 +000089 ASTContext &Ctx = BR.getContext();
Mike Stump11289f42009-09-09 15:08:12 +000090
Ted Kremenek3bfb3142008-07-11 22:40:47 +000091 // Build a DenseMap of the methods for quick querying.
92 typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy;
93 MapTy IMeths;
94 unsigned NumMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +000095
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000096 for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(),
Mike Stump11289f42009-09-09 15:08:12 +000097 E=ID->instmeth_end(); I!=E; ++I) {
98
Ted Kremenek5ef32db2011-08-12 23:37:29 +000099 ObjCMethodDecl *M = *I;
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000100 IMeths[M->getSelector()] = M;
101 ++NumMethods;
102 }
103
104 // Now recurse the class hierarchy chain looking for methods with the
105 // same signatures.
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000106 while (C && NumMethods) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000107 for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
108 E=C->instmeth_end(); I!=E; ++I) {
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000109
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000110 ObjCMethodDecl *M = *I;
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000111 Selector S = M->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000113 MapTy::iterator MI = IMeths.find(S);
114
115 if (MI == IMeths.end() || MI->second == 0)
116 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000117
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000118 --NumMethods;
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000119 ObjCMethodDecl *MethDerived = MI->second;
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000120 MI->second = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000121
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000122 CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000125 C = C->getSuperClass();
126 }
127}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000128
129//===----------------------------------------------------------------------===//
130// ObjCMethSigsChecker
131//===----------------------------------------------------------------------===//
132
133namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000134class ObjCMethSigsChecker : public Checker<
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000135 check::ASTDecl<ObjCImplementationDecl> > {
136public:
137 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
138 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000139 CheckObjCInstMethSignature(D, BR, this);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000140 }
141};
142}
143
144void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
145 mgr.registerChecker<ObjCMethSigsChecker>();
146}