clangd: Do not report inline overrides twice
authorChristian Kandeler <christian.kandeler@qt.io>
Thu, 23 Sep 2021 13:45:01 +0000 (15:45 +0200)
committerSam McCall <sam.mccall@gmail.com>
Thu, 23 Sep 2021 14:09:13 +0000 (16:09 +0200)
... in textDocument/references.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D110324

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

index 4cec85e20ad6602d3fc69b3de0686a531f31e548..ea61cba460efcce7b770bd6930327c59d2ba3824 100644 (file)
@@ -1431,17 +1431,20 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
         !OverriddenBy.Subjects.empty())
       Index->relations(
           OverriddenBy, [&](const SymbolID &Subject, const Symbol &Object) {
-            if (auto LSPLoc =
-                    toLSPLocation(Object.CanonicalDeclaration, *MainFilePath)) {
+            const auto LSPLocDecl =
+                toLSPLocation(Object.CanonicalDeclaration, *MainFilePath);
+            const auto LSPLocDef =
+                toLSPLocation(Object.Definition, *MainFilePath);
+            if (LSPLocDecl && LSPLocDecl != LSPLocDef) {
               ReferencesResult::Reference Result;
-              Result.Loc = std::move(*LSPLoc);
+              Result.Loc = std::move(*LSPLocDecl);
               Result.Attributes =
                   ReferencesResult::Declaration | ReferencesResult::Override;
               Results.References.push_back(std::move(Result));
             }
-            if (auto LSPLoc = toLSPLocation(Object.Definition, *MainFilePath)) {
+            if (LSPLocDef) {
               ReferencesResult::Reference Result;
-              Result.Loc = std::move(*LSPLoc);
+              Result.Loc = std::move(*LSPLocDef);
               Result.Attributes = ReferencesResult::Declaration |
                                   ReferencesResult::Definition |
                                   ReferencesResult::Override;
index 166e0674afea66021bc10d3b2aa6b976e4bb09d7..6a9d355792a67346363c35702b15d6c0b30a8cb4 100644 (file)
@@ -1947,6 +1947,9 @@ TEST(FindReferences, IncludeOverrides) {
           void $overridedecl[[func]]() override;
         };
         void Derived::$overridedef[[func]]() {}
+        class Derived2 : public Base {
+          void $overridedef[[func]]() override {}
+        };
         void test(Derived* D) {
           D->func();  // No references to the overrides.
         })cpp";