[llvm] Fix error handling of llvm::Expected<T>.
@@ -121,12 +121,24 @@ std::unique_ptr<llvm::Module> makeModule(llvm::LLVMContext& context, const Bitco
const std::string& name, Status* status) {
llvm::MemoryBufferRef buffer(llvm::StringRef(bitcode.data(), bitcode.size()), name);
VLOG(3) << "llvm::parseBitcodeFile(" << bitcode.size() << " bits)";
+
llvm::Expected<std::unique_ptr<llvm::Module>> moduleOrError =
llvm::parseBitcodeFile(buffer, context);
- if (moduleOrError) {
+ if (auto error = moduleOrError.takeError()) {
+ *status = Status(StatusCode::INVALID_ARGUMENT,
+ fmt::format("Failed to parse LLVM bitcode: \"{}\"", name));
+ return nullptr;
+ }
+
*status = Status::OK;
std::unique_ptr<llvm::Module> module = std::move(moduleOrError.get());
+ if (!module) {
+ *status = Status(StatusCode::INTERNAL,
+ "llvm::parseBitcodeFile return null");
+ return nullptr;
+ }
+
// Strip the module identifiers and source file names from the module to
// anonymize them. This is to deter learning algorithms from overfitting to
// benchmarks by their name.
@@ -143,11 +155,6 @@ std::unique_ptr<llvm::Module> makeModule(llvm::LLVMContext& context, const Bitco
}
return module;
- } else {
- *status = Status(StatusCode::INVALID_ARGUMENT,
- fmt::format("Failed to parse LLVM bitcode: \"{}\"", name));
- return nullptr;
- }
}
// A benchmark is an LLVM module and the LLVM context that owns it.
