aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-12-16 03:29:14 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-12-16 03:29:14 +0000
commit3ff9563c3e391954b2e224afcf8b2b0fcc3888aa (patch)
treecccde9111a73ba5895f6cefbfb280290fa6c469d /lib
parentb29b20e7deb7297f6a10b2d6922feeca8c6df055 (diff)
MemoryBuffer now return an error_code and returns a OwningPtr<MemoryBuffer> via an out parm.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121958 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Archive/Archive.cpp14
-rw-r--r--lib/Archive/ArchiveWriter.cpp11
-rw-r--r--lib/AsmParser/Parser.cpp7
-rw-r--r--lib/Linker/LinkItems.cpp7
-rw-r--r--lib/Linker/Linker.cpp10
-rw-r--r--lib/Object/ObjectFile.cpp7
-rw-r--r--lib/Support/CommandLine.cpp9
-rw-r--r--lib/Support/FileUtilities.cpp8
-rw-r--r--lib/Support/MemoryBuffer.cpp72
-rw-r--r--lib/Support/SourceMgr.cpp9
-rw-r--r--lib/VMCore/Core.cpp10
11 files changed, 82 insertions, 82 deletions
diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp
index 3ce7fbdc94..20d9deec99 100644
--- a/lib/Archive/Archive.cpp
+++ b/lib/Archive/Archive.cpp
@@ -148,13 +148,13 @@ Archive::Archive(const sys::Path& filename, LLVMContext& C)
bool
Archive::mapToMemory(std::string* ErrMsg) {
- error_code ec;
- mapfile = MemoryBuffer::getFile(archPath.c_str(), ec);
- if (mapfile == 0) {
+ OwningPtr<MemoryBuffer> File;
+ if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
}
+ mapfile = File.take();
base = mapfile->getBufferStart();
return false;
}
@@ -218,10 +218,8 @@ 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(), ec));
- if (!Buffer.get()) {
+ OwningPtr<MemoryBuffer> Buffer;
+ if (error_code ec = MemoryBuffer::getFileOrSTDIN(fName.c_str(), Buffer)) {
if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
+ ec.message();
return true;
@@ -246,7 +244,7 @@ llvm::GetBitcodeSymbols(const char *BufPtr, unsigned Length,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
// Get the module.
- std::auto_ptr<MemoryBuffer> Buffer(
+ OwningPtr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBufferCopy(StringRef(BufPtr, Length),ModuleID.c_str()));
Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index e9222c5e09..07516c6aa4 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -213,13 +213,13 @@ Archive::writeMember(
const char *data = (const char*)member.getData();
MemoryBuffer *mFile = 0;
if (!data) {
- error_code ec;
- mFile = MemoryBuffer::getFile(member.getPath().c_str(), ec);
- if (mFile == 0) {
+ OwningPtr<MemoryBuffer> File;
+ if (error_code ec = MemoryBuffer::getFile(member.getPath().c_str(), File)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
}
+ mFile = File.take();
data = mFile->getBufferStart();
fSize = mFile->getBufferSize();
}
@@ -411,9 +411,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Map in the archive we just wrote.
{
- error_code ec;
- OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(), ec));
- if (arch == 0) {
+ OwningPtr<MemoryBuffer> arch;
+ if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp
index a613a8346a..59fb471f2b 100644
--- a/lib/AsmParser/Parser.cpp
+++ b/lib/AsmParser/Parser.cpp
@@ -42,15 +42,14 @@ Module *llvm::ParseAssembly(MemoryBuffer *F,
Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
LLVMContext &Context) {
- error_code ec;
- MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
- if (F == 0) {
+ OwningPtr<MemoryBuffer> File;
+ if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
Err = SMDiagnostic(Filename,
"Could not open input file: " + ec.message());
return 0;
}
- return ParseAssembly(F, 0, Err, Context);
+ return ParseAssembly(File.take(), 0, Err, Context);
}
Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
diff --git a/lib/Linker/LinkItems.cpp b/lib/Linker/LinkItems.cpp
index 7716b61a79..52a0d175a5 100644
--- a/lib/Linker/LinkItems.cpp
+++ b/lib/Linker/LinkItems.cpp
@@ -161,14 +161,13 @@ 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;
+ OwningPtr<MemoryBuffer> Buffer;
error_code ec;
- if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(ec)) {
+ if (!(ec = MemoryBuffer::getSTDIN(Buffer))) {
if (!Buffer->getBufferSize()) {
- delete Buffer;
Error = "standard input is empty";
} else {
- M.reset(ParseBitcodeFile(Buffer, Context, &Error));
- delete Buffer;
+ M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error));
if (M.get())
if (!LinkInModule(M.get(), &Error))
return false;
diff --git a/lib/Linker/Linker.cpp b/lib/Linker/Linker.cpp
index 9606d067e0..fba91da5dd 100644
--- a/lib/Linker/Linker.cpp
+++ b/lib/Linker/Linker.cpp
@@ -99,14 +99,12 @@ Linker::LoadObject(const sys::Path &FN) {
std::string ParseErrorMessage;
Module *Result = 0;
- error_code ec;
- std::auto_ptr<MemoryBuffer> Buffer(
- MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
- if (Buffer.get())
- Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
- else
+ OwningPtr<MemoryBuffer> Buffer;
+ if (error_code ec = MemoryBuffer::getFileOrSTDIN(FN.c_str(), Buffer))
ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
+ ec.message();
+ else
+ Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
if (Result)
return std::auto_ptr<Module>(Result);
diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp
index 2a6e086137..aa483f357f 100644
--- a/lib/Object/ObjectFile.cpp
+++ b/lib/Object/ObjectFile.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Object/ObjectFile.h"
+#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -63,6 +64,8 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
}
ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
- error_code ec;
- return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
+ OwningPtr<MemoryBuffer> File;
+ if (error_code ec = MemoryBuffer::getFile(ObjectPath, File))
+ return NULL;
+ return createObjectFile(File.take());
}
diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp
index e856509d4f..373a1a2c52 100644
--- a/lib/Support/CommandLine.cpp
+++ b/lib/Support/CommandLine.cpp
@@ -464,11 +464,6 @@ static void ExpandResponseFiles(unsigned argc, char** argv,
const sys::FileStatus *FileStat = respFile.getFileStatus();
if (FileStat && FileStat->getSize() != 0) {
- // Mmap the response file into memory.
- error_code ec;
- OwningPtr<MemoryBuffer>
- respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec));
-
// If we could open the file, parse its contents, otherwise
// pass the @file option verbatim.
@@ -477,7 +472,9 @@ static void ExpandResponseFiles(unsigned argc, char** argv,
// itself contain additional @file options; any such options will be
// processed recursively.")
- if (respFilePtr != 0) {
+ // Mmap the response file into memory.
+ OwningPtr<MemoryBuffer> respFilePtr;
+ if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
ParseCStringVector(newArgv, respFilePtr->getBufferStart());
continue;
}
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index c9e42e9b38..5dbabee7a7 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/lib/Support/FileUtilities.cpp
@@ -201,14 +201,14 @@ int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
// Now its safe to mmap the files into memory becasue both files
// have a non-zero size.
error_code ec;
- OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), ec));
- if (F1 == 0) {
+ OwningPtr<MemoryBuffer> F1;
+ if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) {
if (Error)
*Error = ec.message();
return 2;
}
- OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), ec));
- if (F2 == 0) {
+ OwningPtr<MemoryBuffer> F2;
+ if (error_code ec = MemoryBuffer::getFile(FileB.c_str(), F2)) {
if (Error)
*Error = ec.message();
return 2;
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp
index 0966f9069f..d3c71b9928 100644
--- a/lib/Support/MemoryBuffer.cpp
+++ b/lib/Support/MemoryBuffer.cpp
@@ -35,6 +35,8 @@
#include <fcntl.h>
using namespace llvm;
+namespace { const llvm::error_code success; }
+
//===----------------------------------------------------------------------===//
// MemoryBuffer implementation itself.
//===----------------------------------------------------------------------===//
@@ -143,20 +145,20 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
/// if the Filename is "-". If an error occurs, this returns null and fills
/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
/// returns an empty buffer.
-MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
- error_code &ec,
- int64_t FileSize) {
+error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
+ OwningPtr<MemoryBuffer> &result,
+ int64_t FileSize) {
if (Filename == "-")
- return getSTDIN(ec);
- return getFile(Filename, ec, FileSize);
+ return getSTDIN(result);
+ return getFile(Filename, result, FileSize);
}
-MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
- error_code &ec,
- int64_t FileSize) {
+error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
+ OwningPtr<MemoryBuffer> &result,
+ int64_t FileSize) {
if (strcmp(Filename, "-") == 0)
- return getSTDIN(ec);
- return getFile(Filename, ec, FileSize);
+ return getSTDIN(result);
+ return getFile(Filename, result, FileSize);
}
//===----------------------------------------------------------------------===//
@@ -186,30 +188,32 @@ public:
};
}
-MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, error_code &ec,
- int64_t FileSize) {
+error_code MemoryBuffer::getFile(StringRef Filename,
+ OwningPtr<MemoryBuffer> &result,
+ int64_t FileSize) {
// Ensure the path is null terminated.
SmallString<256> PathBuf(Filename.begin(), Filename.end());
- return MemoryBuffer::getFile(PathBuf.c_str(), ec, FileSize);
+ return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize);
}
-MemoryBuffer *MemoryBuffer::getFile(const char *Filename, error_code &ec,
- int64_t FileSize) {
+error_code MemoryBuffer::getFile(const char *Filename,
+ OwningPtr<MemoryBuffer> &result,
+ int64_t FileSize) {
int OpenFlags = O_RDONLY;
#ifdef O_BINARY
OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
#endif
int FD = ::open(Filename, OpenFlags);
if (FD == -1) {
- ec = error_code(errno, posix_category());
- return 0;
+ return error_code(errno, posix_category());
}
- return getOpenFile(FD, Filename, ec, FileSize);
+ return getOpenFile(FD, Filename, result, FileSize);
}
-MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
- error_code &ec, int64_t FileSize) {
+error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
+ OwningPtr<MemoryBuffer> &result,
+ 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
@@ -218,8 +222,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
struct stat FileInfo;
// TODO: This should use fstat64 when available.
if (fstat(FD, &FileInfo) == -1) {
- ec = error_code(errno, posix_category());
- return 0;
+ return error_code(errno, posix_category());
}
FileSize = FileInfo.st_size;
}
@@ -234,8 +237,9 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (FileSize >= 4096*4 &&
(FileSize & (sys::Process::GetPageSize()-1)) != 0) {
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
- return GetNamedBuffer<MemoryBufferMMapFile>(StringRef(Pages, FileSize),
- Filename);
+ result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
+ StringRef(Pages, FileSize), Filename));
+ return success;
}
}
@@ -243,8 +247,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (!Buf) {
// 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;
+ return make_error_code(errc::not_enough_memory);
}
OwningPtr<MemoryBuffer> SB(Buf);
@@ -257,26 +260,27 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (errno == EINTR)
continue;
// Error while reading.
- ec = error_code(errno, posix_category());
- return 0;
+ return error_code(errno, posix_category());
} else if (NumRead == 0) {
// We hit EOF early, truncate and terminate buffer.
Buf->BufferEnd = BufPtr;
*BufPtr = 0;
- return SB.take();
+ result.swap(SB);
+ return success;
}
BytesLeft -= NumRead;
BufPtr += NumRead;
}
- return SB.take();
+ result.swap(SB);
+ return success;
}
//===----------------------------------------------------------------------===//
// MemoryBuffer::getSTDIN implementation.
//===----------------------------------------------------------------------===//
-MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) {
+error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
// 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
@@ -292,11 +296,11 @@ MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) {
ReadBytes = read(0, Buffer.end(), ChunkSize);
if (ReadBytes == -1) {
if (errno == EINTR) continue;
- ec = error_code(errno, posix_category());
- return 0;
+ return error_code(errno, posix_category());
}
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
- return getMemBufferCopy(Buffer, "<stdin>");
+ result.reset(getMemBufferCopy(Buffer, "<stdin>"));
+ return success;
}
diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp
index 1f62068376..ef099163c2 100644
--- a/lib/Support/SourceMgr.cpp
+++ b/lib/Support/SourceMgr.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
using namespace llvm;
@@ -49,18 +50,18 @@ SourceMgr::~SourceMgr() {
/// ~0, otherwise it returns the buffer ID of the stacked file.
unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
SMLoc IncludeLoc) {
- error_code ec;
- MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str(), ec);
+ OwningPtr<MemoryBuffer> NewBuf;
+ MemoryBuffer::getFile(Filename.c_str(), NewBuf);
// 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(), ec);
+ MemoryBuffer::getFile(IncFile.c_str(), NewBuf);
}
if (NewBuf == 0) return ~0U;
- return AddNewSourceBuffer(NewBuf, IncludeLoc);
+ return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
}
diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp
index a4c77a2670..6bad2f3f44 100644
--- a/lib/VMCore/Core.cpp
+++ b/lib/VMCore/Core.cpp
@@ -2221,9 +2221,10 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
+ OwningPtr<MemoryBuffer> MB;
error_code ec;
- if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, ec)) {
- *OutMemBuf = wrap(MB);
+ if (!(ec = MemoryBuffer::getFile(Path, MB))) {
+ *OutMemBuf = wrap(MB.take());
return 0;
}
@@ -2233,9 +2234,10 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
+ OwningPtr<MemoryBuffer> MB;
error_code ec;
- if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) {
- *OutMemBuf = wrap(MB);
+ if (!(ec = MemoryBuffer::getSTDIN(MB))) {
+ *OutMemBuf = wrap(MB.take());
return 0;
}