[clang] [clang-format] Make ignored files unformatted instead of empty. (PR #170416)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 2 19:46:00 PST 2025
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 <msta at google.com>
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;
}
More information about the cfe-commits
mailing list