diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-12-09 17:36:48 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-12-09 17:36:48 +0000 |
commit | 333fb04506233255f10d8095c9e2de5e5f0fdc6f (patch) | |
tree | 29b15348801de3482b07a438b1fb1f2ba094d3d2 /lib/Support/MemoryBuffer.cpp | |
parent | 908b6ddad6dac40c4c0453d690f0db9422b48b10 (diff) |
Support/MemoryBuffer: Replace all uses of std::string *ErrMsg with error_code &ec. And fix clients.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121379 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/MemoryBuffer.cpp')
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 07a5ed478c..0966f9069f 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -19,6 +19,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" #include "llvm/Support/Program.h" +#include "llvm/Support/system_error.h" #include <cassert> #include <cstdio> #include <cstring> @@ -143,19 +144,19 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) { /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN) /// returns an empty buffer. MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename, - std::string *ErrStr, + error_code &ec, int64_t FileSize) { if (Filename == "-") - return getSTDIN(ErrStr); - return getFile(Filename, ErrStr, FileSize); + return getSTDIN(ec); + return getFile(Filename, ec, FileSize); } MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename, - std::string *ErrStr, + error_code &ec, int64_t FileSize) { if (strcmp(Filename, "-") == 0) - return getSTDIN(ErrStr); - return getFile(Filename, ErrStr, FileSize); + return getSTDIN(ec); + return getFile(Filename, ec, FileSize); } //===----------------------------------------------------------------------===// @@ -185,14 +186,14 @@ public: }; } -MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr, +MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, error_code &ec, int64_t FileSize) { // Ensure the path is null terminated. SmallString<256> PathBuf(Filename.begin(), Filename.end()); - return MemoryBuffer::getFile(PathBuf.c_str(), ErrStr, FileSize); + return MemoryBuffer::getFile(PathBuf.c_str(), ec, FileSize); } -MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr, +MemoryBuffer *MemoryBuffer::getFile(const char *Filename, error_code &ec, int64_t FileSize) { int OpenFlags = O_RDONLY; #ifdef O_BINARY @@ -200,15 +201,15 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr, #endif int FD = ::open(Filename, OpenFlags); if (FD == -1) { - if (ErrStr) *ErrStr = sys::StrError(); + ec = error_code(errno, posix_category()); return 0; } - return getOpenFile(FD, Filename, ErrStr, FileSize); + return getOpenFile(FD, Filename, ec, FileSize); } MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename, - std::string *ErrStr, int64_t FileSize) { + error_code &ec, int64_t FileSize) { FileCloser FC(FD); // Close FD on return. // If we don't know the file size, use fstat to find out. fstat on an open @@ -217,7 +218,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename, struct stat FileInfo; // TODO: This should use fstat64 when available. if (fstat(FD, &FileInfo) == -1) { - if (ErrStr) *ErrStr = sys::StrError(); + ec = error_code(errno, posix_category()); return 0; } FileSize = FileInfo.st_size; @@ -240,8 +241,9 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename, MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename); if (!Buf) { - // Failed to create a buffer. - if (ErrStr) *ErrStr = "could not allocate buffer"; + // Failed to create a buffer. The only way it can fail is if + // new(std::nothrow) returns 0. + ec = make_error_code(errc::not_enough_memory); return 0; } @@ -255,7 +257,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename, if (errno == EINTR) continue; // Error while reading. - if (ErrStr) *ErrStr = sys::StrError(); + ec = error_code(errno, posix_category()); return 0; } else if (NumRead == 0) { // We hit EOF early, truncate and terminate buffer. @@ -274,7 +276,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename, // MemoryBuffer::getSTDIN implementation. //===----------------------------------------------------------------------===// -MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { +MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) { // Read in all of the data from stdin, we cannot mmap stdin. // // FIXME: That isn't necessarily true, we should try to mmap stdin and @@ -290,7 +292,7 @@ MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { ReadBytes = read(0, Buffer.end(), ChunkSize); if (ReadBytes == -1) { if (errno == EINTR) continue; - if (ErrStr) *ErrStr = sys::StrError(); + ec = error_code(errno, posix_category()); return 0; } Buffer.set_size(Buffer.size() + ReadBytes); |