[PATCH] D95812: [clangd] Report only decl of overridding method in xref.

Utkarsh Saxena via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 1 12:23:12 PST 2021


usaxena95 created this revision.
Herald added subscribers: kadircet, arphaman.
usaxena95 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

See: https://github.com/clangd/clangd/issues/668

  struct A { virtual void foo() = 0; };
  struct B : A { void foo() override; };

Find refs on `A::foo()` will show:

- decls of `A::foo()`
- decls of `B::foo()`
- refs to `A::foo()`
- no refs to `B::foo()`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95812

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


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1850,14 +1850,14 @@
       R"cpp(
         class Base {
         public:
-          virtual void [[f^unc]]() = 0;
+          virtual void [[f^unc]]();
         };
         class Derived : public Base {
         public:
           void [[func]]() override;
         };
         void test(Derived* D) {
-          D->[[func]]();
+          D->func();  // No references to the overrides.
         })cpp";
   Annotations T(Test);
   auto TU = TestTU::withCode(T.code());
Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1298,6 +1298,7 @@
   }
 
   RefsRequest Req;
+  RelationsRequest OverriddenBy;
 
   const auto *IdentifierAtCursor =
       syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
@@ -1332,26 +1333,18 @@
       if (auto ID = getSymbolID(D))
         Targets.insert(ID);
 
-    llvm::DenseSet<SymbolID> Overrides;
     if (Index) {
-      RelationsRequest FindOverrides;
-      FindOverrides.Predicate = RelationKind::OverriddenBy;
+      OverriddenBy.Predicate = RelationKind::OverriddenBy;
       for (const NamedDecl *ND : Decls) {
-        // Special case: virtual void meth^od() = 0 includes refs of overrides.
+        // Special case: Inlcude declaration of overridding methods.
         if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
-          if (CMD->isPure())
+          if (CMD->isVirtual())
             if (IdentifierAtCursor && SM.getSpellingLoc(CMD->getLocation()) ==
                                           IdentifierAtCursor->location())
               if (auto ID = getSymbolID(CMD))
-                FindOverrides.Subjects.insert(ID);
+                OverriddenBy.Subjects.insert(ID);
         }
       }
-      if (!FindOverrides.Subjects.empty())
-        Index->relations(FindOverrides,
-                         [&](const SymbolID &Subject, const Symbol &Object) {
-                           Overrides.insert(Object.ID);
-                         });
-      Targets.insert(Overrides.begin(), Overrides.end());
     }
 
     // We traverse the AST to find references in the main file.
@@ -1373,7 +1366,6 @@
       Results.References.push_back(std::move(Result));
     }
     if (Index && Results.References.size() <= Limit) {
-      Req.IDs = std::move(Overrides);
       for (const Decl *D : Decls) {
         // Not all symbols can be referenced from outside (e.g.
         // function-locals).
@@ -1386,6 +1378,15 @@
       }
     }
   }
+  // Add declaration of overridding methods.
+  if (Index && !OverriddenBy.Subjects.empty())
+    Index->relations(
+        OverriddenBy, [&](const SymbolID &Subject, const Symbol &Object) {
+          if (auto LSPLoc =
+                  toLSPLocation(Object.CanonicalDeclaration, *MainFilePath))
+            Results.References.push_back(std::move(*LSPLoc));
+        });
+
   // Now query the index for references from other files.
   if (!Req.IDs.empty() && Index && Results.References.size() <= Limit) {
     Req.Limit = Limit;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95812.320559.patch
Type: text/x-patch
Size: 3317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210201/423d2e88/attachment.bin>


More information about the cfe-commits mailing list