llvm.org GIT mirror llvm / fb1af0a
Pass a std::unique_ptr<MemoryBuffer>& to getLazyBitcodeModule. By taking a reference we can do the ownership transfer in one place instead of expecting every caller to do it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216492 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
8 changed file(s) with 22 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
1515
1616 #include "llvm/Support/ErrorOr.h"
1717 #include "llvm/Support/MemoryBuffer.h"
18 #include
1819 #include
1920
2021 namespace llvm {
2829 /// Read the header of the specified bitcode buffer and prepare for lazy
2930 /// deserialization of function bodies. If successful, this takes ownership
3031 /// of 'buffer. On error, this *does not* take ownership of Buffer.
31 ErrorOr getLazyBitcodeModule(MemoryBuffer *Buffer,
32 ErrorOr getLazyBitcodeModule(std::unique_ptr &Buffer,
3233 LLVMContext &Context);
3334
3435 /// getStreamedBitcodeModule - Read the header of the specified stream
5050 LLVMModuleRef *OutM,
5151 char **OutMessage) {
5252 std::string Message;
53 std::unique_ptr Owner(unwrap(MemBuf));
54
5355 ErrorOr ModuleOrErr =
54 getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
56 getLazyBitcodeModule(Owner, *unwrap(ContextRef));
57 Owner.release();
5558
5659 if (std::error_code EC = ModuleOrErr.getError()) {
5760 *OutM = wrap((Module *)nullptr);
35183518 ///
35193519 /// \param[in] WillMaterializeAll Set to \c true if the caller promises to
35203520 /// materialize everything -- in particular, if this isn't truly lazy.
3521 static ErrorOr getLazyBitcodeModuleImpl(MemoryBuffer *Buffer,
3522 LLVMContext &Context,
3523 bool WillMaterializeAll) {
3521 static ErrorOr
3522 getLazyBitcodeModuleImpl(std::unique_ptr &Buffer,
3523 LLVMContext &Context, bool WillMaterializeAll) {
35243524 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
3525 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3525 BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
35263526 M->setMaterializer(R);
35273527
35283528 auto cleanupOnError = [&](std::error_code EC) {
35393539 if (std::error_code EC = R->materializeForwardReferencedFunctions())
35403540 return cleanupOnError(EC);
35413541
3542 Buffer.release(); // The BitcodeReader owns it now.
35423543 return M;
35433544 }
35443545
3545 ErrorOr llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3546 LLVMContext &Context) {
3546 ErrorOr
3547 llvm::getLazyBitcodeModule(std::unique_ptr &Buffer,
3548 LLVMContext &Context) {
35473549 return getLazyBitcodeModuleImpl(Buffer, Context, false);
35483550 }
35493551
35663568 ErrorOr llvm::parseBitcodeFile(MemoryBufferRef Buffer,
35673569 LLVMContext &Context) {
35683570 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
3569 ErrorOr ModuleOrErr =
3570 getLazyBitcodeModuleImpl(Buf.get(), Context, true);
3571 ErrorOr ModuleOrErr = getLazyBitcodeModuleImpl(Buf, Context, true);
35713572 if (!ModuleOrErr)
35723573 return ModuleOrErr;
3573 Buf.release(); // The BitcodeReader owns it now.
35743574 Module *M = ModuleOrErr.get();
35753575 // Read in the entire module, and destroy the BitcodeReader.
35763576 if (std::error_code EC = M->materializeAllPermanently()) {
3434 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
3535 (const unsigned char *)Buffer->getBufferEnd())) {
3636 std::string ErrMsg;
37 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer.get(), Context);
37 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
3838 if (std::error_code EC = ModuleOrErr.getError()) {
3939 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
4040 EC.message());
4141 return nullptr;
4242 }
43 // getLazyBitcodeModule takes ownership of the Buffer when successful.
44 Buffer.release();
4543 return std::unique_ptr(ModuleOrErr.get());
4644 }
4745
269269
270270 std::unique_ptr Buff(MemoryBuffer::getMemBuffer(Object, false));
271271
272 ErrorOr MOrErr = getLazyBitcodeModule(Buff.get(), Context);
272 ErrorOr MOrErr = getLazyBitcodeModule(Buff, Context);
273273 if (std::error_code EC = MOrErr.getError())
274274 return EC;
275 Buff.release();
276275
277276 std::unique_ptr M(MOrErr.get());
278277 return new IRObjectFile(Object, std::move(M));
551551 if (release_input_file(F.handle) != LDPS_OK)
552552 message(LDPL_FATAL, "Failed to release file information");
553553
554 ErrorOr MOrErr = getLazyBitcodeModule(Buffer.get(), Context);
554 ErrorOr MOrErr = getLazyBitcodeModule(Buffer, Context);
555555
556556 if (std::error_code EC = MOrErr.getError())
557557 message(LDPL_FATAL, "Could not read bitcode from file : %s",
558558 EC.message().c_str());
559 Buffer.release();
560559
561560 std::unique_ptr M(MOrErr.get());
562561
5050 SmallString<1024> &Mem,
5151 const char *Assembly) {
5252 writeModuleToBuffer(parseAssembly(Assembly), Mem);
53 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
53 std::unique_ptr Buffer(
54 MemoryBuffer::getMemBuffer(Mem.str(), "test", false));
5455 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
5556 return std::unique_ptr(ModuleOrErr.get());
5657 }
631631 ExecutionEngine *getJITFromBitcode(
632632 LLVMContext &Context, const std::string &Bitcode, Module *&M) {
633633 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
634 MemoryBuffer *BitcodeBuffer =
635 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
634 std::unique_ptr BitcodeBuffer(
635 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"));
636636 ErrorOr ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
637637 if (std::error_code EC = ModuleOrErr.getError()) {
638638 ADD_FAILURE() << EC.message();
639 delete BitcodeBuffer;
640639 return nullptr;
641640 }
642641 std::unique_ptr Owner(ModuleOrErr.get());