From 02f9128d07e700bc3f392de4afc248fbbb7c627a Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sun, 5 Nov 2023 16:08:07 -0500 Subject: [PATCH] Make some changes noticed while porting to C++ --- include/asm/fstack.h | 11 ++++++----- include/asm/opt.h | 4 ++-- include/asm/section.h | 4 ++-- include/itertools.hpp | 2 ++ include/link/main.h | 12 +++++++----- include/link/script.h | 1 + include/platform.h | 9 +++++++++ src/asm/charmap.c | 3 +-- src/asm/section.c | 4 ++-- src/asm/warning.c | 2 +- src/fix/main.c | 6 ++++-- src/gfx/pal_spec.cpp | 2 +- src/link/object.c | 15 +-------------- src/link/output.c | 10 ++++++---- src/link/script.c | 16 +++++++++------- src/link/sdas_obj.c | 18 +++++++++++------- src/link/section.c | 6 +++--- src/link/symbol.c | 6 +++--- test/gfx/randtilegen.c | 2 +- 19 files changed, 72 insertions(+), 61 deletions(-) diff --git a/include/asm/fstack.h b/include/asm/fstack.h index c7d3c160..e2a751eb 100644 --- a/include/asm/fstack.h +++ b/include/asm/fstack.h @@ -11,6 +11,11 @@ #include "asm/lexer.h" +enum FileStackNodeType { + NODE_REPT, + NODE_FILE, + NODE_MACRO, +}; struct FileStackNode { struct FileStackNode *parent; // Pointer to parent node, for error reporting @@ -21,11 +26,7 @@ struct FileStackNode { bool referenced; // If referenced, don't free! uint32_t ID; // Set only if referenced: ID within the object file, -1 if not output yet - enum { - NODE_REPT, - NODE_FILE, - NODE_MACRO, - } type; + enum FileStackNodeType type; }; struct FileStackReptNode { // NODE_REPT diff --git a/include/asm/opt.h b/include/asm/opt.h index 0d9fd3f0..3bb70958 100644 --- a/include/asm/opt.h +++ b/include/asm/opt.h @@ -11,8 +11,8 @@ void opt_G(char const chars[4]); void opt_P(uint8_t padByte); void opt_Q(uint8_t precision); void opt_L(bool optimize); -void opt_W(char const *flag); -void opt_Parse(char const *option); +void opt_W(char *flag); +void opt_Parse(char *option); void opt_Push(void); void opt_Pop(void); diff --git a/include/asm/section.h b/include/asm/section.h index 6427be78..a71d8bd1 100644 --- a/include/asm/section.h +++ b/include/asm/section.h @@ -38,9 +38,9 @@ struct SectionSpec { extern struct Section *currentSection; struct Section *sect_FindSectionByName(char const *name); -void sect_NewSection(char const *name, uint32_t secttype, uint32_t org, +void sect_NewSection(char const *name, enum SectionType type, uint32_t org, struct SectionSpec const *attributes, enum SectionModifier mod); -void sect_SetLoadSection(char const *name, uint32_t secttype, uint32_t org, +void sect_SetLoadSection(char const *name, enum SectionType type, uint32_t org, struct SectionSpec const *attributes, enum SectionModifier mod); void sect_EndLoadSection(void); diff --git a/include/itertools.hpp b/include/itertools.hpp index d8341f18..bbe66c5f 100644 --- a/include/itertools.hpp +++ b/include/itertools.hpp @@ -6,6 +6,8 @@ #include #include +#include "platform.h" // __PRETTY_FUNCTION__ + template static inline void report() { puts(__PRETTY_FUNCTION__); diff --git a/include/link/main.h b/include/link/main.h index 137af11a..a200a8fa 100644 --- a/include/link/main.h +++ b/include/link/main.h @@ -27,16 +27,18 @@ extern bool beVerbose; extern bool isWRA0Mode; extern bool disablePadding; +enum FileStackNodeType { + NODE_REPT, + NODE_FILE, + NODE_MACRO, +}; + struct FileStackNode { struct FileStackNode *parent; // Line at which the parent context was exited; meaningless for the root level uint32_t lineNo; - enum { - NODE_REPT, - NODE_FILE, - NODE_MACRO, - } type; + enum FileStackNodeType type; union { char *name; // NODE_FILE, NODE_MACRO struct { // NODE_REPT diff --git a/include/link/script.h b/include/link/script.h index 3f4d6cdd..e943a575 100644 --- a/include/link/script.h +++ b/include/link/script.h @@ -5,6 +5,7 @@ #define RGBDS_LINK_SCRIPT_H #include + #include "linkdefs.h" extern FILE * linkerScript; diff --git a/include/platform.h b/include/platform.h index 2641cedd..661aea88 100644 --- a/include/platform.h +++ b/include/platform.h @@ -27,6 +27,15 @@ # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) #endif +// gcc has __PRETTY_FUNCTION__, MSVC has __FUNCSIG__, __func__ is standard +#ifndef __PRETTY_FUNCTION__ +# ifdef __FUNCSIG__ +# define __PRETTY_FUNCTION__ __FUNCSIG__ +# else +# define __PRETTY_FUNCTION__ __func__ +# endif +#endif + // MSVC doesn't use POSIX types or defines for `read` #ifdef _MSC_VER # include diff --git a/src/asm/charmap.c b/src/asm/charmap.c index 798d25fe..9ec85607 100644 --- a/src/asm/charmap.c +++ b/src/asm/charmap.c @@ -122,9 +122,8 @@ void charmap_Set(char const *name) void charmap_Push(void) { - struct CharmapStackEntry *stackEntry; + struct CharmapStackEntry *stackEntry = malloc(sizeof(*stackEntry)); - stackEntry = malloc(sizeof(*stackEntry)); if (stackEntry == NULL) fatalerror("Failed to alloc charmap stack entry: %s\n", strerror(errno)); diff --git a/src/asm/section.c b/src/asm/section.c index c30a7896..aabdcce6 100644 --- a/src/asm/section.c +++ b/src/asm/section.c @@ -386,7 +386,7 @@ static void changeSection(void) } // Set the current section by name and type -void sect_NewSection(char const *name, uint32_t type, uint32_t org, +void sect_NewSection(char const *name, enum SectionType type, uint32_t org, struct SectionSpec const *attribs, enum SectionModifier mod) { if (currentLoadSection) @@ -406,7 +406,7 @@ void sect_NewSection(char const *name, uint32_t type, uint32_t org, } // Set the current section by name and type -void sect_SetLoadSection(char const *name, uint32_t type, uint32_t org, +void sect_SetLoadSection(char const *name, enum SectionType type, uint32_t org, struct SectionSpec const *attribs, enum SectionModifier mod) { // Important info: currently, UNION and LOAD cannot interact, since UNION is prohibited in diff --git a/src/asm/warning.c b/src/asm/warning.c index aca344a5..f68e9a94 100644 --- a/src/asm/warning.c +++ b/src/asm/warning.c @@ -260,7 +260,7 @@ void processWarningFlag(char *flag) // Not an error, then check if this is a negation strncmp(flag, "no-", strlen("no-")) ? WARNING_ENABLED : WARNING_DISABLED; - char const *rootFlag = state == WARNING_DISABLED ? flag + strlen("no-") : flag; + char *rootFlag = state == WARNING_DISABLED ? flag + strlen("no-") : flag; // Is this a "parametric" warning? if (state != WARNING_DISABLED) { // The `no-` form cannot be parametrized diff --git a/src/fix/main.c b/src/fix/main.c index 64fd610c..b7cf80a4 100644 --- a/src/fix/main.c +++ b/src/fix/main.c @@ -325,7 +325,7 @@ do { \ tryReadSlice("MA5"); mbc = BANDAI_TAMA5; break; - case 'P': + case 'P': { tryReadSlice("P1"); // Parse version while (*ptr == ' ' || *ptr == '_') @@ -359,6 +359,7 @@ do { \ tpp1Rev[1] = val; mbc = TPP1; break; + } default: return MBC_BAD; } @@ -1376,7 +1377,7 @@ do { \ sgb = true; break; - case 't': + case 't': { title = musl_optarg; len = strlen(title); uint8_t maxLen = maxTitleLen(); @@ -1388,6 +1389,7 @@ do { \ } titleLen = len; break; + } case 'V': printf("rgbfix %s\n", get_package_version_string()); diff --git a/src/gfx/pal_spec.cpp b/src/gfx/pal_spec.cpp index 5349dc5e..5a63c3dc 100644 --- a/src/gfx/pal_spec.cpp +++ b/src/gfx/pal_spec.cpp @@ -419,7 +419,7 @@ static void parseHEXFile(std::filebuf &file) { static void parseACTFile(std::filebuf &file) { // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626 - std::array buf; + std::array buf{}; auto len = file.sgetn(buf.data(), buf.size()); uint16_t nbColors = 256; diff --git a/src/link/object.c b/src/link/object.c index a7b567e0..0c6bb9d1 100644 --- a/src/link/object.c +++ b/src/link/object.c @@ -28,7 +28,7 @@ static struct SymbolList { } *symbolLists; unsigned int nbObjFiles; -static struct { +static struct FileStackNodes { struct FileStackNode *nodes; uint32_t nbNodes; } *nodes; @@ -92,19 +92,6 @@ static int64_t readlong(FILE *file) /* * Helper macro for reading bytes from a file, and errors out if it fails to. - * Differs from `tryGetc` in that the backing function is fgetc(1). - * Not as a function to avoid overhead in the general case. - * @param var The variable to stash the number into - * @param file The file to read from. Its position will be advanced - * @param ... A format string and related arguments; note that an extra string - * argument is provided, the reason for failure - */ -#define tryFgetc(var, file, ...) \ - tryRead(fgetc, int, EOF, var, file, __VA_ARGS__) - -/* - * Helper macro for reading bytes from a file, and errors out if it fails to. - * Differs from `tryGetc` in that the backing function is fgetc(1). * Not as a function to avoid overhead in the general case. * @param var The variable to stash the number into * @param file The file to read from. Its position will be advanced diff --git a/src/link/output.c b/src/link/output.c index 7c3bee24..95190c83 100644 --- a/src/link/output.c +++ b/src/link/output.c @@ -36,12 +36,14 @@ struct SortedSymbol { uint16_t addr; }; +struct SortedSections { + struct SortedSection *sections; + struct SortedSection *zeroLenSections; +}; + static struct { uint32_t nbBanks; // Size of the array below (which may be NULL if this is 0) - struct SortedSections { - struct SortedSection *sections; - struct SortedSection *zeroLenSections; - } *banks; + struct SortedSections *banks; } sections[SECTTYPE_INVALID]; // Defines the order in which types are output to the sym and map files diff --git a/src/link/script.c b/src/link/script.c index b2f83570..e2dd7470 100644 --- a/src/link/script.c +++ b/src/link/script.c @@ -19,7 +19,7 @@ char *includeFileName; static uint32_t lineNo; -static struct { +static struct FileNode { FILE *file; uint32_t lineNo; char *name; @@ -153,14 +153,16 @@ enum LinkerScriptCommand { COMMAND_INVALID }; +union LinkerScriptTokenAttr { + enum LinkerScriptCommand command; + enum SectionType secttype; + uint32_t number; + char *string; +}; + struct LinkerScriptToken { enum LinkerScriptTokenType type; - union LinkerScriptTokenAttr { - enum LinkerScriptCommand command; - enum SectionType secttype; - uint32_t number; - char *string; - } attr; + union LinkerScriptTokenAttr attr; }; static char const * const commands[] = { diff --git a/src/link/sdas_obj.c b/src/link/sdas_obj.c index caad0365..2ace1fa5 100644 --- a/src/link/sdas_obj.c +++ b/src/link/sdas_obj.c @@ -225,10 +225,11 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { // Now, let's parse the rest of the lines as they come! - struct { + struct FileSection { struct Section *section; uint16_t writeIndex; - } *fileSections = NULL; + }; + struct FileSection *fileSections = NULL; struct Symbol **fileSymbols = malloc(sizeof(*fileSymbols) * expectedNbSymbols); size_t nbSections = 0, nbSymbols = 0; @@ -240,19 +241,18 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { if (!data) fatal(where, lineNo, "Failed to alloc data buffer: %s", strerror(errno)); + for (;;) { lineType = nextLine(&line, &bufLen, &lineNo, where, file); if (lineType == EOF) break; switch (lineType) { - uint32_t tmp; - case 'M': // Module name case 'O': // Assembler flags // Ignored break; - case 'A': + case 'A': { if (nbSections == expectedNbAreas) warning(where, lineNo, "Got more 'A' lines than the expected %" PRIu32, expectedNbAreas); fileSections = realloc(fileSections, sizeof(*fileSections) * (nbSections + 1)); @@ -276,7 +276,9 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { expectToken("size", 'A'); getToken(NULL, "'A' line is too short"); - tmp = parseNumber(where, lineNo, token, numberType); + + uint32_t tmp = parseNumber(where, lineNo, token, numberType); + if (tmp > UINT16_MAX) fatal(where, lineNo, "Area \"%s\" is larger than the GB address space!?", curSection->name); curSection->size = tmp; @@ -353,6 +355,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { #undef curSection ++nbSections; break; + } case 'S': if (nbSymbols == expectedNbSymbols) @@ -465,7 +468,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { // Importantly, now we know that `nbBytes != 0`, which means "pending data" break; - case 'R': // Supposed to directly follow `T` + case 'R': { // Supposed to directly follow `T` if (nbBytes == 0) { warning(where, lineNo, "'R' line with no 'T' line, ignoring"); break; @@ -727,6 +730,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) { nbBytes = 0; // Do not allow two R lines to refer to the same T line break; + } case 'P': default: diff --git a/src/link/section.c b/src/link/section.c index 5ef2813e..89dde79d 100644 --- a/src/link/section.c +++ b/src/link/section.c @@ -15,21 +15,21 @@ HashMap sections; -struct ForEachArg { +struct ForEachSectionArg { void (*callback)(struct Section *section, void *arg); void *arg; }; static void forEach(void *section, void *arg) { - struct ForEachArg *callbackArg = (struct ForEachArg *)arg; + struct ForEachSectionArg *callbackArg = (struct ForEachSectionArg *)arg; callbackArg->callback((struct Section *)section, callbackArg->arg); } void sect_ForEach(void (*callback)(struct Section *, void *), void *arg) { - struct ForEachArg callbackArg = { .callback = callback, .arg = arg}; + struct ForEachSectionArg callbackArg = { .callback = callback, .arg = arg}; hash_ForEach(sections, forEach, &callbackArg); } diff --git a/src/link/symbol.c b/src/link/symbol.c index 9b7089b1..c62ebd72 100644 --- a/src/link/symbol.c +++ b/src/link/symbol.c @@ -13,21 +13,21 @@ HashMap symbols; -struct ForEachArg { +struct ForEachSymbolArg { void (*callback)(struct Symbol *symbol, void *arg); void *arg; }; static void forEach(void *symbol, void *arg) { - struct ForEachArg *callbackArg = (struct ForEachArg *)arg; + struct ForEachSymbolArg *callbackArg = (struct ForEachSymbolArg *)arg; callbackArg->callback((struct Symbol *)symbol, callbackArg->arg); } void sym_ForEach(void (*callback)(struct Symbol *, void *), void *arg) { - struct ForEachArg callbackArg = { .callback = callback, .arg = arg}; + struct ForEachSymbolArg callbackArg = { .callback = callback, .arg = arg}; hash_ForEach(symbols, forEach, &callbackArg); } diff --git a/test/gfx/randtilegen.c b/test/gfx/randtilegen.c index 084e5990..7555433b 100644 --- a/test/gfx/randtilegen.c +++ b/test/gfx/randtilegen.c @@ -32,7 +32,7 @@ struct Attributes { static unsigned long long randbits = 0; static unsigned char randcount = 0; -static _Noreturn void fatal(char const *error) { +_Noreturn static void fatal(char const *error) { fprintf(stderr, "FATAL: %s\n", error); exit(1); }