https://github.com/DaanDeMeyer updated 
https://github.com/llvm/llvm-project/pull/137617

>From ab0fe63baf2044a4768835c359f7068dfba7d81d Mon Sep 17 00:00:00 2001
From: Daan De Meyer <daan.j.deme...@gmail.com>
Date: Mon, 28 Apr 2025 13:07:50 +0200
Subject: [PATCH] clang-format: Add -disable-format option

When https://github.com/llvm/llvm-project/issues/27416 was fixed it
became impossible to only use clang-format for include sorting. Let's
introduce an option -disable-format to make it possible again to only
sort includes with clang-format without doing any other formatting.
---
 clang/docs/ClangFormat.rst                        |  2 ++
 .../disable-format-enable-include-sorting.cpp     |  9 +++++++++
 clang/tools/clang-format/ClangFormat.cpp          | 15 ++++++++++++---
 3 files changed, 23 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Format/disable-format-enable-include-sorting.cpp

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 92af06e5083d6..0ad5948e0a448 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -58,6 +58,8 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# 
code.
                                        Verilog: .sv .svh .v .vh
     --cursor=<uint>                - The position of the cursor when invoking
                                      clang-format from an editor integration
+    --disable-format               - If set, only sort includes if include 
sorting
+                                     is enabled
     --dry-run                      - If set, do not actually make the 
formatting changes
     --dump-config                  - Dump configuration options to stdout and 
exit.
                                      Can be used with -style option.
diff --git a/clang/test/Format/disable-format-enable-include-sorting.cpp 
b/clang/test/Format/disable-format-enable-include-sorting.cpp
new file mode 100644
index 0000000000000..62acf4ab71fbd
--- /dev/null
+++ b/clang/test/Format/disable-format-enable-include-sorting.cpp
@@ -0,0 +1,9 @@
+// RUN: clang-format %s -sort-includes -style=LLVM -disable-format | FileCheck 
%s
+
+#include <b>
+#include <a>
+// CHECK: <a>
+// CHECK-NEXT: <b>
+
+// CHECK: int *a  ;
+int *a  ;
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index c45e3a2c28327..ed5b4fdd87293 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -214,6 +214,12 @@ static cl::opt<bool> ListIgnored("list-ignored",
                                  cl::desc("List ignored files."),
                                  cl::cat(ClangFormatCategory), cl::Hidden);
 
+static cl::opt<bool>
+    DisableFormat("disable-format",
+                  cl::desc("If set, only sort includes if include sorting\n"
+                           "is enabled"),
+                  cl::cat(ClangFormatCategory));
+
 namespace clang {
 namespace format {
 
@@ -506,9 +512,12 @@ static bool format(StringRef FileName, bool 
ErrorOnIncompleteFormat = false) {
   // Get new affected ranges after sorting `#includes`.
   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
   FormattingAttemptStatus Status;
-  Replacements FormatChanges =
-      reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
-  Replaces = Replaces.merge(FormatChanges);
+  Replacements FormatChanges;
+  if (DisableFormat.getNumOccurrences() == 0 || !DisableFormat) {
+    FormatChanges =
+        reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
+    Replaces = Replaces.merge(FormatChanges);
+  }
   if (DryRun) {
     return Replaces.size() > (IsJson ? 1u : 0u) &&
            emitReplacementWarnings(Replaces, AssumedFileName, Code);

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to