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

llvm-svn: 305730
This commit is contained in:
Eric Beckmann 2017-06-19 18:49:05 +00:00
parent 4440ca5fab
commit 0f7352d3aa
3 changed files with 26 additions and 26 deletions

View File

@ -179,9 +179,9 @@ private:
std::vector<std::vector<UTF16>> StringTable; std::vector<std::vector<UTF16>> StringTable;
}; };
Error writeWindowsResourceCOFF(std::unique_ptr<MemoryBuffer> &OutputBuffer, Expected<std::unique_ptr<MemoryBuffer>>
llvm::COFF::MachineTypes MachineType, writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType,
const WindowsResourceParser &Parser); const WindowsResourceParser &Parser);
} // namespace object } // namespace object
} // namespace llvm } // namespace llvm

View File

@ -307,10 +307,9 @@ uint32_t WindowsResourceParser::TreeNode::getTreeSize() const {
class WindowsResourceCOFFWriter { class WindowsResourceCOFFWriter {
public: public:
WindowsResourceCOFFWriter(std::unique_ptr<MemoryBuffer> &OutputBuffer, WindowsResourceCOFFWriter(COFF::MachineTypes MachineType,
COFF::MachineTypes MachineType,
const WindowsResourceParser &Parser, Error &E); const WindowsResourceParser &Parser, Error &E);
Error write(); std::unique_ptr<MemoryBuffer> write();
private: private:
void performFileLayout(); void performFileLayout();
@ -326,7 +325,7 @@ private:
void writeDirectoryTree(); void writeDirectoryTree();
void writeDirectoryStringTable(); void writeDirectoryStringTable();
void writeFirstSectionRelocations(); void writeFirstSectionRelocations();
std::unique_ptr<MemoryBuffer> &OutputBuffer; std::unique_ptr<MemoryBuffer> OutputBuffer;
char *BufferStart; char *BufferStart;
uint64_t CurrentOffset = 0; uint64_t CurrentOffset = 0;
COFF::MachineTypes MachineType; COFF::MachineTypes MachineType;
@ -346,11 +345,10 @@ private:
}; };
WindowsResourceCOFFWriter::WindowsResourceCOFFWriter( WindowsResourceCOFFWriter::WindowsResourceCOFFWriter(
std::unique_ptr<MemoryBuffer> &OutputBuffer, COFF::MachineTypes MachineType, COFF::MachineTypes MachineType, const WindowsResourceParser &Parser,
const WindowsResourceParser &Parser, Error &E) Error &E)
: OutputBuffer(OutputBuffer), MachineType(MachineType), : MachineType(MachineType), Resources(Parser.getTree()),
Resources(Parser.getTree()), Data(Parser.getData()), Data(Parser.getData()), StringTable(Parser.getStringTable()) {
StringTable(Parser.getStringTable()) {
performFileLayout(); performFileLayout();
OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize); OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
@ -417,7 +415,7 @@ static std::time_t getTime() {
return Now; return Now;
} }
Error WindowsResourceCOFFWriter::write() { std::unique_ptr<MemoryBuffer> WindowsResourceCOFFWriter::write() {
BufferStart = const_cast<char *>(OutputBuffer->getBufferStart()); BufferStart = const_cast<char *>(OutputBuffer->getBufferStart());
writeCOFFHeader(); writeCOFFHeader();
@ -428,7 +426,7 @@ Error WindowsResourceCOFFWriter::write() {
writeSymbolTable(); writeSymbolTable();
writeStringTable(); writeStringTable();
return Error::success(); return std::move(OutputBuffer);
} }
void WindowsResourceCOFFWriter::writeCOFFHeader() { void WindowsResourceCOFFWriter::writeCOFFHeader() {
@ -716,13 +714,13 @@ void WindowsResourceCOFFWriter::writeFirstSectionRelocations() {
} }
} }
Error writeWindowsResourceCOFF(std::unique_ptr<MemoryBuffer> &OutputBuffer, Expected<std::unique_ptr<MemoryBuffer>>
COFF::MachineTypes MachineType, writeWindowsResourceCOFF(COFF::MachineTypes MachineType,
const WindowsResourceParser &Parser) { const WindowsResourceParser &Parser) {
Error E = Error::success(); Error E = Error::success();
WindowsResourceCOFFWriter Writer(OutputBuffer, MachineType, Parser, E); WindowsResourceCOFFWriter Writer(MachineType, Parser, E);
if (E) if (E)
return E; return std::move(E);
return Writer.write(); return Writer.write();
} }

View File

@ -89,6 +89,12 @@ void error(Error EC) {
[&](const ErrorInfoBase &EI) { reportError(EI.message()); }); [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
} }
template <typename T> T error(Expected<T> EC) {
if (!EC)
error(EC.takeError());
return std::move(EC.get());
}
int main(int argc_, const char *argv_[]) { int main(int argc_, const char *argv_[]) {
sys::PrintStackTraceOnErrorSignal(argv_[0]); sys::PrintStackTraceOnErrorSignal(argv_[0]);
PrettyStackTraceProgram X(argc_, argv_); PrettyStackTraceProgram X(argc_, argv_);
@ -175,10 +181,7 @@ int main(int argc_, const char *argv_[]) {
if (Verbose) { if (Verbose) {
int EntryNumber = 0; int EntryNumber = 0;
Expected<ResourceEntryRef> EntryOrErr = RF->getHeadEntry(); ResourceEntryRef Entry = error(RF->getHeadEntry());
if (!EntryOrErr)
error(EntryOrErr.takeError());
ResourceEntryRef Entry = EntryOrErr.get();
bool End = false; bool End = false;
while (!End) { while (!End) {
error(Entry.moveNext(End)); error(Entry.moveNext(End));
@ -194,9 +197,8 @@ int main(int argc_, const char *argv_[]) {
Parser.printTree(outs()); Parser.printTree(outs());
} }
std::unique_ptr<MemoryBuffer> OutputBuffer; std::unique_ptr<MemoryBuffer> OutputBuffer =
error(llvm::object::writeWindowsResourceCOFF(OutputBuffer, MachineType, error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser));
Parser));
auto FileOrErr = auto FileOrErr =
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize()); FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
if (!FileOrErr) if (!FileOrErr)