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 @@ -18,6 +18,7 @@ #include "index/Index.h" #include "index/Merge.h" #include "index/Relation.h" +#include "index/SymbolID.h" #include "index/SymbolLocation.h" #include "support/Logger.h" #include "clang/AST/ASTContext.h" @@ -47,6 +48,7 @@ #include "clang/Index/USRGeneration.h" #include "clang/Tooling/Syntax/Tokens.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/None.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/ScopeExit.h" @@ -54,6 +56,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Error.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" @@ -887,8 +890,12 @@ }; ReferenceFinder(const ParsedAST &AST, - const llvm::DenseSet &TargetIDs, bool PerToken) - : PerToken(PerToken), AST(AST), TargetIDs(TargetIDs) {} + const llvm::DenseSet &TD, bool PerToken) + : PerToken(PerToken), AST(AST) { + for (const Decl *D : TD) { + TargetDecls.insert(D->getCanonicalDecl()); + } + } std::vector take() && { llvm::sort(References, [](const Reference &L, const Reference &R) { @@ -913,12 +920,11 @@ llvm::ArrayRef Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override { + if (!TargetDecls.contains(D->getCanonicalDecl())) + return true; const SourceManager &SM = AST.getSourceManager(); if (!isInsideMainFile(Loc, SM)) return true; - SymbolID ID = getSymbolID(D); - if (!TargetIDs.contains(ID)) - return true; const auto &TB = AST.getTokens(); llvm::SmallVector Locs; @@ -942,7 +948,7 @@ for (SourceLocation L : Locs) { L = SM.getFileLoc(L); if (const auto *Tok = TB.spelledTokenAt(L)) - References.push_back({*Tok, Roles, ID}); + References.push_back({*Tok, Roles, getSymbolID(D)}); } return true; } @@ -951,12 +957,13 @@ bool PerToken; // If true, report 3 references for split ObjC selector names. std::vector References; const ParsedAST * - const llvm::DenseSet &TargetIDs; + llvm::DenseSet TargetDecls; }; std::vector -findRefs(const llvm::DenseSet &IDs, ParsedAST &AST, bool PerToken) { - ReferenceFinder RefFinder(AST, IDs, PerToken); +findRefs(const llvm::DenseSet &TargetDecls, ParsedAST &AST, + bool PerToken) { + ReferenceFinder RefFinder(AST, TargetDecls, PerToken); index::IndexingOptions IndexOpts; IndexOpts.SystemSymbolFilter = index::IndexingOptions::SystemSymbolFilterKind::All; @@ -1242,16 +1249,19 @@ if (const SelectionTree::Node *N = ST.commonAncestor()) { DeclRelationSet Relations = DeclRelation::TemplatePattern | DeclRelation::Alias; - auto Decls = - targetDecl(N->ASTNode, Relations, AST.getHeuristicResolver()); - if (!Decls.empty()) { + llvm::DenseSet TargetDecls; + for (const NamedDecl *D : + targetDecl(N->ASTNode, Relations, AST.getHeuristicResolver())) { + TargetDecls.insert(D); + } + if (!TargetDecls.empty()) { // FIXME: we may get multiple DocumentHighlights with the same location // and different kinds, deduplicate them. llvm::DenseSet Targets; - for (const NamedDecl *ND : Decls) - if (auto ID = getSymbolID(ND)) + for (const Decl *D : TargetDecls) + if (auto ID = getSymbolID(D)) Targets.insert(ID); - for (const auto &Ref : findRefs(Targets, AST, /*PerToken=*/true)) + for (const auto &Ref : findRefs(TargetDecls, AST, /*PerToken=*/true)) Result.push_back(toHighlight(Ref, SM)); return true; } @@ -1377,12 +1387,12 @@ DeclRelation::TemplatePattern | DeclRelation::Alias; std::vector Decls = getDeclAtPosition(AST, *CurLoc, Relations); - llvm::DenseSet TargetsInMainFile; + llvm::DenseSet TargetDeclsInMainFile; for (const NamedDecl *D : Decls) { auto ID = getSymbolID(D); if (!ID) continue; - TargetsInMainFile.insert(ID); + TargetDeclsInMainFile.insert(D); // Not all symbols can be referenced from outside (e.g. function-locals). // TODO: we could skip TU-scoped symbols here (e.g. static functions) if // we know this file isn't a header. The details might be tricky. @@ -1408,7 +1418,8 @@ } // We traverse the AST to find references in the main file. - auto MainFileRefs = findRefs(TargetsInMainFile, AST, /*PerToken=*/false); + auto MainFileRefs = + findRefs(TargetDeclsInMainFile, AST, /*PerToken=*/false); // We may get multiple refs with the same location and different Roles, as // cross-reference is only interested in locations, we deduplicate them // by the location to avoid emitting duplicated locations.