[BOLT][NFC] Add BOLTError and return it from passes (1/2) (#81522)

As part of the effort to refactor old error handling code that
would directly call exit(1), in this patch we add a new class
BOLTError and auxiliary functions `createFatalBOLTError()` and
`createNonFatalBOLTError()` that allow BOLT code to bubble up the
problem to the caller by using the Error class as a return
type (or Expected). Also changes passes to use these.

Co-authored-by: Rafael Auler <rafaelauler@fb.com>

Test Plan: NFC
This commit is contained in:
Amir Ayupov
2024-02-12 14:39:59 -08:00
committed by GitHub
parent a5f3d1a803
commit fa7dd4919a
8 changed files with 71 additions and 15 deletions

View File

@@ -83,6 +83,37 @@ cl::opt<std::string> CompDirOverride(
namespace llvm {
namespace bolt {
char BOLTError::ID = 0;
BOLTError::BOLTError(bool IsFatal, const Twine &S)
: IsFatal(IsFatal), Msg(S.str()) {}
void BOLTError::log(raw_ostream &OS) const {
if (IsFatal)
OS << "FATAL ";
StringRef ErrMsg = StringRef(Msg);
// Prepend our error prefix if it is missing
if (ErrMsg.empty()) {
OS << "BOLT-ERROR\n";
} else {
if (!ErrMsg.starts_with("BOLT-ERROR"))
OS << "BOLT-ERROR: ";
OS << ErrMsg << "\n";
}
}
std::error_code BOLTError::convertToErrorCode() const {
return inconvertibleErrorCode();
}
Error createNonFatalBOLTError(const Twine &S) {
return make_error<BOLTError>(/*IsFatal*/ false, S);
}
Error createFatalBOLTError(const Twine &S) {
return make_error<BOLTError>(/*IsFatal*/ true, S);
}
BinaryContext::BinaryContext(std::unique_ptr<MCContext> Ctx,
std::unique_ptr<DWARFContext> DwCtx,
std::unique_ptr<Triple> TheTriple,