Refactor warnings and errors (#1728)

* Remove `err` and `warn`, keep `errx` and `warnx`, using them in RGBGFX too

* Separate RGBGFX and RGBLINK warnings/errors from main options

* Separate `report` function into `error` and `fatal` messages

* Implicit newlines for most RGBASM errors
This commit is contained in:
Rangi
2025-07-08 12:58:23 -04:00
committed by GitHub
parent 991b74dd0d
commit 35962dedc4
39 changed files with 753 additions and 757 deletions

View File

@@ -23,6 +23,7 @@
#include "link/patch.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "link/warning.hpp"
bool isDmgMode; // -d
char const *linkerScriptName; // -l
@@ -44,8 +45,6 @@ bool disablePadding; // -x
FILE *linkerScript;
static uint32_t nbErrors = 0;
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
if (data.holds<std::vector<uint32_t>>()) {
assume(parent); // REPT nodes use their parent's name
@@ -69,72 +68,6 @@ std::string const &FileStackNode::dump(uint32_t curLineNo) const {
}
}
void printDiag(
char const *fmt, va_list args, char const *type, FileStackNode const *where, uint32_t lineNo
) {
fputs(type, stderr);
fputs(": ", stderr);
if (where) {
where->dump(lineNo);
fputs(": ", stderr);
}
vfprintf(stderr, fmt, args);
putc('\n', stderr);
}
void warning(FileStackNode const *where, uint32_t lineNo, char const *fmt, ...) {
va_list args;
va_start(args, fmt);
printDiag(fmt, args, "warning", where, lineNo);
va_end(args);
}
void error(FileStackNode const *where, uint32_t lineNo, char const *fmt, ...) {
va_list args;
va_start(args, fmt);
printDiag(fmt, args, "error", where, lineNo);
va_end(args);
if (nbErrors != UINT32_MAX) {
nbErrors++;
}
}
[[gnu::format(printf, 2, 3)]]
void argErr(char flag, char const *fmt, ...) {
va_list args;
fprintf(stderr, "error: Invalid argument for option '%c': ", flag);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
putc('\n', stderr);
if (nbErrors != UINT32_MAX) {
nbErrors++;
}
}
[[noreturn]]
void fatal(FileStackNode const *where, uint32_t lineNo, char const *fmt, ...) {
va_list args;
va_start(args, fmt);
printDiag(fmt, args, "FATAL", where, lineNo);
va_end(args);
if (nbErrors != UINT32_MAX) {
nbErrors++;
}
fprintf(
stderr, "Linking aborted after %" PRIu32 " error%s\n", nbErrors, nbErrors == 1 ? "" : "s"
);
exit(1);
}
// Short options
static char const *optstring = "dhl:m:Mn:O:o:p:S:tVvwx";
@@ -185,6 +118,19 @@ static void printUsage() {
}
// LCOV_EXCL_STOP
[[gnu::format(printf, 1, 2), noreturn]]
static void fatalWithUsage(char const *fmt, ...) {
va_list ap;
fputs("FATAL: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
printUsage();
exit(1);
}
enum ScrambledRegion {
SCRAMBLE_ROMX,
SCRAMBLE_SRAM,
@@ -327,14 +273,6 @@ next: // Can't `continue` a `for` loop with this nontrivial iteration logic
}
}
[[noreturn]]
void reportErrors() {
fprintf(
stderr, "Linking failed with %" PRIu32 " error%s\n", nbErrors, nbErrors == 1 ? "" : "s"
);
exit(1);
}
int main(int argc, char *argv[]) {
// Parse options
for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) {
@@ -429,11 +367,7 @@ int main(int argc, char *argv[]) {
// If no input files were specified, the user must have screwed up
if (curArgIndex == argc) {
fputs(
"FATAL: Please specify an input file (pass `-` to read from standard input)\n", stderr
);
printUsage();
exit(1);
fatalWithUsage("Please specify an input file (pass `-` to read from standard input)");
}
// Patch the size array depending on command-line options
@@ -461,23 +395,17 @@ int main(int argc, char *argv[]) {
script_ProcessScript(linkerScriptName);
// If the linker script produced any errors, some sections may be in an invalid state
if (nbErrors != 0) {
reportErrors();
}
requireZeroErrors();
}
// then process them,
sect_DoSanityChecks();
if (nbErrors != 0) {
reportErrors();
}
requireZeroErrors();
assign_AssignSections();
patch_CheckAssertions();
// and finally output the result.
patch_ApplyPatches();
if (nbErrors != 0) {
reportErrors();
}
requireZeroErrors();
out_WriteFiles();
}