From 27ce2fb639e83073f5065538edbe482aed51f5db Mon Sep 17 00:00:00 2001 From: Redbeanw44602 Date: Tue, 25 Mar 2025 14:05:05 +0800 Subject: [PATCH] refactor: move `check_llvm*` to `util/llvm_error.h`. --- src/error.h | 53 ------------------------------------ src/object_file/coff.cpp | 1 - src/object_file/coff.h | 2 ++ src/object_file/pdb.cpp | 2 ++ src/util/llvm_error.h | 58 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 src/util/llvm_error.h diff --git a/src/error.h b/src/error.h index 77f9dd9..9410c67 100644 --- a/src/error.h +++ b/src/error.h @@ -1,7 +1,5 @@ #pragma once -#include - namespace di { class BaseException { @@ -131,55 +129,4 @@ public: constexpr std::string category() const { return "exception.enumconvert"; } }; -class LLVMException : public RuntimeException { -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 -constexpr T -check_llvm_result(Expected 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 -constexpr T& -check_llvm_result(Expected 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 diff --git a/src/object_file/coff.cpp b/src/object_file/coff.cpp index 74bb281..e55f444 100644 --- a/src/object_file/coff.cpp +++ b/src/object_file/coff.cpp @@ -1,5 +1,4 @@ #include "object_file/coff.h" -#include "error.h" namespace di::object_file { diff --git a/src/object_file/coff.h b/src/object_file/coff.h index 3faa1fd..682633f 100644 --- a/src/object_file/coff.h +++ b/src/object_file/coff.h @@ -2,6 +2,8 @@ #include +#include "util/llvm_error.h" + namespace di::object_file { class COFF { diff --git a/src/object_file/pdb.cpp b/src/object_file/pdb.cpp index aaa54f7..19f98a4 100644 --- a/src/object_file/pdb.cpp +++ b/src/object_file/pdb.cpp @@ -16,6 +16,8 @@ #include #include +#include "util/llvm_error.h" + using namespace llvm::pdb; using namespace llvm::codeview; diff --git a/src/util/llvm_error.h b/src/util/llvm_error.h new file mode 100644 index 0000000..19874e7 --- /dev/null +++ b/src/util/llvm_error.h @@ -0,0 +1,58 @@ +#pragma once + +#include + +namespace di { + +class LLVMException : public RuntimeException { +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 +constexpr T +check_llvm_result(Expected 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 +constexpr T& +check_llvm_result(Expected 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 \ No newline at end of file