From: Argyrios Kyrtzidis Date: Fri, 21 Apr 2017 22:27:06 +0000 (+0000) Subject: [index] Take into account the category's external_symbol attr for namespacing its... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=54fef63e22057dd3dff6c843f344e9a67aff2fee;p=clang [index] Take into account the category's external_symbol attr for namespacing its methods git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@301051 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Index/USRGeneration.h b/include/clang/Index/USRGeneration.h index e3a4620562..8c661bd63c 100644 --- a/include/clang/Index/USRGeneration.h +++ b/include/clang/Index/USRGeneration.h @@ -31,7 +31,8 @@ bool generateUSRForDecl(const Decl *D, SmallVectorImpl &Buf); /// \brief Generate a USR fragment for an Objective-C class. void generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, - StringRef ExtSymbolDefinedIn = ""); + StringRef ExtSymbolDefinedIn = "", + StringRef CategoryContextExtSymbolDefinedIn = ""); /// \brief Generate a USR fragment for an Objective-C class category. void generateUSRForObjCCategory(StringRef Cls, StringRef Cat, raw_ostream &OS, @@ -58,6 +59,9 @@ void generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS, void generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, StringRef ExtSymbolDefinedIn = ""); +/// Generate a USR fragment for an enum constant. +void generateUSRForEnumConstant(StringRef EnumConstantName, raw_ostream &OS); + /// \brief Generate a USR for a macro, including the USR prefix. /// /// \returns true on error, false on success. diff --git a/lib/Index/USRGeneration.cpp b/lib/Index/USRGeneration.cpp index c373eda98d..ed469f677a 100644 --- a/lib/Index/USRGeneration.cpp +++ b/lib/Index/USRGeneration.cpp @@ -88,7 +88,8 @@ public: void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); void VisitClassTemplateDecl(const ClassTemplateDecl *D); - void VisitObjCContainerDecl(const ObjCContainerDecl *CD); + void VisitObjCContainerDecl(const ObjCContainerDecl *CD, + const ObjCCategoryDecl *CatD = nullptr); void VisitObjCMethodDecl(const ObjCMethodDecl *MD); void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); @@ -138,8 +139,10 @@ public: /// itself. /// Generate a USR for an Objective-C class. - void GenObjCClass(StringRef cls, StringRef ext) { - generateUSRForObjCClass(cls, Out, ext); + void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn, + StringRef CategoryContextExtSymbolDefinedIn) { + generateUSRForObjCClass(cls, Out, ExtSymDefinedIn, + CategoryContextExtSymbolDefinedIn); } /// Generate a USR for an Objective-C class category. @@ -383,7 +386,16 @@ void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { IgnoreResults = true; return; } - Visit(ID); + auto getCategoryContext = [](const ObjCMethodDecl *D) -> + const ObjCCategoryDecl * { + if (auto *CD = dyn_cast(D->getDeclContext())) + return CD; + if (auto *ICD = dyn_cast(D->getDeclContext())) + return ICD->getCategoryDecl(); + return nullptr; + }; + auto *CD = getCategoryContext(D); + VisitObjCContainerDecl(ID, CD); } // Ideally we would use 'GenObjCMethod', but this is such a hot path // for Objective-C code that we don't want to use @@ -392,13 +404,15 @@ void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { << DeclarationName(D->getSelector()); } -void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) { +void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D, + const ObjCCategoryDecl *CatD) { switch (D->getKind()) { default: llvm_unreachable("Invalid ObjC container."); case Decl::ObjCInterface: case Decl::ObjCImplementation: - GenObjCClass(D->getName(), GetExternalSourceContainer(D)); + GenObjCClass(D->getName(), GetExternalSourceContainer(D), + GetExternalSourceContainer(CatD)); break; case Decl::ObjCCategory: { const ObjCCategoryDecl *CD = cast(D); @@ -896,10 +910,26 @@ void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { // USR generation functions. //===----------------------------------------------------------------------===// +static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn, + StringRef CatSymDefinedIn, + raw_ostream &OS) { + if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty()) + return; + if (CatSymDefinedIn.empty()) { + OS << "@M@" << ClsSymDefinedIn << '@'; + return; + } + OS << "@CM@" << CatSymDefinedIn << '@'; + if (ClsSymDefinedIn != CatSymDefinedIn) { + OS << ClsSymDefinedIn << '@'; + } +} + void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, - StringRef ExtSymDefinedIn) { - if (!ExtSymDefinedIn.empty()) - OS << "@M@" << ExtSymDefinedIn << '@'; + StringRef ExtSymDefinedIn, + StringRef CategoryContextExtSymbolDefinedIn) { + combineClassAndCategoryExtContainers(ExtSymDefinedIn, + CategoryContextExtSymbolDefinedIn, OS); OS << "objc(cs)" << Cls; } @@ -907,16 +937,7 @@ void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat, raw_ostream &OS, StringRef ClsSymDefinedIn, StringRef CatSymDefinedIn) { - if (!CatSymDefinedIn.empty() || !ClsSymDefinedIn.empty()) { - OS << "@M@"; - if (!CatSymDefinedIn.empty() && !ClsSymDefinedIn.empty()) - OS << CatSymDefinedIn << '-' << ClsSymDefinedIn; - else if (!CatSymDefinedIn.empty()) - OS << CatSymDefinedIn; - else - OS << ClsSymDefinedIn; - OS << '@'; - } + combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS); OS << "objc(cy)" << Cls << '@' << Cat; } @@ -949,6 +970,11 @@ void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, OS << "@E@" << EnumName; } +void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName, + raw_ostream &OS) { + OS << '@' << EnumConstantName; +} + bool clang::index::generateUSRForDecl(const Decl *D, SmallVectorImpl &Buf) { if (!D) diff --git a/test/Index/Core/external-source-symbol-attr.m b/test/Index/Core/external-source-symbol-attr.m index 800a03e474..b0e9c159cd 100644 --- a/test/Index/Core/external-source-symbol-attr.m +++ b/test/Index/Core/external-source-symbol-attr.m @@ -2,6 +2,7 @@ #define EXT_DECL(mod_name) __attribute__((external_source_symbol(language="Swift", defined_in=mod_name))) #define GEN_DECL(mod_name) __attribute__((external_source_symbol(language="Swift", defined_in=mod_name, generated_declaration))) +#define PUSH_GEN_DECL(mod_name) push(GEN_DECL(mod_name), apply_to=any(enum, objc_interface, objc_category, objc_protocol)) // This should not be indexed. GEN_DECL("some_module") @@ -37,14 +38,14 @@ EXT_DECL("some_module") EXT_DECL("cat_module") @interface I1(cat2) -// CHECK: [[@LINE-1]]:15 | extension/ObjC | cat2 | c:@M@cat_module-some_module@objc(cy)I1@cat2 | +// CHECK: [[@LINE-1]]:15 | extension/ObjC | cat2 | c:@CM@cat_module@some_module@objc(cy)I1@cat2 | -(void)cat_method2; -// CHECK: [[@LINE-1]]:8 | instance-method/ObjC | cat_method2 | c:@M@some_module@objc(cs)I1(im)cat_method2 +// CHECK: [[@LINE-1]]:8 | instance-method/ObjC | cat_method2 | c:@CM@cat_module@some_module@objc(cs)I1(im)cat_method2 @end #define NS_ENUM(_name, _type) enum _name:_type _name; enum _name : _type -#pragma clang attribute push(GEN_DECL("modname"), apply_to=any(enum, objc_interface, objc_category, objc_protocol)) +#pragma clang attribute PUSH_GEN_DECL("modname") @interface I3 // CHECK-NOT: [[@LINE-1]]:12 | @@ -80,9 +81,20 @@ void test2(I3 *i3, id prot2, SomeEnum some) { [i3 meth]; // CHECK: [[@LINE-1]]:7 | instance-method/ObjC | meth | c:@M@modname@objc(cs)I3(im)meth | [i3 meth2]; - // CHECK: [[@LINE-1]]:7 | instance-method/ObjC | meth2 | c:@M@modname@objc(cs)I3(im)meth2 | + // CHECK: [[@LINE-1]]:7 | instance-method/ObjC | meth2 | c:@CM@modname@objc(cs)I3(im)meth2 | [prot2 meth]; // CHECK: [[@LINE-1]]:10 | instance-method/ObjC | meth | c:@M@modname@objc(pl)ExtProt2(im)meth | some = SomeEnumFirst; // CHECK: [[@LINE-1]]:10 | enumerator/C | SomeEnumFirst | c:@M@modname@E@SomeEnum@SomeEnumFirst | } + +#pragma clang attribute PUSH_GEN_DECL("other_mod_for_cat") +@interface I3(cat_other_mod) +-(void)meth_other_mod; +@end +#pragma clang attribute pop + +void test3(I3 *i3) { + [i3 meth_other_mod]; + // CHECK: [[@LINE-1]]:7 | instance-method/ObjC | meth_other_mod | c:@CM@other_mod_for_cat@modname@objc(cs)I3(im)meth_other_mod | +}