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

@@ -336,14 +336,14 @@ int main(int argc, char *argv[]) {
break;
case 'X': {
unsigned long maxErrors = strtoul(musl_optarg, &endptr, 0);
uint64_t maxErrors = strtoul(musl_optarg, &endptr, 0);
if (musl_optarg[0] == '\0' || *endptr != '\0') {
fatal("Invalid argument for option 'X'");
}
if (maxErrors > UINT_MAX) {
fatal("Argument for option 'X' must be between 0 and %u", UINT_MAX);
if (maxErrors > UINT64_MAX) {
fatal("Argument for option 'X' must be between 0 and %" PRIu64, UINT64_MAX);
}
options.maxErrors = maxErrors;
@@ -416,7 +416,9 @@ int main(int argc, char *argv[]) {
// Perform parse (`yy::parser` is auto-generated from `parser.y`)
if (yy::parser parser; parser.parse() != 0) {
forceError();
if (warnings.nbErrors == 0) {
warnings.nbErrors = 1;
}
}
if (!fstk_FailedOnMissingInclude()) {

View File

@@ -17,8 +17,6 @@
#include "asm/lexer.hpp"
#include "asm/main.hpp"
unsigned int nbErrors = 0;
// clang-format off: nested initializers
Diagnostics<WarningLevel, WarningID> warnings = {
.metaWarnings = {
@@ -61,6 +59,7 @@ Diagnostics<WarningLevel, WarningID> warnings = {
{WARNING_UNMAPPED_CHAR_1, WARNING_UNMAPPED_CHAR_2, 1},
},
.state = DiagnosticsState<WarningID>(),
.nbErrors = 0,
};
// clang-format on
@@ -79,14 +78,15 @@ static void printDiag(
}
static void incrementErrors() {
// This intentionally makes 0 act as "unlimited" (or at least "limited to sizeof(unsigned)")
if (++nbErrors == options.maxErrors) {
// This intentionally makes 0 act as "unlimited"
warnings.incrementErrors();
if (warnings.nbErrors == options.maxErrors) {
fprintf(
stderr,
"Assembly aborted after the maximum of %u error%s! (configure with "
"Assembly aborted after the maximum of %" PRIu64 " error%s! (configure with "
"'-X/--max-errors')\n",
nbErrors,
nbErrors == 1 ? "" : "s"
warnings.nbErrors,
warnings.nbErrors == 1 ? "" : "s"
);
exit(1);
}
@@ -125,15 +125,14 @@ void fatal(char const *fmt, ...) {
exit(1);
}
void forceError() {
if (nbErrors == 0) {
nbErrors = 1;
}
}
void requireZeroErrors() {
if (nbErrors != 0) {
fprintf(stderr, "Assembly aborted with %u error%s!\n", nbErrors, nbErrors == 1 ? "" : "s");
if (warnings.nbErrors != 0) {
fprintf(
stderr,
"Assembly aborted with %" PRIu64 " error%s!\n",
warnings.nbErrors,
warnings.nbErrors == 1 ? "" : "s"
);
exit(1);
}
}