Skip to content

Commit 42bd81f

Browse files
CohenArthurphilberty
authored andcommitted
error codes: Add rust_error_codes.def, refactor ErrorCode enum
gcc/rust/ChangeLog: * rust-diagnostics.cc: Rework `make_description` and `make_url` functions. * rust-diagnostics.h: Specify ErrorCode's underlying type, remove error_codes_strings table. * rust_error_codes.def: New file.
1 parent 6114574 commit 42bd81f

File tree

3 files changed

+555
-1038
lines changed

3 files changed

+555
-1038
lines changed

gcc/rust/rust-diagnostics.cc

+28-3
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,40 @@ class rust_error_code_rule : public diagnostic_metadata::rule
197197
public:
198198
rust_error_code_rule (const ErrorCode code) : m_code (code) {}
199199

200+
void format_error_code (char *buffer) const
201+
{
202+
static_assert (
203+
std::is_same<std::underlying_type<ErrorCode>::type, unsigned int>::value,
204+
"invalid format specifier for ErrorCode's underlying type");
205+
206+
snprintf (buffer, 6, "E%04u",
207+
(std::underlying_type<ErrorCode>::type) m_code);
208+
}
209+
200210
char *make_description () const final override
201211
{
202-
return xstrdup (error_code_strings.at (m_code));
212+
// 'E' + 4 characters + \0
213+
char *buffer = new char[6];
214+
215+
// is that needed. does C++ suck that much that you
216+
// can't zero initialize a new[]'d char array
217+
memset (buffer, 0, 6);
218+
219+
format_error_code (buffer);
220+
221+
// we can use the `u` format specifier because the `ErrorCode` enum class
222+
// "inherits" from `unsigned int` - add a static assertion to make sure
223+
// that's the case before we do the formatting
224+
225+
return buffer;
203226
}
204227

205228
char *make_url () const final override
206229
{
207-
return concat ("https://doc.rust-lang.org/error-index.html#",
208-
error_code_strings.at (m_code), NULL);
230+
char buffer[6] = {0};
231+
format_error_code (buffer);
232+
233+
return concat ("https://doc.rust-lang.org/error-index.html#", buffer, NULL);
209234
}
210235

211236
private:

0 commit comments

Comments
 (0)