Skip to content

Commit 5a2912a

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 5a2912a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

clang/docs/ClangFormat.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
9595
determined by the QualifierAlignment style flag
9696
--sort-includes - If set, overrides the include sorting behavior
9797
determined by the SortIncludes style flag
98+
--disable-format - If set, only sort includes if include sorting
99+
is enabled
98100
--style=<string> - Set coding style. <string> can be:
99101
1. A preset: LLVM, GNU, Google, Chromium, Microsoft,
100102
Mozilla, WebKit.
Lines changed: 9 additions & 0 deletions
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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ static cl::opt<bool>
120120
"determined by the SortIncludes style flag"),
121121
cl::cat(ClangFormatCategory));
122122

123+
static cl::opt<bool>
124+
DisableFormat("disable-format",
125+
cl::desc("If set, only sort includes if include sorting\n"
126+
"is enabled"),
127+
cl::cat(ClangFormatCategory));
128+
123129
static cl::opt<std::string> QualifierAlignment(
124130
"qualifier-alignment",
125131
cl::desc("If set, overrides the qualifier alignment style\n"
@@ -506,8 +512,9 @@ 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 = reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
511518
Replaces = Replaces.merge(FormatChanges);
512519
if (DryRun) {
513520
return Replaces.size() > (IsJson ? 1u : 0u) &&

0 commit comments

Comments
 (0)