Make parseBitcodeFile return an ErrorOr<Module *>.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199279 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-01-15 01:08:23 +00:00
parent cd94a7676a
commit 0be7e6ffb1
5 changed files with 30 additions and 39 deletions

View File

@ -50,11 +50,10 @@ namespace llvm {
LLVMContext &Context,
std::string *ErrMsg = 0);
/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
/// If an error occurs, this returns null and fills in *ErrMsg if it is
/// non-null. This method *never* takes ownership of Buffer.
Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
std::string *ErrMsg = 0);
/// Read the specified bitcode file, returning the module.
/// This method *never* takes ownership of Buffer.
ErrorOr<Module *> parseBitcodeFile(MemoryBuffer *Buffer,
LLVMContext &Context);
/// WriteBitcodeToFile - Write the specified module to the specified
/// raw output stream. For streams where it matters, the given stream

View File

@ -30,16 +30,16 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
LLVMMemoryBufferRef MemBuf,
LLVMModuleRef *OutModule,
char **OutMessage) {
std::string Message;
*OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
&Message));
if (!*OutModule) {
ErrorOr<Module *> ModuleOrErr =
parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef));
if (error_code EC = ModuleOrErr.getError()) {
if (OutMessage)
*OutMessage = strdup(Message.c_str());
*OutMessage = strdup(EC.message().c_str());
*OutModule = wrap((Module*)0);
return 1;
}
*OutModule = wrap(ModuleOrErr.get());
return 0;
}

View File

@ -3342,16 +3342,11 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
return M;
}
/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
/// If an error occurs, return null and fill in *ErrMsg if non-null.
Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
std::string *ErrMsg){
ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
LLVMContext &Context) {
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
if (error_code EC = ModuleOrErr.getError()) {
if (ErrMsg)
*ErrMsg = EC.message();
return 0;
}
if (!ModuleOrErr)
return ModuleOrErr;
Module *M = ModuleOrErr.get();
// Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
@ -3360,10 +3355,8 @@ Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
// Read in the entire module, and destroy the BitcodeReader.
if (error_code EC = M->materializeAllPermanently()) {
if (ErrMsg)
*ErrMsg = EC.message();
delete M;
return 0;
return EC;
}
// TODO: Restore the use-lists to the in-memory state when the bitcode was

View File

@ -69,12 +69,14 @@ Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
TimePassesIsEnabled);
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) {
std::string ErrMsg;
Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
if (M == 0)
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
Module *M = 0;
if (error_code EC = ModuleOrErr.getError())
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
ErrMsg);
// ParseBitcodeFile does not take ownership of the Buffer.
EC.message());
else
M = ModuleOrErr.get();
// parseBitcodeFile does not take ownership of the Buffer.
delete Buffer;
return M;
}

View File

@ -570,16 +570,14 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
LLVMContext &Context = getGlobalContext();
std::string ErrorMessage;
if (magic == sys::fs::file_magic::bitcode) {
Module *Result = 0;
Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
if (Result) {
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer.get(), Context);
if (error(ModuleOrErr.getError(), Filename)) {
return;
} else {
Module *Result = ModuleOrErr.get();
DumpSymbolNamesFromModule(Result);
delete Result;
} else {
error(ErrorMessage, Filename);
return;
}
} else if (magic == sys::fs::file_magic::archive) {
OwningPtr<Binary> arch;
@ -616,11 +614,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
OwningPtr<MemoryBuffer> buff;
if (error(i->getMemoryBuffer(buff)))
return;
Module *Result = 0;
if (buff)
Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
if (Result) {
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(buff.get(), Context);
if (ModuleOrErr) {
Module *Result = ModuleOrErr.get();
DumpSymbolNamesFromModule(Result);
delete Result;
}