https://github.com/DynamicProdBreaker created https://github.com/llvm/llvm-project/pull/208916
Linking @TheShermanTanker who originally requested for this feature here. 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 together with DisableFormat. >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] 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; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
