Debian Codegraph @

src/utils/error.hpp

raw
1 #ifndef ERROR_HPP
2 #define ERROR_HPP
3
4 #include <exception>
5 #include <string>
6 #include <utility>
7
8 #include "platform/platform.hpp"
9 #include "utils/microfmt.hpp"
10 #include "utils/string_view.hpp"
11
12 #include <cpptrace/utils.hpp>
13
14 #if IS_MSVC
15 #define CPPTRACE_PFUNC __FUNCSIG__
16 #else
17 #define CPPTRACE_PFUNC __extension__ __PRETTY_FUNCTION__
18 #endif
19
20 CPPTRACE_BEGIN_NAMESPACE
21 namespace detail {
22 class internal_error : public std::exception {
23 std::string msg;
24 public:
25 internal_error(std::string message);
26 template<typename... Args>
27 internal_error(const char* format, Args&&... args) : internal_error(microfmt::format(format, args...)) {}
28 const char* what() const noexcept override {
29 return msg.c_str();
30 }
31 };
32
33 // Lightweight std::source_location.
34 struct source_location {
35 const char* const file;
36 const int line;
37 constexpr source_location(
38 const char* _file,
39 int _line
40 ) : file(_file), line(_line) {}
41 };
42
43 #define CPPTRACE_CURRENT_LOCATION ::cpptrace::detail::source_location(__FILE__, __LINE__)
44
45 enum class assert_type {
46 assert,
47 verify,
48 panic,
49 };
50
51 [[noreturn]] CPPTRACE_EXPORT void assert_fail(
52 assert_type type,
53 const char* expression,
54 const char* signature,
55 source_location location,
56 const char* message
57 );
58
59 [[noreturn]] void panic(
60 const char* signature,
61 source_location location,
62 string_view message = ""
63 );
64
65
66 template<typename... Args>
67 void nullfn(Args&&...);
68
69 #define PHONY_USE(...) (static_cast<decltype(nullfn(__VA_ARGS__))>(0))
70
71 // Work around a compiler warning
72 template<typename T>
73 std::string as_string(T&& value) {
74 return std::string(std::forward<T>(value));
75 }
76
77 inline std::string as_string() {
78 return "";
79 }
80
81 // Check condition in both debug and release. std::runtime_error on failure.
82 #define PANIC(...) ((::cpptrace::detail::panic)(CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION, ::cpptrace::detail::as_string(__VA_ARGS__)))
83
84 inline void assert_impl(
85 bool condition,
86 const char* message,
87 assert_type type,
88 const char* args,
89 const char* signature,
90 source_location location
91 ) {
92 if(!condition) {
93 assert_fail(type, args, signature, location, message);
94 }
95 }
96
97 inline void assert_impl(
98 bool condition,
99 assert_type type,
100 const char* args,
101 const char* signature,
102 source_location location
103 ) {
104 assert_impl(
105 condition,
106 nullptr,
107 type,
108 args,
109 signature,
110 location
111 );
112 }
113
114 // Check condition in both debug and release. std::runtime_error on failure.
115 #define VERIFY(...) ( \
116 assert_impl(__VA_ARGS__, ::cpptrace::detail::assert_type::verify, #__VA_ARGS__, CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION) \
117 )
118
119 #ifndef NDEBUG
120 // Check condition in both debug. std::runtime_error on failure.
121 #define ASSERT(...) ( \
122 assert_impl(__VA_ARGS__, ::cpptrace::detail::assert_type::assert, #__VA_ARGS__, CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION) \
123 )
124 #else
125 // Check condition in both debug. std::runtime_error on failure.
126 #define ASSERT(...) PHONY_USE(__VA_ARGS__)
127 #endif
128
129 void log_and_maybe_propagate_exception(std::exception_ptr);
130 }
131 CPPTRACE_END_NAMESPACE
132
133 #endif