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

@@ -547,7 +547,7 @@ static void
}
static bool processFilename(char const *name, char const *outputName) {
resetErrors();
warnings.nbErrors = 0;
bool inputStdin = !strcmp(name, "-");
if (inputStdin && !outputName) {
@@ -901,7 +901,7 @@ int main(int argc, char *argv[]) {
fatalWithUsage("If `-o` is set then only a single input file may be specified");
}
bool failed = anyErrors();
bool failed = warnings.nbErrors > 0;
do {
failed |= processFilename(*argv, outputFilename);
} while (*++argv);

View File

@@ -1,7 +1,5 @@
#include "fix/warning.hpp"
static uint32_t nbErrors;
// clang-format off: nested initializers
Diagnostics<WarningLevel, WarningID> warnings = {
.metaWarnings = {
@@ -16,34 +14,21 @@ Diagnostics<WarningLevel, WarningID> warnings = {
},
.paramWarnings = {},
.state = DiagnosticsState<WarningID>(),
.nbErrors = 0,
};
// clang-format on
void resetErrors() {
nbErrors = 0;
}
bool anyErrors() {
return nbErrors > 0;
}
uint32_t checkErrors(char const *filename) {
if (anyErrors()) {
if (warnings.nbErrors > 0) {
fprintf(
stderr,
"Fixing \"%s\" failed with %u error%s\n",
"Fixing \"%s\" failed with %" PRIu64 " error%s\n",
filename,
nbErrors,
nbErrors == 1 ? "" : "s"
warnings.nbErrors,
warnings.nbErrors == 1 ? "" : "s"
);
}
return nbErrors;
}
static void incrementErrors() {
if (nbErrors != UINT32_MAX) {
++nbErrors;
}
return warnings.nbErrors;
}
void error(char const *fmt, ...) {
@@ -54,7 +39,7 @@ void error(char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
incrementErrors();
warnings.incrementErrors();
}
void fatal(char const *fmt, ...) {
@@ -91,7 +76,7 @@ void warning(WarningID id, char const *fmt, ...) {
va_end(ap);
putc('\n', stderr);
incrementErrors();
warnings.incrementErrors();
break;
}
}