Enhance MemoryBuffer to return error messages in strings if they occur.
authorChris Lattner <sabre@nondot.org>
Sun, 6 May 2007 23:32:36 +0000 (23:32 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 6 May 2007 23:32:36 +0000 (23:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36899 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/MemoryBuffer.h
lib/Support/MemoryBuffer.cpp

index 80672165f7f9149f42093728a21827d386cc708c..d7e03170e21b9e847ed6d293c54025ecfb4a53b7 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_SUPPORT_MEMORYBUFFER_H
 
 #include "llvm/Support/DataTypes.h"
+#include <string>
 
 namespace llvm {
 
@@ -52,6 +53,7 @@ public:
   /// specified, this means that the client knows that the file exists and that
   /// it has the specified size.
   static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
+                               std::string *ErrStr = 0,
                                int64_t FileSize = -1);
 
   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
@@ -79,14 +81,24 @@ public:
   
   
   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
-  /// if the Filename is "-".
+  /// if the Filename is "-".  If an error occurs, this returns null and fills
+  /// in *ErrStr with a reason.
   static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
+                                      std::string *ErrStr = 0,
                                       int64_t FileSize = -1) {
     if (FnSize == 1 && FilenameStart[0] == '-')
       return getSTDIN();
-    return getFile(FilenameStart, FnSize, FileSize);
+    return getFile(FilenameStart, FnSize, ErrStr, FileSize);
   }
   
+  /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
+  /// if the Filename is "-".  If an error occurs, this returns null and fills
+  /// in *ErrStr with a reason.
+  static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
+                                      std::string *ErrStr = 0,
+                                      int64_t FileSize = -1) {
+    return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
+  }
 };
 
 } // end namespace llvm
index ffbb4beccfab891fd1f76982ee23ade58a87913a..b2c561c77a0798d2f797be4c58696fba2e4d0a68 100644 (file)
@@ -113,7 +113,7 @@ class MemoryBufferMMapFile : public MemoryBuffer {
 public:
   MemoryBufferMMapFile() {}
   
-  bool open(const sys::Path &Filename);
+  bool open(const sys::Path &Filename, std::string *ErrStr);
   
   virtual const char *getBufferIdentifier() const {
     return File.path().c_str();
@@ -123,13 +123,15 @@ public:
 };
 }
 
-bool MemoryBufferMMapFile::open(const sys::Path &Filename) {
+bool MemoryBufferMMapFile::open(const sys::Path &Filename,
+                                std::string *ErrStr) {
   // FIXME: This does an extra stat syscall to figure out the size, but we
   // already know the size!
-  bool Failure = File.open(Filename);
+  bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
   if (Failure) return true;
   
-  File.map();
+  if (!File.map(ErrStr))
+    return true;
   
   size_t Size = File.size();
   
@@ -161,11 +163,13 @@ MemoryBufferMMapFile::~MemoryBufferMMapFile() {
 //===----------------------------------------------------------------------===//
 
 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
-                                    int64_t FileSize) {
+                                    std::string *ErrStr, int64_t FileSize){
+  // FIXME: it would be nice if PathWithStatus didn't copy the filename into a
+  // temporary string. :(
   sys::PathWithStatus P(FilenameStart, FnSize);
 #if 1
   MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
-  if (!M->open(P))
+  if (!M->open(P, ErrStr))
     return M;
   delete M;
   return 0;
@@ -186,7 +190,7 @@ 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) {
     MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
-    if (!M->open(P))
+    if (!M->open(P, ErrStr))
       return M;
     delete M;
     return 0;