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
std::string objectFileName; // -o
uint8_t padByte = 0; // -p
unsigned int maxErrors = 0; // -X
uint64_t maxErrors = 0; // -X
~Options() {
if (dependFile) {

View File

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

View File

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

View File

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

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);
}
}

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;
}
}

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;
}
}

View File

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