Skip to content

Commit 00f2edb

Browse files
committed
clang-format: Add -disable-format option
When #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.
1 parent bb21a68 commit 00f2edb

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

clang/docs/ClangFormat.rst

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
5858
Verilog: .sv .svh .v .vh
5959
--cursor=<uint> - The position of the cursor when invoking
6060
clang-format from an editor integration
61+
--disable-format - If set, only sort includes if include sorting
62+
is enabled
6163
--dry-run - If set, do not actually make the formatting changes
6264
--dump-config - Dump configuration options to stdout and exit.
6365
Can be used with -style option.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: clang-format %s -sort-includes -style=LLVM -disable-format | FileCheck %s
2+
3+
#include <b>
4+
#include <a>
5+
// CHECK: <a>
6+
// CHECK-NEXT: <b>
7+
8+
// CHECK: int *a ;
9+
int *a ;

clang/tools/clang-format/ClangFormat.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ static cl::opt<bool> ListIgnored("list-ignored",
214214
cl::desc("List ignored files."),
215215
cl::cat(ClangFormatCategory), cl::Hidden);
216216

217+
static cl::opt<bool>
218+
DisableFormat("disable-format",
219+
cl::desc("If set, only sort includes if include sorting\n"
220+
"is enabled"),
221+
cl::cat(ClangFormatCategory));
222+
217223
namespace clang {
218224
namespace format {
219225

@@ -506,8 +512,11 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
506512
// Get new affected ranges after sorting `#includes`.
507513
Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
508514
FormattingAttemptStatus Status;
509-
Replacements FormatChanges =
510-
reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
515+
Replacements FormatChanges;
516+
if (DisableFormat.getNumOccurrences() == 0 || !DisableFormat) {
517+
FormatChanges =
518+
reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
519+
}
511520
Replaces = Replaces.merge(FormatChanges);
512521
if (DryRun) {
513522
return Replaces.size() > (IsJson ? 1u : 0u) &&

0 commit comments

Comments
 (0)