summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-12-09 17:36:48 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-12-09 17:36:48 +0000
commit333fb04506233255f10d8095c9e2de5e5f0fdc6f (patch)
tree29b15348801de3482b07a438b1fb1f2ba094d3d2 /lib
parent908b6ddad6dac40c4c0453d690f0db9422b48b10 (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')
-rw-r--r--lib/Archive/Archive.cpp15
-rw-r--r--lib/Archive/ArchiveWriter.cpp18
-rw-r--r--lib/AsmParser/Parser.cpp7
-rw-r--r--lib/Linker/LinkItems.cpp6
-rw-r--r--lib/Linker/Linker.cpp8
-rw-r--r--lib/Object/ObjectFile.cpp4
-rw-r--r--lib/Support/CommandLine.cpp4
-rw-r--r--lib/Support/FileUtilities.cpp18
-rw-r--r--lib/Support/MemoryBuffer.cpp38
-rw-r--r--lib/Support/SourceMgr.cpp7
-rw-r--r--lib/Support/system_error.cpp9
-rw-r--r--lib/VMCore/Core.cpp15
12 files changed, 100 insertions, 49 deletions
diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp
index e2e5593c6bf..3ce7fbdc948 100644
--- a/lib/Archive/Archive.cpp
+++ b/lib/Archive/Archive.cpp
@@ -17,6 +17,7 @@
#include "llvm/Module.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
+#include "llvm/Support/system_error.h"
#include <memory>
#include <cstring>
using namespace llvm;
@@ -147,9 +148,13 @@ Archive::Archive(const sys::Path& filename, LLVMContext& C)
bool
Archive::mapToMemory(std::string* ErrMsg) {
- mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg);
- if (mapfile == 0)
+ error_code ec;
+ mapfile = MemoryBuffer::getFile(archPath.c_str(), ec);
+ if (mapfile == 0) {
+ if (ErrMsg)
+ *ErrMsg = ec.message();
return true;
+ }
base = mapfile->getBufferStart();
return false;
}
@@ -213,10 +218,12 @@ bool llvm::GetBitcodeSymbols(const sys::Path& fName,
LLVMContext& Context,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
+ error_code ec;
std::auto_ptr<MemoryBuffer> Buffer(
- MemoryBuffer::getFileOrSTDIN(fName.c_str()));
+ MemoryBuffer::getFileOrSTDIN(fName.c_str(), ec));
if (!Buffer.get()) {
- if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'";
+ if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
+ + ec.message();
return true;
}
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index de5887e5c79..e9222c5e094 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
#include <fstream>
#include <ostream>
#include <iomanip>
@@ -212,9 +213,13 @@ Archive::writeMember(
const char *data = (const char*)member.getData();
MemoryBuffer *mFile = 0;
if (!data) {
- mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg);
- if (mFile == 0)
+ error_code ec;
+ mFile = MemoryBuffer::getFile(member.getPath().c_str(), ec);
+ if (mFile == 0) {
+ if (ErrMsg)
+ *ErrMsg = ec.message();
return true;
+ }
data = mFile->getBufferStart();
fSize = mFile->getBufferSize();
}
@@ -406,8 +411,13 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Map in the archive we just wrote.
{
- OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str()));
- if (arch == 0) return true;
+ error_code ec;
+ OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(), ec));
+ if (arch == 0) {
+ if (ErrMsg)
+ *ErrMsg = ec.message();
+ return true;
+ }
const char* base = arch->getBufferStart();
// Open another temporary file in order to avoid invalidating the
diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp
index e7cef9b5c3c..a613a8346ab 100644
--- a/lib/AsmParser/Parser.cpp
+++ b/lib/AsmParser/Parser.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
#include <cstring>
using namespace llvm;
@@ -41,11 +42,11 @@ Module *llvm::ParseAssembly(MemoryBuffer *F,
Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
LLVMContext &Context) {
- std::string ErrorStr;
- MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
+ error_code ec;
+ MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
if (F == 0) {
Err = SMDiagnostic(Filename,
- "Could not open input file: " + ErrorStr);
+ "Could not open input file: " + ec.message());
return 0;
}
diff --git a/lib/Linker/LinkItems.cpp b/lib/Linker/LinkItems.cpp
index cbbdd4ba101..7716b61a794 100644
--- a/lib/Linker/LinkItems.cpp
+++ b/lib/Linker/LinkItems.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/system_error.h"
using namespace llvm;
// LinkItems - This function is the main entry point into linking. It takes a
@@ -160,7 +161,8 @@ bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
// Check for a file of name "-", which means "read standard input"
if (File.str() == "-") {
std::auto_ptr<Module> M;
- if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(&Error)) {
+ error_code ec;
+ if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(ec)) {
if (!Buffer->getBufferSize()) {
delete Buffer;
Error = "standard input is empty";
@@ -172,7 +174,7 @@ bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
return false;
}
}
- return error("Cannot link stdin: " + Error);
+ return error("Cannot link stdin: " + ec.message());
}
// Determine what variety of file it is.
diff --git a/lib/Linker/Linker.cpp b/lib/Linker/Linker.cpp
index 6e27fdad03a..9606d067e0a 100644
--- a/lib/Linker/Linker.cpp
+++ b/lib/Linker/Linker.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Config/config.h"
+#include "llvm/Support/system_error.h"
using namespace llvm;
Linker::Linker(StringRef progname, StringRef modname,
@@ -98,11 +99,14 @@ Linker::LoadObject(const sys::Path &FN) {
std::string ParseErrorMessage;
Module *Result = 0;
- std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
+ error_code ec;
+ std::auto_ptr<MemoryBuffer> Buffer(
+ MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
if (Buffer.get())
Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
else
- ParseErrorMessage = "Error reading file '" + FN.str() + "'";
+ ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
+ + ec.message();
if (Result)
return std::auto_ptr<Module>(Result);
diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp
index 3ea39b37936..2a6e086137b 100644
--- a/lib/Object/ObjectFile.cpp
+++ b/lib/Object/ObjectFile.cpp
@@ -15,6 +15,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/system_error.h"
using namespace llvm;
using namespace object;
@@ -62,5 +63,6 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
}
ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
- return createObjectFile(MemoryBuffer::getFile(ObjectPath));
+ error_code ec;
+ return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
}
diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp
index a99e46dbd1c..e856509d4f3 100644
--- a/lib/Support/CommandLine.cpp
+++ b/lib/Support/CommandLine.cpp
@@ -22,6 +22,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Path.h"
@@ -464,8 +465,9 @@ static void ExpandResponseFiles(unsigned argc, char** argv,
if (FileStat && FileStat->getSize() != 0) {
// Mmap the response file into memory.
+ error_code ec;
OwningPtr<MemoryBuffer>
- respFilePtr(MemoryBuffer::getFile(respFile.c_str()));
+ respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec));
// If we could open the file, parse its contents, otherwise
// pass the @file option verbatim.
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index 72200850c57..77de94dfc92 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/lib/Support/FileUtilities.cpp
@@ -16,6 +16,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/system_error.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include <cstdlib>
@@ -199,11 +200,20 @@ int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
// Now its safe to mmap the files into memory becasue both files
// have a non-zero size.
- OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), Error));
- OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), Error));
- if (F1 == 0 || F2 == 0)
+ error_code ec;
+ OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), ec));
+ if (F1 == 0) {
+ if (Error)
+ *Error = ec.message();
+ return 2;
+ }
+ OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), ec));
+ if (F2 == 0) {
+ if (Error)
+ *Error = ec.message();
return 2;
-
+ }
+
// Okay, now that we opened the files, scan them for the first difference.
const char *File1Start = F1->getBufferStart();
const char *File2Start = F2->getBufferStart();
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp
index 07a5ed478c4..0966f9069f2 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);
diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp
index 0dc1331d260..f53169a8cfc 100644
--- a/lib/Support/SourceMgr.cpp
+++ b/lib/Support/SourceMgr.cpp
@@ -17,6 +17,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
using namespace llvm;
namespace {
@@ -48,13 +49,13 @@ SourceMgr::~SourceMgr() {
/// ~0, otherwise it returns the buffer ID of the stacked file.
unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
SMLoc IncludeLoc) {
-
- MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
+ error_code ec;
+ MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str(), ec);
// If the file didn't exist directly, see if it's in an include path.
for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
std::string IncFile = IncludeDirectories[i] + "/" + Filename;
- NewBuf = MemoryBuffer::getFile(IncFile.c_str());
+ NewBuf = MemoryBuffer::getFile(IncFile.c_str(), ec);
}
if (NewBuf == 0) return ~0U;
diff --git a/lib/Support/system_error.cpp b/lib/Support/system_error.cpp
index cd18906f067..56898de3152 100644
--- a/lib/Support/system_error.cpp
+++ b/lib/Support/system_error.cpp
@@ -96,6 +96,15 @@ system_category() {
return s;
}
+const error_category&
+posix_category() {
+#ifdef LLVM_ON_WIN32
+ return generic_category();
+#else
+ return system_category();
+#endif
+}
+
// error_condition
std::string
diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp
index 858f49cdf51..a4c77a26700 100644
--- a/lib/VMCore/Core.cpp
+++ b/lib/VMCore/Core.cpp
@@ -28,6 +28,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
@@ -2220,25 +2221,25 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- std::string Error;
- if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
+ error_code ec;
+ if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, ec)) {
*OutMemBuf = wrap(MB);
return 0;
}
-
- *OutMessage = strdup(Error.c_str());
+
+ *OutMessage = strdup(ec.message().c_str());
return 1;
}
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- std::string Error;
- if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) {
+ error_code ec;
+ if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) {
*OutMemBuf = wrap(MB);
return 0;
}
- *OutMessage = strdup(Error.c_str());
+ *OutMessage = strdup(ec.message().c_str());
return 1;
}