Ted Kremenek | 0d8019e | 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 | |
| 16 | #include "clang/Analysis/LocalCheckers.h" |
| 17 | #include "clang/Analysis/PathDiagnostic.h" |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 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" |
| 24 | #include <sstream> |
| 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. |
| 33 | if ((Derived->isPointerType() || Derived->isObjCQualifiedIdType()) && |
| 34 | (Ancestor->isPointerType() || Ancestor->isObjCQualifiedIdType())) |
| 35 | return true; |
| 36 | |
| 37 | return C.typesAreCompatible(Derived, Ancestor); |
| 38 | } |
| 39 | |
| 40 | static void CompareReturnTypes(ObjCMethodDecl* MethDerived, |
| 41 | ObjCMethodDecl* MethAncestor, |
| 42 | BugReporter& BR, ASTContext& Ctx, |
| 43 | ObjCImplementationDecl* ID) { |
| 44 | |
| 45 | QualType ResDerived = MethDerived->getResultType(); |
| 46 | QualType ResAncestor = MethAncestor->getResultType(); |
| 47 | |
| 48 | if (!AreTypesCompatible(ResDerived, ResAncestor, Ctx)) { |
| 49 | std::ostringstream os; |
| 50 | |
| 51 | os << "The Objective-C class '" |
| 52 | << MethDerived->getClassInterface()->getName() |
| 53 | << "', which is derived from class '" |
| 54 | << MethAncestor->getClassInterface()->getName() |
| 55 | << "', defines the instance method '" |
| 56 | << MethDerived->getSelector().getName() |
| 57 | << "' whose return type is '" |
| 58 | << ResDerived.getAsString() |
Ted Kremenek | e4773eb | 2008-07-11 23:17:01 +0000 | [diff] [blame] | 59 | << "'. A method with the same name (same selector) is also defined in " |
| 60 | "class '" |
| 61 | << MethAncestor->getClassInterface()->getName() |
| 62 | << "' and has a return type of '" |
Ted Kremenek | 0d8019e | 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."; |
| 66 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 67 | BR.EmitBasicReport("incompatible instance method return type", |
| 68 | os.str().c_str(), MethDerived->getLocStart()); |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | void clang::CheckObjCInstMethSignature(ObjCImplementationDecl* ID, |
| 73 | BugReporter& BR) { |
| 74 | |
| 75 | ObjCInterfaceDecl* D = ID->getClassInterface(); |
| 76 | ObjCInterfaceDecl* C = D->getSuperClass(); |
| 77 | |
| 78 | if (!C) |
| 79 | return; |
| 80 | |
| 81 | // Build a DenseMap of the methods for quick querying. |
| 82 | typedef llvm::DenseMap<Selector,ObjCMethodDecl*> MapTy; |
| 83 | MapTy IMeths; |
| 84 | unsigned NumMethods = 0; |
| 85 | |
| 86 | for (ObjCImplementationDecl::instmeth_iterator I=ID->instmeth_begin(), |
| 87 | E=ID->instmeth_end(); I!=E; ++I) { |
| 88 | |
| 89 | ObjCMethodDecl* M = *I; |
| 90 | IMeths[M->getSelector()] = M; |
| 91 | ++NumMethods; |
| 92 | } |
| 93 | |
| 94 | // Now recurse the class hierarchy chain looking for methods with the |
| 95 | // same signatures. |
| 96 | ASTContext& Ctx = BR.getContext(); |
| 97 | |
| 98 | while (C && NumMethods) { |
| 99 | for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(), |
| 100 | E=C->instmeth_end(); I!=E; ++I) { |
| 101 | |
| 102 | ObjCMethodDecl* M = *I; |
| 103 | Selector S = M->getSelector(); |
| 104 | |
| 105 | MapTy::iterator MI = IMeths.find(S); |
| 106 | |
| 107 | if (MI == IMeths.end() || MI->second == 0) |
| 108 | continue; |
| 109 | |
| 110 | --NumMethods; |
| 111 | ObjCMethodDecl* MethDerived = MI->second; |
| 112 | MI->second = 0; |
| 113 | |
| 114 | CompareReturnTypes(MethDerived, M, BR, Ctx, ID); |
| 115 | } |
| 116 | |
| 117 | C = C->getSuperClass(); |
| 118 | } |
| 119 | } |