[clang] [clang-format] Print the names of unfound files in error messages (PR #113640)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 24 20:38:50 PDT 2024


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/113640

>From 274b7cbdd8989c9f34c3df4041fa9098857bcc89 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 24 Oct 2024 20:05:52 -0700
Subject: [PATCH 1/3] [clang-format] Print the names of unfound files in error
 messages

Fixes #113631.
---
 clang/test/Format/error-unfound-files.cpp | 7 +++++++
 clang/tools/clang-format/ClangFormat.cpp  | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Format/error-unfound-files.cpp

diff --git a/clang/test/Format/error-unfound-files.cpp b/clang/test/Format/error-unfound-files.cpp
new file mode 100644
index 00000000000000..3923c8d097443c
--- /dev/null
+++ b/clang/test/Format/error-unfound-files.cpp
@@ -0,0 +1,7 @@
+// RUN: rm -f a.c b.c
+
+// RUN: not clang-format a.c b.c 2>&1 | FileCheck %s
+// CHECK: a.c:
+// CHECK-NEXT: b.c:
+
+// rm -f a.c b.c
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 96fb85e99bf5f0..7c54462b3795dd 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -419,7 +419,7 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
           ? MemoryBuffer::getFileAsStream(FileName)
           : MemoryBuffer::getFileOrSTDIN(FileName, /*IsText=*/true);
   if (std::error_code EC = CodeOrErr.getError()) {
-    errs() << EC.message() << "\n";
+    errs() << FileName << ": " << EC.message() << "\n";
     return true;
   }
   std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());

>From 0a92c3df1497baf40b6ceb293d91a8eb26b1f753 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 24 Oct 2024 20:14:28 -0700
Subject: [PATCH 2/3] Update error-unfound-files.cpp

---
 clang/test/Format/error-unfound-files.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/clang/test/Format/error-unfound-files.cpp b/clang/test/Format/error-unfound-files.cpp
index 3923c8d097443c..1cc57ed064fb42 100644
--- a/clang/test/Format/error-unfound-files.cpp
+++ b/clang/test/Format/error-unfound-files.cpp
@@ -3,5 +3,3 @@
 // RUN: not clang-format a.c b.c 2>&1 | FileCheck %s
 // CHECK: a.c:
 // CHECK-NEXT: b.c:
-
-// rm -f a.c b.c

>From 8a5c1561c7ee39c80556587f9fba3310d660068a Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 24 Oct 2024 20:38:41 -0700
Subject: [PATCH 3/3] Also fix a `return` in ClangFormat.cpp

---
 clang/tools/clang-format/ClangFormat.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 7c54462b3795dd..91b9afd1e28618 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -410,7 +410,7 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
   const bool IsSTDIN = FileName == "-";
   if (!OutputXML && Inplace && IsSTDIN) {
     errs() << "error: cannot use -i when reading from stdin.\n";
-    return false;
+    return true;
   }
   // On Windows, overwriting a file with an open file mapping doesn't work,
   // so read the whole file into memory when formatting in-place.



More information about the cfe-commits mailing list