refactor: move check_llvm* to util/llvm_error.h.

This commit is contained in:
2025-03-25 14:05:05 +08:00
parent fb368a30a0
commit 27ce2fb639
5 changed files with 62 additions and 54 deletions

View File

@@ -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

View File

@@ -1,5 +1,4 @@
#include "object_file/coff.h"
#include "error.h"
namespace di::object_file {

View File

@@ -2,6 +2,8 @@
#include <llvm/Object/COFF.h>
#include "util/llvm_error.h"
namespace di::object_file {
class COFF {

View File

@@ -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
View 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