[llvm-commits] [llvm] r121958 - 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/lto/ tools/macho-dump/ utils/FileCheck/ utils/FileUpdate/ utils/TableGen/

Michael J. Spencer bigcheesegs at gmail.com
Wed Dec 15 19:29:14 PST 2010


Author: mspencer
Date: Wed Dec 15 21:29:14 2010
New Revision: 121958

URL: http://llvm.org/viewvc/llvm-project?rev=121958&view=rev
Log:
MemoryBuffer now return an error_code and returns a OwningPtr<MemoryBuffer> via an out parm.

Modified:
    llvm/trunk/include/llvm/Support/IRReader.h
    llvm/trunk/include/llvm/Support/MemoryBuffer.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/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/lto/LTOCodeGenerator.cpp
    llvm/trunk/tools/lto/LTOModule.cpp
    llvm/trunk/tools/macho-dump/macho-dump.cpp
    llvm/trunk/utils/FileCheck/FileCheck.cpp
    llvm/trunk/utils/FileUpdate/FileUpdate.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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/IRReader.h (original)
+++ llvm/trunk/include/llvm/Support/IRReader.h Wed Dec 15 21:29:14 2010
@@ -19,6 +19,7 @@
 #ifndef LLVM_SUPPORT_IRREADER_H
 #define LLVM_SUPPORT_IRREADER_H
 
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Assembly/Parser.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -57,15 +58,14 @@
   inline Module *getLazyIRFileModule(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 getLazyIRModule(F, Err, Context);
+    return getLazyIRModule(File.take(), Err, Context);
   }
 
   /// If the given MemoryBuffer holds a bitcode image, return a Module
@@ -95,15 +95,14 @@
   inline Module *ParseIRFile(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 ParseIR(F, Err, Context);
+    return ParseIR(File.take(), Err, Context);
   }
 
 }

Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Wed Dec 15 21:29:14 2010
@@ -20,6 +20,7 @@
 namespace llvm {
 
 class error_code;
+template<class T> class OwningPtr;
 
 /// MemoryBuffer - This interface provides simple read-only access to a block
 /// of memory, and provides simple methods for reading files and standard input
@@ -61,17 +62,18 @@
   /// 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, error_code &ec,
-                               int64_t FileSize = -1);
-  static MemoryBuffer *getFile(const char *Filename, error_code &ec,
-                               int64_t FileSize = -1);
+  static error_code getFile(StringRef Filename, OwningPtr<MemoryBuffer> &result,
+                            int64_t FileSize = -1);
+  static error_code getFile(const char *Filename,
+                            OwningPtr<MemoryBuffer> &result,
+                            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,
-                                   error_code &ec,
-                                   int64_t FileSize = -1);
+  static error_code getOpenFile(int FD, const char *Filename,
+                                OwningPtr<MemoryBuffer> &result,
+                                int64_t FileSize = -1);
 
   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
   /// that InputData must be null terminated.
@@ -99,18 +101,18 @@
 
   /// getSTDIN - Read all of stdin into a file buffer, and return it.
   /// If an error occurs, this returns null and sets ec.
-  static MemoryBuffer *getSTDIN(error_code &ec);
+  static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
 
 
   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
   /// if the Filename is "-".  If an error occurs, this returns null and sets
   /// ec.
-  static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
-                                      error_code &ec,
-                                      int64_t FileSize = -1);
-  static MemoryBuffer *getFileOrSTDIN(const char *Filename,
-                                      error_code &ec,
-                                      int64_t FileSize = -1);
+  static error_code getFileOrSTDIN(StringRef Filename,
+                                   OwningPtr<MemoryBuffer> &result,
+                                   int64_t FileSize = -1);
+  static error_code getFileOrSTDIN(const char *Filename,
+                                   OwningPtr<MemoryBuffer> &result,
+                                   int64_t FileSize = -1);
 };
 
 } // end namespace llvm

Modified: llvm/trunk/lib/Archive/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/Archive.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Archive/Archive.cpp (original)
+++ llvm/trunk/lib/Archive/Archive.cpp Wed Dec 15 21:29:14 2010
@@ -148,13 +148,13 @@
 
 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 @@
                              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 @@
                         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);

