Return ErrorOr from FileOutputBuffer::create. NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244848 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2015-08-13 00:31:39 +00:00
parent 7e5609b45c
commit fe4c063626
3 changed files with 23 additions and 19 deletions

View File

@ -17,6 +17,7 @@
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
namespace llvm { namespace llvm {
@ -37,9 +38,8 @@ public:
/// Factory method to create an OutputBuffer object which manages a read/write /// Factory method to create an OutputBuffer object which manages a read/write
/// buffer of the specified size. When committed, the buffer will be written /// buffer of the specified size. When committed, the buffer will be written
/// to the file at the specified path. /// to the file at the specified path.
static std::error_code create(StringRef FilePath, size_t Size, static ErrorOr<std::unique_ptr<FileOutputBuffer>>
std::unique_ptr<FileOutputBuffer> &Result, create(StringRef FilePath, size_t Size, unsigned Flags = 0);
unsigned Flags = 0);
/// Returns a pointer to the start of the buffer. /// Returns a pointer to the start of the buffer.
uint8_t *getBufferStart() { uint8_t *getBufferStart() {

View File

@ -34,10 +34,8 @@ FileOutputBuffer::~FileOutputBuffer() {
sys::fs::remove(Twine(TempPath)); sys::fs::remove(Twine(TempPath));
} }
std::error_code ErrorOr<std::unique_ptr<FileOutputBuffer>>
FileOutputBuffer::create(StringRef FilePath, size_t Size, FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
std::unique_ptr<FileOutputBuffer> &Result,
unsigned Flags) {
// If file already exists, it must be a regular file (to be mappable). // If file already exists, it must be a regular file (to be mappable).
sys::fs::file_status Stat; sys::fs::file_status Stat;
std::error_code EC = sys::fs::status(FilePath, Stat); std::error_code EC = sys::fs::status(FilePath, Stat);
@ -95,10 +93,9 @@ FileOutputBuffer::create(StringRef FilePath, size_t Size,
if (Ret) if (Ret)
return std::error_code(errno, std::generic_category()); return std::error_code(errno, std::generic_category());
Result.reset( std::unique_ptr<FileOutputBuffer> Buf(
new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath)); new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
return std::move(Buf);
return std::error_code();
} }
std::error_code FileOutputBuffer::commit() { std::error_code FileOutputBuffer::commit() {

View File

@ -39,8 +39,10 @@ TEST(FileOutputBuffer, Test) {
SmallString<128> File1(TestDirectory); SmallString<128> File1(TestDirectory);
File1.append("/file1"); File1.append("/file1");
{ {
std::unique_ptr<FileOutputBuffer> Buffer; ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer)); FileOutputBuffer::create(File1, 8192);
ASSERT_NO_ERROR(BufferOrErr.getError());
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
// Start buffer with special header. // Start buffer with special header.
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
// Write to end of buffer to verify it is writable. // Write to end of buffer to verify it is writable.
@ -59,8 +61,10 @@ TEST(FileOutputBuffer, Test) {
SmallString<128> File2(TestDirectory); SmallString<128> File2(TestDirectory);
File2.append("/file2"); File2.append("/file2");
{ {
std::unique_ptr<FileOutputBuffer> Buffer2; ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2)); FileOutputBuffer::create(File2, 8192);
ASSERT_NO_ERROR(Buffer2OrErr.getError());
std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr;
// Fill buffer with special header. // Fill buffer with special header.
memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
// Do *not* commit buffer. // Do *not* commit buffer.
@ -74,8 +78,10 @@ TEST(FileOutputBuffer, Test) {
SmallString<128> File3(TestDirectory); SmallString<128> File3(TestDirectory);
File3.append("/file3"); File3.append("/file3");
{ {
std::unique_ptr<FileOutputBuffer> Buffer; ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer)); FileOutputBuffer::create(File3, 8192000);
ASSERT_NO_ERROR(BufferOrErr.getError());
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
// Start buffer with special header. // Start buffer with special header.
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
// Write to end of buffer to verify it is writable. // Write to end of buffer to verify it is writable.
@ -93,9 +99,10 @@ TEST(FileOutputBuffer, Test) {
SmallString<128> File4(TestDirectory); SmallString<128> File4(TestDirectory);
File4.append("/file4"); File4.append("/file4");
{ {
std::unique_ptr<FileOutputBuffer> Buffer; ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer, FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
FileOutputBuffer::F_executable)); ASSERT_NO_ERROR(BufferOrErr.getError());
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
// Start buffer with special header. // Start buffer with special header.
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
// Commit buffer. // Commit buffer.