Skip to content

Commit a1d6587

Browse files
authored
feat: codegen error reporting (#44)
* feat: codegen error reporting * chore: typos * chore: formatting
1 parent 7384f5f commit a1d6587

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

ecsact/codegen/plugin.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,48 @@ typedef void (*ecsact_codegen_write_fn_t)( //
3636
int32_t str_len
3737
);
3838

39+
typedef enum ecsact_codegen_report_type {
40+
/**
41+
* Informational report. Mostly used for debugging. May or may not be shown to
42+
* user.
43+
*/
44+
ECSACT_CODEGEN_REPORT_INFO,
45+
46+
/**
47+
* Warning. May or may not be shown to user.
48+
*/
49+
ECSACT_CODEGEN_REPORT_WARNING,
50+
51+
/**
52+
* An error has occurred, but can still continue running. Will be shown to
53+
* user when possible.
54+
*/
55+
ECSACT_CODEGEN_REPORT_ERROR,
56+
57+
/**
58+
* An error has occurred and plugin cannot continue running. Will be shown to
59+
* user when possible.
60+
*/
61+
ECSACT_CODEGEN_REPORT_FATAL,
62+
} ecsact_codegen_report_message_type;
63+
64+
/**
65+
* Message passed to this function is reported to the codegen host and may be
66+
* shown to the user. Characters that go beyond @p msg_len are not read. Some
67+
* report types may hault the codegen process. @see ecsact_codegen_report_type
68+
*
69+
* @NOTE: it is _NOT_ assumed that @p msg is null-terminated, you must set
70+
* @p msg_len properly.
71+
*
72+
* @param msg - array of characters of length @p msg_len
73+
* @param msg_len - length of array of characters @p msg
74+
*/
75+
typedef void (*ecsact_codegen_report_fn_t)( //
76+
ecsact_codegen_report_type report_type,
77+
const char* msg,
78+
int32_t msg_len
79+
);
80+
3981
ECSACT_CODEGEN_PLUGIN_API const char* ecsact_codegen_plugin_name();
4082

4183
/**
@@ -50,8 +92,9 @@ ECSACT_CODEGEN_PLUGIN_API const char* ecsact_codegen_plugin_name();
5092
* output
5193
*/
5294
ECSACT_CODEGEN_PLUGIN_API void ecsact_codegen_plugin( //
53-
ecsact_package_id package_id,
54-
ecsact_codegen_write_fn_t write_fn
95+
ecsact_package_id package_id,
96+
ecsact_codegen_write_fn_t write_fn,
97+
ecsact_codegen_report_fn_t report_fn
5598
);
5699

57100
#endif // ECSACT_CODEGEN_PLUGIN_H

ecsact/codegen/plugin.hh

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <string_view>
77
#include <cstring>
88
#include <iterator>
9+
#ifdef __cpp_lib_format
10+
# include <format>
11+
#endif
912
#include "ecsact/runtime/common.h"
1013
#include "ecsact/codegen/plugin.h"
1114

@@ -15,23 +18,36 @@ namespace ecsact {
1518
* Helper type to give a more C++ friendly write function
1619
* @example
1720
* void ecsact_codegen_plugin
18-
* ( ecsact_package_id package_id
19-
* , ecsact_codegen_write_fn_t write_fn
21+
* ( ecsact_package_id package_id
22+
* , ecsact_codegen_write_fn_t write_fn
23+
* , ecsact_codegen_report_fn_t report_fn
2024
* )
2125
* {
22-
* ecsact::codegen_plugin_context ctx{package_id, write_fn};
23-
* ctx.write("Hello, World!\n");
26+
* ecsact::codegen_plugin_context ctx{package_id, write_fn, report_fn};
27+
* ctx.writef("Hello, World!\n");
28+
* ctx.info("We made it!");
2429
* }
2530
*/
2631
struct codegen_plugin_context {
27-
const ecsact_package_id package_id;
28-
const ecsact_codegen_write_fn_t write_fn;
29-
int indentation = 0;
32+
const ecsact_package_id package_id;
33+
const ecsact_codegen_write_fn_t write_fn;
34+
const ecsact_codegen_report_fn_t report_fn;
35+
int indentation = 0;
3036

3137
std::string get_indent_str() {
3238
return std::string(indentation, '\t');
3339
}
3440

41+
void report_(
42+
ecsact_codegen_report_type report_type,
43+
const char* str_data,
44+
int32_t str_data_len
45+
) {
46+
if(report_fn != nullptr) {
47+
report_fn(report_type, str_data, str_data_len);
48+
}
49+
}
50+
3551
void write_(const char* str_data, int32_t str_data_len) {
3652
assert(indentation >= 0);
3753

@@ -56,6 +72,7 @@ struct codegen_plugin_context {
5672
}
5773

5874
template<typename T>
75+
[[deprecated("use writef instead")]]
5976
void write(T&& arg) {
6077
using NoRefT = std::remove_cvref_t<T>;
6178

@@ -72,6 +89,7 @@ struct codegen_plugin_context {
7289
}
7390

7491
template<typename... T>
92+
[[deprecated("use writef instead")]]
7593
void write(T&&... args) {
7694
(write<T>(std::forward<T>(args)), ...);
7795
}
@@ -87,6 +105,38 @@ struct codegen_plugin_context {
87105
}
88106
}
89107
}
108+
109+
#ifdef __cpp_lib_format
110+
template<typename... Args>
111+
auto writef(std::format_string<Args...> fmt, Args&&... args) {
112+
auto str = std::format(fmt, std::make_format_args(args...));
113+
write_(str.data(), static_cast<int32_t>(str.size()));
114+
}
115+
116+
template<typename... Args>
117+
auto info(std::format_string<Args...> fmt, Args&&... args) {
118+
auto str = std::format(fmt, std::make_format_args(args...));
119+
report_(ECSACT_CODEGEN_REPORT_INFO, str.data(), str.size());
120+
}
121+
122+
template<typename... Args>
123+
auto warn(std::format_string<Args...> fmt, Args&&... args) {
124+
auto str = std::format(fmt, std::make_format_args(args...));
125+
report_(ECSACT_CODEGEN_REPORT_WARNING, str.data(), str.size());
126+
}
127+
128+
template<typename... Args>
129+
auto error(std::format_string<Args...> fmt, Args&&... args) {
130+
auto str = std::format(fmt, std::make_format_args(args...));
131+
report_(ECSACT_CODEGEN_REPORT_ERROR, str.data(), str.size());
132+
}
133+
134+
template<typename... Args>
135+
auto fatal(std::format_string<Args...> fmt, Args&&... args) {
136+
auto str = std::format(fmt, std::make_format_args(args...));
137+
report_(ECSACT_CODEGEN_REPORT_FATAL, str.data(), str.size());
138+
}
139+
#endif
90140
};
91141

92142
} // namespace ecsact

0 commit comments

Comments
 (0)