https://github.com/DynamicProdBreaker updated https://github.com/llvm/llvm-project/pull/208916
>From 5d844e7c71fbd9745e113824a8b6dd270e47ae0a Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Sat, 11 Jul 2026 22:53:27 +0800 Subject: [PATCH 01/10] Allow SortIncludes to override DisableFormat when both are explicitly set to true As https://github.com/llvm/llvm-project/issues/201422 mentions, after the commit that addressed https://github.com/llvm/llvm-project/issues/34447 it became impossible to only sort includes with clang format, as DisableFormat is now hardcoded into the include sorter to disable it when true. Prior discussion to the patch that changed this logic mentioned that there aren't many approaches that are acceptable to allowing only includes to be sorted again, but raised the suggestion that having DisableFormat turn off SortIncludes normally, but letting SortIncludes enable itself again if it is explicitly specified might be acceptable. In light of this, reimplement how DisableFormat disables include sorting by having the enabled flag for SortIncludes be disabled at the moment DisableFormat is set, to allow the actual SortIncludes setting to override it later, which implements the proposed behaviour of DisableFormat turning off SortIncludes when SortIncludes isn't requested, but allowing include sorting to happen when SortIncludes is set explicitly togehter with DisableFormat. --- clang/lib/Format/Format.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 037111d8e9e5d..0b33ca3f33a11 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1355,6 +1355,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); IO.mapOptional("DisableFormat", Style.DisableFormat); + if (Style.DisableFormat) + Style.SortIncludes.Enabled = false; IO.mapOptional("EmptyLineAfterAccessModifier", Style.EmptyLineAfterAccessModifier); IO.mapOptional("EmptyLineBeforeAccessModifier", @@ -4034,7 +4036,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName, unsigned *Cursor) { tooling::Replacements Replaces; - if (!Style.SortIncludes.Enabled || Style.DisableFormat) + if (!Style.SortIncludes.Enabled) return Replaces; if (isLikelyXml(Code)) return Replaces; >From 94920844b826fb4742d1d0ae9c3a5f71c7d39f5f Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Wed, 15 Jul 2026 16:14:14 +0800 Subject: [PATCH 02/10] Remove the implementation for disabling for now while I rework the whole thing --- clang/lib/Format/Format.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0b33ca3f33a11..1438aa3045cac 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1355,8 +1355,6 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); IO.mapOptional("DisableFormat", Style.DisableFormat); - if (Style.DisableFormat) - Style.SortIncludes.Enabled = false; IO.mapOptional("EmptyLineAfterAccessModifier", Style.EmptyLineAfterAccessModifier); IO.mapOptional("EmptyLineBeforeAccessModifier", >From 924355d4d0057c2eb63308729421cc9eef92ced7 Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 22:35:08 +0800 Subject: [PATCH 03/10] Refactor DisableFormat to use enum struct --- clang/include/clang/Format/Format.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 27b2d8f4a405b..9f28797014389 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2940,7 +2940,11 @@ struct FormatStyle { /// Disables formatting completely. /// \version 3.7 - bool DisableFormat; + enum struct DisableFormatOptions { + DF_Disabled, + DF_SortIncludesOnly, + DF_Enabled + } DisableFormat; /// Different styles for empty line after access modifiers. /// ``EmptyLineBeforeAccessModifier`` configuration handles the number of >From d34202cccede9d8e45c71cf801ab80c600b28059 Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:00:20 +0800 Subject: [PATCH 04/10] Refactor DisableFormat to use structured options --- clang/lib/Format/Format.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 1438aa3045cac..fe2435d035b05 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -838,6 +838,20 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ShortRecordStyle> { } }; +template <> struct MappingTraits<FormatStyle::DisableFormatOptions> { + static void enumInput(IO &IO, FormatStyle::DisableFormatOptions &Value) { + IO.enumCase(Value, "false", FormatStyle::DisableFormatOptions{/*DisableSortIncludes=*/false, + /*DisablePostPreprocessorFormatting=*/false}); + IO.enumCase(Value, "true", FormatStyle::DisableFormatOptions{/*DisableSortIncludes=*/true, + /*DisablePostPreprocessorFormatting=*/true}); + } + + static void mapping(IO &IO, FormatStyle::DisableFormatOptions &Value) { + IO.mapOptional("DisableIncludeSorting", Value.DisableSortIncludes); + IO.mapOptional("DisablePostPreprocessorFormatting", Value.DisablePostPreprocessorFormatting); + } +}; + template <> struct MappingTraits<FormatStyle::SortIncludesOptions> { static void enumInput(IO &IO, FormatStyle::SortIncludesOptions &Value) { IO.enumCase(Value, "Never", FormatStyle::SortIncludesOptions{}); @@ -1604,6 +1618,9 @@ template <> struct MappingTraits<FormatStyle> { } Style.SpacesInParens = FormatStyle::SIPO_Custom; } + + if (Style.DisableFormat.DisableIncludeSorting) + Style.SortIncludes.Enabled = false; } }; @@ -1920,7 +1937,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.ContinuationIndentWidth = 4; LLVMStyle.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment; LLVMStyle.DerivePointerAlignment = false; - LLVMStyle.DisableFormat = false; + LLVMStyle.DisableFormat = {/*DisableIncludeSorting=*/false, /*DisablePostPreprocessorFormatting=*/false}; LLVMStyle.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; LLVMStyle.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; LLVMStyle.EnumTrailingComma = FormatStyle::ETC_Leave; @@ -2395,7 +2412,7 @@ FormatStyle getClangFormatStyle() { FormatStyle getNoStyle() { FormatStyle NoStyle = getLLVMStyle(); - NoStyle.DisableFormat = true; + NoStyle.DisableFormat = {/*DisableIncludeSorting=*/true, /*DisablePostPreprocessorFormatting=*/true}; NoStyle.SortIncludes = {}; NoStyle.SortUsingDeclarations = FormatStyle::SUD_Never; return NoStyle; @@ -4227,7 +4244,7 @@ reformat(const FormatStyle &Style, StringRef Code, if (Expanded.BraceWrapping.AfterEnum) Expanded.AllowShortEnumsOnASingleLine = false; - if (Expanded.DisableFormat) + if (Expanded.DisableFormat.DisablePostPreprocessorFormatting) return {tooling::Replacements(), 0}; if (isLikelyXml(Code)) return {tooling::Replacements(), 0}; >From 75dc5e92efb36553f48c275c80e4d9938a29d077 Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:01:36 +0800 Subject: [PATCH 05/10] Rename DisableIncludeSorting to DisableSortIncludes --- clang/lib/Format/Format.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index fe2435d035b05..8987214bc1dd5 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1619,7 +1619,7 @@ template <> struct MappingTraits<FormatStyle> { Style.SpacesInParens = FormatStyle::SIPO_Custom; } - if (Style.DisableFormat.DisableIncludeSorting) + if (Style.DisableFormat.DisableSortIncludes) Style.SortIncludes.Enabled = false; } }; @@ -1937,7 +1937,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.ContinuationIndentWidth = 4; LLVMStyle.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment; LLVMStyle.DerivePointerAlignment = false; - LLVMStyle.DisableFormat = {/*DisableIncludeSorting=*/false, /*DisablePostPreprocessorFormatting=*/false}; + LLVMStyle.DisableFormat = {/*DisableSortIncludes=*/false, /*DisablePostPreprocessorFormatting=*/false}; LLVMStyle.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; LLVMStyle.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; LLVMStyle.EnumTrailingComma = FormatStyle::ETC_Leave; @@ -2412,7 +2412,7 @@ FormatStyle getClangFormatStyle() { FormatStyle getNoStyle() { FormatStyle NoStyle = getLLVMStyle(); - NoStyle.DisableFormat = {/*DisableIncludeSorting=*/true, /*DisablePostPreprocessorFormatting=*/true}; + NoStyle.DisableFormat = {/*DisableSortIncludes=*/true, /*DisablePostPreprocessorFormatting=*/true}; NoStyle.SortIncludes = {}; NoStyle.SortUsingDeclarations = FormatStyle::SUD_Never; return NoStyle; >From 6989f265e141e9602ab74ba3c9d852015411a50a Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:05:18 +0800 Subject: [PATCH 06/10] Add DisableFormatOptions struct to Format.h --- clang/include/clang/Format/Format.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 9f28797014389..50bff1506127c 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2938,13 +2938,24 @@ struct FormatStyle { /// \version 3.7 bool DerivePointerAlignment; + struct DisableFormatOptions { + + bool DisableSortIncludes; + + bool DisablePostPreprocessorFormatting; + + bool operator==(const DisableFormatOptions &R) const { + return DisableSortIncludes == R.DisableSortIncludes && + DisablePostPreprocessorFormatting == R.DisablePostPreprocessorFormatting; + } + bool operator!=(const DisableFormatOptions &R) const { + return !(*this == R); + } + }; + /// Disables formatting completely. /// \version 3.7 - enum struct DisableFormatOptions { - DF_Disabled, - DF_SortIncludesOnly, - DF_Enabled - } DisableFormat; + DisableFormatOptions DisableFormat; /// Different styles for empty line after access modifiers. /// ``EmptyLineBeforeAccessModifier`` configuration handles the number of >From c1de4bcdec0002703aad05e9af140989b7cfe58a Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:07:15 +0800 Subject: [PATCH 07/10] Update JSON formatting style options --- clang/unittests/Format/FormatTestJson.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/unittests/Format/FormatTestJson.cpp b/clang/unittests/Format/FormatTestJson.cpp index 60e9f17855f72..bc5beaeda40f7 100644 --- a/clang/unittests/Format/FormatTestJson.cpp +++ b/clang/unittests/Format/FormatTestJson.cpp @@ -27,7 +27,7 @@ class FormatTestJson : public testing::Test { // Mock up what ClangFormat.cpp will do for JSON by adding a variable // to trick JSON into being JavaScript - if (Style.isJson() && !Style.DisableFormat) { + if (Style.isJson() && !Style.DisableFormat.DisablePostPreprocessorFormatting) { auto Err = Replaces.add( tooling::Replacement(tooling::Replacement("", 0, 0, "x = "))); if (Err) @@ -227,7 +227,7 @@ TEST_F(FormatTestJson, DisableJsonFormat) { // Since we have to disable formatting to run this test, we shall refrain from // calling test::messUp lest we change the unformatted code and cannot format // it back to how it started. - Style.DisableFormat = true; + Style.DisableFormat.DisablePostPreprocessorFormatting = true; verifyFormatStable("{}", Style); verifyFormatStable("{\n" " \"name\": 1\n" >From 31f0cdc028e857f678d91f7a9f91dfc9d6311abf Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:08:30 +0800 Subject: [PATCH 08/10] Update DisableFormat to DisableSortIncludes --- clang/unittests/Format/SortIncludesTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp index 48ecd5d32d034..fef9ff8c9d710 100644 --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -1323,7 +1323,7 @@ TEST_F(SortIncludesTest, DisableFormatDisablesIncludeSorting) { StringRef Unsorted = "#include <b.h>\n" "#include <a.h>\n"; verifyFormat(Sorted, sort(Unsorted)); - FmtStyle.DisableFormat = true; + FmtStyle.DisableFormat.DisableSortIncludes = true; verifyFormat(Unsorted, sort(Unsorted, "input.cpp", 0)); } >From 8c17f15aba47eb0856763973fb21a9bef9a0f8ef Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:10:35 +0800 Subject: [PATCH 09/10] Update DisableFormat checks to nested boolean --- clang/unittests/Format/ConfigParseTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 205cc9bcb6d31..8f3aa1edfaa15 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -188,7 +188,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(CompactNamespaces); CHECK_PARSE_BOOL(DerivePointerAlignment); CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); - CHECK_PARSE_BOOL(DisableFormat); + CHECK_PARSE_NESTED_BOOL(DisableFormat, DisableSortIncludes); + CHECK_PARSE_NESTED_BOOL(DisableFormat, DisablePostPreprocessorFormatting); CHECK_PARSE_BOOL(IndentAccessModifiers); CHECK_PARSE_BOOL(IndentCaseBlocks); CHECK_PARSE_BOOL(IndentCaseLabels); >From 60a1a6650d862c71f2d22cb388e350ed99037a1d Mon Sep 17 00:00:00 2001 From: Joseph Tay <[email protected]> Date: Tue, 21 Jul 2026 23:11:56 +0800 Subject: [PATCH 10/10] Fix condition for JSON formatting in ClangFormat --- clang/tools/clang-format/ClangFormat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index de513d942df1a..016e2f67bf122 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -484,7 +484,7 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) { // To format JSON insert a variable to trick the code into thinking its // JavaScript. - if (IsJson && !FormatStyle->DisableFormat) { + if (IsJson && !FormatStyle->DisableFormat.DisablePostPreprocessorFormatting) { auto Err = Replaces.add(tooling::Replacement(AssumedFileName, 0, 0, "x = ")); if (Err) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
