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 <[email protected]>
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

Reply via email to