kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: usaxena95, arphaman.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Depends on D98029 <https://reviews.llvm.org/D98029>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98165

Files:
  clang-tools-extra/clangd/index/ProjectAware.cpp
  clang-tools-extra/clangd/index/ProjectAware.h
  clang-tools-extra/clangd/tool/ClangdMain.cpp
  clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp

Index: clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp
@@ -33,11 +33,11 @@
 }
 
 TEST(ProjectAware, Test) {
-  IndexFactory Gen = [](const Config::ExternalIndexSpec &, AsyncTaskRunner &) {
+  IndexFactory Gen = [](const Config::ExternalIndexSpec &, AsyncTaskRunner *) {
     return createIndex();
   };
 
-  auto Idx = createProjectAwareIndex(std::move(Gen));
+  auto Idx = createProjectAwareIndex(std::move(Gen), true);
   FuzzyFindRequest Req;
   Req.Query = "1";
   Req.AnyScope = true;
@@ -54,12 +54,12 @@
 
 TEST(ProjectAware, CreatedOnce) {
   unsigned InvocationCount = 0;
-  IndexFactory Gen = [&](const Config::ExternalIndexSpec &, AsyncTaskRunner &) {
+  IndexFactory Gen = [&](const Config::ExternalIndexSpec &, AsyncTaskRunner *) {
     ++InvocationCount;
     return createIndex();
   };
 
-  auto Idx = createProjectAwareIndex(std::move(Gen));
+  auto Idx = createProjectAwareIndex(std::move(Gen), true);
   // No invocation at start.
   EXPECT_EQ(InvocationCount, 0U);
   FuzzyFindRequest Req;
Index: clang-tools-extra/clangd/tool/ClangdMain.cpp
===================================================================
--- clang-tools-extra/clangd/tool/ClangdMain.cpp
+++ clang-tools-extra/clangd/tool/ClangdMain.cpp
@@ -537,7 +537,7 @@
 
 std::unique_ptr<SymbolIndex>
 loadExternalIndex(const Config::ExternalIndexSpec &External,
-                  AsyncTaskRunner &Tasks) {
+                  AsyncTaskRunner *Tasks) {
   switch (External.Kind) {
   case Config::ExternalIndexSpec::Server:
     log("Associating {0} with remote index at {1}.", External.MountPoint,
@@ -552,13 +552,11 @@
       if (auto Idx = loadIndex(File, /*UseDex=*/true))
         PlaceHolder->reset(std::move(Idx));
     };
-    // FIXME: The signature should contain a null-able TaskRunner istead, and
-    // the task should be scheduled accordingly.
-    if (Sync) {
-      IndexLoadTask();
+    if (Tasks) {
+      Tasks->runAsync("Load-index:" + External.Location,
+                      std::move(IndexLoadTask));
     } else {
-      Tasks.runAsync("Load-index:" + External.Location,
-                     std::move(IndexLoadTask));
+      IndexLoadTask();
     }
     return std::move(NewIndex);
   }
@@ -804,7 +802,7 @@
   }
 #endif
   Opts.BackgroundIndex = EnableBackgroundIndex;
-  auto PAI = createProjectAwareIndex(loadExternalIndex);
+  auto PAI = createProjectAwareIndex(loadExternalIndex, Sync);
   if (StaticIdx) {
     IdxStack.emplace_back(std::move(StaticIdx));
     IdxStack.emplace_back(
Index: clang-tools-extra/clangd/index/ProjectAware.h
===================================================================
--- clang-tools-extra/clangd/index/ProjectAware.h
+++ clang-tools-extra/clangd/index/ProjectAware.h
@@ -19,15 +19,16 @@
 
 /// A functor to create an index for an external index specification. Functor
 /// should perform any high latency operation in a separate thread through
-/// AsyncTaskRunner.
+/// AsyncTaskRunner, if set.
 using IndexFactory = std::function<std::unique_ptr<SymbolIndex>(
-    const Config::ExternalIndexSpec &, AsyncTaskRunner &)>;
+    const Config::ExternalIndexSpec &, AsyncTaskRunner *)>;
 
 /// Returns an index that answers queries using external indices. IndexFactory
-/// specifies how to generate an index from an external source.
+/// specifies how to generate an index from an external source. If \p Sync is
+/// set, index won't own any asnyc task runner.
 /// IndexFactory must be injected because this code cannot depend on the remote
 /// index client.
-std::unique_ptr<SymbolIndex> createProjectAwareIndex(IndexFactory);
+std::unique_ptr<SymbolIndex> createProjectAwareIndex(IndexFactory, bool Sync);
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/index/ProjectAware.cpp
===================================================================
--- clang-tools-extra/clangd/index/ProjectAware.cpp
+++ clang-tools-extra/clangd/index/ProjectAware.cpp
@@ -57,7 +57,10 @@
   llvm::unique_function<IndexContents(llvm::StringRef) const>
   indexedFiles() const override;
 
-  ProjectAwareIndex(IndexFactory Gen) : Gen(std::move(Gen)) {}
+  ProjectAwareIndex(IndexFactory Gen, bool Sync) : Gen(std::move(Gen)) {
+    if (!Sync)
+      Tasks = std::make_unique<AsyncTaskRunner>();
+  }
 
 private:
   // Returns the index associated with current context, if any.
@@ -68,7 +71,7 @@
   mutable llvm::DenseMap<Config::ExternalIndexSpec,
                          std::unique_ptr<SymbolIndex>>
       IndexForSpec;
-  mutable AsyncTaskRunner Tasks;
+  mutable std::unique_ptr<AsyncTaskRunner> Tasks;
 
   const IndexFactory Gen;
 };
@@ -131,14 +134,15 @@
   std::lock_guard<std::mutex> Lock(Mu);
   auto Entry = IndexForSpec.try_emplace(External, nullptr);
   if (Entry.second)
-    Entry.first->getSecond() = Gen(External, Tasks);
+    Entry.first->getSecond() = Gen(External, Tasks.get());
   return Entry.first->second.get();
 }
 } // namespace
 
-std::unique_ptr<SymbolIndex> createProjectAwareIndex(IndexFactory Gen) {
+std::unique_ptr<SymbolIndex> createProjectAwareIndex(IndexFactory Gen,
+                                                     bool Sync) {
   assert(Gen);
-  return std::make_unique<ProjectAwareIndex>(std::move(Gen));
+  return std::make_unique<ProjectAwareIndex>(std::move(Gen), Sync);
 }
 } // namespace clangd
 } // namespace clang
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to