Modified: llvm/trunk/lib/Archive/ArchiveWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/ArchiveWriter.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Archive/ArchiveWriter.cpp (original)
+++ llvm/trunk/lib/Archive/ArchiveWriter.cpp Wed Dec 15 21:29:14 2010
@@ -213,13 +213,13 @@
   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 @@
 
     // 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;

Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Wed Dec 15 21:29:14 2010
@@ -42,15 +42,14 @@
 
 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,

Modified: llvm/trunk/lib/Linker/LinkItems.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkItems.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkItems.cpp (original)
+++ llvm/trunk/lib/Linker/LinkItems.cpp Wed Dec 15 21:29:14 2010
@@ -161,14 +161,13 @@
   // 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;

Modified: llvm/trunk/lib/Linker/Linker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/Linker.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/Linker.cpp (original)
+++ llvm/trunk/lib/Linker/Linker.cpp Wed Dec 15 21:29:14 2010
@@ -99,14 +99,12 @@
   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);

Modified: llvm/trunk/lib/Object/ObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/ObjectFile.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Object/ObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/ObjectFile.cpp Wed Dec 15 21:29:14 2010
@@ -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(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());
 }

Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Wed Dec 15 21:29:14 2010
@@ -464,11 +464,6 @@
       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 @@
         // 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;
         }

Modified: llvm/trunk/lib/Support/FileUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileUtilities.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileUtilities.cpp (original)
+++ llvm/trunk/lib/Support/FileUtilities.cpp Wed Dec 15 21:29:14 2010
@@ -201,14 +201,14 @@
   // 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;

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Wed Dec 15 21:29:14 2010
@@ -35,6 +35,8 @@
 #include <fcntl.h>
 using namespace llvm;
 
+namespace { const llvm::error_code success; }
+
 //===----------------------------------------------------------------------===//
 // MemoryBuffer implementation itself.
 //===----------------------------------------------------------------------===//
@@ -143,20 +145,20 @@
 /// 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 @@
 };
 }
 
-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 @@
     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 @@
   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 @@
   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 @@
       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 @@
     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;
 }

Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Wed Dec 15 21:29:14 2010
@@ -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 @@
 /// ~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);
 }
 
 

Modified: llvm/trunk/lib/VMCore/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Core.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Core.cpp (original)
+++ llvm/trunk/lib/VMCore/Core.cpp Wed Dec 15 21:29:14 2010
@@ -2221,9 +2221,10 @@
     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 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;
   }
 

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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Wed Dec 15 21:29:14 2010
@@ -27,6 +27,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
@@ -58,7 +59,7 @@
 
 static cl::opt<bool>
 NonSymbolic("non-symbolic",
-            cl::desc("Emit numberic info in dump even if"
+            cl::desc("Emit numeric info in dump even if"
                      " symbolic info is available"));
 
 namespace {
@@ -481,11 +482,10 @@
 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
 static int AnalyzeBitcode() {
   // Read the input file.
-  error_code ec;
-  MemoryBuffer *MemBuf =
-    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
+  OwningPtr<MemoryBuffer> MemBuf;
 
-  if (MemBuf == 0)
+  if (error_code ec =
+        MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf))
     return Error("Error reading '" + InputFilename + "': " + ec.message());
 
   if (MemBuf->getBufferSize() & 3)

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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dis/llvm-dis.cpp (original)
+++ llvm/trunk/tools/llvm-dis/llvm-dis.cpp Wed Dec 15 21:29:14 2010
@@ -81,12 +81,13 @@
   std::string ErrorMessage;
   error_code ec;
   std::auto_ptr<Module> M;
+  OwningPtr<MemoryBuffer> BufferPtr;
 
-  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec)) {
-    M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
-    delete Buffer;
-  } else
+  if (ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
     ErrorMessage = ec.message();
+  else
+    M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
+  MemoryBuffer *Buffer = BufferPtr.take();
 
   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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Wed Dec 15 21:29:14 2010
@@ -168,12 +168,12 @@
 }
 
 static int AsLexInput(const char *ProgName) {
-  error_code ec;
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
-  if (Buffer == 0) {
+  OwningPtr<MemoryBuffer> BufferPtr;
+  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
     errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
+  MemoryBuffer *Buffer = BufferPtr.take();
 
   SourceMgr SrcMgr;
   
@@ -281,12 +281,12 @@
   if (!TheTarget)
     return 1;
 
-  error_code ec;
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
-  if (Buffer == 0) {
+  OwningPtr<MemoryBuffer> BufferPtr;
+  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
     errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
+  MemoryBuffer *Buffer = BufferPtr.take();
   
   SourceMgr SrcMgr;
   
@@ -387,9 +387,8 @@
   if (!TheTarget)
     return 0;
 
-  error_code ec;
-  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
-  if (Buffer == 0) {
+  OwningPtr<MemoryBuffer> Buffer;
+  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
     errs() << ProgName << ": " << ec.message() << '\n';
     return 1;
   }
@@ -400,9 +399,11 @@
 
   int Res;
   if (Enhanced)
-    Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, Out->os());
+    Res =
+      Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
   else
-    Res = Disassembler::disassemble(*TheTarget, TripleName, *Buffer, Out->os());
+    Res = Disassembler::disassemble(*TheTarget, TripleName,
+                                    *Buffer.take(), Out->os());
 
   // Keep output if no errors.
   if (Res == 0) Out->keep();

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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Wed Dec 15 21:29:14 2010
@@ -144,10 +144,8 @@
   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, ec));
