llvm.org GIT mirror llvm / 99c7fec
Update getLazyBitcodeModule to use ErrorOr for error handling. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199125 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
7 changed file(s) with 41 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_BITCODE_READERWRITER_H
1414 #define LLVM_BITCODE_READERWRITER_H
1515
16 #include "llvm/Support/ErrorOr.h"
1617 #include
1718
1819 namespace llvm {
2425 class ModulePass;
2526 class raw_ostream;
2627
27 /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
28 /// and prepare for lazy deserialization of function bodies. If successful,
29 /// this takes ownership of 'buffer' and returns a non-null pointer. On
30 /// error, this returns null, *does not* take ownership of Buffer, and fills
31 /// in *ErrMsg with an error description if ErrMsg is non-null.
32 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
33 LLVMContext &Context,
34 std::string *ErrMsg = 0);
28 /// Read the header of the specified bitcode buffer and prepare for lazy
29 /// deserialization of function bodies. If successful, this takes ownership
30 /// of 'buffer. On error, this *does not* take ownership of Buffer.
31 ErrorOr getLazyBitcodeModule(MemoryBuffer *Buffer,
32 LLVMContext &Context);
3533
3634 /// getStreamedBitcodeModule - Read the header of the specified stream
3735 /// and prepare for lazy deserialization and streaming of function bodies.
5050 LLVMModuleRef *OutM,
5151 char **OutMessage) {
5252 std::string Message;
53 ErrorOr ModuleOrErr =
54 getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
5355
54 *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef),
55 &Message));
56 if (!*OutM) {
56 if (error_code EC = ModuleOrErr.getError()) {
57 *OutM = wrap((Module *)NULL);
5758 if (OutMessage)
58 *OutMessage = strdup(Message.c_str());
59 *OutMessage = strdup(EC.message().c_str());
5960 return 1;
6061 }
62
63 *OutM = wrap(ModuleOrErr.get());
6164
6265 return 0;
6366
32733273
32743274 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
32753275 ///
3276 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3277 LLVMContext& Context,
3278 std::string *ErrMsg) {
3276 ErrorOr llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
3277 LLVMContext &Context) {
32793278 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
32803279 BitcodeReader *R = new BitcodeReader(Buffer, Context);
32813280 M->setMaterializer(R);
32823281 if (error_code EC = R->ParseBitcodeInto(M)) {
3283 if (ErrMsg)
3284 *ErrMsg = EC.message();
3285
32863282 delete M; // Also deletes R.
3287 return 0;
3283 return EC;
32883284 }
32893285 // Have the BitcodeReader dtor delete 'Buffer'.
32903286 R->setBufferOwned(true);
33163312 /// If an error occurs, return null and fill in *ErrMsg if non-null.
33173313 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
33183314 std::string *ErrMsg){
3319 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
3320 if (!M) return 0;
3315 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
3316 if (error_code EC = ModuleOrErr.getError()) {
3317 if (ErrMsg)
3318 *ErrMsg = EC.message();
3319 return 0;
3320 }
3321 Module *M = ModuleOrErr.get();
33213322
33223323 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
33233324 // there was an error.
3535 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
3636 (const unsigned char *)Buffer->getBufferEnd())) {
3737 std::string ErrMsg;
38 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
39 if (M == 0) {
38 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
39 if (error_code EC = ModuleOrErr.getError()) {
4040 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
41 ErrMsg);
41 EC.message());
4242 // ParseBitcodeFile does not take ownership of the Buffer in the
4343 // case of an error.
4444 delete Buffer;
45 return NULL;
4546 }
46 return M;
47 return ModuleOrErr.get();
4748 }
4849
4950 return ParseAssembly(Buffer, 0, Err, Context);
134134 TargetOptions options,
135135 std::string &errMsg) {
136136 // parse bitcode buffer
137 OwningPtr m(getLazyBitcodeModule(buffer, getGlobalContext(),
138 &errMsg));
139 if (!m) {
137 ErrorOr ModuleOrErr =
138 getLazyBitcodeModule(buffer, getGlobalContext());
139 if (error_code EC = ModuleOrErr.getError()) {
140 errMsg = EC.message();
140141 delete buffer;
141142 return NULL;
142143 }
144 OwningPtr m(ModuleOrErr.get());
143145
144146 std::string TripleStr = m->getTargetTriple();
145147 if (TripleStr.empty())
5353 SmallString<1024> Mem;
5454 writeModuleToBuffer(Mem);
5555 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
56 std::string errMsg;
57 OwningPtr m(getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg));
56 ErrorOr ModuleOrErr =
57 getLazyBitcodeModule(Buffer, getGlobalContext());
58 OwningPtr m(ModuleOrErr.get());
5859 PassManager passes;
5960 passes.add(createVerifierPass());
6061 passes.run(*m);
630630 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
631631 MemoryBuffer *BitcodeBuffer =
632632 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
633 std::string errMsg;
634 M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
635 if (M == NULL) {
636 ADD_FAILURE() << errMsg;
633 ErrorOr ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
634 if (error_code EC = ModuleOrErr.getError()) {
635 ADD_FAILURE() << EC.message();
637636 delete BitcodeBuffer;
638637 return NULL;
639638 }
639 M = ModuleOrErr.get();
640 std::string errMsg;
640641 ExecutionEngine *TheJIT = EngineBuilder(M)
641642 .setEngineKind(EngineKind::JIT)
642643 .setErrorStr(&errMsg)