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 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
