[PATCH] D83501: [clangd][ObjC] Improve xrefs for protocols and classes

David Goldman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 10 10:09:44 PDT 2020


dgoldman updated this revision to Diff 277084.
dgoldman added a comment.

Swap to getPreferredDecl and improve targetDecl


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83501/new/

https://reviews.llvm.org/D83501

Files:
  clang-tools-extra/clangd/FindTarget.cpp
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/FindTargetTests.cpp


Index: clang-tools-extra/clangd/unittests/FindTargetTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -608,6 +608,24 @@
   )cpp";
   EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
 
+  Code = R"cpp(
+    @interface Foo
+    @end
+    @implementation [[Foo]]
+    @end
+  )cpp";
+  EXPECT_DECLS("ObjCImplementationDecl", "@interface Foo");
+
+  Code = R"cpp(
+    @interface Foo
+    @end
+    @interface Foo (Ext)
+    @end
+    @implementation [[Foo]] (Ext)
+    @end
+  )cpp";
+  EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
+
   Code = R"cpp(
     @protocol Foo
     @end
Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -239,10 +239,10 @@
 // - implementation: @implementation MyClass ... @end
 //
 // Clang will consider the forward declaration to be the canonical declaration
-// because it is first, but we actually want the class definition if it is
-// available since that is what a programmer would consider the real definition
-// to be.
-const NamedDecl *getNonStandardCanonicalDecl(const NamedDecl *D) {
+// because it is first. We actually want the class definition if it is
+// available since that is what a programmer would consider the primary
+// declaration to be.
+const NamedDecl *getPreferredDecl(const NamedDecl *D) {
   D = llvm::cast<NamedDecl>(D->getCanonicalDecl());
 
   // Prefer Objective-C class definitions over the forward declaration.
@@ -268,7 +268,7 @@
   llvm::DenseMap<SymbolID, size_t> ResultIndex;
 
   auto AddResultDecl = [&](const NamedDecl *D) {
-    D = getNonStandardCanonicalDecl(D);
+    D = getPreferredDecl(D);
     auto Loc =
         makeLocation(AST.getASTContext(), nameLocation(*D, SM), MainFilePath);
     if (!Loc)
Index: clang-tools-extra/clangd/FindTarget.cpp
===================================================================
--- clang-tools-extra/clangd/FindTarget.cpp
+++ clang-tools-extra/clangd/FindTarget.cpp
@@ -270,6 +270,15 @@
       // Record the underlying decl instead, if allowed.
       D = USD->getTargetDecl();
       Flags |= Rel::Underlying; // continue with the underlying decl.
+    } else if (const ObjCImplementationDecl *IID =
+                   dyn_cast<ObjCImplementationDecl>(D)) {
+      // Objective-C implementation should map back to its interface.
+      D = IID->getClassInterface();
+    } else if (const ObjCCategoryImplDecl *CID =
+                   dyn_cast<ObjCCategoryImplDecl>(D)) {
+      // Objective-C category implementation should map back to its category
+      // declaration.
+      D = CID->getCategoryDecl();
     }
 
     if (const Decl *Pat = getTemplatePattern(D)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83501.277084.patch
Type: text/x-patch
Size: 2899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200710/85431577/attachment.bin>


More information about the cfe-commits mailing list