diff --git a/include/asm/main.hpp b/include/asm/main.hpp index c70fd83e..75330651 100644 --- a/include/asm/main.hpp +++ b/include/asm/main.hpp @@ -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) { diff --git a/include/asm/warning.hpp b/include/asm/warning.hpp index b479c983..c51530ac 100644 --- a/include/asm/warning.hpp +++ b/include/asm/warning.hpp @@ -80,7 +80,6 @@ void error(char const *fmt, ...); // once. void error(std::function callback); -void forceError(); void requireZeroErrors(); #endif // RGBDS_ASM_WARNING_HPP diff --git a/include/diagnostics.hpp b/include/diagnostics.hpp index 1e96fecd..1ab6c041 100644 --- a/include/diagnostics.hpp +++ b/include/diagnostics.hpp @@ -59,6 +59,13 @@ struct Diagnostics { std::vector> warningFlags; std::vector> paramWarnings; DiagnosticsState state; + uint64_t nbErrors; + + void incrementErrors() { + if (nbErrors != UINT64_MAX) { + ++nbErrors; + } + } WarningBehavior getWarningBehavior(W id) const; std::string processWarningFlag(char const *flag); diff --git a/include/fix/warning.hpp b/include/fix/warning.hpp index 728177c3..370a142b 100644 --- a/include/fix/warning.hpp +++ b/include/fix/warning.hpp @@ -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 diff --git a/src/asm/main.cpp b/src/asm/main.cpp index 68771434..1f13eb91 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -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()) { diff --git a/src/asm/warning.cpp b/src/asm/warning.cpp index 77b460fc..05199224 100644 --- a/src/asm/warning.cpp +++ b/src/asm/warning.cpp @@ -17,8 +17,6 @@ #include "asm/lexer.hpp" #include "asm/main.hpp" -unsigned int nbErrors = 0; - // clang-format off: nested initializers Diagnostics warnings = { .metaWarnings = { @@ -61,6 +59,7 @@ Diagnostics warnings = { {WARNING_UNMAPPED_CHAR_1, WARNING_UNMAPPED_CHAR_2, 1}, }, .state = DiagnosticsState(), + .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); } } diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 75d86192..35a8c43e 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -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); diff --git a/src/fix/warning.cpp b/src/fix/warning.cpp index 8fa7558f..47136706 100644 --- a/src/fix/warning.cpp +++ b/src/fix/warning.cpp @@ -1,7 +1,5 @@ #include "fix/warning.hpp" -static uint32_t nbErrors; - // clang-format off: nested initializers Diagnostics warnings = { .metaWarnings = { @@ -16,34 +14,21 @@ Diagnostics warnings = { }, .paramWarnings = {}, .state = DiagnosticsState(), + .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; } } diff --git a/src/gfx/warning.cpp b/src/gfx/warning.cpp index f3a2e952..33ea9dc0 100644 --- a/src/gfx/warning.cpp +++ b/src/gfx/warning.cpp @@ -2,14 +2,11 @@ #include "gfx/warning.hpp" -#include #include #include #include #include -static uintmax_t nbErrors; - // clang-format off: nested initializers Diagnostics warnings = { .metaWarnings = { @@ -22,27 +19,27 @@ Diagnostics warnings = { }, .paramWarnings = {}, .state = DiagnosticsState(), + .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::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; } } diff --git a/src/link/warning.cpp b/src/link/warning.cpp index 56c712c5..3cf677cb 100644 --- a/src/link/warning.cpp +++ b/src/link/warning.cpp @@ -7,8 +7,6 @@ #include "link/main.hpp" -static uint32_t nbErrors = 0; - // clang-format off: nested initializers Diagnostics warnings = { .metaWarnings = { @@ -25,6 +23,7 @@ Diagnostics warnings = { }, .paramWarnings = {}, .state = DiagnosticsState(), + .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; }