llvm.org GIT mirror llvm / b741c53
Have writeCOFFWriter return Expected<unique_ptr>. Summary: Have writeCOFFWriter return Expected<unique_ptr> instead of requiring being passed an uninitialized unique_ptr. Reviewers: zturner, ruiu Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D34307 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305730 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Beckmann 2 years ago
3 changed file(s) with 26 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
178178 std::vector> StringTable;
179179 };
180180
181 Error writeWindowsResourceCOFF(std::unique_ptr &OutputBuffer,
182 llvm::COFF::MachineTypes MachineType,
183 const WindowsResourceParser &Parser);
181 Expected>
182 writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType,
183 const WindowsResourceParser &Parser);
184184
185185 } // namespace object
186186 } // namespace llvm
306306
307307 class WindowsResourceCOFFWriter {
308308 public:
309 WindowsResourceCOFFWriter(std::unique_ptr &OutputBuffer,
310 COFF::MachineTypes MachineType,
309 WindowsResourceCOFFWriter(COFF::MachineTypes MachineType,
311310 const WindowsResourceParser &Parser, Error &E);
312 Error write();
311 std::unique_ptr write();
313312
314313 private:
315314 void performFileLayout();
325324 void writeDirectoryTree();
326325 void writeDirectoryStringTable();
327326 void writeFirstSectionRelocations();
328 std::unique_ptr &OutputBuffer;
327 std::unique_ptr OutputBuffer;
329328 char *BufferStart;
330329 uint64_t CurrentOffset = 0;
331330 COFF::MachineTypes MachineType;
345344 };
346345
347346 WindowsResourceCOFFWriter::WindowsResourceCOFFWriter(
348 std::unique_ptr &OutputBuffer, COFF::MachineTypes MachineType,
349 const WindowsResourceParser &Parser, Error &E)
350 : OutputBuffer(OutputBuffer), MachineType(MachineType),
351 Resources(Parser.getTree()), Data(Parser.getData()),
352 StringTable(Parser.getStringTable()) {
347 COFF::MachineTypes MachineType, const WindowsResourceParser &Parser,
348 Error &E)
349 : MachineType(MachineType), Resources(Parser.getTree()),
350 Data(Parser.getData()), StringTable(Parser.getStringTable()) {
353351 performFileLayout();
354352
355353 OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
416414 return Now;
417415 }
418416
419 Error WindowsResourceCOFFWriter::write() {
417 std::unique_ptr WindowsResourceCOFFWriter::write() {
420418 BufferStart = const_cast(OutputBuffer->getBufferStart());
421419
422420 writeCOFFHeader();
427425 writeSymbolTable();
428426 writeStringTable();
429427
430 return Error::success();
428 return std::move(OutputBuffer);
431429 }
432430
433431 void WindowsResourceCOFFWriter::writeCOFFHeader() {
715713 }
716714 }
717715
718 Error writeWindowsResourceCOFF(std::unique_ptr &OutputBuffer,
719 COFF::MachineTypes MachineType,
720 const WindowsResourceParser &Parser) {
716 Expected>
717 writeWindowsResourceCOFF(COFF::MachineTypes MachineType,
718 const WindowsResourceParser &Parser) {
721719 Error E = Error::success();
722 WindowsResourceCOFFWriter Writer(OutputBuffer, MachineType, Parser, E);
720 WindowsResourceCOFFWriter Writer(MachineType, Parser, E);
723721 if (E)
724 return E;
722 return std::move(E);
725723 return Writer.write();
726724 }
727725
8888 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
8989 }
9090
91 template T error(Expected EC) {
92 if (!EC)
93 error(EC.takeError());
94 return std::move(EC.get());
95 }
96
9197 int main(int argc_, const char *argv_[]) {
9298 sys::PrintStackTraceOnErrorSignal(argv_[0]);
9399 PrettyStackTraceProgram X(argc_, argv_);
174180
175181 if (Verbose) {
176182 int EntryNumber = 0;
177 Expected EntryOrErr = RF->getHeadEntry();
178 if (!EntryOrErr)
179 error(EntryOrErr.takeError());
180 ResourceEntryRef Entry = EntryOrErr.get();
183 ResourceEntryRef Entry = error(RF->getHeadEntry());
181184 bool End = false;
182185 while (!End) {
183186 error(Entry.moveNext(End));
193196 Parser.printTree(outs());
194197 }
195198
196 std::unique_ptr OutputBuffer;
197 error(llvm::object::writeWindowsResourceCOFF(OutputBuffer, MachineType,
198 Parser));
199 std::unique_ptr OutputBuffer =
200 error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser));
199201 auto FileOrErr =
200202 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
201203 if (!FileOrErr)