diff options
author | Jordan Rupprecht <rupprecht@google.com> | 2019-01-22 23:49:16 +0000 |
---|---|---|
committer | Jordan Rupprecht <rupprecht@google.com> | 2019-01-22 23:49:16 +0000 |
commit | 881cae7a4576c477d38da864bc97ceaed2484617 (patch) | |
tree | 8d5687c70f11ec5192188edf4b3ce01662dc1a9b /llvm/tools/llvm-objcopy/Buffer.cpp | |
parent | 5f51e09c7ba6b1bca8f74176a68986f4e3b28155 (diff) | |
download | bcm5719-llvm-881cae7a4576c477d38da864bc97ceaed2484617.tar.gz bcm5719-llvm-881cae7a4576c477d38da864bc97ceaed2484617.zip |
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
Diffstat (limited to 'llvm/tools/llvm-objcopy/Buffer.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/Buffer.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/llvm/tools/llvm-objcopy/Buffer.cpp b/llvm/tools/llvm-objcopy/Buffer.cpp index 2da03dee1af..1789097f276 100644 --- a/llvm/tools/llvm-objcopy/Buffer.cpp +++ b/llvm/tools/llvm-objcopy/Buffer.cpp @@ -17,23 +17,31 @@ namespace objcopy { Buffer::~Buffer() {} -void FileBuffer::allocate(size_t Size) { +Error FileBuffer::allocate(size_t Size) { Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); - handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) { - error("failed to open " + getName() + ": " + E.message()); - }); + // FileOutputBuffer::create() returns an Error that is just a wrapper around + // std::error_code. Wrap it in FileError to include the actual filename. + if (!BufferOrErr) + return createFileError(getName(), BufferOrErr.takeError()); Buf = std::move(*BufferOrErr); + return Error::success(); } -Error FileBuffer::commit() { return Buf->commit(); } +Error FileBuffer::commit() { + Error Err = Buf->commit(); + // FileOutputBuffer::commit() returns an Error that is just a wrapper around + // std::error_code. Wrap it in FileError to include the actual filename. + return Err ? createFileError(getName(), std::move(Err)) : std::move(Err); +} uint8_t *FileBuffer::getBufferStart() { return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); } -void MemBuffer::allocate(size_t Size) { +Error MemBuffer::allocate(size_t Size) { Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); + return Error::success(); } Error MemBuffer::commit() { return Error::success(); } |