llvm.org GIT mirror llvm / 0be7e6f
Make parseBitcodeFile return an ErrorOr<Module *>. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199279 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
5 changed file(s) with 31 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
4949 LLVMContext &Context,
5050 std::string *ErrMsg = 0);
5151
52 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
53 /// If an error occurs, this returns null and fills in *ErrMsg if it is
54 /// non-null. This method *never* takes ownership of Buffer.
55 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
56 std::string *ErrMsg = 0);
52 /// Read the specified bitcode file, returning the module.
53 /// This method *never* takes ownership of Buffer.
54 ErrorOr parseBitcodeFile(MemoryBuffer *Buffer,
55 LLVMContext &Context);
5756
5857 /// WriteBitcodeToFile - Write the specified module to the specified
5958 /// raw output stream. For streams where it matters, the given stream
2929 LLVMMemoryBufferRef MemBuf,
3030 LLVMModuleRef *OutModule,
3131 char **OutMessage) {
32 std::string Message;
33
34 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
35 &Message));
36 if (!*OutModule) {
32 ErrorOr ModuleOrErr =
33 parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef));
34 if (error_code EC = ModuleOrErr.getError()) {
3735 if (OutMessage)
38 *OutMessage = strdup(Message.c_str());
36 *OutMessage = strdup(EC.message().c_str());
37 *OutModule = wrap((Module*)0);
3938 return 1;
4039 }
4140
41 *OutModule = wrap(ModuleOrErr.get());
4242 return 0;
4343 }
4444
33413341 return M;
33423342 }
33433343
3344 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3345 /// If an error occurs, return null and fill in *ErrMsg if non-null.
3346 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
3347 std::string *ErrMsg){
3344 ErrorOr llvm::parseBitcodeFile(MemoryBuffer *Buffer,
3345 LLVMContext &Context) {
33483346 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
3349 if (error_code EC = ModuleOrErr.getError()) {
3350 if (ErrMsg)
3351 *ErrMsg = EC.message();
3352 return 0;
3353 }
3347 if (!ModuleOrErr)
3348 return ModuleOrErr;
33543349 Module *M = ModuleOrErr.get();
33553350
33563351 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
33593354
33603355 // Read in the entire module, and destroy the BitcodeReader.
33613356 if (error_code EC = M->materializeAllPermanently()) {
3362 if (ErrMsg)
3363 *ErrMsg = EC.message();
33643357 delete M;
3365 return 0;
3358 return EC;
33663359 }
33673360
33683361 // TODO: Restore the use-lists to the in-memory state when the bitcode was
6868 TimePassesIsEnabled);
6969 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
7070 (const unsigned char *)Buffer->getBufferEnd())) {
71 std::string ErrMsg;
72 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
73 if (M == 0)
71 ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context);
72 Module *M = 0;
73 if (error_code EC = ModuleOrErr.getError())
7474 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
75 ErrMsg);
76 // ParseBitcodeFile does not take ownership of the Buffer.
75 EC.message());
76 else
77 M = ModuleOrErr.get();
78 // parseBitcodeFile does not take ownership of the Buffer.
7779 delete Buffer;
7880 return M;
7981 }
569569 sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
570570
571571 LLVMContext &Context = getGlobalContext();
572 std::string ErrorMessage;
573572 if (magic == sys::fs::file_magic::bitcode) {
574 Module *Result = 0;
575 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
576 if (Result) {
573 ErrorOr ModuleOrErr = parseBitcodeFile(Buffer.get(), Context);
574 if (error(ModuleOrErr.getError(), Filename)) {
575 return;
576 } else {
577 Module *Result = ModuleOrErr.get();
577578 DumpSymbolNamesFromModule(Result);
578579 delete Result;
579 } else {
580 error(ErrorMessage, Filename);
581 return;
582580 }
583581 } else if (magic == sys::fs::file_magic::archive) {
584582 OwningPtr arch;
615613 OwningPtr buff;
616614 if (error(i->getMemoryBuffer(buff)))
617615 return;
618 Module *Result = 0;
619 if (buff)
620 Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
621
622 if (Result) {
616
617 ErrorOr ModuleOrErr = parseBitcodeFile(buff.get(), Context);
618 if (ModuleOrErr) {
619 Module *Result = ModuleOrErr.get();
623620 DumpSymbolNamesFromModule(Result);
624621 delete Result;
625622 }