Index: clang-tools-extra/clangd/index/SymbolYAML.h =================================================================== --- clang-tools-extra/clangd/index/SymbolYAML.h +++ clang-tools-extra/clangd/index/SymbolYAML.h @@ -41,6 +41,9 @@ // The YAML result is safe to concatenate if you have multiple symbol slabs. void SymbolsToYAML(const SymbolSlab &Symbols, llvm::raw_ostream &OS); +std::unique_ptr buildStaticIndex(llvm::StringRef YamlSymbolFile, + bool UseDex = true); + } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/index/SymbolYAML.cpp =================================================================== --- clang-tools-extra/clangd/index/SymbolYAML.cpp +++ clang-tools-extra/clangd/index/SymbolYAML.cpp @@ -9,6 +9,7 @@ #include "SymbolYAML.h" #include "Index.h" +#include "dex/DexIndex.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Errc.h" @@ -25,18 +26,18 @@ using clang::clangd::SymbolID; using clang::clangd::SymbolLocation; using clang::index::SymbolInfo; -using clang::index::SymbolLanguage; using clang::index::SymbolKind; +using clang::index::SymbolLanguage; // Helper to (de)serialize the SymbolID. We serialize it as a hex string. struct NormalizedSymbolID { NormalizedSymbolID(IO &) {} - NormalizedSymbolID(IO &, const SymbolID& ID) { + NormalizedSymbolID(IO &, const SymbolID &ID) { llvm::raw_string_ostream OS(HexString); OS << ID; } - SymbolID denormalize(IO&) { + SymbolID denormalize(IO &) { SymbolID ID; HexString >> ID; return ID; @@ -167,7 +168,7 @@ return S; } -void SymbolsToYAML(const SymbolSlab& Symbols, llvm::raw_ostream &OS) { +void SymbolsToYAML(const SymbolSlab &Symbols, llvm::raw_ostream &OS) { llvm::yaml::Output Yout(OS); for (Symbol S : Symbols) // copy: Yout<< requires mutability. Yout << S; @@ -181,5 +182,25 @@ return OS.str(); } +// Build an in-memory static index for global symbols from a YAML-format file. +// The size of global symbols should be relatively small, so that all symbols +// can be managed in memory. +std::unique_ptr buildStaticIndex(llvm::StringRef YamlSymbolFile, + bool UseDex) { + auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile); + if (!Buffer) { + llvm::errs() << "Can't open " << YamlSymbolFile << "\n"; + return nullptr; + } + auto Slab = symbolsFromYAML(Buffer.get()->getBuffer()); + SymbolSlab::Builder SymsBuilder; + for (auto Sym : Slab) + SymsBuilder.insert(Sym); + + return UseDex ? dex::DexIndex::build(std::move(SymsBuilder).build()) + : MemIndex::build(std::move(SymsBuilder).build(), + SymbolOccurrenceSlab::createEmpty()); +} + } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/tool/ClangdMain.cpp =================================================================== --- clang-tools-extra/clangd/tool/ClangdMain.cpp +++ clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -39,25 +39,6 @@ enum class PCHStorageFlag { Disk, Memory }; -// Build an in-memory static index for global symbols from a YAML-format file. -// The size of global symbols should be relatively small, so that all symbols -// can be managed in memory. -std::unique_ptr buildStaticIndex(llvm::StringRef YamlSymbolFile) { - auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile); - if (!Buffer) { - llvm::errs() << "Can't open " << YamlSymbolFile << "\n"; - return nullptr; - } - auto Slab = symbolsFromYAML(Buffer.get()->getBuffer()); - SymbolSlab::Builder SymsBuilder; - for (auto Sym : Slab) - SymsBuilder.insert(Sym); - - return UseDex ? dex::DexIndex::build(std::move(SymsBuilder).build()) - : MemIndex::build(std::move(SymsBuilder).build(), - SymbolOccurrenceSlab::createEmpty()); -} - } // namespace static llvm::cl::opt CompileCommandsDir( @@ -299,7 +280,7 @@ Opts.BuildDynamicSymbolIndex = EnableIndex; std::unique_ptr StaticIdx; if (EnableIndex && !YamlSymbolFile.empty()) { - StaticIdx = buildStaticIndex(YamlSymbolFile); + StaticIdx = buildStaticIndex(YamlSymbolFile, UseDex); Opts.StaticIndex = StaticIdx.get(); } Opts.AsyncThreadsCount = WorkerThreadsCount;