-    if (Buffer.get() == 0)
+    OwningPtr<MemoryBuffer> Buffer;
+    if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
       ErrorMessage = ec.message();
     Module *Result = 0;
     if (Buffer.get())

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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-prof/llvm-prof.cpp (original)
+++ llvm/trunk/tools/llvm-prof/llvm-prof.cpp Wed Dec 15 21:29:14 2010
@@ -264,11 +264,11 @@
 
   // Read in the bitcode file...
   std::string ErrorMessage;
+  OwningPtr<MemoryBuffer> Buffer;
   error_code ec;
   Module *M = 0;
-  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, ec)) {
-    M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
-    delete Buffer;
+  if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
+    M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
   } else
     ErrorMessage = ec.message();
   if (M == 0) {

Modified: llvm/trunk/tools/lto/LTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOCodeGenerator.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/tools/lto/LTOCodeGenerator.cpp Wed Dec 15 21:29:14 2010
@@ -224,10 +224,10 @@
         delete _nativeObjectFile;
 
         // read .o file into memory buffer
-        error_code ec;
-        _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(), ec);
-        if (ec)
+        OwningPtr<MemoryBuffer> BuffPtr;
+        if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr))
           errMsg = ec.message();
+        _nativeObjectFile = BuffPtr.take();
     }
 
     // remove temp files

Modified: llvm/trunk/tools/lto/LTOModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOModule.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOModule.cpp (original)
+++ llvm/trunk/tools/lto/LTOModule.cpp Wed Dec 15 21:29:14 2010
@@ -57,18 +57,17 @@
 
 bool LTOModule::isBitcodeFileForTarget(const char *path,
                                        const char *triplePrefix) {
-  error_code ec;
-  MemoryBuffer *buffer = MemoryBuffer::getFile(path, ec);
-  if (buffer == NULL)
+  OwningPtr<MemoryBuffer> buffer;
+  if (MemoryBuffer::getFile(path, buffer))
     return false;
-  return isTargetMatch(buffer, triplePrefix);
+  return isTargetMatch(buffer.take(), triplePrefix);
 }
 
 // Takes ownership of buffer.
 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
   std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
   delete buffer;
-  return (strncmp(Triple.c_str(), triplePrefix, 
+  return (strncmp(Triple.c_str(), triplePrefix,
  		  strlen(triplePrefix)) == 0);
 }
 
@@ -80,9 +79,8 @@
 
 LTOModule *LTOModule::makeLTOModule(const char *path,
                                     std::string &errMsg) {
-  error_code ec;
-  OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, ec));
-  if (!buffer) {
+  OwningPtr<MemoryBuffer> buffer;
+  if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
     errMsg = ec.message();
     return NULL;
   }

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=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (original)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp Wed Dec 15 21:29:14 2010
@@ -366,10 +366,8 @@
 
   // Load the input file.
   std::string ErrorStr;
