Index: clangd/index/Serialization.cpp =================================================================== --- clangd/index/Serialization.cpp +++ clangd/index/Serialization.cpp @@ -435,8 +435,9 @@ } trace::Span Tracer("BuildIndex"); - auto Index = UseDex ? dex::Dex::build(std::move(Symbols), URISchemes) - : MemIndex::build(std::move(Symbols), std::move(Refs)); + auto Index = + UseDex ? dex::Dex::build(std::move(Symbols), std::move(Refs), URISchemes) + : MemIndex::build(std::move(Symbols), std::move(Refs)); vlog("Loaded {0} from {1} with estimated memory usage {2}", UseDex ? "Dex" : "MemIndex", SymbolFilename, Index->estimateMemoryUsage()); Index: clangd/index/dex/Dex.h =================================================================== --- clangd/index/dex/Dex.h +++ clangd/index/dex/Dex.h @@ -41,9 +41,10 @@ // index on disk and then load it if available. class Dex : public SymbolIndex { public: - // All symbols must outlive this index. - template - Dex(Range &&Symbols, llvm::ArrayRef Schemes) + // All data must outlive this index. + template + Dex(SymbolRange &&Symbols, RefsRange &&Refs, + llvm::ArrayRef Schemes) : Corpus(0), URISchemes(Schemes) { // If Schemes don't contain any items, fall back to SymbolCollector's // default URI schemes. @@ -53,26 +54,24 @@ } for (auto &&Sym : Symbols) this->Symbols.push_back(&Sym); + for (auto &&Ref : Refs) + this->Refs.try_emplace(Ref.first, Ref.second); buildIndex(); } - // Symbols are owned by BackingData, Index takes ownership. - template - Dex(Range &&Symbols, Payload &&BackingData, size_t BackingDataSize, - llvm::ArrayRef URISchemes) - : Dex(std::forward(Symbols), URISchemes) { + // Symbols and Refs are owned by BackingData, Index takes ownership. + template + Dex(SymbolRange &&Symbols, RefsRange &&Refs, Payload &&BackingData, + size_t BackingDataSize, llvm::ArrayRef URISchemes) + : Dex(std::forward(Symbols), std::forward(Refs), + URISchemes) { KeepAlive = std::shared_ptr( std::make_shared(std::move(BackingData)), nullptr); this->BackingDataSize = BackingDataSize; } - /// Builds an index from a slab. The index takes ownership of the slab. + /// Builds an index from slabs. The index takes ownership of the slab. static std::unique_ptr - build(SymbolSlab Slab, llvm::ArrayRef URISchemes) { - // Store Slab size before it is moved. - const auto BackingDataSize = Slab.bytes(); - return llvm::make_unique(Slab, std::move(Slab), BackingDataSize, - URISchemes); - } + build(SymbolSlab, RefSlab, llvm::ArrayRef URISchemes); bool fuzzyFind(const FuzzyFindRequest &Req, @@ -102,6 +101,7 @@ /// during the fuzzyFind process. llvm::DenseMap InvertedIndex; dex::Corpus Corpus; + llvm::DenseMap> Refs; std::shared_ptr KeepAlive; // poor man's move-only std::any // Size of memory retained by KeepAlive. size_t BackingDataSize = 0; Index: clangd/index/dex/Dex.cpp =================================================================== --- clangd/index/dex/Dex.cpp +++ clangd/index/dex/Dex.cpp @@ -24,6 +24,15 @@ namespace clangd { namespace dex { +std::unique_ptr +Dex::build(SymbolSlab Symbols, RefSlab Refs, + llvm::ArrayRef URISchemes) { + auto Size = Symbols.bytes() + Refs.bytes(); + auto Data = std::make_pair(std::move(Symbols), std::move(Refs)); + return llvm::make_unique(Data.first, Data.second, std::move(Data), Size, + std::move(URISchemes)); +} + namespace { // Mark symbols which are can be used for code completion. @@ -254,7 +263,10 @@ void Dex::refs(const RefsRequest &Req, llvm::function_ref Callback) const { trace::Span Tracer("Dex refs"); - log("refs is not implemented."); + for (const auto &ID : Req.IDs) + for (const auto &Ref : Refs.lookup(ID)) + if (static_cast(Req.Filter & Ref.Kind)) + Callback(Ref); } size_t Dex::estimateMemoryUsage() const { @@ -264,6 +276,7 @@ Bytes += InvertedIndex.getMemorySize(); for (const auto &TokenToPostingList : InvertedIndex) Bytes += TokenToPostingList.second.bytes(); + Bytes += Refs.getMemorySize(); return Bytes + BackingDataSize; } Index: clangd/indexer/IndexerMain.cpp =================================================================== --- clangd/indexer/IndexerMain.cpp +++ clangd/indexer/IndexerMain.cpp @@ -47,7 +47,7 @@ "human-readable YAML format"), clEnumValN(IndexFileFormat::RIFF, "binary", "binary RIFF format")), - llvm::cl::init(IndexFileFormat::YAML)); + llvm::cl::init(IndexFileFormat::RIFF)); class IndexActionFactory : public tooling::FrontendActionFactory { public: Index: unittests/clangd/DexTests.cpp =================================================================== --- unittests/clangd/DexTests.cpp +++ unittests/clangd/DexTests.cpp @@ -423,7 +423,8 @@ //===----------------------------------------------------------------------===// TEST(Dex, Lookup) { - auto I = Dex::build(generateSymbols({"ns::abc", "ns::xyz"}), URISchemes); + auto I = Dex::build(generateSymbols({"ns::abc", "ns::xyz"}), RefSlab(), + URISchemes); EXPECT_THAT(lookup(*I, SymbolID("ns::abc")), UnorderedElementsAre("ns::abc")); EXPECT_THAT(lookup(*I, {SymbolID("ns::abc"), SymbolID("ns::xyz")}), UnorderedElementsAre("ns::abc", "ns::xyz")); @@ -436,7 +437,7 @@ auto Index = Dex::build(generateSymbols({"ns::ABC", "ns::BCD", "::ABC", "ns::nested::ABC", "other::ABC", "other::A"}), - URISchemes); + RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "ABC"; Req.Scopes = {"ns::"}; @@ -466,13 +467,13 @@ symbol("2") /* duplicate */}; FuzzyFindRequest Req; Req.Query = "2"; - Dex I(Symbols, URISchemes); + Dex I(Symbols, RefSlab(), URISchemes); EXPECT_FALSE(Req.Limit); EXPECT_THAT(match(I, Req), ElementsAre("2", "2")); } TEST(DexTest, DexLimitedNumMatches) { - auto I = Dex::build(generateNumSymbols(0, 100), URISchemes); + auto I = Dex::build(generateNumSymbols(0, 100), RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "5"; Req.Limit = 3; @@ -486,7 +487,7 @@ TEST(DexTest, FuzzyMatch) { auto I = Dex::build( generateSymbols({"LaughingOutLoud", "LionPopulation", "LittleOldLady"}), - URISchemes); + RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "lol"; Req.Limit = 2; @@ -495,14 +496,16 @@ } TEST(DexTest, MatchQualifiedNamesWithoutSpecificScope) { - auto I = Dex::build(generateSymbols({"a::y1", "b::y2", "y3"}), URISchemes); + auto I = Dex::build(generateSymbols({"a::y1", "b::y2", "y3"}), RefSlab(), + URISchemes); FuzzyFindRequest Req; Req.Query = "y"; EXPECT_THAT(match(*I, Req), UnorderedElementsAre("a::y1", "b::y2", "y3")); } TEST(DexTest, MatchQualifiedNamesWithGlobalScope) { - auto I = Dex::build(generateSymbols({"a::y1", "b::y2", "y3"}), URISchemes); + auto I = Dex::build(generateSymbols({"a::y1", "b::y2", "y3"}), RefSlab(), + URISchemes); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {""}; @@ -510,8 +513,9 @@ } TEST(DexTest, MatchQualifiedNamesWithOneScope) { - auto I = Dex::build( - generateSymbols({"a::y1", "a::y2", "a::x", "b::y2", "y3"}), URISchemes); + auto I = + Dex::build(generateSymbols({"a::y1", "a::y2", "a::x", "b::y2", "y3"}), + RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a::"}; @@ -520,7 +524,7 @@ TEST(DexTest, MatchQualifiedNamesWithMultipleScopes) { auto I = Dex::build( - generateSymbols({"a::y1", "a::y2", "a::x", "b::y3", "y3"}), URISchemes); + generateSymbols({"a::y1", "a::y2", "a::x", "b::y3", "y3"}), RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a::", "b::"}; @@ -528,7 +532,7 @@ } TEST(DexTest, NoMatchNestedScopes) { - auto I = Dex::build(generateSymbols({"a::y1", "a::b::y2"}), URISchemes); + auto I = Dex::build(generateSymbols({"a::y1", "a::b::y2"}), RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a::"}; @@ -537,7 +541,7 @@ TEST(DexTest, WildcardScope) { auto I = - Dex::build(generateSymbols({"a::y1", "a::b::y2", "c::y3"}), URISchemes); + Dex::build(generateSymbols({"a::y1", "a::b::y2", "c::y3"}), RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a::"}; @@ -547,7 +551,7 @@ } TEST(DexTest, IgnoreCases) { - auto I = Dex::build(generateSymbols({"ns::ABC", "ns::abc"}), URISchemes); + auto I = Dex::build(generateSymbols({"ns::ABC", "ns::abc"}), RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "AB"; Req.Scopes = {"ns::"}; @@ -555,7 +559,7 @@ } TEST(DexTest, Lookup) { - auto I = Dex::build(generateSymbols({"ns::abc", "ns::xyz"}), URISchemes); + auto I = Dex::build(generateSymbols({"ns::abc", "ns::xyz"}), RefSlab(), URISchemes); EXPECT_THAT(lookup(*I, SymbolID("ns::abc")), UnorderedElementsAre("ns::abc")); EXPECT_THAT(lookup(*I, {SymbolID("ns::abc"), SymbolID("ns::xyz")}), UnorderedElementsAre("ns::abc", "ns::xyz")); @@ -570,7 +574,7 @@ CodeCompletionSymbol.Flags = Symbol::SymbolFlag::IndexedForCodeCompletion; NonCodeCompletionSymbol.Flags = Symbol::SymbolFlag::None; std::vector Symbols{CodeCompletionSymbol, NonCodeCompletionSymbol}; - Dex I(Symbols, URISchemes); + Dex I(Symbols, RefSlab(), URISchemes); FuzzyFindRequest Req; Req.RestrictForCodeCompletion = false; EXPECT_THAT(match(I, Req), ElementsAre("Completion", "NoCompletion")); @@ -585,7 +589,7 @@ CloseSymbol.CanonicalDeclaration.FileURI = "unittest:///a/b/c/d/e/f/file.h"; std::vector Symbols{CloseSymbol, RootSymbol}; - Dex I(Symbols, URISchemes); + Dex I(Symbols, RefSlab(), URISchemes); FuzzyFindRequest Req; Req.Query = "abc"; @@ -603,6 +607,31 @@ EXPECT_THAT(match(I, Req), ElementsAre("root::abc")); } +TEST(DexTests, Refs) { + DenseMap> Refs; + auto AddRef = [&](const Symbol& Sym, StringRef Filename, RefKind Kind) { + auto& SymbolRefs = Refs[Sym.ID]; + SymbolRefs.emplace_back(); + SymbolRefs.back().Kind = Kind; + SymbolRefs.back().Location.FileURI = Filename; + }; + auto Foo = symbol("foo"); + auto Bar = symbol("bar"); + AddRef(Foo, "foo.h", RefKind::Declaration); + AddRef(Foo, "reffoo.h", RefKind::Reference); + AddRef(Bar, "bar.h", RefKind::Declaration); + + std::vector Files; + RefsRequest Req; + Req.IDs.insert(Foo.ID); + Req.Filter = RefKind::Declaration | RefKind::Definition; + Dex(std::vector{Foo, Bar}, Refs, {}).refs(Req, [&](const Ref &R) { + Files.push_back(R.Location.FileURI); + }); + + EXPECT_THAT(Files, ElementsAre("foo.h")); +} + } // namespace } // namespace dex } // namespace clangd