Index: lld/COFF/DriverUtils.cpp =================================================================== --- lld/COFF/DriverUtils.cpp +++ lld/COFF/DriverUtils.cpp @@ -604,12 +604,11 @@ fatal(EC, "failed to parse .res file"); } - std::unique_ptr MB; - if (auto EC = - llvm::object::writeWindowsResourceCOFF(MB, Config->Machine, Parser)) - fatal(EC, "failed to write resources to buffer"); - - return MB; + Expected> E = + llvm::object::writeWindowsResourceCOFF(Config->Machine, Parser); + if (!E) + fatal(errorToErrorCode(E.takeError()), "failed to write .res to COFF"); + return std::move(E.get()); } // Run MSVC link.exe for given in-memory object files. Index: llvm/include/llvm/Object/WindowsResource.h =================================================================== --- llvm/include/llvm/Object/WindowsResource.h +++ llvm/include/llvm/Object/WindowsResource.h @@ -179,9 +179,9 @@ std::vector> StringTable; }; -Error writeWindowsResourceCOFF(std::unique_ptr &OutputBuffer, - llvm::COFF::MachineTypes MachineType, - const WindowsResourceParser &Parser); +Expected> +writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType, + const WindowsResourceParser &Parser); } // namespace object } // namespace llvm Index: llvm/lib/Object/WindowsResource.cpp =================================================================== --- llvm/lib/Object/WindowsResource.cpp +++ llvm/lib/Object/WindowsResource.cpp @@ -307,10 +307,9 @@ class WindowsResourceCOFFWriter { public: - WindowsResourceCOFFWriter(std::unique_ptr &OutputBuffer, - COFF::MachineTypes MachineType, + WindowsResourceCOFFWriter(COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, Error &E); - Error write(); + std::unique_ptr write(); private: void performFileLayout(); @@ -326,7 +325,7 @@ void writeDirectoryTree(); void writeDirectoryStringTable(); void writeFirstSectionRelocations(); - std::unique_ptr &OutputBuffer; + std::unique_ptr OutputBuffer; char *BufferStart; uint64_t CurrentOffset = 0; COFF::MachineTypes MachineType; @@ -346,11 +345,10 @@ }; WindowsResourceCOFFWriter::WindowsResourceCOFFWriter( - std::unique_ptr &OutputBuffer, COFF::MachineTypes MachineType, - const WindowsResourceParser &Parser, Error &E) - : OutputBuffer(OutputBuffer), MachineType(MachineType), - Resources(Parser.getTree()), Data(Parser.getData()), - StringTable(Parser.getStringTable()) { + COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, + Error &E) + : MachineType(MachineType), Resources(Parser.getTree()), + Data(Parser.getData()), StringTable(Parser.getStringTable()) { performFileLayout(); OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize); @@ -417,7 +415,7 @@ return Now; } -Error WindowsResourceCOFFWriter::write() { +std::unique_ptr WindowsResourceCOFFWriter::write() { BufferStart = const_cast(OutputBuffer->getBufferStart()); writeCOFFHeader(); @@ -428,7 +426,7 @@ writeSymbolTable(); writeStringTable(); - return Error::success(); + return std::move(OutputBuffer); } void WindowsResourceCOFFWriter::writeCOFFHeader() { @@ -716,13 +714,13 @@ } } -Error writeWindowsResourceCOFF(std::unique_ptr &OutputBuffer, - COFF::MachineTypes MachineType, - const WindowsResourceParser &Parser) { +Expected> +writeWindowsResourceCOFF(COFF::MachineTypes MachineType, + const WindowsResourceParser &Parser) { Error E = Error::success(); - WindowsResourceCOFFWriter Writer(OutputBuffer, MachineType, Parser, E); + WindowsResourceCOFFWriter Writer(MachineType, Parser, E); if (E) - return E; + return std::move(E); return Writer.write(); } Index: llvm/tools/llvm-cvtres/llvm-cvtres.cpp =================================================================== --- llvm/tools/llvm-cvtres/llvm-cvtres.cpp +++ llvm/tools/llvm-cvtres/llvm-cvtres.cpp @@ -89,6 +89,12 @@ [&](const ErrorInfoBase &EI) { reportError(EI.message()); }); } +template T error(Expected EC) { + if (!EC) + error(EC.takeError()); + return std::move(EC.get()); +} + int main(int argc_, const char *argv_[]) { sys::PrintStackTraceOnErrorSignal(argv_[0]); PrettyStackTraceProgram X(argc_, argv_); @@ -175,10 +181,7 @@ if (Verbose) { int EntryNumber = 0; - Expected EntryOrErr = RF->getHeadEntry(); - if (!EntryOrErr) - error(EntryOrErr.takeError()); - ResourceEntryRef Entry = EntryOrErr.get(); + ResourceEntryRef Entry = error(RF->getHeadEntry()); bool End = false; while (!End) { error(Entry.moveNext(End)); @@ -194,9 +197,8 @@ Parser.printTree(outs()); } - std::unique_ptr OutputBuffer; - error(llvm::object::writeWindowsResourceCOFF(OutputBuffer, MachineType, - Parser)); + std::unique_ptr OutputBuffer = + error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser)); auto FileOrErr = FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize()); if (!FileOrErr)