diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -57,11 +57,11 @@ Edits; } CompileFlags; - enum class BackgroundPolicy { Build, Skip }; + enum class BackgroundPolicy { Auto, Build, Skip }; /// Controls background-index behavior. struct { /// Whether this TU should be indexed. - BackgroundPolicy Background = BackgroundPolicy::Build; + BackgroundPolicy Background = BackgroundPolicy::Auto; /// Describes an external index configuration. At most one of Server or File /// will be set at a time. struct { diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp --- a/clang-tools-extra/clangd/ConfigCompile.cpp +++ b/clang-tools-extra/clangd/ConfigCompile.cpp @@ -302,10 +302,10 @@ else C.Index.External.Server.emplace(std::move(**External.Server)); - // Disable background indexing for the files under the mountpoint. - // Note that this will overwrite statements in any previous fragments - // (including the current one). - C.Index.Background = Config::BackgroundPolicy::Skip; + // Disable background indexing for the files under the mountpoint. If + // user didn't take any explicit actions. + if (C.Index.Background == Config::BackgroundPolicy::Auto) + C.Index.Background = Config::BackgroundPolicy::Skip; }); } diff --git a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp --- a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp +++ b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp @@ -117,7 +117,7 @@ Frag = {}; Frag.Index.Background.emplace("Foo"); EXPECT_TRUE(compileAndApply()); - EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Build) + EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Auto) << "by default"; EXPECT_THAT( Diags.Diagnostics, @@ -205,13 +205,27 @@ DiagKind(llvm::SourceMgr::DK_Error)))); } -TEST_F(ConfigCompileTests, ExternalBlockDisablesBackgroundIndex) { +TEST_F(ConfigCompileTests, ExternalBlockAndBackgroundIndex) { Parm.Path = "/foo/bar/baz.h"; - Frag.Index.Background.emplace("Build"); - Fragment::IndexBlock::ExternalBlock External; - External.File.emplace("/foo"); - External.MountPoint.emplace("/foo/bar"); - Frag.Index.External = std::move(External); + auto CreateFragWithBackground = [](llvm::StringRef Background) { + Fragment::IndexBlock::ExternalBlock External; + External.File.emplace("/foo"); + External.MountPoint.emplace("/foo/bar"); + + Fragment Frag; + Frag.Index.External = std::move(External); + if (!Background.empty()) + Frag.Index.Background.emplace(Background.str()); + return Frag; + }; + + // Preserves when explicitly mentioned. + Frag = CreateFragWithBackground("Build"); + compileAndApply(); + EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Build); + + // Overwrites to Skip otherwise. + Frag = CreateFragWithBackground(""); compileAndApply(); EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Skip); }