[llvm-commits] [llvm] r121379 - in /llvm/trunk: include/llvm/Support/ lib/Archive/ lib/AsmParser/ lib/Linker/ lib/Object/ lib/Support/ lib/VMCore/ tools/llvm-bcanalyzer/ tools/llvm-dis/ tools/llvm-mc/ tools/llvm-nm/ tools/llvm-prof/ tools/macho-dump/ utils/FileCheck/ utils/TableGen/

Michael J. Spencer bigcheesegs at gmail.com
Thu Dec 9 09:36:48 PST 2010


Author: mspencer
Date: Thu Dec  9 11:36:48 2010
New Revision: 121379

URL: http://llvm.org/viewvc/llvm-project?rev=121379&view=rev
Log:
Support/MemoryBuffer: Replace all uses of std::string *ErrMsg with error_code &ec. And fix clients.

Modified:
    llvm/trunk/include/llvm/Support/IRReader.h
    llvm/trunk/include/llvm/Support/MemoryBuffer.h
    llvm/trunk/include/llvm/Support/system_error.h
    llvm/trunk/lib/Archive/Archive.cpp
    llvm/trunk/lib/Archive/ArchiveWriter.cpp
    llvm/trunk/lib/AsmParser/Parser.cpp
    llvm/trunk/lib/Linker/LinkItems.cpp
    llvm/trunk/lib/Linker/Linker.cpp
    llvm/trunk/lib/Object/ObjectFile.cpp
    llvm/trunk/lib/Support/CommandLine.cpp
    llvm/trunk/lib/Support/FileUtilities.cpp
    llvm/trunk/lib/Support/MemoryBuffer.cpp
    llvm/trunk/lib/Support/SourceMgr.cpp
    llvm/trunk/lib/Support/system_error.cpp
    llvm/trunk/lib/VMCore/Core.cpp
    llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
    llvm/trunk/tools/llvm-dis/llvm-dis.cpp
    llvm/trunk/tools/llvm-mc/llvm-mc.cpp
    llvm/trunk/tools/llvm-nm/llvm-nm.cpp
    llvm/trunk/tools/llvm-prof/llvm-prof.cpp
    llvm/trunk/tools/macho-dump/macho-dump.cpp
    llvm/trunk/utils/FileCheck/FileCheck.cpp
    llvm/trunk/utils/TableGen/TableGen.cpp

Modified: llvm/trunk/include/llvm/Support/IRReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/IRReader.h?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/IRReader.h (original)
+++ llvm/trunk/include/llvm/Support/IRReader.h Thu Dec  9 11:36:48 2010
@@ -23,6 +23,7 @@
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/system_error.h"
 
 namespace llvm {
 
@@ -56,11 +57,11 @@
   inline Module *getLazyIRFileModule(const std::string &Filename,
                                      SMDiagnostic &Err,
                                      LLVMContext &Context) {
-    std::string ErrMsg;
-    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
+    error_code ec;
+    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, 
-                         "Could not open input file: " + ErrMsg);
+      Err = SMDiagnostic(Filename,
+                         "Could not open input file: " + ec.message());
       return 0;
     }
 
