Remove BitcodeReader::setBufferOwned.

We do have use cases for the bitcode reader owning the buffer or not, but we
always know which one we have when we construct it.

It might be possible to simplify this further, but this is a step in the
right direction.

llvm-svn: 211205
This commit is contained in:
Rafael Espindola 2014-06-18 18:55:41 +00:00
parent b2ad56effc
commit a1ea4ccc06
3 changed files with 12 additions and 23 deletions

View File

@ -30,7 +30,8 @@ namespace llvm {
/// deserialization of function bodies. If successful, this takes ownership
/// of 'buffer. On error, this *does not* take ownership of Buffer.
ErrorOr<Module *> getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext &Context);
LLVMContext &Context,
bool BufferOwned = true);
/// getStreamedBitcodeModule - Read the header of the specified stream
/// and prepare for lazy deserialization and streaming of function bodies.

View File

@ -3374,16 +3374,15 @@ const std::error_category &BitcodeReader::BitcodeErrorCategory() {
/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
///
ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext &Context) {
LLVMContext &Context,
bool BufferOwned) {
Module *M = new Module(Buffer->getBufferIdentifier(), Context);
BitcodeReader *R = new BitcodeReader(Buffer, Context);
BitcodeReader *R = new BitcodeReader(Buffer, Context, BufferOwned);
M->setMaterializer(R);
if (std::error_code EC = R->ParseBitcodeInto(M)) {
delete M; // Also deletes R.
return EC;
}
// Have the BitcodeReader dtor delete 'Buffer'.
R->setBufferOwned(true);
R->materializeForwardReferencedFunctions();
@ -3404,21 +3403,16 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
delete M; // Also deletes R.
return nullptr;
}
R->setBufferOwned(false); // no buffer to delete
return M;
}
ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
LLVMContext &Context) {
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context, false);
if (!ModuleOrErr)
return ModuleOrErr;
Module *M = ModuleOrErr.get();
// Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
// there was an error.
static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
// Read in the entire module, and destroy the BitcodeReader.
if (std::error_code EC = M->materializeAllPermanently()) {
delete M;
@ -3434,9 +3428,7 @@ ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
LLVMContext& Context,
std::string *ErrMsg) {
BitcodeReader *R = new BitcodeReader(Buffer, Context);
// Don't let the BitcodeReader dtor delete 'Buffer'.
R->setBufferOwned(false);
BitcodeReader *R = new BitcodeReader(Buffer, Context, /*BufferOwned*/ false);
std::string Triple("");
if (std::error_code EC = R->ParseTriple(Triple))

View File

@ -223,11 +223,11 @@ public:
return std::error_code(E, BitcodeErrorCategory());
}
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
: Context(C), TheModule(nullptr), Buffer(buffer), BufferOwned(false),
LazyStreamer(nullptr), NextUnreadBit(0), SeenValueSymbolTable(false),
ValueList(C), MDValueList(C), SeenFirstFunctionBody(false),
UseRelativeIDs(false) {}
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, bool BufferOwned)
: Context(C), TheModule(nullptr), Buffer(buffer),
BufferOwned(BufferOwned), LazyStreamer(nullptr), NextUnreadBit(0),
SeenValueSymbolTable(false), ValueList(C), MDValueList(C),
SeenFirstFunctionBody(false), UseRelativeIDs(false) {}
explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
: Context(C), TheModule(nullptr), Buffer(nullptr), BufferOwned(false),
LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false),
@ -239,10 +239,6 @@ public:
void FreeState();
/// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
/// when the reader is destroyed.
void setBufferOwned(bool Owned) { BufferOwned = Owned; }
bool isMaterializable(const GlobalValue *GV) const override;
bool isDematerializable(const GlobalValue *GV) const override;
std::error_code Materialize(GlobalValue *GV) override;