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 9083547ae3
commit ddcfbf7d0a
4 changed files with 31 additions and 32 deletions

View File

@ -604,12 +604,11 @@ convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
fatal(EC, "failed to parse .res file");
}
std::unique_ptr<MemoryBuffer> MB;
if (auto EC =
llvm::object::writeWindowsResourceCOFF(MB, Config->Machine, Parser))
fatal(EC, "failed to write resources to buffer");
return MB;
Expected<std::unique_ptr<MemoryBuffer>> 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.

View File

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

View File

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

View File

@ -89,6 +89,12 @@ void error(Error EC) {
[&](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_[]) {
sys::PrintStackTraceOnErrorSignal(argv_[0]);
PrettyStackTraceProgram X(argc_, argv_);
@ -175,10 +181,7 @@ int main(int argc_, const char *argv_[]) {
if (Verbose) {
int EntryNumber = 0;
Expected<ResourceEntryRef> 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 @@ int main(int argc_, const char *argv_[]) {
Parser.printTree(outs());
}
std::unique_ptr<MemoryBuffer> OutputBuffer;
error(llvm::object::writeWindowsResourceCOFF(OutputBuffer, MachineType,
Parser));
std::unique_ptr<MemoryBuffer> OutputBuffer =
error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser));
auto FileOrErr =
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
if (!FileOrErr)