@@ -94,11 +95,11 @@
   inline Module *ParseIRFile(const std::string &Filename,
                              SMDiagnostic &Err,
                              LLVMContext &Context) {
-    std::string ErrMsg;
-    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
+    error_code ec;
+    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, 
-                         "Could not open input file: " + ErrMsg);
+      Err = SMDiagnostic(Filename,
+                         "Could not open input file: " + ec.message());
       return 0;
     }
 

Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Thu Dec  9 11:36:48 2010
@@ -16,10 +16,11 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/DataTypes.h"
-#include <string>
 
 namespace llvm {
 
+class error_code;
+
 /// MemoryBuffer - This interface provides simple read-only access to a block
 /// of memory, and provides simple methods for reading files and standard input
 /// into a memory buffer.  In addition to basic access to the characters in the
@@ -46,8 +47,8 @@
   const char *getBufferEnd() const   { return BufferEnd; }
   size_t getBufferSize() const { return BufferEnd-BufferStart; }
 
-  StringRef getBuffer() const { 
-    return StringRef(BufferStart, getBufferSize()); 
+  StringRef getBuffer() const {
+    return StringRef(BufferStart, getBufferSize());
   }
 
   /// getBufferIdentifier - Return an identifier for this buffer, typically the
@@ -60,16 +61,16 @@
   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
   /// specified, this means that the client knows that the file exists and that
   /// it has the specified size.
-  static MemoryBuffer *getFile(StringRef Filename, std::string *ErrStr = 0,
+  static MemoryBuffer *getFile(StringRef Filename, error_code &ec,
                                int64_t FileSize = -1);
-  static MemoryBuffer *getFile(const char *Filename, std::string *ErrStr = 0,
+  static MemoryBuffer *getFile(const char *Filename, error_code &ec,
                                int64_t FileSize = -1);
 
   /// getOpenFile - Given an already-open file descriptor, read the file and
   /// return a MemoryBuffer.  This takes ownership of the descriptor,
   /// immediately closing it after reading the file.
   static MemoryBuffer *getOpenFile(int FD, const char *Filename,
-                                   std::string *ErrStr = 0,
+                                   error_code &ec,
                                    int64_t FileSize = -1);
   
   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
@@ -97,18 +98,18 @@
                                              StringRef BufferName = "");
 
   /// getSTDIN - Read all of stdin into a file buffer, and return it.
-  /// If an error occurs, this returns null and fills in *ErrStr with a reason.
-  static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
+  /// If an error occurs, this returns null and sets ec.
+  static MemoryBuffer *getSTDIN(error_code &ec);
 
 
   /// 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.
+  /// if the Filename is "-".  If an error occurs, this returns null and sets
+  /// ec.
   static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
-                                      std::string *ErrStr = 0,
+                                      error_code &ec,
                                       int64_t FileSize = -1);
   static MemoryBuffer *getFileOrSTDIN(const char *Filename,
-                                      std::string *ErrStr = 0,
+                                      error_code &ec,
                                       int64_t FileSize = -1);
 };
 

Modified: llvm/trunk/include/llvm/Support/system_error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/system_error.h?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/system_error.h (original)
+++ llvm/trunk/include/llvm/Support/system_error.h Thu Dec  9 11:36:48 2010
@@ -668,6 +668,11 @@
 const error_category& generic_category();
 const error_category& system_category();
 
+/// Get the error_category used for errno values from POSIX functions. This is
+/// the same as the system_category on POISIX systems, but is the same as the
+/// generic_category on Windows.
+const error_category& posix_category();
+
 class error_condition
 {
   int _val_;

Modified: llvm/trunk/lib/Archive/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/Archive.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Archive/Archive.cpp (original)
+++ llvm/trunk/lib/Archive/Archive.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
 
 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 @@
                              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;
   }
   

Modified: llvm/trunk/lib/Archive/ArchiveWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/ArchiveWriter.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Archive/ArchiveWriter.cpp (original)
+++ llvm/trunk/lib/Archive/ArchiveWriter.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
   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 @@
 
     // 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 

Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Thu Dec  9 11:36:48 2010
@@ -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::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;
   }
 

Modified: llvm/trunk/lib/Linker/LinkItems.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkItems.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkItems.cpp (original)
+++ llvm/trunk/lib/Linker/LinkItems.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
   // 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 @@
             return false;
       }
     }
-    return error("Cannot link stdin: " + Error);
+    return error("Cannot link stdin: " + ec.message());
   }
 
   // Determine what variety of file it is.

Modified: llvm/trunk/lib/Linker/Linker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/Linker.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/Linker.cpp (original)
+++ llvm/trunk/lib/Linker/Linker.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
   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);

Modified: llvm/trunk/lib/Object/ObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/ObjectFile.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Object/ObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/ObjectFile.cpp Thu Dec  9 11:36:48 2010
@@ -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(StringRef ObjectPath) {
-  return createObjectFile(MemoryBuffer::getFile(ObjectPath));
+  error_code ec;
+  return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
 }

Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
       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.

Modified: llvm/trunk/lib/Support/FileUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileUtilities.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileUtilities.cpp (original)
+++ llvm/trunk/lib/Support/FileUtilities.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
 
   // 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();

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
 /// 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 @@
 };
 }
 
