diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -1332,26 +1332,19 @@ if (auto ID = getSymbolID(D)) Targets.insert(ID); - llvm::DenseSet Overrides; + RelationsRequest OverriddenBy; 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(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. @@ -1372,8 +1365,17 @@ Result.uri = URIMainFile; Results.References.push_back(std::move(Result)); } + // Add declaration of overridding methods. + if (Index && Results.References.size() <= Limit && + !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)); + }); + 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 +1388,7 @@ } } } + // Now query the index for references from other files. if (!Req.IDs.empty() && Index && Results.References.size() <= Limit) { Req.Limit = Limit; diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/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());