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

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 18 21:55:05 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-objectyaml

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/144835.diff


2 Files Affected:

- (added) llvm/test/tools/yaml2obj/basic.test (+4) 
- (modified) llvm/tools/yaml2obj/yaml2obj.cpp (+6-3) 


``````````diff
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);

``````````

</details>


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


More information about the llvm-commits mailing list