ioeric updated this revision to Diff 163276.
ioeric added a comment.

- revert unintended change


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D51475

Files:
  clangd/tool/ClangdMain.cpp

Index: clangd/tool/ClangdMain.cpp
===================================================================
--- clangd/tool/ClangdMain.cpp
+++ clangd/tool/ClangdMain.cpp
@@ -14,6 +14,8 @@
 #include "index/SymbolYAML.h"
 #include "index/dex/DexIndex.h"
 #include "clang/Basic/Version.h"
+#include "clang/Basic/Version.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
@@ -39,22 +41,80 @@
 
 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<SymbolIndex> buildStaticIndex(llvm::StringRef YamlSymbolFile) {
-  auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
-  if (!Buffer) {
-    llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
-    return nullptr;
+// Loads the index asynchornously. This acts like an empty index before
+// finishing loading and proxies index requests to the loaded index after
+// loading.
+class AsyncLoadIndex : public SymbolIndex {
+public:
+  AsyncLoadIndex(
+      llvm::unique_function<std::unique_ptr<SymbolIndex>()> LoadIndex)
+      : AsyncLoad(runAsync(std::move(LoadIndex))) {}
+
+  bool
+  fuzzyFind(const FuzzyFindRequest &Req,
+            llvm::function_ref<void(const Symbol &)> Callback) const override {
+    if (!index())
+      return false; // More
+    return index()->fuzzyFind(Req, Callback);
   }
-  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());
+  void
+  lookup(const LookupRequest &Req,
+         llvm::function_ref<void(const Symbol &)> Callback) const override {
+    if (!index())
+      return;
+    return index()->lookup(Req, Callback);
+  }
+
+  void findOccurrences(const OccurrencesRequest &Req,
+                       llvm::function_ref<void(const SymbolOccurrence &)>
+                           Callback) const override {
+    if (!index())
+      return;
+    return index()->findOccurrences(Req, Callback);
+  }
+
+  size_t estimateMemoryUsage() const override { return 0; }
+
+private:
+  const SymbolIndex *index() const {
+    if (Index)
+      return Index.get();
+    if (AsyncLoad.wait_for(std::chrono::seconds(0)) !=
+        std::future_status::ready)
+      return nullptr;
+    Index = AsyncLoad.get();
+    return Index.get();
+  }
+  mutable std::unique_ptr<SymbolIndex> Index;
+  mutable std::future<std::unique_ptr<SymbolIndex>> AsyncLoad;
+};
+// Asynchronously 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<SymbolIndex> buildStaticIndex(llvm::StringRef YamlSymbolFile) {
+  return llvm::make_unique<AsyncLoadIndex>(
+      [YamlSymbolFile]() -> std::unique_ptr<SymbolIndex> {
+        trace::Span Tracer("Build static index");
+
+        auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
+        if (!Buffer) {
+          llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
+          return nullptr;
+        }
+
+        SymbolSlab::Builder SymsBuilder;
+        {
+          trace::Span Tracer("YAML to symbols");
+          auto Slab = symbolsFromYAML(Buffer.get()->getBuffer());
+          for (auto Sym : Slab)
+            SymsBuilder.insert(Sym);
+        }
+
+        trace::Span Build("Build index");
+        return UseDex ? dex::DexIndex::build(std::move(SymsBuilder).build())
+                      : MemIndex::build(std::move(SymsBuilder).build());
+      });
 }
 
 } // namespace
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to