https://github.com/MythreyaK updated https://github.com/llvm/llvm-project/pull/128503
>From 8a0714cc760d5e14de6f3b34ec4058c9c13b8613 Mon Sep 17 00:00:00 2001 From: Mythreya <g...@mythreya.dev> Date: Mon, 24 Feb 2025 04:34:38 -0800 Subject: [PATCH] [clangd] Add `HeaderInsertion` yaml config option This is the yaml config equivalent of `--header-insertion` CLI option --- clang-tools-extra/clangd/ClangdServer.cpp | 1 + clang-tools-extra/clangd/CodeComplete.cpp | 3 ++- clang-tools-extra/clangd/CodeComplete.h | 6 ++---- clang-tools-extra/clangd/Config.h | 9 +++++++++ clang-tools-extra/clangd/ConfigCompile.cpp | 11 +++++++++++ clang-tools-extra/clangd/ConfigFragment.h | 8 ++++++++ clang-tools-extra/clangd/ConfigYAML.cpp | 4 ++++ clang-tools-extra/clangd/tool/ClangdMain.cpp | 4 ++-- .../clangd/unittests/CodeCompleteTests.cpp | 3 ++- 9 files changed, 41 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index 52be15d3da936..9027ae855e409 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -455,6 +455,7 @@ void ClangdServer::codeComplete(PathRef File, Position Pos, CodeCompleteOpts.MainFileSignals = IP->Signals; CodeCompleteOpts.AllScopes = Config::current().Completion.AllScopes; CodeCompleteOpts.ArgumentLists = Config::current().Completion.ArgumentLists; + CodeCompleteOpts.InsertIncludes = Config::current().HeaderInsertion.Policy; // FIXME(ibiryukov): even if Preamble is non-null, we may want to check // both the old and the new version in case only one of them matches. CodeCompleteResult Result = clangd::codeComplete( diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index a8182ce98ebe0..5db8eeaee1027 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -294,7 +294,8 @@ struct CompletionCandidate { std::optional<llvm::StringRef> headerToInsertIfAllowed(const CodeCompleteOptions &Opts, CodeCompletionContext::Kind ContextKind) const { - if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert || + if (Opts.InsertIncludes == + CodeCompleteOptions::IncludeInsertion::NeverInsert || RankedIncludeHeaders.empty() || !contextAllowsHeaderInsertion(ContextKind)) return std::nullopt; diff --git a/clang-tools-extra/clangd/CodeComplete.h b/clang-tools-extra/clangd/CodeComplete.h index bb2ebd9478645..8df0637d8aae6 100644 --- a/clang-tools-extra/clangd/CodeComplete.h +++ b/clang-tools-extra/clangd/CodeComplete.h @@ -71,10 +71,8 @@ struct CodeCompleteOptions { /// Whether to present doc comments as plain-text or markdown. MarkupKind DocumentationFormat = MarkupKind::PlainText; - enum IncludeInsertion { - IWYU, - NeverInsert, - } InsertIncludes = IncludeInsertion::IWYU; + using IncludeInsertion = Config::HeaderInsertionPolicy; + Config::HeaderInsertionPolicy InsertIncludes = IncludeInsertion::IWYU; /// Whether include insertions for Objective-C code should use #import instead /// of #include. diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h index 586d031d58481..eb33f1b8c79ae 100644 --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -151,6 +151,15 @@ struct Config { ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders; } Completion; + enum class HeaderInsertionPolicy { + IWYU, // Include what you use + NeverInsert // Never insert headers as part of code completion + }; + + struct { + HeaderInsertionPolicy Policy = HeaderInsertionPolicy::IWYU; + } HeaderInsertion; + /// Configures hover feature. struct { /// Whether hover show a.k.a type. diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp index aa2561e081047..fb02b1baaebc4 100644 --- a/clang-tools-extra/clangd/ConfigCompile.cpp +++ b/clang-tools-extra/clangd/ConfigCompile.cpp @@ -685,6 +685,17 @@ struct FragmentCompiler { C.Completion.ArgumentLists = *Val; }); } + if (F.HeaderInsertion) { + if (auto Val = + compileEnum<Config::HeaderInsertionPolicy>("HeaderInsertion", + *F.HeaderInsertion) + .map("IWYU", Config::HeaderInsertionPolicy::IWYU) + .map("Never", Config::HeaderInsertionPolicy::NeverInsert) + .value()) + Out.Apply.push_back([Val](const Params &, Config &C) { + C.HeaderInsertion.Policy = *Val; + }); + } } void compile(Fragment::HoverBlock &&F) { diff --git a/clang-tools-extra/clangd/ConfigFragment.h b/clang-tools-extra/clangd/ConfigFragment.h index 9535b20253b13..c4c7515c5c25e 100644 --- a/clang-tools-extra/clangd/ConfigFragment.h +++ b/clang-tools-extra/clangd/ConfigFragment.h @@ -333,6 +333,14 @@ struct Fragment { /// Delimiters: empty pair of delimiters "()" or "<>" /// FullPlaceholders: full name of both type and parameter std::optional<Located<std::string>> ArgumentLists; + /// Add #include directives when accepting code completions. Config + /// equivalent of the CLI option '--header-insertion' + /// Valid values are enum Config::HeaderInsertionPolicy values: + /// "IWYU": Include what you use. Insert the owning header for top-level + /// symbols, unless the header is already directly included or the + /// symbol is forward-declared + /// "NeverInsert": Never insert headers + std::optional<Located<std::string>> HeaderInsertion; }; CompletionBlock Completion; diff --git a/clang-tools-extra/clangd/ConfigYAML.cpp b/clang-tools-extra/clangd/ConfigYAML.cpp index 95cc5c1f9f1cf..34fe61afddadf 100644 --- a/clang-tools-extra/clangd/ConfigYAML.cpp +++ b/clang-tools-extra/clangd/ConfigYAML.cpp @@ -241,6 +241,10 @@ class Parser { if (auto ArgumentLists = scalarValue(N, "ArgumentLists")) F.ArgumentLists = *ArgumentLists; }); + Dict.handle("HeaderInsertion", [&](Node &N) { + if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion")) + F.HeaderInsertion = *HeaderInsertion; + }); Dict.parse(N); } diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index 714891703b6f3..44fd1d450ec64 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -257,13 +257,13 @@ opt<CodeCompleteOptions::IncludeInsertion> HeaderInsertion{ desc("Add #include directives when accepting code completions"), init(CodeCompleteOptions().InsertIncludes), values( - clEnumValN(CodeCompleteOptions::IWYU, "iwyu", + clEnumValN(CodeCompleteOptions::IncludeInsertion::IWYU, "iwyu", "Include what you use. " "Insert the owning header for top-level symbols, unless the " "header is already directly included or the symbol is " "forward-declared"), clEnumValN( - CodeCompleteOptions::NeverInsert, "never", + CodeCompleteOptions::IncludeInsertion::NeverInsert, "never", "Never insert #include directives as part of code completion")), }; diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp index b12f8275b8a26..042ad73b8a280 100644 --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -882,7 +882,8 @@ TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) { ElementsAre(AllOf(named("X"), insertInclude("\"bar.h\"")))); // Can be disabled via option. CodeCompleteOptions NoInsertion; - NoInsertion.InsertIncludes = CodeCompleteOptions::NeverInsert; + NoInsertion.InsertIncludes = + CodeCompleteOptions::IncludeInsertion::NeverInsert; Results = completions(TU, Test.point(), {Sym}, NoInsertion); EXPECT_THAT(Results.Completions, ElementsAre(AllOf(named("X"), Not(insertInclude())))); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits