[clang] clang-format: Add -disable-format option (PR #137617)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 04:11:58 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Daan De Meyer (DaanDeMeyer)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/137617.diff
2 Files Affected:
- (modified) clang/docs/ClangFormat.rst (+2)
- (modified) clang/tools/clang-format/ClangFormat.cpp (+9-2)
``````````diff
diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 92af06e5083d6..a0de662b5c93b 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -95,6 +95,8 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
determined by the QualifierAlignment style flag
--sort-includes - If set, overrides the include sorting behavior
determined by the SortIncludes style flag
+ --disable-format - If set, only sort includes if include sorting
+ is enabled
--style=<string> - Set coding style. <string> can be:
1. A preset: LLVM, GNU, Google, Chromium, Microsoft,
Mozilla, WebKit.
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index c45e3a2c28327..da3d7edf2649f 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -120,6 +120,12 @@ static cl::opt<bool>
"determined by the SortIncludes style flag"),
cl::cat(ClangFormatCategory));
+static cl::opt<bool>
+ DisableFormat("disable-format",
+ cl::desc("If set, only sort includes if include sorting\n"
+ "is enabled"),
+ cl::cat(ClangFormatCategory));
+
static cl::opt<std::string> QualifierAlignment(
"qualifier-alignment",
cl::desc("If set, overrides the qualifier alignment style\n"
@@ -506,8 +512,9 @@ 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);
+ 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) &&
``````````
</details>
https://github.com/llvm/llvm-project/pull/137617
More information about the cfe-commits
mailing list