[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
llvm-ci wrote: LLVM Buildbot has detected a new failure on builder `clang-aarch64-global-isel` running on `linaro-clang-aarch64-global-isel` while building `clang` at step 14 "test-suite". Full details are available at: https://lab.llvm.org/buildbot/#/builders/125/builds/14465 Here is the relevant piece of the build log for the reference ``` Step 14 (test-suite) failure: test (failure) TEST 'test-suite :: tools/test/alloc_mem.test' FAILED /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/timeit-target --timeout 7200 --limit-core 0 --limit-cpu 7200 --limit-file-size 209715200 --limit-rss-size 838860800 --redirect-input /dev/null --summary /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/Output/alloc_mem.test.time /usr/bin/python3.10 /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/test_maxrss.py /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/timeit-target /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/alloc_mem maxrss too low: 132120576 < 134217728 + /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/timeit-target --timeout 7200 --limit-core 0 --limit-cpu 7200 --limit-file-size 209715200 --limit-rss-size 838860800 --redirect-input /dev/null --summary /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/Output/alloc_mem.test.time /usr/bin/python3.10 /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/test_maxrss.py /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/timeit-target /home/tcwg-buildbot/worker/clang-aarch64-global-isel/test/sandbox/build/tools/test/alloc_mem ``` https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 closed https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 updated
https://github.com/llvm/llvm-project/pull/170416
>From ea548b90eea4af1913f2a3fe274f21e5cbe29192 Mon Sep 17 00:00:00 2001
From: Matt Stark
Date: Wed, 3 Dec 2025 13:21:32 +1100
Subject: [PATCH] [clang-format] Make ignored files unformatted instead of
empty.
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
---
clang/test/Format/clang-format-ignore.cpp | 38 +++
clang/tools/clang-format/ClangFormat.cpp | 9 +-
2 files changed, 26 insertions(+), 21 deletions(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp
b/clang/test/Format/clang-format-ignore.cpp
index 6689137b48074..11e7b0e7534d4 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -46,26 +46,24 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
-// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
-
+// RUN: clang-format -assume-filename=foo.c < foo.c | diff foo.c -
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i;
// RUN: echo "foo.h" > .clang-format-ignore
// RUN: echo "!foo.c" >> .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: echo "int i ;" > foo.h
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
-// CHECK8: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: Formatting [1/1] foo.c
// RUN: clang-format -verbose foo.h 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK9 -allow-empty
-// CHECK9-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK8 -allow-empty
+// CHECK8-NOT: int
// RUN: mkdir -p %t.dir/ignore.dir/format.dir
// RUN: echo "ignore.dir/*" > .clang-format-ignore
@@ -73,26 +71,26 @@
// RUN: echo "int i ;" > ignore.dir/foo.c
// RUN: echo "int i ;" > ignore.dir/format.dir/bar.c
// RUN: clang-format -verbose ignore.dir/**/*.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK10 -match-full-lines
-// CHECK10: {{Formatting \[1/1] .*ignore\.dir[/\\]format\.dir[/\\]bar\.c}}
+// RUN: | FileCheck %s -check-prefix=CHECK9 -match-full-lines
+// CHECK9: {{Formatting \[1/1] .*ignore\.dir[/\\]format\.dir[/\\]bar\.c}}
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK11 -allow-empty
-// CHECK11-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK10 -allow-empty
+// CHECK10-NOT: int
// RUN: echo "!foo.c" >> .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK12 -match-full-lines
-// CHECK12: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK11 -match-full-lines
+// CHECK11: Formatting [1/1] foo.c
// RUN: echo "!foo.*" > .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK13 -match-full-lines
-// CHECK13: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK12 -match-full-lines
+// CHECK12: Formatting [1/1] foo.c
// RUN: echo "foo.c" >> .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK14 -allow-empty
-// CHECK14-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK13 -allow-empty
+// CHECK13-NOT: int
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 1a38ef195cbac..de513d942df1a 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -701,8 +701,15 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
+if (isIgnored(AssumeFileName)) {
+ // The user should be able to expect that running
+ // `cat foo | clang-format --assume-filename foo` and writing the output
+ // to foo will format foo.
+ // Thus, we need to just output stdin untouched if it is ignored.
+ if (!OutputXML)
+outs() << MemoryBuffer::getSTDIN()->get()->getBuffer();
return 0;
+}
return clang::format::format("-", FailOnIncompleteFormat);
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 updated
https://github.com/llvm/llvm-project/pull/170416
>From ff90ce08bbdeac87338ec2e6dd1660be1143c5ab Mon Sep 17 00:00:00 2001
From: Matt Stark
Date: Wed, 3 Dec 2025 13:21:32 +1100
Subject: [PATCH] [clang-format] Make ignored files unformatted instead of
empty.
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
---
clang/test/Format/clang-format-ignore.cpp | 38 +++
clang/tools/clang-format/ClangFormat.cpp | 16 +-
2 files changed, 33 insertions(+), 21 deletions(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp
b/clang/test/Format/clang-format-ignore.cpp
index 6689137b48074..11e7b0e7534d4 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -46,26 +46,24 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
-// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
-
+// RUN: clang-format -assume-filename=foo.c < foo.c | diff foo.c -
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i;
// RUN: echo "foo.h" > .clang-format-ignore
// RUN: echo "!foo.c" >> .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: echo "int i ;" > foo.h
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
-// CHECK8: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: Formatting [1/1] foo.c
// RUN: clang-format -verbose foo.h 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK9 -allow-empty
-// CHECK9-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK8 -allow-empty
+// CHECK8-NOT: int
// RUN: mkdir -p %t.dir/ignore.dir/format.dir
// RUN: echo "ignore.dir/*" > .clang-format-ignore
@@ -73,26 +71,26 @@
// RUN: echo "int i ;" > ignore.dir/foo.c
// RUN: echo "int i ;" > ignore.dir/format.dir/bar.c
// RUN: clang-format -verbose ignore.dir/**/*.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK10 -match-full-lines
-// CHECK10: {{Formatting \[1/1] .*ignore\.dir[/\\]format\.dir[/\\]bar\.c}}
+// RUN: | FileCheck %s -check-prefix=CHECK9 -match-full-lines
+// CHECK9: {{Formatting \[1/1] .*ignore\.dir[/\\]format\.dir[/\\]bar\.c}}
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK11 -allow-empty
-// CHECK11-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK10 -allow-empty
+// CHECK10-NOT: int
// RUN: echo "!foo.c" >> .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK12 -match-full-lines
-// CHECK12: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK11 -match-full-lines
+// CHECK11: Formatting [1/1] foo.c
// RUN: echo "!foo.*" > .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK13 -match-full-lines
-// CHECK13: Formatting [1/1] foo.c
+// RUN: | FileCheck %s -check-prefix=CHECK12 -match-full-lines
+// CHECK12: Formatting [1/1] foo.c
// RUN: echo "foo.c" >> .clang-format-ignore
// RUN: clang-format -verbose foo.c 2>&1 \
-// RUN: | FileCheck %s -check-prefix=CHECK14 -allow-empty
-// CHECK14-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK13 -allow-empty
+// CHECK13-NOT: int
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 1a38ef195cbac..00cbc1203a520 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -701,8 +701,22 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
+if (isIgnored(AssumeFileName)) {
+ // The user should be able to expect that running
+ // `cat foo | clang-format --assume-filename foo` and writing the output
+ // to foo will format foo.
+ // Thus, we need to just output stdin untouched if it is ignored.
+ if (!OutputXML)
+outs() << MemoryBuffer::getSTDIN()->get()->getBuffer();
return 0;
+}
+
+if (isIgnored(AssumeFileName)) {
+ // The user should expect
+ if (!OutputXML)
+outs() << MemoryBuffer::getSTDIN()->get()->getBuffer();
+ return 0;
+}
return clang::format::format("-", FailOnIncompleteFormat);
}
___
cfe-commits mailing list
[email protected]
https://lis
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/owenca requested changes to this pull request. cc @mydeveloperday https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -701,9 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
owenca wrote:
`.clang-format-ignore` was designed to skip ignored filenames on the command
line as if they were not provided. This is also consistent with its treatment
of empty files whether or not `--output-replacements-xml` is specified. We
shouldn't change this behavior now as people's workflows may be dependent on it.
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -701,9 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
owenca wrote:
> * The problem also persists with `clang-format ignored-file.cc`
Why should clang-format output the ignored files?
> * We also shouldn't always output the input file. When
> `--output-replacements-xml` is provided, we need to do different logic.
We can condition it on `!OutputXML` as shown in the updated suggested change
above.
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 edited https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -701,9 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
matts1 wrote:
This doesn't work, as it's not limited to `--assume-filename`.
* The problem also persists with `clang-format ignored-file.cc`
* We also shouldn't always output that. When `--output-replacements-xml` is
provided, we need to do different logic.
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
owenca wrote:
```suggestion
// RUN: clang-format -assume-filename=foo.c < foo.c | diff foo.c -
```
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i ;
+// RUN: clang-format foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i ;
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
+// CHECK8: int i;
owenca wrote:
```suggestion
// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
// CHECK6: int i;
```
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i ;
+// RUN: clang-format foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i ;
owenca wrote:
```suggestion
```
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -401,8 +401,12 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
};
// Returns true on error.
-static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
+static bool format(StringRef FileName, bool ErrorOnIncompleteFormat,
+ bool IsIgnored) {
owenca wrote:
```suggestion
static bool format(StringRef FileName, bool ErrorOnIncompleteFormat) {
```
And leave the function body alone.
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
@@ -701,9 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
owenca wrote:
```suggestion
if (isIgnored(AssumeFileName)) {
outs() << MemoryBuffer::getSTDIN()->get()->getBuffer();
return 0;
}
return clang::format::format("-", FailOnIncompleteFormat);
```
And leave the rest of the function alone.
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/HazardyKnusperkeks approved this pull request. Please wait a bit, if someone else want's to review. https://github.com/llvm/llvm-project/pull/170416 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 updated
https://github.com/llvm/llvm-project/pull/170416
>From 267f6e2a2137229bb5bcb9529b22b8c463c2a577 Mon Sep 17 00:00:00 2001
From: Matt Stark
Date: Wed, 3 Dec 2025 13:21:32 +1100
Subject: [PATCH] [clang-format] Make ignored files unformatted instead of
empty.
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
---
clang/test/Format/clang-format-ignore.cpp | 12
clang/tools/clang-format/ClangFormat.cpp | 23 +++
2 files changed, 23 insertions(+), 12 deletions(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp
b/clang/test/Format/clang-format-ignore.cpp
index 67d68aebde5d0..b04274009d5fb 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i ;
+// RUN: clang-format foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i ;
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
+// CHECK8: int i;
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 12ac8a31f24ab..01d1730afa438 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -401,8 +401,12 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
};
// Returns true on error.
-static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
+static bool format(StringRef FileName, bool ErrorOnIncompleteFormat,
+ bool IsIgnored) {
const bool IsSTDIN = FileName == "-";
+ // We don't need to do anything, there are no changes to make.
+ if (IsIgnored && (Inplace || OutputXML))
+return false;
if (!OutputXML && Inplace && IsSTDIN) {
errs() << "error: cannot use -i when reading from stdin.\n";
return true;
@@ -422,6 +426,12 @@ static bool format(StringRef FileName, bool
ErrorOnIncompleteFormat = false) {
return false; // Empty files are formatted correctly.
StringRef BufStr = Code->getBuffer();
+ // The user is requesting to output the formatted file to stdout. If the file
+ // is ignored, this means we just leave it untouched and print it.
+ if (IsIgnored) {
+outs() << BufStr;
+return false;
+ }
const char *InvalidBOM = SrcMgr::ContentCache::getInvalidBOM(BufStr);
@@ -701,9 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
}
if (FileNames.size() > 1 &&
@@ -722,13 +731,11 @@ int main(int argc, const char **argv) {
outs() << FileName << '\n';
continue;
}
-if (Ignored)
- continue;
-if (Verbose) {
+if (!Ignored && Verbose) {
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
<< FileName << "\n";
}
-Error |= clang::format::format(FileName, FailOnIncompleteFormat);
+Error |= clang::format::format(FileName, FailOnIncompleteFormat, Ignored);
}
return Error ? 1 : 0;
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
github-actions[bot] wrote:
:warning: C/C++ code formatter, clang-format found issues in your code.
:warning:
You can test this locally with the following command:
``bash
git-clang-format --diff origin/main HEAD --extensions cpp --
clang/test/Format/clang-format-ignore.cpp
clang/tools/clang-format/ClangFormat.cpp --diff_from_common_commit
``
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
View the diff from clang-format here.
``diff
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 99d35f134..01d1730af 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -401,12 +401,12 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer
{
};
// Returns true on error.
-static bool format(StringRef FileName, bool ErrorOnIncompleteFormat, bool
IsIgnored) {
+static bool format(StringRef FileName, bool ErrorOnIncompleteFormat,
+ bool IsIgnored) {
const bool IsSTDIN = FileName == "-";
// We don't need to do anything, there are no changes to make.
- if (IsIgnored && (Inplace || OutputXML)) {
+ if (IsIgnored && (Inplace || OutputXML))
return false;
- }
if (!OutputXML && Inplace && IsSTDIN) {
errs() << "error: cannot use -i when reading from stdin.\n";
return true;
@@ -711,7 +711,8 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-return clang::format::format("-", FailOnIncompleteFormat,
isIgnored(AssumeFileName));
+return clang::format::format("-", FailOnIncompleteFormat,
+ isIgnored(AssumeFileName));
}
if (FileNames.size() > 1 &&
``
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
llvmbot wrote:
@llvm/pr-subscribers-clang-format
Author: Matt (matts1)
Changes
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
---
Full diff: https://github.com/llvm/llvm-project/pull/170416.diff
2 Files Affected:
- (modified) clang/test/Format/clang-format-ignore.cpp (+8-4)
- (modified) clang/tools/clang-format/ClangFormat.cpp (+14-8)
``diff
diff --git a/clang/test/Format/clang-format-ignore.cpp
b/clang/test/Format/clang-format-ignore.cpp
index 67d68aebde5d0..b04274009d5fb 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i ;
+// RUN: clang-format foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i ;
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
+// CHECK8: int i;
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 12ac8a31f24ab..99d35f134e683 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -401,8 +401,12 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
};
// Returns true on error.
-static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
+static bool format(StringRef FileName, bool ErrorOnIncompleteFormat, bool
IsIgnored) {
const bool IsSTDIN = FileName == "-";
+ // We don't need to do anything, there are no changes to make.
+ if (IsIgnored && (Inplace || OutputXML)) {
+return false;
+ }
if (!OutputXML && Inplace && IsSTDIN) {
errs() << "error: cannot use -i when reading from stdin.\n";
return true;
@@ -422,6 +426,12 @@ static bool format(StringRef FileName, bool
ErrorOnIncompleteFormat = false) {
return false; // Empty files are formatted correctly.
StringRef BufStr = Code->getBuffer();
+ // The user is requesting to output the formatted file to stdout. If the file
+ // is ignored, this means we just leave it untouched and print it.
+ if (IsIgnored) {
+outs() << BufStr;
+return false;
+ }
const char *InvalidBOM = SrcMgr::ContentCache::getInvalidBOM(BufStr);
@@ -701,9 +711,7 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
isIgnored(AssumeFileName));
}
if (FileNames.size() > 1 &&
@@ -722,13 +730,11 @@ int main(int argc, const char **argv) {
outs() << FileName << '\n';
continue;
}
-if (Ignored)
- continue;
-if (Verbose) {
+if (!Ignored && Verbose) {
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
<< FileName << "\n";
}
-Error |= clang::format::format(FileName, FailOnIncompleteFormat);
+Error |= clang::format::format(FileName, FailOnIncompleteFormat, Ignored);
}
return Error ? 1 : 0;
}
``
https://github.com/llvm/llvm-project/pull/170416
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
https://github.com/matts1 created
https://github.com/llvm/llvm-project/pull/170416
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
>From 2fd9cbe9c8136238e9e0523e12a3dfe41733a561 Mon Sep 17 00:00:00 2001
From: Matt Stark
Date: Wed, 3 Dec 2025 13:21:32 +1100
Subject: [PATCH] [clang-format] Make ignored files unformatted instead of
empty.
Tools rely on the expectation that clang-format will output a formatted file.
In the case of ignored files, the formatted file should just be the input file,
untouched.
Fixes #170407
---
clang/test/Format/clang-format-ignore.cpp | 12
clang/tools/clang-format/ClangFormat.cpp | 22 ++
2 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp
b/clang/test/Format/clang-format-ignore.cpp
index 67d68aebde5d0..b04274009d5fb 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -46,15 +46,19 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js
+// Check that ignored files are unformatted, but still output.
// RUN: echo "foo.*" > .clang-format-ignore
// RUN: echo "int i ;" > foo.c
// RUN: clang-format -assume-filename=foo.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: int
+// RUN: | FileCheck %s -check-prefix=CHECK6 -match-full-lines
+// CHECK6: int i ;
+// RUN: clang-format foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i ;
// RUN: clang-format -assume-filename=bar.c < foo.c \
-// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
-// CHECK7: int i;
+// RUN: | FileCheck %s -check-prefix=CHECK8 -match-full-lines
+// CHECK8: int i;
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp
b/clang/tools/clang-format/ClangFormat.cpp
index 12ac8a31f24ab..99d35f134e683 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -401,8 +401,12 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
};
// Returns true on error.
-static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
+static bool format(StringRef FileName, bool ErrorOnIncompleteFormat, bool
IsIgnored) {
const bool IsSTDIN = FileName == "-";
+ // We don't need to do anything, there are no changes to make.
+ if (IsIgnored && (Inplace || OutputXML)) {
+return false;
+ }
if (!OutputXML && Inplace && IsSTDIN) {
errs() << "error: cannot use -i when reading from stdin.\n";
return true;
@@ -422,6 +426,12 @@ static bool format(StringRef FileName, bool
ErrorOnIncompleteFormat = false) {
return false; // Empty files are formatted correctly.
StringRef BufStr = Code->getBuffer();
+ // The user is requesting to output the formatted file to stdout. If the file
+ // is ignored, this means we just leave it untouched and print it.
+ if (IsIgnored) {
+outs() << BufStr;
+return false;
+ }
const char *InvalidBOM = SrcMgr::ContentCache::getInvalidBOM(BufStr);
@@ -701,9 +711,7 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
-if (isIgnored(AssumeFileName))
- return 0;
-return clang::format::format("-", FailOnIncompleteFormat);
+return clang::format::format("-", FailOnIncompleteFormat,
isIgnored(AssumeFileName));
}
if (FileNames.size() > 1 &&
@@ -722,13 +730,11 @@ int main(int argc, const char **argv) {
outs() << FileName << '\n';
continue;
}
-if (Ignored)
- continue;
-if (Verbose) {
+if (!Ignored && Verbose) {
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
<< FileName << "\n";
}
-Error |= clang::format::format(FileName, FailOnIncompleteFormat);
+Error |= clang::format::format(FileName, FailOnIncompleteFormat, Ignored);
}
return Error ? 1 : 0;
}
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
