llvm.org GIT mirror llvm / 2292996
Pass a MemoryBufferRef when we can avoid taking ownership. The attached patch simplifies a few interfaces that don't need to take ownership of a buffer. For example, both parseAssembly and parseBitcodeFile will parse the entire buffer before returning. There is no need to take ownership. Using a MemoryBufferRef makes it obvious in the type signature that there is no ownership transfer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216488 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
16 changed file(s) with 58 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_ASMPARSER_PARSER_H
1414 #define LLVM_ASMPARSER_PARSER_H
1515
16 #include "llvm/ADT/StringRef.h"
17 #include
16 #include "llvm/Support/MemoryBuffer.h"
1817
1918 namespace llvm {
2019
2120 class Module;
22 class MemoryBuffer;
2321 class SMDiagnostic;
2422 class LLVMContext;
2523
5452 /// @param F The MemoryBuffer containing assembly
5553 /// @param Err Error result info.
5654 /// @param Context Context in which to allocate globals info.
57 std::unique_ptr parseAssembly(std::unique_ptr F,
58 SMDiagnostic &Err, LLVMContext &Context);
55 std::unique_ptr parseAssembly(MemoryBufferRef F, SMDiagnostic &Err,
56 LLVMContext &Context);
5957
6058 /// This function is the low-level interface to the LLVM Assembly Parser.
6159 /// This is kept as an independent function instead of being inlined into
6664 /// @param M The module to add data to.
6765 /// @param Err Error result info.
6866 /// @return true on error.
69 bool parseAssemblyInto(std::unique_ptr F, Module &M,
70 SMDiagnostic &Err);
67 bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err);
7168
7269 } // End llvm namespace
7370
1414 #define LLVM_BITCODE_READERWRITER_H
1515
1616 #include "llvm/Support/ErrorOr.h"
17 #include "llvm/Support/MemoryBuffer.h"
1718 #include
1819
1920 namespace llvm {
2021 class BitstreamWriter;
21 class MemoryBuffer;
2222 class DataStreamer;
2323 class LLVMContext;
2424 class Module;
4141 std::string *ErrMsg = nullptr);
4242
4343 /// Read the header of the specified bitcode buffer and extract just the
44 /// triple information. If successful, this returns a string and *does not*
45 /// take ownership of 'buffer'. On error, this returns "".
46 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
44 /// triple information. If successful, this returns a string. On error, this
45 /// returns "".
46 std::string getBitcodeTargetTriple(MemoryBufferRef Buffer,
4747 LLVMContext &Context);
4848
4949 /// Read the specified bitcode file, returning the module.
50 /// This method *never* takes ownership of Buffer.
51 ErrorOr parseBitcodeFile(MemoryBuffer *Buffer,
50 ErrorOr parseBitcodeFile(MemoryBufferRef Buffer,
5251 LLVMContext &Context);
5352
5453 /// WriteBitcodeToFile - Write the specified module to the specified
5353 /// Make sure the entire Module has been completely read.
5454 ///
5555 virtual std::error_code MaterializeModule(Module *M) = 0;
56
57 virtual void releaseBuffer() = 0;
5856 };
5957
6058 } // End llvm namespace
482482 /// Make sure all GlobalValues in this Module are fully read and clear the
483483 /// Materializer. If the module is corrupt, this DOES NOT clear the old
484484 /// Materializer.
485 std::error_code materializeAllPermanently(bool ReleaseBuffer = false);
485 std::error_code materializeAllPermanently();
486486
487487 /// @}
488488 /// @name Direct access to the globals list, functions list, and symbol table
1414 #ifndef LLVM_IRREADER_IRREADER_H
1515 #define LLVM_IRREADER_IRREADER_H
1616
17 #include "llvm/ADT/StringRef.h"
18 #include
17 #include "llvm/Support/MemoryBuffer.h"
1918 #include
2019
2120 namespace llvm {
2221
2322 class Module;
24 class MemoryBuffer;
2523 class SMDiagnostic;
2624 class LLVMContext;
2725
3533
3634 /// If the given MemoryBuffer holds a bitcode image, return a Module
3735 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
38 /// a Module for it. This function *never* takes ownership of Buffer.
39 std::unique_ptr parseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
36 /// a Module for it.
37 std::unique_ptr parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
4038 LLVMContext &Context);
4139
4240 /// If the given file holds a bitcode image, return a Module for it.
101101 StringRef BufferName = "",
102102 bool RequiresNullTerminator = true);
103103
104 static std::unique_ptr
105 getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
106
104107 /// Open the specified memory range as a MemoryBuffer, copying the contents
105108 /// and taking ownership of it. InputData does not have to be null terminated.
106109 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
2020 #include
2121 using namespace llvm;
2222
23 bool llvm::parseAssemblyInto(std::unique_ptr F, Module &M,
24 SMDiagnostic &Err) {
23 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
2524 SourceMgr SM;
26 StringRef Buf = F->getBuffer();
27 SM.AddNewSourceBuffer(std::move(F), SMLoc());
25 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(F, false);
26 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
2827
29 return LLParser(Buf, SM, Err, &M).Run();
28 return LLParser(F.getBuffer(), SM, Err, &M).Run();
3029 }
3130
32 std::unique_ptr llvm::parseAssembly(std::unique_ptr F,
31 std::unique_ptr llvm::parseAssembly(MemoryBufferRef F,
3332 SMDiagnostic &Err,
3433 LLVMContext &Context) {
3534 std::unique_ptr M =
36 make_unique(F->getBufferIdentifier(), Context);
35 make_unique(F.getBufferIdentifier(), Context);
3736
38 if (parseAssemblyInto(std::move(F), *M, Err))
37 if (parseAssemblyInto(F, *M, Err))
3938 return nullptr;
4039
4140 return std::move(M);
5251 return nullptr;
5352 }
5453
55 return parseAssembly(std::move(FileOrErr.get()), Err, Context);
54 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context);
5655 }
5756
5857 std::unique_ptr llvm::parseAssemblyString(StringRef AsmString,
5958 SMDiagnostic &Err,
6059 LLVMContext &Context) {
61 std::unique_ptr F(
62 MemoryBuffer::getMemBuffer(AsmString, ""));
63
64 return parseAssembly(std::move(F), Err, Context);
60 MemoryBufferRef F(AsmString, "");
61 return parseAssembly(F, Err, Context);
6562 }
3030 LLVMModuleRef *OutModule,
3131 char **OutMessage) {
3232 ErrorOr ModuleOrErr =
33 parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef));
33 parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef));
3434 if (std::error_code EC = ModuleOrErr.getError()) {
3535 if (OutMessage)
3636 *OutMessage = strdup(EC.message().c_str());
35633563 return M;
35643564 }
35653565
3566 ErrorOr llvm::parseBitcodeFile(MemoryBuffer *Buffer,
3566 ErrorOr llvm::parseBitcodeFile(MemoryBufferRef Buffer,
35673567 LLVMContext &Context) {
3568 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
35683569 ErrorOr ModuleOrErr =
3569 getLazyBitcodeModuleImpl(Buffer, Context, true);
3570 getLazyBitcodeModuleImpl(Buf.get(), Context, true);
35703571 if (!ModuleOrErr)
35713572 return ModuleOrErr;
3573 Buf.release(); // The BitcodeReader owns it now.
35723574 Module *M = ModuleOrErr.get();
35733575 // Read in the entire module, and destroy the BitcodeReader.
3574 if (std::error_code EC = M->materializeAllPermanently(true)) {
3576 if (std::error_code EC = M->materializeAllPermanently()) {
35753577 delete M;
35763578 return EC;
35773579 }
35823584 return M;
35833585 }
35843586
3585 std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3587 std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
35863588 LLVMContext &Context) {
3587 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3589 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
3590 auto R = llvm::make_unique(Buf.get(), Context);
35883591 ErrorOr Triple = R->parseTriple();
3589 R->releaseBuffer();
3590 delete R;
35913592 if (Triple.getError())
35923593 return "";
35933594 return Triple.get();
220220
221221 void FreeState();
222222
223 void releaseBuffer() override;
223 void releaseBuffer();
224224
225225 bool isMaterializable(const GlobalValue *GV) const override;
226226 bool isDematerializable(const GlobalValue *GV) const override;
412412 return Materializer->MaterializeModule(this);
413413 }
414414
415 std::error_code Module::materializeAllPermanently(bool ReleaseBuffer) {
415 std::error_code Module::materializeAllPermanently() {
416416 if (std::error_code EC = materializeAll())
417417 return EC;
418
419 if (ReleaseBuffer)
420 Materializer->releaseBuffer();
421418
422419 Materializer.reset();
423420 return std::error_code();
4545 return std::unique_ptr(ModuleOrErr.get());
4646 }
4747
48 return parseAssembly(std::move(Buffer), Err, Context);
48 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
4949 }
5050
5151 std::unique_ptr llvm::getLazyIRFileModule(StringRef Filename,
6262 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
6363 }
6464
65 std::unique_ptr llvm::parseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
65 std::unique_ptr llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
6666 LLVMContext &Context) {
6767 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
6868 TimePassesIsEnabled);
69 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70 (const unsigned char *)Buffer->getBufferEnd())) {
69 if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
70 (const unsigned char *)Buffer.getBufferEnd())) {
7171 ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context);
7272 if (std::error_code EC = ModuleOrErr.getError()) {
73 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
73 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
7474 EC.message());
7575 return nullptr;
7676 }
7777 return std::unique_ptr(ModuleOrErr.get());
7878 }
7979
80 return parseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer(
81 Buffer->getBuffer(), Buffer->getBufferIdentifier())),
82 Err, Context);
80 return parseAssembly(Buffer, Err, Context);
8381 }
8482
8583 std::unique_ptr llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
9290 return nullptr;
9391 }
9492
95 return parseIR(FileOrErr.get().get(), Err, Context);
93 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
9694 }
9795
9896 //===----------------------------------------------------------------------===//
105103 SMDiagnostic Diag;
106104
107105 std::unique_ptr MB(unwrap(MemBuf));
108 *OutM = wrap(parseIR(MB.get(), Diag, *unwrap(ContextRef)).release());
106 *OutM =
107 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
109108
110109 if(!*OutM) {
111110 if (OutMessage) {
6464
6565 bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
6666 StringRef triplePrefix) {
67 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
67 std::string Triple =
68 getBitcodeTargetTriple(buffer->getMemBufferRef(), getGlobalContext());
6869 return StringRef(Triple).startswith(triplePrefix);
6970 }
7071
111112 LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
112113 TargetOptions options,
113114 std::string &errMsg) {
114 StringRef Data = Buffer.getBuffer();
115 StringRef FileName = Buffer.getBufferIdentifier();
116 std::unique_ptr MemBuf(
117 makeBuffer(Data.begin(), Data.size(), FileName));
118 if (!MemBuf)
119 return nullptr;
120
121 ErrorOr MOrErr = parseBitcodeFile(MemBuf.get(), getGlobalContext());
115 ErrorOr MOrErr = parseBitcodeFile(Buffer, getGlobalContext());
122116 if (std::error_code EC = MOrErr.getError()) {
123117 errMsg = EC.message();
124118 return nullptr;
267267 llvm::object::IRObjectFile::createIRObjectFile(MemoryBufferRef Object,
268268 LLVMContext &Context) {
269269
270 StringRef Data = Object.getBuffer();
271 StringRef FileName = Object.getBufferIdentifier();
272 std::unique_ptr Buff(
273 MemoryBuffer::getMemBuffer(Data, FileName, false));
270 std::unique_ptr Buff(MemoryBuffer::getMemBuffer(Object, false));
274271
275272 ErrorOr MOrErr = getLazyBitcodeModule(Buff.get(), Context);
276273 if (std::error_code EC = MOrErr.getError())
102102 MemoryBufferMem(InputData, RequiresNullTerminator);
103103 }
104104
105 std::unique_ptr
106 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
107 return std::unique_ptr(getMemBuffer(
108 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
109 }
110
105111 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
106112 /// copying the contents and taking ownership of it. This has no requirements
107113 /// on EndPtr[0].
156156 }
157157
158158 MemoryBuffer *Buffer = BufferOr.get().get();
159 ErrorOr ModuleOr = parseBitcodeFile(Buffer, Context);
159 ErrorOr ModuleOr =
160 parseBitcodeFile(Buffer->getMemBufferRef(), Context);
160161 if (!ModuleOr) {
161162 DEBUG(dbgs() << "error: " << ModuleOr.getError().message() << "\n");
162163 return nullptr;