| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 1 | //=- 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 Kremenek | 6296e09 | 2010-01-26 22:59:55 +0000 | [diff] [blame] | 16 | #include "clang/Checker/Checkers/LocalCheckers.h" | 
| Ted Kremenek | fe0fc40 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/PathDiagnostic.h" | 
|  | 18 | #include "clang/Checker/BugReporter/BugReporter.h" | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 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 Kremenek | 6c3413c | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | using namespace clang; | 
|  | 27 |  | 
|  | 28 | static 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. | 
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 33 | if (Derived->isAnyPointerType() &&  Ancestor->isAnyPointerType()) | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 34 | return true; | 
|  | 35 |  | 
|  | 36 | return C.typesAreCompatible(Derived, Ancestor); | 
|  | 37 | } | 
|  | 38 |  | 
| Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 39 | static void CompareReturnTypes(const ObjCMethodDecl *MethDerived, | 
|  | 40 | const ObjCMethodDecl *MethAncestor, | 
|  | 41 | BugReporter &BR, ASTContext &Ctx, | 
|  | 42 | const ObjCImplementationDecl *ID) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 44 | QualType ResDerived  = MethDerived->getResultType(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | QualType ResAncestor = MethAncestor->getResultType(); | 
|  | 46 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 47 | if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) { | 
| Ted Kremenek | 6c3413c | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 48 | std::string sbuf; | 
|  | 49 | llvm::raw_string_ostream os(sbuf); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 51 | os << "The Objective-C class '" | 
| Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 52 | << MethDerived->getClassInterface() | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 53 | << "', which is derived from class '" | 
| Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 54 | << MethAncestor->getClassInterface() | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 55 | << "', defines the instance method '" | 
| Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 56 | << MethDerived->getSelector().getAsString() | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 57 | << "' whose return type is '" | 
|  | 58 | << ResDerived.getAsString() | 
| Ted Kremenek | e5b5953 | 2008-07-11 23:17:01 +0000 | [diff] [blame] | 59 | << "'.  A method with the same name (same selector) is also defined in " | 
|  | 60 | "class '" | 
| Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 61 | << MethAncestor->getClassInterface() | 
| Ted Kremenek | e5b5953 | 2008-07-11 23:17:01 +0000 | [diff] [blame] | 62 | << "' and has a return type of '" | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 63 | << ResAncestor.getAsString() | 
|  | 64 | << "'.  These two types are incompatible, and may result in undefined " | 
|  | 65 | "behavior for clients of these classes."; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 |  | 
| Ted Kremenek | 6c3413c | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 67 | BR.EmitBasicReport("Incompatible instance method return type", | 
| Benjamin Kramer | 6341553 | 2009-11-29 18:27:55 +0000 | [diff] [blame] | 68 | os.str(), MethDerived->getLocStart()); | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
| Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 72 | void clang::CheckObjCInstMethSignature(const ObjCImplementationDecl* ID, | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 73 | BugReporter& BR) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 |  | 
| Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 75 | const ObjCInterfaceDecl* D = ID->getClassInterface(); | 
|  | 76 | const ObjCInterfaceDecl* C = D->getSuperClass(); | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 77 |  | 
|  | 78 | if (!C) | 
|  | 79 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 |  | 
| Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 81 | ASTContext& Ctx = BR.getContext(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 83 | // Build a DenseMap of the methods for quick querying. | 
|  | 84 | typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy; | 
|  | 85 | MapTy IMeths; | 
|  | 86 | unsigned NumMethods = 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 |  | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 88 | for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(), | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | E=ID->instmeth_end(); I!=E; ++I) { | 
|  | 90 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 91 | ObjCMethodDecl* M = *I; | 
|  | 92 | IMeths[M->getSelector()] = M; | 
|  | 93 | ++NumMethods; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | // Now recurse the class hierarchy chain looking for methods with the | 
|  | 97 | // same signatures. | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 98 | while (C && NumMethods) { | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 99 | for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(), | 
|  | 100 | E=C->instmeth_end(); I!=E; ++I) { | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 101 |  | 
|  | 102 | ObjCMethodDecl* M = *I; | 
|  | 103 | Selector S = M->getSelector(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 105 | MapTy::iterator MI = IMeths.find(S); | 
|  | 106 |  | 
|  | 107 | if (MI == IMeths.end() || MI->second == 0) | 
|  | 108 | continue; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 110 | --NumMethods; | 
|  | 111 | ObjCMethodDecl* MethDerived = MI->second; | 
|  | 112 | MI->second = 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 114 | CompareReturnTypes(MethDerived, M, BR, Ctx, ID); | 
|  | 115 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 |  | 
| Ted Kremenek | 3bfb314 | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 117 | C = C->getSuperClass(); | 
|  | 118 | } | 
|  | 119 | } |