-  error_code ec;
-  OwningPtr<MemoryBuffer> InputBuffer(
-    MemoryBuffer::getFileOrSTDIN(InputFile, ec));
-  if (!InputBuffer)
+  OwningPtr<MemoryBuffer> InputBuffer;
+  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
     return Error("unable to read input: '" + ec.message() + "'");
 
   // Construct the Mach-O wrapper object.

Modified: llvm/trunk/utils/FileCheck/FileCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileCheck/FileCheck.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/utils/FileCheck/FileCheck.cpp (original)
+++ llvm/trunk/utils/FileCheck/FileCheck.cpp Wed Dec 15 21:29:14 2010
@@ -16,6 +16,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -489,13 +490,14 @@
 static bool ReadCheckFile(SourceMgr &SM,
                           std::vector<CheckString> &CheckStrings) {
   // Open the check file, and tell SourceMgr about it.
-  error_code ec;
-  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), ec);
-  if (F == 0) {
+  OwningPtr<MemoryBuffer> File;
+  if (error_code ec =
+        MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
     errs() << "Could not open check file '" << CheckFilename << "': "
            << ec.message() << '\n';
     return true;
   }
+  MemoryBuffer *F = File.take();
 
   // If we want to canonicalize whitespace, strip excess whitespace from the
   // buffer containing the CHECK lines.
@@ -648,13 +650,14 @@
     return 2;
 
   // Open the file to check and add it to SourceMgr.
-  error_code ec;
-  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
-  if (F == 0) {
+  OwningPtr<MemoryBuffer> File;
+  if (error_code ec =
+        MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
     errs() << "Could not open input file '" << InputFilename << "': "
            << ec.message() << '\n';
     return true;
   }
+  MemoryBuffer *F = File.take();
 
   // Remove duplicate spaces in the input file if requested.
   if (!NoCanonicalizeWhiteSpace)

Modified: llvm/trunk/utils/FileUpdate/FileUpdate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileUpdate/FileUpdate.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/utils/FileUpdate/FileUpdate.cpp (original)
+++ llvm/trunk/utils/FileUpdate/FileUpdate.cpp Wed Dec 15 21:29:14 2010
@@ -15,6 +15,7 @@
 
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/Signals.h"
@@ -43,17 +44,16 @@
   }
 
   // Get the input data.
-  error_code ec;
-  MemoryBuffer *In =
-    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
-  if (In == 0) {
+  OwningPtr<MemoryBuffer> In;
+  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), In)) {
     errs() << argv[0] << ": error: Unable to get input '"
            << InputFilename << "': " << ec.message() << '\n';
     return 1;
   }
 
   // Get the output data.
-  MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), ec);
+  OwningPtr<MemoryBuffer> Out;
+  MemoryBuffer::getFile(OutputFilename.c_str(), Out);
 
   // If the output exists and the contents match, we are done.
   if (Out && In->getBufferSize() == Out->getBufferSize() &&
@@ -65,8 +65,6 @@
     return 0;
   }
 
-  delete Out;
-
   // Otherwise, overwrite the output.
   if (!Quiet)
     errs() << argv[0] << ": Updating '" << OutputFilename

Modified: llvm/trunk/utils/TableGen/TableGen.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TableGen.cpp?rev=121958&r1=121957&r2=121958&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/TableGen.cpp (original)
+++ llvm/trunk/utils/TableGen/TableGen.cpp Wed Dec 15 21:29:14 2010
@@ -37,6 +37,7 @@
 #include "ARMDecoderEmitter.h"
 #include "SubtargetEmitter.h"
 #include "TGParser.h"
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -190,13 +191,13 @@
                       const std::vector<std::string> &IncludeDirs,
                       SourceMgr &SrcMgr,
                       RecordKeeper &Records) {
-  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)) {
     errs() << "Could not open input file '" << Filename << "': "
            << ec.message() <<"\n";
     return true;
   }
+  MemoryBuffer *F = File.take();
 
   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
   SrcMgr.AddNewSourceBuffer(F, SMLoc());





More information about the llvm-commits mailing list