[llvm] 513bcf6 - [yaml2obj] Report error when the input filename does not exist

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 19 18:39:02 PDT 2025


Author: Fangrui Song
Date: 2025-06-19T18:38:58-07:00
New Revision: 513bcf6d012f7f9483af784de8487ee04cb9971a

URL: https://github.com/llvm/llvm-project/commit/513bcf6d012f7f9483af784de8487ee04cb9971a
DIFF: https://github.com/llvm/llvm-project/commit/513bcf6d012f7f9483af784de8487ee04cb9971a.diff

LOG: [yaml2obj] Report error when the input filename does not exist

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.

Pull Request: https://github.com/llvm/llvm-project/pull/144835

Added: 
    llvm/test/tools/yaml2obj/basic.test

Modified: 
    llvm/tools/yaml2obj/yaml2obj.cpp

Removed: 
    


################################################################################
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