diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index ea1f58fc6d59..630d8d741fa8 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -100,7 +100,8 @@ LinkerDriver::getArchiveMembers(MemoryBufferRef MB) { File->getFileName()); V.push_back(MBRef); } - check(std::move(Err)); + if (Err) + Error(Err); // Take ownership of memory buffers created for members of thin archives. for (std::unique_ptr &MB : File->takeThinBuffers()) diff --git a/lld/ELF/Error.h b/lld/ELF/Error.h index b27af01f07f6..552f50498464 100644 --- a/lld/ELF/Error.h +++ b/lld/ELF/Error.h @@ -31,33 +31,28 @@ template void error(const ErrorOr &V, const Twine &Prefix) { LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg); LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg, const Twine &Prefix); -void check(std::error_code EC); -void check(Error Err); - -template T check(ErrorOr EO) { - if (EO) - return std::move(*EO); - fatal(EO.getError().message()); +template T check(ErrorOr E) { + if (auto EC = E.getError()) + fatal(EC.message()); + return std::move(*E); } -template T check(Expected EO) { - if (EO) - return std::move(*EO); - check(EO.takeError()); - return T(); +template T check(Expected E) { + if (!E) + fatal(errorToErrorCode(E.takeError()).message()); + return std::move(*E); } -template T check(ErrorOr EO, const Twine &Prefix) { - if (EO) - return std::move(*EO); - fatal(EO.getError().message(), Prefix); +template T check(ErrorOr E, const Twine &Prefix) { + if (auto EC = E.getError()) + fatal(EC.message(), Prefix); + return std::move(*E); } -template T check(Expected EO, const Twine &Prefix) { - if (EO) - return std::move(*EO); - error(errorToErrorCode(EO.takeError()), Prefix); - return T(); +template T check(Expected E, const Twine &Prefix) { + if (!E) + fatal(errorToErrorCode(E.takeError()).message(), Prefix); + return std::move(*E); } } // namespace elf diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index de38fc42ef7b..a05007ea66d0 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -41,7 +41,8 @@ template static ELFFile createELFObj(MemoryBufferRef MB) { std::error_code EC; ELFFile F(MB.getBuffer(), EC); - check(EC); + if (EC) + error(EC, "failed to read " + MB.getBufferIdentifier()); return F; } diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index 33d18c5e8e70..3c2c212b640a 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -44,22 +44,24 @@ using namespace lld::elf; // This is for use when debugging LTO. static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) { - SmallString<128> Filename = Config->OutputFile; + SmallString<128> Path = Config->OutputFile; if (Many) - Filename += utostr(I); - Filename += ".lto.o"; + Path += utostr(I); + Path += ".lto.o"; std::error_code EC; - raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None); - check(EC); + raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); + if (EC) + error(EC, "cannot create " + Path); OS << Buffer; } // This is for use when debugging LTO. static void saveBCFile(Module &M, StringRef Suffix) { + std::string Path = (Config->OutputFile + Suffix).str(); std::error_code EC; - raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC, - sys::fs::OpenFlags::F_None); - check(EC); + raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); + if (EC) + error(EC, "cannot create " + Path); WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); } diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 7b9d9402ae8a..3ac0191503b9 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -261,7 +261,8 @@ template void Writer::run() { writeBuildId(); if (HasError) return; - check(Buffer->commit()); + if (auto EC = Buffer->commit()) + error(EC, "failed to write to the output file"); } template