[llvm] [yaml2obj] Report error when the input filename does not exist (PR #144835)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 18 21:54:36 PDT 2025


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/144835

I invoked yaml2obj with a mistyped filename and received no error
message. I nearly thought the program had succeeded, but the shell's
exit code prompt tipped me off.


>From cfedf1aadc82be99c8416eb5a7f9302650d0330f Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 18 Jun 2025 21:54:27 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 llvm/test/tools/yaml2obj/basic.test | 4 ++++
 llvm/tools/yaml2obj/yaml2obj.cpp    | 9 ++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/tools/yaml2obj/basic.test

diff --git a/llvm/test/tools/yaml2obj/basic.test b/llvm/test/tools/yaml2obj/basic.test
new file mode 100644
index 0000000000000..d673accfee881
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/basic.test
@@ -0,0 +1,4 @@
+# Test case when the input file does not exist.
+RUN: not yaml2obj %t.blah 2>&1 | FileCheck -DMSG=%errc_ENOENT --check-prefix=ENOENT %s
+
+ENOENT: yaml2obj: error: {{.*}}.blah: [[MSG]]
diff --git a/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp
index 45aa3828311fe..dcd6dfcd3de2a 100644
--- a/llvm/tools/yaml2obj/yaml2obj.cpp
+++ b/llvm/tools/yaml2obj/yaml2obj.cpp
@@ -117,8 +117,9 @@ int main(int argc, char **argv) {
       argc, argv, "Create an object file from a YAML description", nullptr,
       nullptr, /*LongOptionsUseDoubleDash=*/true);
 
-  auto ErrHandler = [](const Twine &Msg) {
-    WithColor::error(errs(), "yaml2obj") << Msg << "\n";
+  constexpr StringRef ProgName = "yaml2obj";
+  auto ErrHandler = [&](const Twine &Msg) {
+    WithColor::error(errs(), ProgName) << Msg << "\n";
   };
 
   std::error_code EC;
@@ -131,8 +132,10 @@ int main(int argc, char **argv) {
 
   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
       MemoryBuffer::getFileOrSTDIN(Input, /*IsText=*/true);
-  if (!Buf)
+  if (std::error_code EC = Buf.getError()) {
+    WithColor::error(errs(), ProgName) << Input << ": " << EC.message() << '\n';
     return 1;
+  }
 
   std::optional<std::string> Buffer =
       preprocess(Buf.get()->getBuffer(), ErrHandler);



More information about the llvm-commits mailing list