-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 @@
 #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 @@
     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 *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 @@
       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::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 @@
     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);

Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
 /// ~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;

Modified: llvm/trunk/lib/Support/system_error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/system_error.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/Support/system_error.cpp (original)
+++ llvm/trunk/lib/Support/system_error.cpp Thu Dec  9 11:36:48 2010
@@ -96,6 +96,15 @@
   return s;
 }
 
+const error_category&
+posix_category() {
+#ifdef LLVM_ON_WIN32
+  return generic_category();
+#else
+  return system_category();
+#endif
+}
+
 // error_condition
 
 std::string

Modified: llvm/trunk/lib/VMCore/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Core.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Core.cpp (original)
+++ llvm/trunk/lib/VMCore/Core.cpp Thu Dec  9 11:36:48 2010
@@ -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 @@
     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;
 }
 

Modified: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Thu Dec  9 11:36:48 2010
@@ -38,6 +38,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include <cstdio>
 #include <map>
 #include <algorithm>
@@ -473,10 +474,12 @@
 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
 static int AnalyzeBitcode() {
   // Read the input file.
-  MemoryBuffer *MemBuf = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
+  error_code ec;
+  MemoryBuffer *MemBuf =
+    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
 
   if (MemBuf == 0)
-    return Error("Error reading '" + InputFilename + "'.");
+    return Error("Error reading '" + InputFilename + "': " + ec.message());
 
   if (MemBuf->getBufferSize() & 3)
     return Error("Bitcode stream should be a multiple of 4 bytes in length");

Modified: llvm/trunk/tools/llvm-dis/llvm-dis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dis/llvm-dis.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dis/llvm-dis.cpp (original)
+++ llvm/trunk/tools/llvm-dis/llvm-dis.cpp Thu Dec  9 11:36:48 2010
@@ -28,6 +28,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 using namespace llvm;
 
 static cl::opt<std::string>
@@ -78,13 +79,14 @@
   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
 
   std::string ErrorMessage;
+  error_code ec;
   std::auto_ptr<Module> M;
- 
-  if (MemoryBuffer *Buffer
-         = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
+
+  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec)) {
     M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
     delete Buffer;
-  }
+  } else
+    ErrorMessage = ec.message();
 
   if (M.get() == 0) {
     errs() << argv[0] << ": ";

Modified: llvm/trunk/tools/llvm-mc/llvm-mc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/llvm-mc.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Thu Dec  9 11:36:48 2010
@@ -37,6 +37,7 @@
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include "Disassembler.h"
 using namespace llvm;
 
@@ -164,15 +165,10 @@
 }
 
 static int AsLexInput(const char *ProgName) {
-  std::string ErrorMessage;
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
-                                                      &ErrorMessage);
+  error_code ec;
+  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
   if (Buffer == 0) {
-    errs() << ProgName << ": ";
-    if (ErrorMessage.size())
-      errs() << ErrorMessage << "\n";
-    else
-      errs() << "input file didn't read correctly.\n";
+    errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
 
@@ -282,14 +278,10 @@
   if (!TheTarget)
     return 1;
 
-  std::string Error;
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, &Error);
+  error_code ec;
+  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
   if (Buffer == 0) {
-    errs() << ProgName << ": ";
-    if (Error.size())
-      errs() << Error << "\n";
-    else
-      errs() << "input file didn't read correctly.\n";
+    errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
   
@@ -383,18 +375,11 @@
   const Target *TheTarget = GetTarget(ProgName);
   if (!TheTarget)
     return 0;
-  
-  std::string ErrorMessage;
-  
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
-                                                      &ErrorMessage);
 
