Skip to content

Commit 513bcf6

Browse files
authored
[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: #144835
1 parent a05393a commit 513bcf6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

llvm/test/tools/yaml2obj/basic.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Test case when the input file does not exist.
2+
RUN: not yaml2obj %t.blah 2>&1 | FileCheck -DMSG=%errc_ENOENT --check-prefix=ENOENT %s
3+
4+
ENOENT: yaml2obj: error: {{.*}}.blah: [[MSG]]

llvm/tools/yaml2obj/yaml2obj.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ int main(int argc, char **argv) {
117117
argc, argv, "Create an object file from a YAML description", nullptr,
118118
nullptr, /*LongOptionsUseDoubleDash=*/true);
119119

120-
auto ErrHandler = [](const Twine &Msg) {
121-
WithColor::error(errs(), "yaml2obj") << Msg << "\n";
120+
constexpr StringRef ProgName = "yaml2obj";
121+
auto ErrHandler = [&](const Twine &Msg) {
122+
WithColor::error(errs(), ProgName) << Msg << "\n";
122123
};
123124

124125
std::error_code EC;
@@ -131,8 +132,10 @@ int main(int argc, char **argv) {
131132

132133
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
133134
MemoryBuffer::getFileOrSTDIN(Input, /*IsText=*/true);
134-
if (!Buf)
135+
if (std::error_code EC = Buf.getError()) {
136+
WithColor::error(errs(), ProgName) << Input << ": " << EC.message() << '\n';
135137
return 1;
138+
}
136139

137140
std::optional<std::string> Buffer =
138141
preprocess(Buf.get()->getBuffer(), ErrHandler);

0 commit comments

Comments
 (0)