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

@@ -25,7 +25,7 @@ struct Options {
bool generatePhonyDeps = false; // -MP bool generatePhonyDeps = false; // -MP
std::string objectFileName; // -o std::string objectFileName; // -o
uint8_t padByte = 0; // -p uint8_t padByte = 0; // -p
unsigned int maxErrors = 0; // -X uint64_t maxErrors = 0; // -X
~Options() { ~Options() {
if (dependFile) { if (dependFile) {

View File

@@ -80,7 +80,6 @@ void error(char const *fmt, ...);
// once. // once.
void error(std::function<void()> callback); void error(std::function<void()> callback);
void forceError();
void requireZeroErrors(); void requireZeroErrors();
#endif // RGBDS_ASM_WARNING_HPP #endif // RGBDS_ASM_WARNING_HPP

View File

@@ -59,6 +59,13 @@ struct Diagnostics {
std::vector<WarningFlag<L>> warningFlags; std::vector<WarningFlag<L>> warningFlags;
std::vector<ParamWarning<W>> paramWarnings; std::vector<ParamWarning<W>> paramWarnings;
DiagnosticsState<W> state; DiagnosticsState<W> state;
uint64_t nbErrors;
void incrementErrors() {
if (nbErrors != UINT64_MAX) {
++nbErrors;
}
}
WarningBehavior getWarningBehavior(W id) const; WarningBehavior getWarningBehavior(W id) const;
std::string processWarningFlag(char const *flag); std::string processWarningFlag(char const *flag);

View File

@@ -36,8 +36,6 @@ void error(char const *fmt, ...);
[[gnu::format(printf, 1, 2)]] [[gnu::format(printf, 1, 2)]]
void fatal(char const *fmt, ...); void fatal(char const *fmt, ...);
void resetErrors();
bool anyErrors();
uint32_t checkErrors(char const *filename); uint32_t checkErrors(char const *filename);
#endif // RGBDS_FIX_WARNING_HPP #endif // RGBDS_FIX_WARNING_HPP

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -7,8 +7,6 @@
#include "link/main.hpp" #include "link/main.hpp"
static uint32_t nbErrors = 0;
// clang-format off: nested initializers // clang-format off: nested initializers
Diagnostics<WarningLevel, WarningID> warnings = { Diagnostics<WarningLevel, WarningID> warnings = {
.metaWarnings = { .metaWarnings = {
@@ -25,6 +23,7 @@ Diagnostics<WarningLevel, WarningID> warnings = {
}, },
.paramWarnings = {}, .paramWarnings = {},
.state = DiagnosticsState<WarningID>(), .state = DiagnosticsState<WarningID>(),
.nbErrors = 0,
}; };
// clang-format on // clang-format on
@@ -50,20 +49,14 @@ static void printDiag(
putc('\n', stderr); putc('\n', stderr);
} }
static void incrementErrors() {
if (nbErrors != UINT32_MAX) {
++nbErrors;
}
}
[[noreturn]] [[noreturn]]
static void abortLinking(char const *verb) { static void abortLinking(char const *verb) {
fprintf( fprintf(
stderr, stderr,
"Linking %s with %" PRIu32 " error%s\n", "Linking %s with %" PRIu64 " error%s\n",
verb ? verb : "aborted", verb ? verb : "aborted",
nbErrors, warnings.nbErrors,
nbErrors == 1 ? "" : "s" warnings.nbErrors == 1 ? "" : "s"
); );
exit(1); exit(1);
} }
@@ -88,7 +81,7 @@ void error(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
printDiag(src, lineNo, fmt, args, "error", nullptr, 0); printDiag(src, lineNo, fmt, args, "error", nullptr, 0);
va_end(args); va_end(args);
incrementErrors(); warnings.incrementErrors();
} }
void error(char const *fmt, ...) { void error(char const *fmt, ...) {
@@ -97,7 +90,7 @@ void error(char const *fmt, ...) {
printDiag(nullptr, 0, fmt, args, "error", nullptr, 0); printDiag(nullptr, 0, fmt, args, "error", nullptr, 0);
va_end(args); va_end(args);
incrementErrors(); warnings.incrementErrors();
} }
void errorNoDump(char const *fmt, ...) { void errorNoDump(char const *fmt, ...) {
@@ -107,7 +100,7 @@ void errorNoDump(char const *fmt, ...) {
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
incrementErrors(); warnings.incrementErrors();
} }
void argErr(char flag, char const *fmt, ...) { void argErr(char flag, char const *fmt, ...) {
@@ -118,7 +111,7 @@ void argErr(char flag, char const *fmt, ...) {
va_end(args); va_end(args);
putc('\n', stderr); putc('\n', stderr);
incrementErrors(); warnings.incrementErrors();
} }
[[noreturn]] [[noreturn]]
@@ -128,7 +121,7 @@ void fatal(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
printDiag(src, lineNo, fmt, args, "FATAL", nullptr, 0); printDiag(src, lineNo, fmt, args, "FATAL", nullptr, 0);
va_end(args); va_end(args);
incrementErrors(); warnings.incrementErrors();
abortLinking(nullptr); abortLinking(nullptr);
} }
@@ -139,12 +132,12 @@ void fatal(char const *fmt, ...) {
printDiag(nullptr, 0, fmt, args, "FATAL", nullptr, 0); printDiag(nullptr, 0, fmt, args, "FATAL", nullptr, 0);
va_end(args); va_end(args);
incrementErrors(); warnings.incrementErrors();
abortLinking(nullptr); abortLinking(nullptr);
} }
void requireZeroErrors() { void requireZeroErrors() {
if (nbErrors != 0) { if (warnings.nbErrors != 0) {
abortLinking("failed"); abortLinking("failed");
} }
} }
@@ -166,7 +159,7 @@ void warning(FileStackNode const *src, uint32_t lineNo, WarningID id, char const
case WarningBehavior::ERROR: case WarningBehavior::ERROR:
printDiag(src, lineNo, fmt, args, "error", "[-Werror=%s]", flag); printDiag(src, lineNo, fmt, args, "error", "[-Werror=%s]", flag);
incrementErrors(); warnings.incrementErrors();
break; break;
} }