refactor: move check_llvm* to util/llvm_error.h.
This commit is contained in:
53
src/error.h
53
src/error.h
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <llvm/Support/Error.h>
|
||||
|
||||
namespace di {
|
||||
|
||||
class BaseException {
|
||||
@@ -131,55 +129,4 @@ public:
|
||||
constexpr std::string category() const { return "exception.enumconvert"; }
|
||||
};
|
||||
|
||||
class LLVMException : public RuntimeException<LLVMException> {
|
||||
public:
|
||||
explicit LLVMException(
|
||||
std::string_view error_message_di = "",
|
||||
std::string_view error_message_llvm = ""
|
||||
)
|
||||
: RuntimeException("There were some problems when calling LLVM.") {
|
||||
if (!error_message_di.empty())
|
||||
add_context("error_message_di", error_message_di);
|
||||
if (!error_message_llvm.empty())
|
||||
add_context("error_message_llvm", error_message_llvm);
|
||||
}
|
||||
|
||||
constexpr std::string category() const { return "exception.llvm"; }
|
||||
};
|
||||
|
||||
constexpr void check_llvm_result(Error err, std::string_view msg = "") {
|
||||
if (err) {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T
|
||||
check_llvm_result(Expected<T> val_or_err, std::string_view msg = "") {
|
||||
if (val_or_err) return std::move(*val_or_err);
|
||||
else {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
auto err = val_or_err.takeError();
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T&
|
||||
check_llvm_result(Expected<T&> val_or_err, std::string_view msg = "") {
|
||||
if (val_or_err) return *val_or_err;
|
||||
else {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
auto err = val_or_err.takeError();
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace di
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "object_file/coff.h"
|
||||
#include "error.h"
|
||||
|
||||
namespace di::object_file {
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <llvm/Object/COFF.h>
|
||||
|
||||
#include "util/llvm_error.h"
|
||||
|
||||
namespace di::object_file {
|
||||
|
||||
class COFF {
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <llvm/DebugInfo/PDB/PDB.h>
|
||||
#include <llvm/DebugInfo/PDB/PDBTypes.h>
|
||||
|
||||
#include "util/llvm_error.h"
|
||||
|
||||
using namespace llvm::pdb;
|
||||
using namespace llvm::codeview;
|
||||
|
||||
|
||||
58
src/util/llvm_error.h
Normal file
58
src/util/llvm_error.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <llvm/Support/Error.h>
|
||||
|
||||
namespace di {
|
||||
|
||||
class LLVMException : public RuntimeException<LLVMException> {
|
||||
public:
|
||||
explicit LLVMException(
|
||||
std::string_view error_message_di = "",
|
||||
std::string_view error_message_llvm = ""
|
||||
)
|
||||
: RuntimeException("There were some problems when calling LLVM.") {
|
||||
if (!error_message_di.empty())
|
||||
add_context("error_message_di", error_message_di);
|
||||
if (!error_message_llvm.empty())
|
||||
add_context("error_message_llvm", error_message_llvm);
|
||||
}
|
||||
|
||||
constexpr std::string category() const { return "exception.llvm"; }
|
||||
};
|
||||
|
||||
constexpr void check_llvm_result(Error err, std::string_view msg = "") {
|
||||
if (err) {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T
|
||||
check_llvm_result(Expected<T> val_or_err, std::string_view msg = "") {
|
||||
if (val_or_err) return std::move(*val_or_err);
|
||||
else {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
auto err = val_or_err.takeError();
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T&
|
||||
check_llvm_result(Expected<T&> val_or_err, std::string_view msg = "") {
|
||||
if (val_or_err) return *val_or_err;
|
||||
else {
|
||||
std::string err_detail;
|
||||
raw_string_ostream os(err_detail);
|
||||
auto err = val_or_err.takeError();
|
||||
os << err;
|
||||
throw LLVMException(msg, err_detail);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace di
|
||||
Reference in New Issue
Block a user