Skip to content

Commit 95403b4

Browse files
dmpolukhinkuganvAaronBallman
authored
Apply format only if --format is specified (#79466)
clang-apply-replacements used to apply format even without --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. Also added note to ReleaseNotes.rst Based on #70801 --------- Co-authored-by: Kugan <[email protected]> Co-authored-by: Aaron Ballman <[email protected]>
1 parent 3e230bb commit 95403b4

File tree

7 files changed

+69
-1
lines changed

7 files changed

+69
-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;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,16 @@ Changes in existing checks
153153
Removed checks
154154
^^^^^^^^^^^^^^
155155

156+
Miscellaneous
157+
^^^^^^^^^^^^^
158+
156159
- Removed `cert-dcl21-cpp`, which was deprecated since :program:`clang-tidy` 17,
157160
since the rule DCL21-CPP has been removed from the CERT guidelines.
158161

162+
- Fixed incorrect formatting in ``clang-apply-repalcements`` when no ``--format``
163+
option is specified. Now ``clang-apply-replacements`` applies formatting only with
164+
the option.
165+
159166
Improvements to include-fixer
160167
-----------------------------
161168

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)