CPP 16
FileIndex.cpp Guest on 4th October 2020 12:23:36 AM
  1. //===--- FileIndex.cpp - Indexes for files. ------------------------ C++-*-===//
  2. //
  3. //                     The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9.  
  10. #include "FileIndex.h"
  11. #include "SymbolCollector.h"
  12. #include "clang/Index/IndexingAction.h"
  13.  
  14. namespace clang {
  15. namespace clangd {
  16. namespace {
  17.  
  18. /// Retrieves namespace and class level symbols in \p Decls.
  19. std::unique_ptr<SymbolSlab> indexAST(ASTContext &Ctx,
  20.                                      llvm::ArrayRef<const Decl *> Decls) {
  21.   auto Collector = std::make_shared<SymbolCollector>();
  22.   index::IndexingOptions IndexOpts;
  23.   IndexOpts.SystemSymbolFilter =
  24.       index::IndexingOptions::SystemSymbolFilterKind::All;
  25.   IndexOpts.IndexFunctionLocals = false;
  26.  
  27.   index::indexTopLevelDecls(Ctx, Decls, Collector, IndexOpts);
  28.   auto Symbols = llvm::make_unique<SymbolSlab>();
  29.   *Symbols = Collector->takeSymbols();
  30.   return Symbols;
  31. }
  32.  
  33. } // namespace
  34.  
  35. void FileSymbols::update(PathRef Path, std::unique_ptr<SymbolSlab> Slab) {
  36.   std::lock_guard<std::mutex> Lock(Mutex);
  37.   if (!Slab)
  38.     FileToSlabs.erase(Path);
  39.   else
  40.     FileToSlabs[Path] = std::move(Slab);
  41. }
  42.  
  43. std::shared_ptr<std::vector<const Symbol *>> FileSymbols::allSymbols() {
  44.   // The snapshot manages life time of symbol slabs and provides pointers of all
  45.   // symbols in all slabs.
  46.   struct Snapshot {
  47.     std::vector<const Symbol *> Pointers;
  48.     std::vector<std::shared_ptr<SymbolSlab>> KeepAlive;
  49.   };
  50.   auto Snap = std::make_shared<Snapshot>();
  51.   {
  52.     std::lock_guard<std::mutex> Lock(Mutex);
  53.  
  54.     for (const auto &FileAndSlab : FileToSlabs) {
  55.       Snap->KeepAlive.push_back(FileAndSlab.second);
  56.       for (const auto &Iter : *FileAndSlab.second)
  57.         Snap->Pointers.push_back(&Iter);
  58.     }
  59.   }
  60.   auto *Pointers = &Snap->Pointers;
  61.   // Use aliasing constructor to keep the snapshot alive along with the
  62.   // pointers.
  63.   return {std::move(Snap), Pointers};
  64. }
  65.  
  66. void FileIndex::update(const Context &Ctx, PathRef Path, ParsedAST *AST) {
  67.   if (!AST) {
  68.     FSymbols.update(Path, nullptr);
  69.   } else {
  70.     auto Slab = indexAST(AST->getASTContext(), AST->getTopLevelDecls());
  71.     FSymbols.update(Path, std::move(Slab));
  72.   }
  73.   auto Symbols = FSymbols.allSymbols();
  74.   Index.build(std::move(Symbols));
  75. }
  76.  
  77. bool FileIndex::fuzzyFind(
  78.     const Context &Ctx, const FuzzyFindRequest &Req,
  79.     llvm::function_ref<void(const Symbol &)> Callback) const {
  80.   return Index.fuzzyFind(Ctx, Req, Callback);
  81. }
  82.  
  83. } // namespace clangd
  84. } // namespace clang

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.