Skip to content

Commit ac5aae4

Browse files
committed
Apply format only if --format is specified
clang-apply-replacements is now applying format even when --format is specified. This because, methods like createReplacementsForHeaders only takes the Spec.Style and would re-order the headers even when it was not requested. The fix is to set up Spec.Style only if --format is provided.
1 parent a6dabed commit ac5aae4

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ int main(int argc, char **argv) {
141141

142142
tooling::ApplyChangesSpec Spec;
143143
Spec.Cleanup = true;
144-
Spec.Style = FormatStyle;
145144
Spec.Format = DoFormat ? tooling::ApplyChangesSpec::kAll
146145
: tooling::ApplyChangesSpec::kNone;
146+
Spec.Style = DoFormat ? FormatStyle : format::getNoStyle();
147147

148148
for (const auto &FileChange : Changes) {
149149
FileEntryRef Entry = FileChange.first;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <string>
2+
// CHECK: #include <string>
3+
// CHECK-NEXT: #include <memory>
4+
// CHECK-NEXT: #include "bar.h"
5+
#include <memory>
6+
#include "foo.h"
7+
#include "bar.h"
8+
9+
void foo() {
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
MainSourceFile: no.cpp
3+
Diagnostics:
4+
- DiagnosticName: test-header-format
5+
DiagnosticMessage:
6+
Message: Fix
7+
FilePath: $(path)/no.cpp
8+
FileOffset: 36
9+
Replacements:
10+
- FilePath: $(path)/no.cpp
11+
Offset: 36
12+
Length: 17
13+
ReplacementText: ""
14+
...
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <string>
2+
// CHECK: #include "bar.h"
3+
// CHECK-NEXT: #include <memory>
4+
// CHECK-NEXT: #include <string>
5+
#include <memory>
6+
#include "foo.h"
7+
#include "bar.h"
8+
9+
void foo() {
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
MainSourceFile: yes.cpp
3+
Diagnostics:
4+
- DiagnosticName: test-header-format
5+
DiagnosticMessage:
6+
Message: Fix
7+
FilePath: $(path)/yes.cpp
8+
FileOffset: 36
9+
Replacements:
10+
- FilePath: $(path)/yes.cpp
11+
Offset: 36
12+
Length: 17
13+
ReplacementText: ""
14+
...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: mkdir -p %T/Inputs/format_header_yes
2+
// RUN: mkdir -p %T/Inputs/format_header_no
3+
//
4+
//
5+
// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/format_header/yes.cpp > %T/Inputs/format_header_yes/yes.cpp
6+
// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/format_header/no.cpp > %T/Inputs/format_header_no/no.cpp
7+
// RUN: sed "s#\$(path)#%/T/Inputs/format_header_yes#" %S/Inputs/format_header/yes.yaml > %T/Inputs/format_header_yes/yes.yaml
8+
// RUN: sed "s#\$(path)#%/T/Inputs/format_header_no#" %S/Inputs/format_header/no.yaml > %T/Inputs/format_header_no/no.yaml
9+
// RUN: clang-apply-replacements -format -style="{BasedOnStyle: llvm, SortIncludes: CaseSensitive}" %T/Inputs/format_header_yes
10+
// RUN: clang-apply-replacements %T/Inputs/format_header_no
11+
// RUN: FileCheck --strict-whitespace -input-file=%T/Inputs/format_header_yes/yes.cpp %S/Inputs/format_header/yes.cpp
12+
// RUN: FileCheck --strict-whitespace -input-file=%T/Inputs/format_header_no/no.cpp %S/Inputs/format_header/no.cpp
13+
//

0 commit comments

Comments
 (0)