Implement a single nbErrors counter inside generic diagnostic code

This commit is contained in:
Rangi42
2025-07-26 12:48:25 -04:00
parent ac632d9223
commit 92acb6e547
10 changed files with 61 additions and 81 deletions

View File

@@ -2,14 +2,11 @@
#include "gfx/warning.hpp"
#include <limits>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static uintmax_t nbErrors;
// clang-format off: nested initializers
Diagnostics<WarningLevel, WarningID> warnings = {
.metaWarnings = {
@@ -22,27 +19,27 @@ Diagnostics<WarningLevel, WarningID> warnings = {
},
.paramWarnings = {},
.state = DiagnosticsState<WarningID>(),
.nbErrors = 0,
};
// clang-format on
[[noreturn]]
void giveUp() {
fprintf(stderr, "Conversion aborted after %ju error%s\n", nbErrors, nbErrors == 1 ? "" : "s");
fprintf(
stderr,
"Conversion aborted after %" PRIu64 " error%s\n",
warnings.nbErrors,
warnings.nbErrors == 1 ? "" : "s"
);
exit(1);
}
void requireZeroErrors() {
if (nbErrors != 0) {
if (warnings.nbErrors != 0) {
giveUp();
}
}
static void incrementErrors() {
if (nbErrors != std::numeric_limits<decltype(nbErrors)>::max()) {
++nbErrors;
}
}
void error(char const *fmt, ...) {
va_list ap;
fputs("error: ", stderr);
@@ -51,7 +48,7 @@ void error(char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
incrementErrors();
warnings.incrementErrors();
}
[[noreturn]]
@@ -63,7 +60,7 @@ void fatal(char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
incrementErrors();
warnings.incrementErrors();
giveUp();
}
@@ -90,7 +87,7 @@ void warning(WarningID id, char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
incrementErrors();
warnings.incrementErrors();
break;
}
}