Skip to content

Commit

Permalink
ParseIR: don't take ownership of the MemoryBuffer
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211861 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
atoker committed Jun 27, 2014
1 parent 4935128 commit d0996e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions include/llvm/IRReader/IRReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/AsmParser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
"<string>");
MemoryBuffer::getMemBuffer(StringRef(AsmString), "<string>");

return ParseAssembly(F, M, Err, Context);
}
17 changes: 10 additions & 7 deletions lib/IRReader/IRReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,25 +62,27 @@ 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<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
ErrorOr<Module *> ModuleOrErr =
parseBitcodeFile(const_cast<MemoryBuffer *>(Buffer), Context);
Module *M = nullptr;
if (std::error_code EC = ModuleOrErr.getError())
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
EC.message());
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,
Expand All @@ -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);
}

//===----------------------------------------------------------------------===//
Expand All @@ -104,7 +106,8 @@ LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
char **OutMessage) {
SMDiagnostic Diag;

*OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
*OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));

if(!*OutM) {
if (OutMessage) {
Expand Down

0 comments on commit d0996e5

Please sign in to comment.