From 044e645605c6d75223e33d23e3c5701cb389969f Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Thu, 8 Mar 2012 00:20:03 +0000 Subject: [libclang] Enhance clang_getOverriddenCursors. Basically the current design is: -for an implementation method, show as overridden the interface method. This is not useful, and is inconsistent with the C++ side -for an interface method, show as overridden the protocols methods (this is desirable) and the methods from the categories; methods from categories are not useful since they are considered the same method (same USR). -If there is a protocol method or category method reported, it does not check the super class for overridden methods. This is really problematic since overridden methods from super class is what we want to give back. Change clang_getOverriddenCursors to show as overridden any method in the class's base class, its protocols, or its categories' protocols, that has the same selector and is of the same kind (class or instance). If no such method exists, the search continues to the class's superclass, its protocols, and its categories, and so on. A method from an Objective-C implementation is considered to override the same methods as its corresponding method in the interface. rdar://10967206 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152270 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/libclang/CXCursor.cpp | 63 +++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'tools') diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index ac8cf28880..a141af3c2b 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -794,16 +794,21 @@ static void CollectOverriddenMethods(CXTranslationUnit TU, if (!Ctx) return; - // If we have a class or category implementation, jump straight to the - // interface. - if (ObjCImplDecl *Impl = dyn_cast(Ctx)) - return CollectOverriddenMethods(TU, Impl->getClassInterface(), - Method, Methods); - ObjCContainerDecl *Container = dyn_cast(Ctx); if (!Container) return; + // In categories look for overriden methods from protocols. A method from + // category is not "overriden" since it is considered as the "same" method + // (same USR) as the one from the interface. + if (ObjCCategoryDecl *Category = dyn_cast(Container)) { + for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), + PEnd = Category->protocol_end(); + P != PEnd; ++P) + CollectOverriddenMethods(TU, *P, Method, Methods); + return; + } + // Check whether we have a matching method at this level. if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), Method->isInstanceMethod())) @@ -821,13 +826,6 @@ static void CollectOverriddenMethods(CXTranslationUnit TU, CollectOverriddenMethods(TU, *P, Method, Methods); } - if (ObjCCategoryDecl *Category = dyn_cast(Container)) { - for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), - PEnd = Category->protocol_end(); - P != PEnd; ++P) - CollectOverriddenMethods(TU, *P, Method, Methods); - } - if (ObjCInterfaceDecl *Interface = dyn_cast(Container)) { for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), PEnd = Interface->protocol_end(); @@ -838,10 +836,8 @@ static void CollectOverriddenMethods(CXTranslationUnit TU, Category; Category = Category->getNextClassCategory()) CollectOverriddenMethods(TU, Category, Method, Methods); - // We only look into the superclass if we haven't found anything yet. - if (Methods.empty()) - if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) - return CollectOverriddenMethods(TU, Super, Method, Methods); + if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) + return CollectOverriddenMethods(TU, Super, Method, Methods); } } @@ -869,8 +865,37 @@ void cxcursor::getOverriddenCursors(CXCursor cursor, if (!Method) return; - // Handle Objective-C methods. - CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden); + if (ObjCProtocolDecl * + ProtD = dyn_cast(Method->getDeclContext())) { + CollectOverriddenMethods(TU, ProtD, Method, overridden); + + } else if (ObjCImplDecl * + IMD = dyn_cast(Method->getDeclContext())) { + ObjCInterfaceDecl *ID = IMD->getClassInterface(); + if (!ID) + return; + // Start searching for overridden methods using the method from the + // interface as starting point. + if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), + Method->isInstanceMethod())) + Method = IFaceMeth; + CollectOverriddenMethods(TU, ID, Method, overridden); + + } else if (ObjCCategoryDecl * + CatD = dyn_cast(Method->getDeclContext())) { + ObjCInterfaceDecl *ID = CatD->getClassInterface(); + if (!ID) + return; + // Start searching for overridden methods using the method from the + // interface as starting point. + if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), + Method->isInstanceMethod())) + Method = IFaceMeth; + CollectOverriddenMethods(TU, ID, Method, overridden); + + } else { + CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden); + } } std::pair -- cgit v1.2.3