Skip to content

Commit

Permalink
error codes: Add rust_error_codes.def, refactor ErrorCode enum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CohenArthur committed Jul 28, 2023
1 parent b4d1406 commit 51e5022
Show file tree
Hide file tree
Showing 3 changed files with 555 additions and 1,038 deletions.
31 changes: 28 additions & 3 deletions gcc/rust/rust-diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,40 @@ class rust_error_code_rule : public diagnostic_metadata::rule
public:
rust_error_code_rule (const ErrorCode code) : m_code (code) {}

void format_error_code (char *buffer) const
{
static_assert (
std::is_same<std::underlying_type<ErrorCode>::type, unsigned int>::value,
"invalid format specifier for ErrorCode's underlying type");

snprintf (buffer, 6, "E%04u",
(std::underlying_type<ErrorCode>::type) m_code);
}

char *make_description () const final override
{
return xstrdup (error_code_strings.at (m_code));
// 'E' + 4 characters + \0
char *buffer = new char[6];

// is that needed. does C++ suck that much that you
// can't zero initialize a new[]'d char array
memset (buffer, 0, 6);

format_error_code (buffer);

// we can use the `u` format specifier because the `ErrorCode` enum class
// "inherits" from `unsigned int` - add a static assertion to make sure
// that's the case before we do the formatting

return buffer;
}

char *make_url () const final override
{
return concat ("https://doc.rust-lang.org/error-index.html#",
error_code_strings.at (m_code), NULL);
char buffer[6] = {0};
format_error_code (buffer);

return concat ("https://doc.rust-lang.org/error-index.html#", buffer, NULL);
}

private:
Expand Down
Loading

0 comments on commit 51e5022

Please sign in to comment.