+  error_code ec;
+  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
   if (Buffer == 0) {
-    errs() << ProgName << ": ";
-    if (ErrorMessage.size())
-      errs() << ErrorMessage << "\n";
-    else
-      errs() << "input file didn't read correctly.\n";
+    errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
   

Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Thu Dec  9 11:36:48 2010
@@ -26,6 +26,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include <algorithm>
 #include <cctype>
 #include <cerrno>
@@ -143,8 +144,11 @@
   sys::Path aPath(Filename);
   // Note: Currently we do not support reading an archive from stdin.
   if (Filename == "-" || aPath.isBitcodeFile()) {
+    error_code ec;
     std::auto_ptr<MemoryBuffer> Buffer(
-                   MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
+                   MemoryBuffer::getFileOrSTDIN(Filename, ec));
+    if (Buffer.get() == 0)
+      ErrorMessage = ec.message();
     Module *Result = 0;
     if (Buffer.get())
       Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);

Modified: llvm/trunk/tools/llvm-prof/llvm-prof.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-prof/llvm-prof.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-prof/llvm-prof.cpp (original)
+++ llvm/trunk/tools/llvm-prof/llvm-prof.cpp Thu Dec  9 11:36:48 2010
@@ -30,6 +30,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include <algorithm>
 #include <iomanip>
 #include <map>
@@ -263,12 +264,13 @@
 
   // Read in the bitcode file...
   std::string ErrorMessage;
+  error_code ec;
   Module *M = 0;
-  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
-                                                          &ErrorMessage)) {
+  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, ec)) {
     M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
     delete Buffer;
-  }
+  } else
+    ErrorMessage = ec.message();
   if (M == 0) {
     errs() << argv[0] << ": " << BitcodeFile << ": "
       << ErrorMessage << "\n";

Modified: llvm/trunk/tools/macho-dump/macho-dump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/macho-dump/macho-dump.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (original)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp Thu Dec  9 11:36:48 2010
@@ -19,6 +19,7 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
 using namespace llvm;
 using namespace llvm::object;
 
@@ -365,10 +366,11 @@
 
   // Load the input file.
   std::string ErrorStr;
+  error_code ec;
   OwningPtr<MemoryBuffer> InputBuffer(
-    MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
+    MemoryBuffer::getFileOrSTDIN(InputFile, ec));
   if (!InputBuffer)
-    return Error("unable to read input: '" + ErrorStr + "'");
+    return Error("unable to read input: '" + ec.message() + "'");
 
   // Construct the Mach-O wrapper object.
   OwningPtr<MachOObject> InputObject(

Modified: llvm/trunk/utils/FileCheck/FileCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileCheck/FileCheck.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/utils/FileCheck/FileCheck.cpp (original)
+++ llvm/trunk/utils/FileCheck/FileCheck.cpp Thu Dec  9 11:36:48 2010
@@ -23,6 +23,7 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
 #include <algorithm>
@@ -488,12 +489,11 @@
 static bool ReadCheckFile(SourceMgr &SM,
                           std::vector<CheckString> &CheckStrings) {
   // Open the check file, and tell SourceMgr about it.
-  std::string ErrorStr;
-  MemoryBuffer *F =
-    MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
+  error_code ec;
+  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), ec);
   if (F == 0) {
     errs() << "Could not open check file '" << CheckFilename << "': "
-           << ErrorStr << '\n';
+           << ec.message() << '\n';
     return true;
   }
 
@@ -648,12 +648,11 @@
     return 2;
 
   // Open the file to check and add it to SourceMgr.
-  std::string ErrorStr;
-  MemoryBuffer *F =
-    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
+  error_code ec;
+  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
   if (F == 0) {
     errs() << "Could not open input file '" << InputFilename << "': "
-           << ErrorStr << '\n';
+           << ec.message() << '\n';
     return true;
   }
 

Modified: llvm/trunk/utils/TableGen/TableGen.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TableGen.cpp?rev=121379&r1=121378&r2=121379&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/TableGen.cpp (original)
+++ llvm/trunk/utils/TableGen/TableGen.cpp Thu Dec  9 11:36:48 2010
@@ -42,6 +42,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include <algorithm>
 #include <cstdio>
 using namespace llvm;
@@ -188,11 +189,11 @@
 static bool ParseFile(const std::string &Filename,
                       const std::vector<std::string> &IncludeDirs,
                       SourceMgr &SrcMgr) {
-  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) {
     errs() << "Could not open input file '" << Filename << "': "
-           << ErrorStr <<"\n";
+           << ec.message() <<"\n";
     return true;
   }
 





More information about the llvm-commits mailing list