From 3ff9563c3e391954b2e224afcf8b2b0fcc3888aa Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 16 Dec 2010 03:29:14 +0000 Subject: [PATCH] MemoryBuffer now return an error_code and returns a OwningPtr via an out parm. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121958 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/IRReader.h | 15 +++-- include/llvm/Support/MemoryBuffer.h | 30 +++++----- lib/Archive/Archive.cpp | 14 ++--- lib/Archive/ArchiveWriter.cpp | 11 ++-- lib/AsmParser/Parser.cpp | 7 +-- lib/Linker/LinkItems.cpp | 7 +-- lib/Linker/Linker.cpp | 10 ++-- lib/Object/ObjectFile.cpp | 7 ++- lib/Support/CommandLine.cpp | 9 +-- lib/Support/FileUtilities.cpp | 8 +-- lib/Support/MemoryBuffer.cpp | 72 ++++++++++++----------- lib/Support/SourceMgr.cpp | 9 +-- lib/VMCore/Core.cpp | 10 ++-- tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 10 ++-- tools/llvm-dis/llvm-dis.cpp | 9 +-- tools/llvm-mc/llvm-mc.cpp | 23 ++++---- tools/llvm-nm/llvm-nm.cpp | 6 +- tools/llvm-prof/llvm-prof.cpp | 6 +- tools/lto/LTOCodeGenerator.cpp | 6 +- tools/lto/LTOModule.cpp | 14 ++--- tools/macho-dump/macho-dump.cpp | 6 +- utils/FileCheck/FileCheck.cpp | 15 +++-- utils/FileUpdate/FileUpdate.cpp | 12 ++-- utils/TableGen/TableGen.cpp | 7 ++- 24 files changed, 161 insertions(+), 162 deletions(-) diff --git a/include/llvm/Support/IRReader.h b/include/llvm/Support/IRReader.h index a2002ef2241..292c001e09f 100644 --- a/include/llvm/Support/IRReader.h +++ b/include/llvm/Support/IRReader.h @@ -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 @@ namespace llvm { 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 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 @@ namespace llvm { 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 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); } } diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index e33483cb181..fa1942341b9 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -20,6 +20,7 @@ namespace llvm { class error_code; +template 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 @@ public: /// 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 &result, + int64_t FileSize = -1); + static error_code getFile(const char *Filename, + OwningPtr &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 &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 @@ public: /// 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 &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 &result, + int64_t FileSize = -1); + static error_code getFileOrSTDIN(const char *Filename, + OwningPtr &result, + int64_t FileSize = -1); }; } // end namespace llvm diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp index 3ce7fbdc948..20d9deec99c 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 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& symbols, std::string* ErrMsg) { - error_code ec; - std::auto_ptr Buffer( - MemoryBuffer::getFileOrSTDIN(fName.c_str(), ec)); - if (!Buffer.get()) { + OwningPtr 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& symbols, std::string* ErrMsg) { // Get the module. - std::auto_ptr Buffer( + OwningPtr 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 e9222c5e094..07516c6aa45 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 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 arch(MemoryBuffer::getFile(TmpArchive.c_str(), ec)); - if (arch == 0) { + OwningPtr 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 a613a8346ab..59fb471f2b9 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 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 7716b61a794..52a0d175a5c 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 M; + OwningPtr 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 9606d067e0a..fba91da5ddd 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 Buffer( - MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec)); - if (Buffer.get()) - Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage); - else + OwningPtr 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(Result); diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index 2a6e086137b..aa483f357f5 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 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 e856509d4f3..373a1a2c52a 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 - 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 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 c9e42e9b38f..5dbabee7a7e 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 F1(MemoryBuffer::getFile(FileA.c_str(), ec)); - if (F1 == 0) { + OwningPtr F1; + if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) { if (Error) *Error = ec.message(); return 2; } - OwningPtr F2(MemoryBuffer::getFile(FileB.c_str(), ec)); - if (F2 == 0) { + OwningPtr 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 0966f9069f2..d3c71b99288 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -35,6 +35,8 @@ #include 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 &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 &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 &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 &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 &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(StringRef(Pages, FileSize), - Filename); + result.reset(GetNamedBuffer( + 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 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 &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, ""); + result.reset(getMemBufferCopy(Buffer, "")); + return success; } diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index 1f62068376b..ef099163c22 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 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 a4c77a26700..6bad2f3f442 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -2221,9 +2221,10 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( LLVMMemoryBufferRef *OutMemBuf, char **OutMessage) { + OwningPtr 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 MB; error_code ec; - if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) { - *OutMemBuf = wrap(MB); + if (!(ec = MemoryBuffer::getSTDIN(MB))) { + *OutMemBuf = wrap(MB.take()); return 0; } diff --git a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp index e6c82a326aa..604477e79cf 100644 --- a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -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 NoHistogram("disable-histogram", static cl::opt 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 @@ static void PrintSize(uint64_t Bits) { /// 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 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) diff --git a/tools/llvm-dis/llvm-dis.cpp b/tools/llvm-dis/llvm-dis.cpp index 781bca95eae..7a72cec753c 100644 --- a/tools/llvm-dis/llvm-dis.cpp +++ b/tools/llvm-dis/llvm-dis.cpp @@ -81,12 +81,13 @@ int main(int argc, char **argv) { std::string ErrorMessage; error_code ec; std::auto_ptr M; + OwningPtr 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] << ": "; diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 6bf4931cac3..87082047eef 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -168,12 +168,12 @@ static tool_output_file *GetOutputStream() { } static int AsLexInput(const char *ProgName) { - error_code ec; - MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec); - if (Buffer == 0) { + OwningPtr 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 @@ static int AssembleInput(const char *ProgName) { if (!TheTarget) return 1; - error_code ec; - MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec); - if (Buffer == 0) { + OwningPtr 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 @@ static int DisassembleInput(const char *ProgName, bool Enhanced) { if (!TheTarget) return 0; - error_code ec; - MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec); - if (Buffer == 0) { + OwningPtr Buffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) { errs() << ProgName << ": " << ec.message() << '\n'; return 1; } @@ -400,9 +399,11 @@ static int DisassembleInput(const char *ProgName, bool Enhanced) { 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(); diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 16615964a30..7daf792c718 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -144,10 +144,8 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { 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 Buffer( - MemoryBuffer::getFileOrSTDIN(Filename, ec)); - if (Buffer.get() == 0) + OwningPtr Buffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) ErrorMessage = ec.message(); Module *Result = 0; if (Buffer.get()) diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp index aa9cf004c74..9d0b46833be 100644 --- a/tools/llvm-prof/llvm-prof.cpp +++ b/tools/llvm-prof/llvm-prof.cpp @@ -264,11 +264,11 @@ int main(int argc, char **argv) { // Read in the bitcode file... std::string ErrorMessage; + OwningPtr 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) { diff --git a/tools/lto/LTOCodeGenerator.cpp b/tools/lto/LTOCodeGenerator.cpp index d49e6a77b30..439bac19f66 100644 --- a/tools/lto/LTOCodeGenerator.cpp +++ b/tools/lto/LTOCodeGenerator.cpp @@ -224,10 +224,10 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) delete _nativeObjectFile; // read .o file into memory buffer - error_code ec; - _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(), ec); - if (ec) + OwningPtr BuffPtr; + if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr)) errMsg = ec.message(); + _nativeObjectFile = BuffPtr.take(); } // remove temp files diff --git a/tools/lto/LTOModule.cpp b/tools/lto/LTOModule.cpp index a33b8b473e2..59c8b3517ff 100644 --- a/tools/lto/LTOModule.cpp +++ b/tools/lto/LTOModule.cpp @@ -57,18 +57,17 @@ bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, bool LTOModule::isBitcodeFileForTarget(const char *path, const char *triplePrefix) { - error_code ec; - MemoryBuffer *buffer = MemoryBuffer::getFile(path, ec); - if (buffer == NULL) + OwningPtr 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(Module *m, TargetMachine *t) LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) { - error_code ec; - OwningPtr buffer(MemoryBuffer::getFile(path, ec)); - if (!buffer) { + OwningPtr buffer; + if (error_code ec = MemoryBuffer::getFile(path, buffer)) { errMsg = ec.message(); return NULL; } diff --git a/tools/macho-dump/macho-dump.cpp b/tools/macho-dump/macho-dump.cpp index f4854bec470..c4c558d9acd 100644 --- a/tools/macho-dump/macho-dump.cpp +++ b/tools/macho-dump/macho-dump.cpp @@ -366,10 +366,8 @@ int main(int argc, char **argv) { // Load the input file. std::string ErrorStr; - error_code ec; - OwningPtr InputBuffer( - MemoryBuffer::getFileOrSTDIN(InputFile, ec)); - if (!InputBuffer) + OwningPtr InputBuffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) return Error("unable to read input: '" + ec.message() + "'"); // Construct the Mach-O wrapper object. diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp index b8c14f0e9b5..a4aa693448b 100644 --- a/utils/FileCheck/FileCheck.cpp +++ b/utils/FileCheck/FileCheck.cpp @@ -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 MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { static bool ReadCheckFile(SourceMgr &SM, std::vector &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 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 @@ int main(int argc, char **argv) { 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 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) diff --git a/utils/FileUpdate/FileUpdate.cpp b/utils/FileUpdate/FileUpdate.cpp index 3514d0f2157..3ea1e4f306e 100644 --- a/utils/FileUpdate/FileUpdate.cpp +++ b/utils/FileUpdate/FileUpdate.cpp @@ -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 @@ int main(int argc, char **argv) { } // Get the input data. - error_code ec; - MemoryBuffer *In = - MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec); - if (In == 0) { + OwningPtr 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 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 @@ int main(int argc, char **argv) { return 0; } - delete Out; - // Otherwise, overwrite the output. if (!Quiet) errs() << argv[0] << ": Updating '" << OutputFilename diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index f206d2b03ea..61a81416e48 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -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 @@ static bool ParseFile(const std::string &Filename, const std::vector &IncludeDirs, SourceMgr &SrcMgr, RecordKeeper &Records) { - error_code ec; - MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec); - if (F == 0) { + OwningPtr 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());