Skip to content

Commit 7bca020

Browse files
committed
Refactor: add severity as explicit field for diagnostics
Each error type listed in error.h has a severity. The severity is implicitly described by the first message. I think this is awkward. Make the severity explicitly a separate field. This will let us replace ERROR/WARNING/NOTE with a single MESSAGE macro in the future. This commit should not change behavior.
1 parent a00b4bc commit 7bca020

File tree

11 files changed

+295
-206
lines changed

11 files changed

+295
-206
lines changed

src/buffering-error-reporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct buffering_error_reporter::impl {
2121
union underlying_error {
2222
explicit underlying_error() noexcept {}
2323

24-
#define QLJS_ERROR_TYPE(name, code, struct_body, format) \
24+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format) \
2525
::quick_lint_js::name name; \
2626
static_assert(std::is_trivially_copyable_v<::quick_lint_js::name>, \
2727
#name " should be trivially copyable");
@@ -58,7 +58,7 @@ buffering_error_reporter::~buffering_error_reporter() = default;
5858

5959
void buffering_error_reporter::report_impl(error_type type, void *error) {
6060
static constexpr unsigned char error_sizes[] = {
61-
#define QLJS_ERROR_TYPE(name, code, struct_body, format) \
61+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format) \
6262
sizeof(::quick_lint_js::name),
6363
QLJS_X_ERROR_TYPES
6464
#undef QLJS_ERROR_TYPE

src/diagnostic.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ constexpr void strcpy(char* out, const char* in) noexcept {
4040
template <class Error>
4141
class diagnostic_info_builder {
4242
public:
43-
constexpr explicit diagnostic_info_builder(const char* code) {
43+
constexpr explicit diagnostic_info_builder(const char* code,
44+
diagnostic_severity sev) {
45+
this->info_.severity = sev;
4446
strcpy(this->info_.code, code);
4547
}
4648

@@ -49,6 +51,16 @@ class diagnostic_info_builder {
4951
constexpr diagnostic_info_builder add(diagnostic_severity sev,
5052
const translatable_message& message,
5153
const Args&... arg_infos) {
54+
if (this->current_message_index_ == 0) {
55+
if (sev != this->info_.severity) {
56+
#if __cpp_constexpr >= 201907L && !defined(_MSC_VER)
57+
// If you see an error with the following line, error.h contains a
58+
// QLJS_ERROR_TYPE call with inconsistent diagnostic severities.
59+
asm("");
60+
#endif
61+
}
62+
}
63+
5264
diagnostic_message_info& message_info =
5365
this->info_.messages[this->current_message_index_++];
5466
message_info.format = message;
@@ -124,13 +136,14 @@ struct diagnostic_info_for_error;
124136
#define NOTE(message_format, ...) \
125137
.add(diagnostic_severity::note, message_format, MAKE_ARGS(__VA_ARGS__))
126138

127-
#define QLJS_ERROR_TYPE(name, code, struct_body, format_call) \
139+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format_call) \
128140
template <> \
129141
struct diagnostic_info_for_error<name> { \
130142
using error_class = name; \
131143
\
132144
static DIAGNOSTIC_CONSTEXPR_IF_POSSIBLE diagnostic_info get() noexcept { \
133-
return diagnostic_info_builder<name>(code) format_call.build(); \
145+
return diagnostic_info_builder<name>(code, severity) \
146+
format_call.build(); \
134147
} \
135148
};
136149
QLJS_X_ERROR_TYPES
@@ -139,7 +152,7 @@ QLJS_X_ERROR_TYPES
139152

140153
DIAGNOSTIC_CONSTEXPR_IF_POSSIBLE const diagnostic_info
141154
all_diagnostic_infos[] = {
142-
#define QLJS_ERROR_TYPE(name, code, struct_body, format_call) \
155+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format_call) \
143156
diagnostic_info_for_error<name>::get(),
144157
QLJS_X_ERROR_TYPES
145158
#undef QLJS_ERROR_TYPE

src/error-debug.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace quick_lint_js {
99
std::ostream& operator<<(std::ostream& out, error_type type) {
1010
switch (type) {
11-
#define QLJS_ERROR_TYPE(name, code, struct_body, format_call) \
12-
case error_type::name: \
11+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format_call) \
12+
case error_type::name: \
1313
return out << #name;
1414
QLJS_X_ERROR_TYPES
1515
#undef QLJS_ERROR_TYPE

src/quick-lint-js/diagnostic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct diagnostic_message_info {
4848

4949
struct diagnostic_info {
5050
char code[6];
51+
diagnostic_severity severity QLJS_WORK_AROUND_GCC_BUG_105191;
5152
// If we support more than two infos (i.e. more than one note), the VS Code
5253
// plugin needs to be updated. See NOTE(multiple notes).
5354
diagnostic_message_info messages[2];

src/quick-lint-js/error-reporter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class error_reporter {
1919

2020
virtual ~error_reporter() = default;
2121

22-
#define QLJS_ERROR_TYPE(name, code, struct_body, format) \
23-
void report(name error) { \
24-
this->report_impl(error_type_from_type<name>, &error); \
22+
#define QLJS_ERROR_TYPE(name, code, severity, struct_body, format) \
23+
void report(name error) { \
24+
this->report_impl(error_type_from_type<name>, &error); \
2525
}
2626
QLJS_X_ERROR_TYPES
2727
#undef QLJS_ERROR_TYPE

0 commit comments

Comments
 (0)