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

View File

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

View File

@ -3342,16 +3342,11 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
return M; return M;
} }
/// ParseBitcodeFile - Read the specified bitcode file, returning the module. ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer,
/// If an error occurs, return null and fill in *ErrMsg if non-null. LLVMContext &Context) {
Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
std::string *ErrMsg){
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
if (error_code EC = ModuleOrErr.getError()) { if (!ModuleOrErr)
if (ErrMsg) return ModuleOrErr;
*ErrMsg = EC.message();
return 0;
}
Module *M = ModuleOrErr.get(); Module *M = ModuleOrErr.get();
// Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether // 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. // Read in the entire module, and destroy the BitcodeReader.
if (error_code EC = M->materializeAllPermanently()) { if (error_code EC = M->materializeAllPermanently()) {
if (ErrMsg)
*ErrMsg = EC.message();
delete M; delete M;
return 0; return EC;
} }
// TODO: Restore the use-lists to the in-memory state when the bitcode was // 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); TimePassesIsEnabled);
if (isBitcode((const unsigned char *)Buffer->getBufferStart(), if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) { (const unsigned char *)Buffer->getBufferEnd())) {
std::string ErrMsg; ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg); Module *M = 0;
if (M == 0) if (error_code EC = ModuleOrErr.getError())
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
ErrMsg); EC.message());
// ParseBitcodeFile does not take ownership of the Buffer. else
M = ModuleOrErr.get();
// parseBitcodeFile does not take ownership of the Buffer.
delete Buffer; delete Buffer;
return M; 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()); sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
LLVMContext &Context = getGlobalContext(); LLVMContext &Context = getGlobalContext();
std::string ErrorMessage;
if (magic == sys::fs::file_magic::bitcode) { if (magic == sys::fs::file_magic::bitcode) {
Module *Result = 0; ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer.get(), Context);
Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); if (error(ModuleOrErr.getError(), Filename)) {
if (Result) { return;
} else {
Module *Result = ModuleOrErr.get();
DumpSymbolNamesFromModule(Result); DumpSymbolNamesFromModule(Result);
delete Result; delete Result;
} else {
error(ErrorMessage, Filename);
return;
} }
} else if (magic == sys::fs::file_magic::archive) { } else if (magic == sys::fs::file_magic::archive) {
OwningPtr<Binary> arch; OwningPtr<Binary> arch;
@ -616,11 +614,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
OwningPtr<MemoryBuffer> buff; OwningPtr<MemoryBuffer> buff;
if (error(i->getMemoryBuffer(buff))) if (error(i->getMemoryBuffer(buff)))
return; 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); DumpSymbolNamesFromModule(Result);
delete Result; delete Result;
} }