blob: dc53602bfc4f4d8bcad242372559e498e4f80c56 [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
Aaron Ballmanf26acce2014-03-13 19:50:17 +000096 for (auto *M : ID->instance_methods()) {
Ted Kremenek3bfb3142008-07-11 22:40:47 +000097 IMeths[M->getSelector()] = M;
98 ++NumMethods;
99 }
100
101 // Now recurse the class hierarchy chain looking for methods with the
102 // same signatures.
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000103 while (C && NumMethods) {
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000104 for (const auto *M : C->instance_methods()) {
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000105 Selector S = M->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000107 MapTy::iterator MI = IMeths.find(S);
108
109 if (MI == IMeths.end() || MI->second == 0)
110 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000111
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000112 --NumMethods;
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000113 ObjCMethodDecl *MethDerived = MI->second;
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000114 MI->second = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000115
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000116 CompareReturnTypes(MethDerived, M, BR, Ctx, ID, Checker);
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000119 C = C->getSuperClass();
120 }
121}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000122
123//===----------------------------------------------------------------------===//
124// ObjCMethSigsChecker
125//===----------------------------------------------------------------------===//
126
127namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000128class ObjCMethSigsChecker : public Checker<
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000129 check::ASTDecl<ObjCImplementationDecl> > {
130public:
131 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
132 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000133 CheckObjCInstMethSignature(D, BR, this);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000134 }
135};
136}
137
138void ento::registerObjCMethSigsChecker(CheckerManager &mgr) {
139 mgr.registerChecker<ObjCMethSigsChecker>();
140}