diff options
author | Nico Weber <thakis@chromium.org> | 2021-01-26 19:11:56 -0500 |
---|---|---|
committer | Nico Weber <thakis@chromium.org> | 2021-01-26 19:13:30 -0500 |
commit | f3c9687a4f79fc5e31890278507294594d8787bd (patch) | |
tree | 812ea4e18d37ff13f90ff9f6a1c717c47b1b5b63 /llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp | |
parent | 3caa2d3354e31827ba7a5e258f0025bac5336cbe (diff) | |
download | llvm-f3c9687a4f79fc5e31890278507294594d8787bd.zip llvm-f3c9687a4f79fc5e31890278507294594d8787bd.tar.gz llvm-f3c9687a4f79fc5e31890278507294594d8787bd.tar.bz2 |
llvm-lib: Pull error printing code out of two functions
Slightly changes the output in error code, but no behavior change in
normal use. This is for preparation for using these two functions
elsewhere.
Diffstat (limited to 'llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp')
-rw-r--r-- | llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp index cd39428..f3904b9 100644 --- a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp +++ b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp @@ -139,35 +139,28 @@ static void doList(opt::InputArgList& Args) { fatalOpenError(std::move(Err), B->getBufferIdentifier()); } -static COFF::MachineTypes getCOFFFileMachine(MemoryBufferRef MB) { +static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) { std::error_code EC; auto Obj = object::COFFObjectFile::create(MB); - if (!Obj) { - llvm::errs() << MB.getBufferIdentifier() - << ": failed to open: " << Obj.takeError() << '\n'; - exit(1); - } + if (!Obj) + return Obj.takeError(); uint16_t Machine = (*Obj)->getMachine(); if (Machine != COFF::IMAGE_FILE_MACHINE_I386 && Machine != COFF::IMAGE_FILE_MACHINE_AMD64 && Machine != COFF::IMAGE_FILE_MACHINE_ARMNT && Machine != COFF::IMAGE_FILE_MACHINE_ARM64) { - llvm::errs() << MB.getBufferIdentifier() << ": unknown machine: " << Machine - << '\n'; - exit(1); + return createStringError(inconvertibleErrorCode(), + "unknown machine: " + std::to_string(Machine)); } return static_cast<COFF::MachineTypes>(Machine); } -static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) { +static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) { Expected<std::string> TripleStr = getBitcodeTargetTriple(MB); - if (!TripleStr) { - llvm::errs() << MB.getBufferIdentifier() - << ": failed to get target triple from bitcode\n"; - exit(1); - } + if (!TripleStr) + return TripleStr.takeError(); switch (Triple(*TripleStr).getArch()) { case Triple::x86: @@ -179,9 +172,8 @@ static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) { case Triple::aarch64: return COFF::IMAGE_FILE_MACHINE_ARM64; default: - llvm::errs() << MB.getBufferIdentifier() - << ": unknown arch in target triple " << *TripleStr << '\n'; - exit(1); + return createStringError(inconvertibleErrorCode(), + "unknown arch in target triple: " + *TripleStr); } } @@ -201,7 +193,7 @@ static void appendFile(std::vector<NewArchiveMember> &Members, // If a user attempts to add an archive to another archive, llvm-lib doesn't // handle the first archive file as a single file. Instead, it extracts all - // members from the archive and add them to the second archive. This beahvior + // members from the archive and add them to the second archive. This behavior // is for compatibility with Microsoft's lib command. if (Magic == file_magic::archive) { Error Err = Error::success(); @@ -233,9 +225,17 @@ static void appendFile(std::vector<NewArchiveMember> &Members, // in writeArchive() which needs to support many tools, can't assume the // input is COFF, and doesn't have a good way to report errors. if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) { - COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object) - ? getCOFFFileMachine(MB) - : getBitcodeFileMachine(MB); + Expected<COFF::MachineTypes> MaybeFileMachine = + (Magic == file_magic::coff_object) ? getCOFFFileMachine(MB) + : getBitcodeFileMachine(MB); + if (!MaybeFileMachine) { + handleAllErrors(MaybeFileMachine.takeError(), [&](const ErrorInfoBase &EIB) { + llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message() + << "\n"; + }); + exit(1); + } + COFF::MachineTypes FileMachine = *MaybeFileMachine; // FIXME: Once lld-link rejects multiple resource .obj files: // Call convertResToCOFF() on .res files and add the resulting |