#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
-#include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
namespace llvm {
/// Factory method to create an OutputBuffer object which manages a read/write
/// buffer of the specified size. When committed, the buffer will be written
/// to the file at the specified path.
- static ErrorOr<std::unique_ptr<FileOutputBuffer>>
+ static Expected<std::unique_ptr<FileOutputBuffer>>
create(StringRef FilePath, size_t Size, unsigned Flags = 0);
/// Returns a pointer to the start of the buffer.
std::unique_ptr<fs::mapped_file_region> Buf)
: FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
- static ErrorOr<std::unique_ptr<OnDiskBuffer>>
+ static Expected<std::unique_ptr<OnDiskBuffer>>
create(StringRef Path, size_t Size, unsigned Mode);
uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
: FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
- static ErrorOr<std::unique_ptr<InMemoryBuffer>>
+ static Expected<std::unique_ptr<InMemoryBuffer>>
create(StringRef Path, size_t Size, unsigned Mode) {
std::error_code EC;
MemoryBlock MB = Memory::allocateMappedMemory(
Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
if (EC)
- return EC;
+ return errorCodeToError(EC);
return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
}
unsigned Mode;
};
-ErrorOr<std::unique_ptr<OnDiskBuffer>>
+Expected<std::unique_ptr<OnDiskBuffer>>
OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
// Create new file in same directory but with random name.
SmallString<128> TempPath;
int FD;
if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
- return EC;
+ return errorCodeToError(EC);
sys::RemoveFileOnSignal(TempPath);
// pretty slow just like it writes specified amount of bytes,
// so we should avoid calling that function.
if (auto EC = fs::resize_file(FD, Size))
- return EC;
+ return errorCodeToError(EC);
#endif
// Mmap it.
FD, fs::mapped_file_region::readwrite, Size, 0, EC);
close(FD);
if (EC)
- return EC;
+ return errorCodeToError(EC);
return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
}
// Create an instance of FileOutputBuffer.
-ErrorOr<std::unique_ptr<FileOutputBuffer>>
+Expected<std::unique_ptr<FileOutputBuffer>>
FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
unsigned Mode = fs::all_read | fs::all_write;
if (Flags & F_executable)
// destination file and write to it on commit().
switch (Stat.type()) {
case fs::file_type::directory_file:
- return errc::is_a_directory;
+ return errorCodeToError(errc::is_a_directory);
case fs::file_type::regular_file:
case fs::file_type::file_not_found:
case fs::file_type::status_error:
std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest();
if (!OutputBuffer)
reportError("empty manifest not written");
- ErrorOr<std::unique_ptr<FileOutputBuffer>> FileOrErr =
+ Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr =
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
if (!FileOrErr)
- reportError(OutputFile, FileOrErr.getError());
+ reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
FileBuffer->getBufferStart());
template <class ELFT>
void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
std::unique_ptr<FileOutputBuffer> Buffer;
- ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+ Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
FileOutputBuffer::create(File, Obj.totalSize(),
FileOutputBuffer::F_executable);
- if (BufferOrErr.getError())
+ if (BufferOrErr.takeError())
error("failed to open " + OutputFilename);
else
Buffer = std::move(*BufferOrErr);