Skip to content

clang-format: Add -disable-format option #137617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ClangFormat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Format/disable-format-enable-include-sorting.cpp
Original file line number Diff line number Diff line change
@@ -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 ;
15 changes: 12 additions & 3 deletions clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down