summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-05-06 07:24:46 +0000
committerChris Lattner <sabre@nondot.org>2007-05-06 07:24:46 +0000
commit5db36d3d82769e982ba502d8f768bd40449f97ed (patch)
tree5fbf6137bc043b24fc0bc30c6104481a99a45179 /llvm/lib
parent9b35b3e863cc92db6532e0f275f76c5ff1ff470f (diff)
downloadbcm5719-llvm-5db36d3d82769e982ba502d8f768bd40449f97ed.tar.gz
bcm5719-llvm-5db36d3d82769e982ba502d8f768bd40449f97ed.zip
Fix MemoryBuffer::getFile to return null if it has an error opening the
file instead of aborting. llvm-svn: 36858
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Support/MemoryBuffer.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 96035354e52..ffbb4beccfa 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -111,7 +111,9 @@ namespace {
class MemoryBufferMMapFile : public MemoryBuffer {
sys::MappedFile File;
public:
- MemoryBufferMMapFile(const sys::Path &Filename);
+ MemoryBufferMMapFile() {}
+
+ bool open(const sys::Path &Filename);
virtual const char *getBufferIdentifier() const {
return File.path().c_str();
@@ -121,12 +123,11 @@ public:
};
}
-MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path &Filename) {
+bool MemoryBufferMMapFile::open(const sys::Path &Filename) {
// FIXME: This does an extra stat syscall to figure out the size, but we
// already know the size!
bool Failure = File.open(Filename);
- Failure = Failure; // Silence warning in no-asserts mode.
- assert(!Failure && "Can't open file??");
+ if (Failure) return true;
File.map();
@@ -147,10 +148,12 @@ MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path &Filename) {
// No need to keep the file mapped any longer.
File.unmap();
}
+ return false;
}
MemoryBufferMMapFile::~MemoryBufferMMapFile() {
- File.unmap();
+ if (File.isMapped())
+ File.unmap();
}
//===----------------------------------------------------------------------===//
@@ -161,7 +164,11 @@ MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
int64_t FileSize) {
sys::PathWithStatus P(FilenameStart, FnSize);
#if 1
- return new MemoryBufferMMapFile(P);
+ MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
+ if (!M->open(P))
+ return M;
+ delete M;
+ return 0;
#else
// FIXME: We need an efficient and portable method to open a file and then use
// 'read' to copy the bits out. The unix implementation is below. This is
@@ -177,8 +184,13 @@ MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
}
// If the file is larger than some threshold, use mmap, otherwise use 'read'.
- if (FileSize >= 4096*4)
- return new MemoryBufferMMapFile(P);
+ if (FileSize >= 4096*4) {
+ MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
+ if (!M->open(P))
+ return M;
+ delete M;
+ return 0;
+ }
MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
char *BufPtr = const_cast<char*>(SB->getBufferStart());
OpenPOWER on IntegriCloud