diff --git a/clang-tools-extra/clangd/index/ProjectAware.h b/clang-tools-extra/clangd/index/ProjectAware.h --- a/clang-tools-extra/clangd/index/ProjectAware.h +++ b/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( - 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 createProjectAwareIndex(IndexFactory); +std::unique_ptr createProjectAwareIndex(IndexFactory, bool Sync); } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/index/ProjectAware.cpp b/clang-tools-extra/clangd/index/ProjectAware.cpp --- a/clang-tools-extra/clangd/index/ProjectAware.cpp +++ b/clang-tools-extra/clangd/index/ProjectAware.cpp @@ -57,7 +57,10 @@ llvm::unique_function 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(); + } private: // Returns the index associated with current context, if any. @@ -68,7 +71,7 @@ mutable llvm::DenseMap> IndexForSpec; - mutable AsyncTaskRunner Tasks; + mutable std::unique_ptr Tasks; const IndexFactory Gen; }; @@ -131,14 +134,15 @@ std::lock_guard 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 createProjectAwareIndex(IndexFactory Gen) { +std::unique_ptr createProjectAwareIndex(IndexFactory Gen, + bool Sync) { assert(Gen); - return std::make_unique(std::move(Gen)); + return std::make_unique(std::move(Gen), Sync); } } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -537,7 +537,7 @@ std::unique_ptr 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); } @@ -806,7 +804,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( diff --git a/clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp b/clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/ProjectAwareIndexTests.cpp +++ b/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;