[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: > > What is the relationship between this patch, and clangd 17's ["missing > > include" > > warning](https://clangd.llvm.org/guides/include-cleaner#missing-include-warning)? > > Does the quick-fix for the "missing include" warning also respect these > > config options? > > Same question from me -- this is the main reason I personally care about this > PR :) We have directories in our project tree that we'd like to be included > as `#include ` (as those would be installed system-wide in an > open-source version) and others that we'd like to be included as `#include > "dir/foo.h"` (internal headers that are in the same subtree as the C++ > source) and I'd really like clangd in vscode to suggest fixing the missing > include warnings appropriately. Thanks! As far as I can tell, clangd uses the correct header style for inserted includes regardles of how those includes are created (via auto-include on code completion or fix suggestions). https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
https://github.com/kleinesfilmroellchen updated https://github.com/llvm/llvm-project/pull/67749 From 8a2b22e69f82e3f4caa271b57b998d1c03b21d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Thu, 4 Jan 2024 11:53:23 +0100 Subject: [PATCH] [clangd] Allow specifying what headers are always included via "" or <> Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. --- clang-tools-extra/clangd/CodeComplete.cpp | 15 -- clang-tools-extra/clangd/Config.h | 4 ++ clang-tools-extra/clangd/ConfigCompile.cpp| 49 +++ clang-tools-extra/clangd/ConfigFragment.h | 13 + clang-tools-extra/clangd/ConfigYAML.cpp | 8 +++ clang-tools-extra/clangd/Headers.cpp | 34 +++-- clang-tools-extra/clangd/Headers.h| 11 - clang-tools-extra/clangd/IncludeCleaner.h | 1 - clang-tools-extra/clangd/ParsedAST.cpp| 3 +- .../clangd/unittests/ConfigCompileTests.cpp | 36 ++ .../clangd/unittests/ConfigYAMLTests.cpp | 8 ++- .../clangd/unittests/HeadersTests.cpp | 29 +-- 12 files changed, 193 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 0e5f08cec440ce..419bd659898506 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -21,6 +21,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" #include "Compiler.h" +#include "Config.h" #include "ExpectedTypes.h" #include "Feature.h" #include "FileDistance.h" @@ -786,8 +787,8 @@ SpecifiedScope getQueryScopes(CodeCompletionContext &CCContext, llvm::StringRef SpelledSpecifier = Lexer::getSourceText( CharSourceRange::getCharRange(SemaSpecifier->getRange()), CCSema.SourceMgr, clang::LangOptions()); - if (SpelledSpecifier.consume_front("::")) - Scopes.QueryScopes = {""}; + if (SpelledSpecifier.consume_front("::")) +Scopes.QueryScopes = {""}; Scopes.UnresolvedQualifier = std::string(SpelledSpecifier); // Sema excludes the trailing "::". if (!Scopes.UnresolvedQualifier->empty()) @@ -1580,7 +1581,7 @@ class CodeCompleteFlow { CompletionPrefix HeuristicPrefix; std::optional Filter; // Initialized once Sema runs. Range ReplacedRange; - std::vector QueryScopes; // Initialized once Sema runs. + std::vector QueryScopes; // Initialized once Sema runs. std::vector AccessibleScopes; // Initialized once Sema runs. // Initialized once QueryScopes is initialized, if there are scopes. std::optional ScopeProximity; @@ -1639,7 +1640,9 @@ class CodeCompleteFlow { Inserter.emplace( SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style, SemaCCInput.ParseInput.CompileCommand.Directory, - &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); + &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo(), + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); for (const auto &Inc : Includes.MainFileIncludes) Inserter->addExisting(Inc); @@ -1722,7 +1725,9 @@ class CodeCompleteFlow { auto Style = getFormatStyleForFile(FileName, Content, TFS); // This will only insert verbatim headers. Inserter.emplace(FileName, Content, Style, - /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); + /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr, + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); auto Identifiers = collectIdentifiers(Content, Style); std::vector IdentifierResults; diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h index 4371c80a6c5877..12ed1420c93e19 100644 --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -123,6 +123,10 @@ struct Config { // declarations, always spell out the whole name (with or without leading // ::). All nested namespaces are affected as well. std::vector FullyQualifiedNamespaces; + +// List of regexes for inserting certain headers with <> or "". +std::vector> QuotedHeaders; +std::vector> AngledHeaders; } Style; /// Configures code completion feature. diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/cl
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
@@ -296,6 +296,19 @@ struct Fragment { // ::). All nested namespaces are affected as well. // Affects availability of the AddUsing tweak. std::vector> FullyQualifiedNamespaces; + +/// List of regexes for headers that should always be included with a +/// ""-style include. By default, and in case of a conflict with +/// AngledHeaders (i.e. a header matches a regex in both QuotedHeaders and +/// AngledHeaders), system headers use <> and non-system headers use "". +/// These can match any suffix of the header file in question. kleinesfilmroellchen wrote: We're matching against the spelling; at least in SerenityOS this is a necessary distinction: Naming a same-directory header via `"Widget.h"` will result in quotes, but naming the same header via an "absolute" path like `` should get you an angled include. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
@@ -211,10 +214,12 @@ class IncludeInserter { // include path of non-verbatim header will not be shortened. IncludeInserter(StringRef FileName, StringRef Code, const format::FormatStyle &Style, StringRef BuildDir, - HeaderSearch *HeaderSearchInfo) + HeaderSearch *HeaderSearchInfo, HeaderFilter QuotedHeaders, kleinesfilmroellchen wrote: I'm not sure how much difference that makes. The ArrayRef usage is based on other code I've seen around, and while the config lives for a while, the IncludeInserter is only briefly used to add an include. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: > It's sad that there's not a single codepath that would handle all these, but > maybe the perfect is the enemy of the good here. The code duplication was my reason for looking at the common path. If you're fine with duplicating the code I will go ahead and do that. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
https://github.com/kleinesfilmroellchen updated https://github.com/llvm/llvm-project/pull/67749 From 67971ca27ef5e2767aba5cfe2cec1021c4de5ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Thu, 25 Jan 2024 18:04:35 +0100 Subject: [PATCH] [clangd] Allow specifying what headers are always included via "" or <> Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. --- clang-tools-extra/clangd/CodeComplete.cpp | 15 +++-- clang-tools-extra/clangd/Config.h | 4 ++ clang-tools-extra/clangd/ConfigCompile.cpp| 49 clang-tools-extra/clangd/ConfigFragment.h | 17 ++ clang-tools-extra/clangd/ConfigYAML.cpp | 8 +++ clang-tools-extra/clangd/Headers.cpp | 34 +-- clang-tools-extra/clangd/Headers.h| 10 +++- clang-tools-extra/clangd/IncludeCleaner.h | 1 - clang-tools-extra/clangd/ParsedAST.cpp| 3 +- .../clangd/unittests/CodeCompleteTests.cpp| 56 +++ .../clangd/unittests/ConfigCompileTests.cpp | 38 + .../clangd/unittests/ConfigYAMLTests.cpp | 8 ++- .../clangd/unittests/HeadersTests.cpp | 29 +- 13 files changed, 244 insertions(+), 28 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 0e5f08cec440ce..419bd659898506 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -21,6 +21,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" #include "Compiler.h" +#include "Config.h" #include "ExpectedTypes.h" #include "Feature.h" #include "FileDistance.h" @@ -786,8 +787,8 @@ SpecifiedScope getQueryScopes(CodeCompletionContext &CCContext, llvm::StringRef SpelledSpecifier = Lexer::getSourceText( CharSourceRange::getCharRange(SemaSpecifier->getRange()), CCSema.SourceMgr, clang::LangOptions()); - if (SpelledSpecifier.consume_front("::")) - Scopes.QueryScopes = {""}; + if (SpelledSpecifier.consume_front("::")) +Scopes.QueryScopes = {""}; Scopes.UnresolvedQualifier = std::string(SpelledSpecifier); // Sema excludes the trailing "::". if (!Scopes.UnresolvedQualifier->empty()) @@ -1580,7 +1581,7 @@ class CodeCompleteFlow { CompletionPrefix HeuristicPrefix; std::optional Filter; // Initialized once Sema runs. Range ReplacedRange; - std::vector QueryScopes; // Initialized once Sema runs. + std::vector QueryScopes; // Initialized once Sema runs. std::vector AccessibleScopes; // Initialized once Sema runs. // Initialized once QueryScopes is initialized, if there are scopes. std::optional ScopeProximity; @@ -1639,7 +1640,9 @@ class CodeCompleteFlow { Inserter.emplace( SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style, SemaCCInput.ParseInput.CompileCommand.Directory, - &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); + &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo(), + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); for (const auto &Inc : Includes.MainFileIncludes) Inserter->addExisting(Inc); @@ -1722,7 +1725,9 @@ class CodeCompleteFlow { auto Style = getFormatStyleForFile(FileName, Content, TFS); // This will only insert verbatim headers. Inserter.emplace(FileName, Content, Style, - /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); + /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr, + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); auto Identifiers = collectIdentifiers(Content, Style); std::vector IdentifierResults; diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h index 4371c80a6c5877..3559591fce00cc 100644 --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -123,6 +123,10 @@ struct Config { // declarations, always spell out the whole name (with or without leading // ::). All nested namespaces are affected as well. std::vector FullyQualifiedNamespaces; + +// List of matcher functions for inserting certain headers with <> or "". +std::vector> QuotedHeaders; +std::vector> AngledHeaders; } Style; /// Configures code comple
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: > It's sad that there's not a single codepath that would handle all these, but > maybe the perfect is the enemy of the good here. The code duplication was my reason for looking at the common path. If you're fine with duplicating the code I will go ahead and do that. I have updated the commit with the review comments and added end-to-end tests. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: > Sorry, I'm still struggling with Github reviews compared to Phabricator... > how do I get to an interdiff showing the latest version of the patch compared > to the version I reviewed? I'm fairly sure that GitHub has no built-in feature for this; I've never used it for my own reviews but it's unfortunate that that is missing of course. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: After some discussion, I would like to land the feature "partially" as-is without support for "include cleaner" header insertion. (Case 3 in @HighCommander4's elaboration above.) This is motivated by two factors: - Having looked at a way of supporting all three insertion cases, it seems like some significant refactoring is required. The common code between the three cases is either upstream in Clang (`HeaderSearch`), which would probably be a *very* leaky and unclean abstraction. Or downstream in replacement generation (`HeaderIncludes`), where the relevant information to make the decision is no longer available. I'm honestly not excited about performing such a refactor, as I am not particularly familiar with the complex code base. - The other cases seem to be more common in practice, and have already been very useful in my dogfooding with SerenityOS. Within our project this patch would already be appreciated as a great improvement over the status quo. If maintainers agree, I'd like to have this merged before 18. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
https://github.com/kleinesfilmroellchen created https://github.com/llvm/llvm-project/pull/67749 Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. CC @HighCommander4 @adkaster. I have no merge rights so someone else needs to commit this. From d2bb8241dfd396c1c77413918f1983b1afe16517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 29 Sep 2023 00:22:12 +0200 Subject: [PATCH] [clangd] Allow specifying what headers are always included via "" or <> Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. --- clang-tools-extra/clangd/CodeComplete.cpp | 15 +++-- clang-tools-extra/clangd/Config.h | 4 ++ clang-tools-extra/clangd/ConfigCompile.cpp| 66 +++ clang-tools-extra/clangd/ConfigFragment.h | 13 clang-tools-extra/clangd/ConfigYAML.cpp | 8 +++ clang-tools-extra/clangd/Headers.cpp | 34 -- clang-tools-extra/clangd/Headers.h| 11 +++- clang-tools-extra/clangd/IncludeCleaner.h | 1 - clang-tools-extra/clangd/ParsedAST.cpp| 3 +- .../clangd/unittests/ConfigCompileTests.cpp | 34 ++ .../clangd/unittests/ConfigYAMLTests.cpp | 8 ++- .../clangd/unittests/HeadersTests.cpp | 29 +++- 12 files changed, 208 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 70c4d7e65b76db3..388e032e160474a 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -21,6 +21,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" #include "Compiler.h" +#include "Config.h" #include "ExpectedTypes.h" #include "Feature.h" #include "FileDistance.h" @@ -786,8 +787,8 @@ SpecifiedScope getQueryScopes(CodeCompletionContext &CCContext, llvm::StringRef SpelledSpecifier = Lexer::getSourceText( CharSourceRange::getCharRange(SemaSpecifier->getRange()), CCSema.SourceMgr, clang::LangOptions()); - if (SpelledSpecifier.consume_front("::")) - Scopes.QueryScopes = {""}; + if (SpelledSpecifier.consume_front("::")) +Scopes.QueryScopes = {""}; Scopes.UnresolvedQualifier = std::string(SpelledSpecifier); // Sema excludes the trailing "::". if (!Scopes.UnresolvedQualifier->empty()) @@ -1540,7 +1541,7 @@ class CodeCompleteFlow { CompletionPrefix HeuristicPrefix; std::optional Filter; // Initialized once Sema runs. Range ReplacedRange; - std::vector QueryScopes; // Initialized once Sema runs. + std::vector QueryScopes; // Initialized once Sema runs. std::vector AccessibleScopes; // Initialized once Sema runs. // Initialized once QueryScopes is initialized, if there are scopes. std::optional ScopeProximity; @@ -1599,7 +1600,9 @@ class CodeCompleteFlow { Inserter.emplace( SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style, SemaCCInput.ParseInput.CompileCommand.Directory, - &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); + &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo(), + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); for (const auto &Inc : Includes.MainFileIncludes) Inserter->addExisting(Inc); @@ -1682,7 +1685,9 @@ class CodeCompleteFlow { auto Style = getFormatStyleForFile(FileName, Content, TFS); // This will only insert verbatim headers. Inserter.emplace(FileName, Content, Style, - /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); + /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr, + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); auto Identifiers = collectIdentifiers(Content, Styl
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: I investigated the test failures some more in the context of where the bug affects real uses and I'm unsure as to what's going on. ```yaml Style: QuotedHeaders: ["blahblahblah"] AngledHeaders: ["AK/.*", "Userland/.*", "Kernel/.*", "Applications/.*", "Lib.*/.*"] ``` will match all regexes correctly. ```yaml Style: AngledHeaders: ["AK/.*", "Userland/.*", "Kernel/.*", "Applications/.*", "Lib.*/.*"] ``` will make AngledHeaders completely disappear from the parsed and compiled config; apparently the dictionary key doesn't even exist anymore. Exactly this behavior makes a test fail on CI. ```yaml Style: QuotedHeaders: ["blahblahblah"] ``` will keep QuotedHeaders in the parsed and compiled config and works as expected. There is no apparent difference in how these two keys are handled, I have cross-checked my config parser and compiler code with existing code countless times. Really no clue what's happening here other than QuotedHeaders apparently needs to exist or AngledHeaders vanishes. CC @HighCommander4 https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
https://github.com/kleinesfilmroellchen updated https://github.com/llvm/llvm-project/pull/67749 From 167048c7025a516a5e57009947333c3980017107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 1 Oct 2023 14:48:57 +0200 Subject: [PATCH] [clangd] Allow specifying what headers are always included via "" or <> Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. --- clang-tools-extra/clangd/CodeComplete.cpp | 15 -- clang-tools-extra/clangd/Config.h | 4 ++ clang-tools-extra/clangd/ConfigCompile.cpp| 48 +++ clang-tools-extra/clangd/ConfigFragment.h | 13 + clang-tools-extra/clangd/ConfigYAML.cpp | 8 clang-tools-extra/clangd/Headers.cpp | 34 +++-- clang-tools-extra/clangd/Headers.h| 11 - clang-tools-extra/clangd/IncludeCleaner.h | 1 - clang-tools-extra/clangd/ParsedAST.cpp| 3 +- .../clangd/unittests/ConfigCompileTests.cpp | 36 ++ .../clangd/unittests/ConfigYAMLTests.cpp | 8 +++- .../clangd/unittests/HeadersTests.cpp | 29 +-- 12 files changed, 192 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 70c4d7e65b76db3..388e032e160474a 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -21,6 +21,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" #include "Compiler.h" +#include "Config.h" #include "ExpectedTypes.h" #include "Feature.h" #include "FileDistance.h" @@ -786,8 +787,8 @@ SpecifiedScope getQueryScopes(CodeCompletionContext &CCContext, llvm::StringRef SpelledSpecifier = Lexer::getSourceText( CharSourceRange::getCharRange(SemaSpecifier->getRange()), CCSema.SourceMgr, clang::LangOptions()); - if (SpelledSpecifier.consume_front("::")) - Scopes.QueryScopes = {""}; + if (SpelledSpecifier.consume_front("::")) +Scopes.QueryScopes = {""}; Scopes.UnresolvedQualifier = std::string(SpelledSpecifier); // Sema excludes the trailing "::". if (!Scopes.UnresolvedQualifier->empty()) @@ -1540,7 +1541,7 @@ class CodeCompleteFlow { CompletionPrefix HeuristicPrefix; std::optional Filter; // Initialized once Sema runs. Range ReplacedRange; - std::vector QueryScopes; // Initialized once Sema runs. + std::vector QueryScopes; // Initialized once Sema runs. std::vector AccessibleScopes; // Initialized once Sema runs. // Initialized once QueryScopes is initialized, if there are scopes. std::optional ScopeProximity; @@ -1599,7 +1600,9 @@ class CodeCompleteFlow { Inserter.emplace( SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style, SemaCCInput.ParseInput.CompileCommand.Directory, - &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); + &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo(), + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); for (const auto &Inc : Includes.MainFileIncludes) Inserter->addExisting(Inc); @@ -1682,7 +1685,9 @@ class CodeCompleteFlow { auto Style = getFormatStyleForFile(FileName, Content, TFS); // This will only insert verbatim headers. Inserter.emplace(FileName, Content, Style, - /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); + /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr, + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); auto Identifiers = collectIdentifiers(Content, Style); std::vector IdentifierResults; diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h index daae8d1c0c833eb..5d70ed3376715e5 100644 --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -121,6 +121,10 @@ struct Config { // declarations, always spell out the whole name (with or without leading // ::). All nested namespaces are affected as well. std::vector FullyQualifiedNamespaces; + +// List of regexes for inserting certain headers with <> or "". +std::vector> QuotedHeaders; +std::vector> AngledHeaders; } Style; /// Configures code completion feature. diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: Thanks for the finding @zyn0217, refactored the config compiler code a little. Tests should pass now and I have re-tested this on SerenityOS and other small project where it works as expected in practice. https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
https://github.com/kleinesfilmroellchen updated https://github.com/llvm/llvm-project/pull/67749 From 2e3f397ffc8967fdc13ced1ec0c4a7dc55a2400d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 2 Oct 2023 17:51:16 +0200 Subject: [PATCH] [clangd] Allow specifying what headers are always included via "" or <> Projects can now add config fragments like this to their .clangd: ```yaml Style: QuotedHeaders: "src/.*" AngledHeaders: ["path/sdk/.*", "third-party/.*"] ``` to force headers inserted via the --header-insertion=iwyu mode matching at least one of the regexes to have <> (AngledHeaders) or "" (QuotedHeaders) around them, respectively. For other headers (and in conflicting cases where both styles have a matching regex), the current system header detection remains. Ref https://github.com/clangd/clangd/issues/1247 Based on https://reviews.llvm.org/D145843 This solution does not affect other clang tools like clang-format. --- clang-tools-extra/clangd/CodeComplete.cpp | 15 -- clang-tools-extra/clangd/Config.h | 4 ++ clang-tools-extra/clangd/ConfigCompile.cpp| 50 +++ clang-tools-extra/clangd/ConfigFragment.h | 13 + clang-tools-extra/clangd/ConfigYAML.cpp | 8 +++ clang-tools-extra/clangd/Headers.cpp | 34 +++-- clang-tools-extra/clangd/Headers.h| 11 +++- clang-tools-extra/clangd/IncludeCleaner.h | 1 - clang-tools-extra/clangd/ParsedAST.cpp| 3 +- .../clangd/unittests/ConfigCompileTests.cpp | 36 + .../clangd/unittests/ConfigYAMLTests.cpp | 8 ++- .../clangd/unittests/HeadersTests.cpp | 29 +-- 12 files changed, 194 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 70c4d7e65b76db3..388e032e160474a 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -21,6 +21,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" #include "Compiler.h" +#include "Config.h" #include "ExpectedTypes.h" #include "Feature.h" #include "FileDistance.h" @@ -786,8 +787,8 @@ SpecifiedScope getQueryScopes(CodeCompletionContext &CCContext, llvm::StringRef SpelledSpecifier = Lexer::getSourceText( CharSourceRange::getCharRange(SemaSpecifier->getRange()), CCSema.SourceMgr, clang::LangOptions()); - if (SpelledSpecifier.consume_front("::")) - Scopes.QueryScopes = {""}; + if (SpelledSpecifier.consume_front("::")) +Scopes.QueryScopes = {""}; Scopes.UnresolvedQualifier = std::string(SpelledSpecifier); // Sema excludes the trailing "::". if (!Scopes.UnresolvedQualifier->empty()) @@ -1540,7 +1541,7 @@ class CodeCompleteFlow { CompletionPrefix HeuristicPrefix; std::optional Filter; // Initialized once Sema runs. Range ReplacedRange; - std::vector QueryScopes; // Initialized once Sema runs. + std::vector QueryScopes; // Initialized once Sema runs. std::vector AccessibleScopes; // Initialized once Sema runs. // Initialized once QueryScopes is initialized, if there are scopes. std::optional ScopeProximity; @@ -1599,7 +1600,9 @@ class CodeCompleteFlow { Inserter.emplace( SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style, SemaCCInput.ParseInput.CompileCommand.Directory, - &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo()); + &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo(), + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); for (const auto &Inc : Includes.MainFileIncludes) Inserter->addExisting(Inc); @@ -1682,7 +1685,9 @@ class CodeCompleteFlow { auto Style = getFormatStyleForFile(FileName, Content, TFS); // This will only insert verbatim headers. Inserter.emplace(FileName, Content, Style, - /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); + /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr, + Config::current().Style.QuotedHeaders, + Config::current().Style.AngledHeaders); auto Identifiers = collectIdentifiers(Content, Style); std::vector IdentifierResults; diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h index daae8d1c0c833eb..5d70ed3376715e5 100644 --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -121,6 +121,10 @@ struct Config { // declarations, always spell out the whole name (with or without leading // ::). All nested namespaces are affected as well. std::vector FullyQualifiedNamespaces; + +// List of regexes for inserting certain headers with <> or "". +std::vector> QuotedHeaders; +std::vector> AngledHeaders; } Style; /// Configures code completion feature. diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
@@ -483,6 +483,56 @@ struct FragmentCompiler { FullyQualifiedNamespaces.begin(), FullyQualifiedNamespaces.end()); }); } +auto QuotedFilter = compileHeaderRegexes(F.QuotedHeaders); +if (QuotedFilter.has_value()) { + Out.Apply.push_back( + [QuotedFilter = *QuotedFilter](const Params &, Config &C) { +C.Style.QuotedHeaders.emplace_back(QuotedFilter); + }); +} +auto AngledFilter = compileHeaderRegexes(F.AngledHeaders); +if (AngledFilter.has_value()) { + Out.Apply.push_back( + [AngledFilter = *AngledFilter](const Params &, Config &C) { +C.Style.AngledHeaders.emplace_back(AngledFilter); + }); +} + } + + auto compileHeaderRegexes(llvm::ArrayRef> HeaderPatterns) + -> std::optional> { +// TODO: Share this code with Diagnostics.Includes.IgnoreHeader +#ifdef CLANGD_PATH_CASE_INSENSITIVE +static llvm::Regex::RegexFlags Flags = llvm::Regex::IgnoreCase; +#else +static llvm::Regex::RegexFlags Flags = llvm::Regex::NoFlags; +#endif +auto Filters = std::make_shared>(); +for (auto &HeaderPattern : HeaderPatterns) { + elog("found angle pattern {0}", *HeaderPattern); kleinesfilmroellchen wrote: Oops, that's not supposed to be there anymore 😅 https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Allow specifying what headers are always included via "" or <> (PR #67749)
kleinesfilmroellchen wrote: I don't have any energy and the massive reachitecting requested would take me tons of time. Please adopt this. Serenity folks are also waiting fwiw https://github.com/llvm/llvm-project/pull/67749 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits