Skip to content

Commit 0fa582d

Browse files
committed
Convert FileOutputBuffer to Expected. NFC.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317649 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 4831923 commit 0fa582d

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

include/llvm/Support/FileOutputBuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "llvm/ADT/SmallString.h"
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/Support/DataTypes.h"
20-
#include "llvm/Support/ErrorOr.h"
20+
#include "llvm/Support/Error.h"
2121
#include "llvm/Support/FileSystem.h"
2222

2323
namespace llvm {
@@ -37,7 +37,7 @@ class FileOutputBuffer {
3737
/// Factory method to create an OutputBuffer object which manages a read/write
3838
/// buffer of the specified size. When committed, the buffer will be written
3939
/// to the file at the specified path.
40-
static ErrorOr<std::unique_ptr<FileOutputBuffer>>
40+
static Expected<std::unique_ptr<FileOutputBuffer>>
4141
create(StringRef FilePath, size_t Size, unsigned Flags = 0);
4242

4343
/// Returns a pointer to the start of the buffer.

lib/DebugInfo/PDB/Native/PDBFileBuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ Error PDBFileBuilder::commit(StringRef Filename) {
176176

177177
uint64_t Filesize = Layout.SB->BlockSize * Layout.SB->NumBlocks;
178178
auto OutFileOrError = FileOutputBuffer::create(Filename, Filesize);
179-
if (OutFileOrError.getError())
180-
return llvm::make_error<pdb::GenericError>(generic_error_code::invalid_path,
181-
Filename);
179+
if (auto E = OutFileOrError.takeError())
180+
return E;
182181
FileBufferByteStream Buffer(std::move(*OutFileOrError),
183182
llvm::support::little);
184183
BinaryStreamWriter Writer(Buffer);

lib/Support/FileOutputBuffer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OnDiskBuffer : public FileOutputBuffer {
3838
std::unique_ptr<fs::mapped_file_region> Buf)
3939
: FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
4040

41-
static ErrorOr<std::unique_ptr<OnDiskBuffer>>
41+
static Expected<std::unique_ptr<OnDiskBuffer>>
4242
create(StringRef Path, size_t Size, unsigned Mode);
4343

4444
uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
@@ -78,13 +78,13 @@ class InMemoryBuffer : public FileOutputBuffer {
7878
InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
7979
: FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
8080

81-
static ErrorOr<std::unique_ptr<InMemoryBuffer>>
81+
static Expected<std::unique_ptr<InMemoryBuffer>>
8282
create(StringRef Path, size_t Size, unsigned Mode) {
8383
std::error_code EC;
8484
MemoryBlock MB = Memory::allocateMappedMemory(
8585
Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
8686
if (EC)
87-
return EC;
87+
return errorCodeToError(EC);
8888
return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
8989
}
9090

@@ -111,13 +111,13 @@ class InMemoryBuffer : public FileOutputBuffer {
111111
unsigned Mode;
112112
};
113113

114-
ErrorOr<std::unique_ptr<OnDiskBuffer>>
114+
Expected<std::unique_ptr<OnDiskBuffer>>
115115
OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
116116
// Create new file in same directory but with random name.
117117
SmallString<128> TempPath;
118118
int FD;
119119
if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
120-
return EC;
120+
return errorCodeToError(EC);
121121

122122
sys::RemoveFileOnSignal(TempPath);
123123

@@ -128,7 +128,7 @@ OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
128128
// pretty slow just like it writes specified amount of bytes,
129129
// so we should avoid calling that function.
130130
if (auto EC = fs::resize_file(FD, Size))
131-
return EC;
131+
return errorCodeToError(EC);
132132
#endif
133133

134134
// Mmap it.
@@ -137,12 +137,12 @@ OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
137137
FD, fs::mapped_file_region::readwrite, Size, 0, EC);
138138
close(FD);
139139
if (EC)
140-
return EC;
140+
return errorCodeToError(EC);
141141
return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
142142
}
143143

144144
// Create an instance of FileOutputBuffer.
145-
ErrorOr<std::unique_ptr<FileOutputBuffer>>
145+
Expected<std::unique_ptr<FileOutputBuffer>>
146146
FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
147147
unsigned Mode = fs::all_read | fs::all_write;
148148
if (Flags & F_executable)
@@ -161,7 +161,7 @@ FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
161161
// destination file and write to it on commit().
162162
switch (Stat.type()) {
163163
case fs::file_type::directory_file:
164-
return errc::is_a_directory;
164+
return errorCodeToError(errc::is_a_directory);
165165
case fs::file_type::regular_file:
166166
case fs::file_type::file_not_found:
167167
case fs::file_type::status_error:

tools/llvm-cvtres/llvm-cvtres.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int main(int argc_, const char *argv_[]) {
202202
auto FileOrErr =
203203
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
204204
if (!FileOrErr)
205-
reportError(OutputFile, FileOrErr.getError());
205+
reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
206206
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
207207
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
208208
FileBuffer->getBufferStart());

tools/llvm-mt/llvm-mt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ int main(int argc, const char **argv) {
146146
std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest();
147147
if (!OutputBuffer)
148148
reportError("empty manifest not written");
149-
ErrorOr<std::unique_ptr<FileOutputBuffer>> FileOrErr =
149+
Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr =
150150
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
151151
if (!FileOrErr)
152-
reportError(OutputFile, FileOrErr.getError());
152+
reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
153153
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
154154
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
155155
FileBuffer->getBufferStart());

tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) {
113113
template <class ELFT>
114114
void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
115115
std::unique_ptr<FileOutputBuffer> Buffer;
116-
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
116+
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
117117
FileOutputBuffer::create(File, Obj.totalSize(),
118118
FileOutputBuffer::F_executable);
119-
if (BufferOrErr.getError())
119+
if (BufferOrErr.takeError())
120120
error("failed to open " + OutputFilename);
121121
else
122122
Buffer = std::move(*BufferOrErr);

0 commit comments

Comments
 (0)