From: Alp Toker Date: Fri, 27 Jun 2014 04:33:58 +0000 (+0000) Subject: ParseIR: don't take ownership of the MemoryBuffer X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5ebb7b311223bcd21d2b3d25413d1edacefcc63d;p=platform%2Fupstream%2Fllvm.git ParseIR: don't take ownership of the MemoryBuffer clang was needlessly duplicating whole memory buffer contents in an attempt to satisfy unclear ownership semantics. Let's just hide internal LLVM quirks and present a simple non-owning interface. The public C API preserves previous behaviour for stability. llvm-svn: 211861 --- diff --git a/llvm/include/llvm/IRReader/IRReader.h b/llvm/include/llvm/IRReader/IRReader.h index e2ae5f7..20022cf 100644 --- a/llvm/include/llvm/IRReader/IRReader.h +++ b/llvm/include/llvm/IRReader/IRReader.h @@ -40,9 +40,9 @@ Module *getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err, /// If the given MemoryBuffer holds a bitcode image, return a Module /// for it. Otherwise, attempt to parse it as LLVM Assembly and return -/// a Module for it. This function *always* takes ownership of the given -/// MemoryBuffer. -Module *ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, LLVMContext &Context); +/// a Module for it. This function *never* takes ownership of Buffer. +Module *ParseIR(const MemoryBuffer *Buffer, SMDiagnostic &Err, + LLVMContext &Context); /// If the given file holds a bitcode image, return a Module for it. /// Otherwise, attempt to parse it as LLVM Assembly and return a Module diff --git a/llvm/lib/AsmParser/Parser.cpp b/llvm/lib/AsmParser/Parser.cpp index 067cac8..2138fde 100644 --- a/llvm/lib/AsmParser/Parser.cpp +++ b/llvm/lib/AsmParser/Parser.cpp @@ -54,8 +54,7 @@ Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err, Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, SMDiagnostic &Err, LLVMContext &Context) { MemoryBuffer *F = - MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)), - ""); + MemoryBuffer::getMemBuffer(StringRef(AsmString), ""); return ParseAssembly(F, M, Err, Context); } diff --git a/llvm/lib/IRReader/IRReader.cpp b/llvm/lib/IRReader/IRReader.cpp index 01aa074..2bbe075 100644 --- a/llvm/lib/IRReader/IRReader.cpp +++ b/llvm/lib/IRReader/IRReader.cpp @@ -39,7 +39,7 @@ Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err, if (std::error_code EC = ModuleOrErr.getError()) { Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, EC.message()); - // ParseBitcodeFile does not take ownership of the Buffer in the + // getLazyBitcodeModule does not take ownership of the Buffer in the // case of an error. delete Buffer; return nullptr; @@ -62,13 +62,14 @@ Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err return getLazyIRModule(File.release(), Err, Context); } -Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, +Module *llvm::ParseIR(const MemoryBuffer *Buffer, SMDiagnostic &Err, LLVMContext &Context) { NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName, TimePassesIsEnabled); if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { - ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context); + ErrorOr ModuleOrErr = + parseBitcodeFile(const_cast(Buffer), Context); Module *M = nullptr; if (std::error_code EC = ModuleOrErr.getError()) Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, @@ -76,11 +77,12 @@ Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, else M = ModuleOrErr.get(); // parseBitcodeFile does not take ownership of the Buffer. - delete Buffer; return M; } - return ParseAssembly(Buffer, nullptr, Err, Context); + return ParseAssembly(MemoryBuffer::getMemBuffer( + Buffer->getBuffer(), Buffer->getBufferIdentifier()), + nullptr, Err, Context); } Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, @@ -92,7 +94,7 @@ Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, return nullptr; } - return ParseIR(File.release(), Err, Context); + return ParseIR(File.get(), Err, Context); } //===----------------------------------------------------------------------===// @@ -104,7 +106,8 @@ LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef, char **OutMessage) { SMDiagnostic Diag; - *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef))); + std::unique_ptr MB(unwrap(MemBuf)); + *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef))); if(!*OutM) { if (OutMessage) {