From 0c962345324ce3dc45108facdc2aab5af431bbf3 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Thu, 17 Jul 2025 14:59:51 -0400 Subject: [PATCH] Use concrete types instead of `auto` when convenient and not redundant (#1757) --- include/diagnostics.hpp | 2 +- include/gfx/main.hpp | 2 +- include/gfx/rgba.hpp | 4 +++- include/itertools.hpp | 2 +- src/asm/lexer.cpp | 16 ++++++------- src/asm/macro.cpp | 2 +- src/asm/output.cpp | 2 +- src/gfx/main.cpp | 10 ++++---- src/gfx/pal_packing.cpp | 52 ++++++++++++++++++++--------------------- src/gfx/pal_sorting.cpp | 2 +- src/gfx/pal_spec.cpp | 23 +++++++++--------- src/gfx/reverse.cpp | 6 ++--- src/link/assign.cpp | 2 +- src/link/output.cpp | 6 ++--- src/link/sdas_obj.cpp | 2 +- src/link/section.cpp | 2 +- 16 files changed, 67 insertions(+), 68 deletions(-) diff --git a/include/diagnostics.hpp b/include/diagnostics.hpp index 2ca67953..7de45e01 100644 --- a/include/diagnostics.hpp +++ b/include/diagnostics.hpp @@ -133,7 +133,7 @@ std::string Diagnostics::processWarningFlag(char const *flag) { // Try to match the flag against a parametric warning // If there was an equals sign, it will have set `param`; if not, `param` will be 0, which // applies to all levels - for (auto const ¶mWarning : paramWarnings) { + for (ParamWarning const ¶mWarning : paramWarnings) { W baseID = paramWarning.firstID; uint8_t maxParam = paramWarning.lastID - baseID + 1; assume(paramWarning.defaultLevel <= maxParam); diff --git a/include/gfx/main.hpp b/include/gfx/main.hpp index 16aa930f..6937eb85 100644 --- a/include/gfx/main.hpp +++ b/include/gfx/main.hpp @@ -110,7 +110,7 @@ struct Palette { }; // Flipping tends to happen fairly often, so take a bite out of dcache to speed it up -static constexpr auto flipTable = ([]() constexpr { +static std::array flipTable = ([]() constexpr { std::array table{}; for (uint16_t i = 0; i < table.size(); i++) { // To flip all the bits, we'll flip both nibbles, then each nibble half, etc. diff --git a/include/gfx/rgba.hpp b/include/gfx/rgba.hpp index e26389f0..859564dc 100644 --- a/include/gfx/rgba.hpp +++ b/include/gfx/rgba.hpp @@ -33,7 +33,9 @@ struct Rgba { // Returns this RGBA as a 32-bit number that can be printed in hex (`%08x`) to yield its CSS // representation uint32_t toCSS() const { - auto shl = [](uint8_t val, unsigned shift) { return static_cast(val) << shift; }; + constexpr auto shl = [](uint8_t val, unsigned shift) { + return static_cast(val) << shift; + }; return shl(red, 24) | shl(green, 16) | shl(blue, 8) | shl(alpha, 0); } diff --git a/include/itertools.hpp b/include/itertools.hpp index 805a1838..0ffeba65 100644 --- a/include/itertools.hpp +++ b/include/itertools.hpp @@ -22,7 +22,7 @@ class EnumSeq { return *this; } - auto operator*() const { return _value; } + T operator*() const { return _value; } bool operator==(Iterator const &rhs) const { return _value == rhs._value; } }; diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index dff28c81..9800381f 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -685,7 +685,7 @@ static uint32_t readBracketedMacroArgNum() { static std::shared_ptr readMacroArg(char name) { if (name == '@') { - auto str = fstk_GetUniqueIDStr(); + std::shared_ptr str = fstk_GetUniqueIDStr(); if (!str) { error("'\\@' cannot be used outside of a macro or REPT/FOR block"); } @@ -697,7 +697,7 @@ static std::shared_ptr readMacroArg(char name) { return nullptr; } - auto str = macroArgs->getAllArgs(); + std::shared_ptr str = macroArgs->getAllArgs(); assume(str); // '\#' should always be defined (at least as an empty string) return str; } else if (name == '<') { @@ -713,7 +713,7 @@ static std::shared_ptr readMacroArg(char name) { return nullptr; } - auto str = macroArgs->getArg(num); + std::shared_ptr str = macroArgs->getArg(num); if (!str) { error("Macro argument '\\<%" PRId32 ">' not defined", num); } @@ -727,7 +727,7 @@ static std::shared_ptr readMacroArg(char name) { return nullptr; } - auto str = macroArgs->getArg(name - '0'); + std::shared_ptr str = macroArgs->getArg(name - '0'); if (!str) { error("Macro argument '\\%c' not defined", name); } @@ -840,7 +840,7 @@ static int peek() { } else if (c == '{' && !lexerState->disableInterpolation) { // If character is an open brace, do symbol interpolation shiftChar(); - if (auto str = readInterpolation(0); str) { + if (std::shared_ptr str = readInterpolation(0); str) { beginExpansion(str, *str); } return peek(); @@ -1341,7 +1341,7 @@ static std::shared_ptr readInterpolation(size_t depth) { for (Defer reset{[&] { lexerState->disableInterpolation = disableInterpolation; }};;) { if (int c = peek(); c == '{') { // Nested interpolation shiftChar(); - if (auto str = readInterpolation(depth + 1); str) { + if (std::shared_ptr str = readInterpolation(depth + 1); str) { beginExpansion(str, *str); } continue; // Restart, reading from the new buffer @@ -1501,7 +1501,7 @@ static void appendCharInLiteral(std::string &str, int c) { case '9': case '<': shiftChar(); - if (auto arg = readMacroArg(c); arg) { + if (std::shared_ptr arg = readMacroArg(c); arg) { appendExpandedString(str, *arg); } break; @@ -1523,7 +1523,7 @@ static void appendCharInLiteral(std::string &str, int c) { // We'll be exiting the string/character scope, so re-enable expansions // (Not interpolations, since they're handled by the function itself...) lexerState->disableMacroArgs = false; - if (auto interpolation = readInterpolation(0); interpolation) { + if (std::shared_ptr interpolation = readInterpolation(0); interpolation) { appendExpandedString(str, *interpolation); } lexerState->disableMacroArgs = true; diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index 57816d9c..90a0eb76 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -37,7 +37,7 @@ std::shared_ptr MacroArgs::getAllArgs() const { str->reserve(len + 1); // 1 for comma for (uint32_t i = shift; i < nbArgs; i++) { - auto const &arg = args[i]; + std::shared_ptr const &arg = args[i]; str->append(*arg); diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 4b8e550a..3d4d9896 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -499,7 +499,7 @@ static bool dumpMacros(FILE *file) { std::sort(RANGE(macros), compareSymbols); for (Symbol const *sym : macros) { - auto const &body = sym->getMacro(); + ContentSpan const &body = sym->getMacro(); fprintf(file, "macro %s\n", sym->name.c_str()); fwrite(body.ptr.get(), 1, body.size, file); fputs("endm\n", file); diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index fb4ec63e..9a2b1266 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -692,7 +692,7 @@ static void verboseOutputConfig() { fputs("\t[\n", stderr); for (auto const &pal : options.palSpec) { fputs("\t\t", stderr); - for (auto &color : pal) { + for (auto const &color : pal) { if (color) { fprintf(stderr, "#%06x, ", color->toCSS() >> 8); } else { @@ -757,14 +757,14 @@ int main(int argc, char *argv[]) { // We need to allocate a new arg pool for each at-file, so as not to invalidate pointers // previous at-files may have generated to their own arg pools. // But for the same reason, the arg pool must also outlive the at-file's stack entry! - auto &argPool = argPools.emplace_back(); + std::vector &argPool = argPools.emplace_back(); // Copy `argv[0]` for error reporting, and because option parsing skips it AtFileStackEntry &stackEntry = atFileStack.emplace_back(musl_optind, std::vector{atFileName}); // It would be nice to compute the char pointers on the fly, but reallocs don't allow // that; so we must compute the offsets after the pool is fixed - auto offsets = readAtFile(&musl_optarg[1], argPool); + std::vector offsets = readAtFile(&musl_optarg[1], argPool); stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs for (size_t ofs : offsets) { stackEntry.argv.push_back(&argPool.data()[ofs]); @@ -797,7 +797,7 @@ int main(int argc, char *argv[]) { curArgc = argc; curArgv = argv; } else { - auto &vec = atFileStack.back().argv; + std::vector &vec = atFileStack.back().argv; curArgc = vec.size(); curArgv = vec.data(); } @@ -816,7 +816,7 @@ int main(int argc, char *argv[]) { auto autoOutPath = [](bool autoOptEnabled, std::string &path, char const *extension) { if (autoOptEnabled) { - auto &image = localOptions.groupOutputs ? options.output : options.input; + std::string &image = localOptions.groupOutputs ? options.output : options.input; if (image.empty()) { fatalWithUsage( "No %s specified", diff --git a/src/gfx/pal_packing.cpp b/src/gfx/pal_packing.cpp index 6a99a0ce..82ea820e 100644 --- a/src/gfx/pal_packing.cpp +++ b/src/gfx/pal_packing.cpp @@ -178,7 +178,7 @@ public: // Returns the number of distinct colors size_t volume() const { return uniqueColors().size(); } bool canFit(ProtoPalette const &protoPal) const { - auto &colors = uniqueColors(); + std::unordered_set &colors = uniqueColors(); colors.insert(RANGE(protoPal)); return colors.size() <= options.maxOpaqueColors(); } @@ -206,7 +206,8 @@ public: uint32_t relSize = 0; for (uint16_t color : protoPal) { - auto multiplicity = // How many of our proto-palettes does this color also belong to? + // How many of our proto-palettes does this color also belong to? + uint32_t multiplicity = std::count_if(RANGE(*this), [this, &color](ProtoPalAttrs const &attrs) { ProtoPalette const &pal = (*_protoPals)[attrs.protoPalIndex]; return std::find(RANGE(pal), color) != pal.end(); @@ -225,21 +226,36 @@ public: // Computes the "relative size" of a set of proto-palettes on this palette template - auto combinedVolume(Iter &&begin, Iter const &end, std::vector const &protoPals) + size_t combinedVolume(Iter &&begin, Iter const &end, std::vector const &protoPals) const { - auto &colors = uniqueColors(); + std::unordered_set &colors = uniqueColors(); addUniqueColors(colors, std::forward(begin), end, protoPals); return colors.size(); } // Computes the "relative size" of a set of colors on this palette template - auto combinedVolume(Iter &&begin, Iter &&end) const { - auto &colors = uniqueColors(); + size_t combinedVolume(Iter &&begin, Iter &&end) const { + std::unordered_set &colors = uniqueColors(); colors.insert(std::forward(begin), std::forward(end)); return colors.size(); } }; +static void verboseOutputAssignments( + std::vector const &assignments, std::vector const &protoPalettes +) { + for (AssignedProtos const &assignment : assignments) { + fputs("{ ", stderr); + for (ProtoPalAttrs const &attrs : assignment) { + fprintf(stderr, "[%zu] ", attrs.protoPalIndex); + for (uint16_t colorIndex : protoPalettes[attrs.protoPalIndex]) { + fprintf(stderr, "%04" PRIx16 ", ", colorIndex); + } + } + fprintf(stderr, "} (volume = %zu)\n", assignment.volume()); + } +} + static void decant( std::vector &assignments, std::vector const &protoPalettes ) { @@ -423,7 +439,7 @@ std::tuple, size_t> attrs.protoPalIndex, bestPalIndex ); - auto &bestPal = assignments[bestPalIndex]; + AssignedProtos &bestPal = assignments[bestPalIndex]; // Add the color to that palette bestPal.assign(std::move(attrs)); @@ -550,16 +566,7 @@ std::tuple, size_t> // LCOV_EXCL_START if (options.verbosity >= Options::VERB_INTERM) { - for (auto &&assignment : assignments) { - fputs("{ ", stderr); - for (auto &&attrs : assignment) { - fprintf(stderr, "[%zu] ", attrs.protoPalIndex); - for (auto &&colorIndex : protoPalettes[attrs.protoPalIndex]) { - fprintf(stderr, "%04" PRIx16 ", ", colorIndex); - } - } - fprintf(stderr, "} (volume = %zu)\n", assignment.volume()); - } + verboseOutputAssignments(assignments, protoPalettes); } // LCOV_EXCL_STOP @@ -569,16 +576,7 @@ std::tuple, size_t> // LCOV_EXCL_START if (options.verbosity >= Options::VERB_INTERM) { - for (auto &&assignment : assignments) { - fputs("{ ", stderr); - for (auto &&attrs : assignment) { - fprintf(stderr, "[%zu] ", attrs.protoPalIndex); - for (auto &&colorIndex : protoPalettes[attrs.protoPalIndex]) { - fprintf(stderr, "%04" PRIx16 ", ", colorIndex); - } - } - fprintf(stderr, "} (volume = %zu)\n", assignment.volume()); - } + verboseOutputAssignments(assignments, protoPalettes); } // LCOV_EXCL_STOP diff --git a/src/gfx/pal_sorting.cpp b/src/gfx/pal_sorting.cpp index a5351e12..8c9112e3 100644 --- a/src/gfx/pal_sorting.cpp +++ b/src/gfx/pal_sorting.cpp @@ -18,7 +18,7 @@ void sortIndexed( options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes using embedded palette...\n"); auto pngToRgb = [&palRGB, &palAlphaSize, &palAlpha](int index) { - auto const &c = palRGB[index]; + png_color const &c = palRGB[index]; return Rgba( c.red, c.green, c.blue, palAlpha && index < palAlphaSize ? palAlpha[index] : 0xFF ); diff --git a/src/gfx/pal_spec.cpp b/src/gfx/pal_spec.cpp index dc43c664..09af880e 100644 --- a/src/gfx/pal_spec.cpp +++ b/src/gfx/pal_spec.cpp @@ -26,6 +26,8 @@ using namespace std::string_view_literals; +static char const *hexDigits = "0123456789ABCDEFabcdef"; + template // Should be std::string or std::string_view static void skipWhitespace(Str const &str, size_t &pos) { pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length()); @@ -70,10 +72,10 @@ void parseInlinePalSpec(char const * const rawArg) { " ", rawArg ); - for (auto i = ofs; i; --i) { + for (size_t i = ofs; i; --i) { putc(' ', stderr); } - for (auto i = len; i; --i) { + for (size_t i = len; i; --i) { putc('^', stderr); } putc('\n', stderr); @@ -93,7 +95,7 @@ void parseInlinePalSpec(char const * const rawArg) { color = {}; n += literal_strlen("none"); } else { - auto pos = std::min(arg.find_first_not_of("0123456789ABCDEFabcdef"sv, n), arg.length()); + size_t pos = std::min(arg.find_first_not_of(hexDigits, n), arg.length()); switch (pos - n) { case 3: color = Rgba( @@ -196,7 +198,7 @@ static T readLE(U const *bytes) { static bool readLine(std::filebuf &file, std::string &buffer) { assume(buffer.empty()); for (;;) { - auto c = file.sbumpc(); + int c = file.sbumpc(); if (c == std::filebuf::traits_type::eof()) { return !buffer.empty(); } @@ -396,8 +398,7 @@ static void parseHEXFile(std::filebuf &file) { continue; } - if (line.length() != 6 - || line.find_first_not_of("0123456789ABCDEFabcdef"sv) != std::string::npos) { + if (line.length() != 6 || line.find_first_not_of(hexDigits) != std::string::npos) { error( "Failed to parse color #%d (\"%s\"): invalid \"rrggbb\" line", nbColors + 1, @@ -432,7 +433,7 @@ static void parseACTFile(std::filebuf &file) { // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626 std::array buf{}; - auto len = file.sgetn(buf.data(), buf.size()); + size_t len = file.sgetn(buf.data(), buf.size()); uint16_t nbColors = 256; if (len == 772) { @@ -555,8 +556,7 @@ static void parseGBCFile(std::filebuf &file) { for (;;) { char buf[2 * 4]; - auto len = file.sgetn(buf, sizeof(buf)); - if (len == 0) { + if (size_t len = file.sgetn(buf, sizeof(buf)); len == 0) { break; } else if (len != sizeof(buf)) { error( @@ -625,8 +625,7 @@ void parseDmgPalSpec(char const * const rawArg) { std::string_view arg(rawArg); - if (arg.length() != 2 - || arg.find_first_not_of("0123456789ABCDEFabcdef"sv) != std::string_view::npos) { + if (arg.length() != 2 || arg.find_first_not_of(hexDigits) != std::string_view::npos) { error("Unknown DMG palette specification \"%s\"", rawArg); return; } @@ -660,7 +659,7 @@ void parseBackgroundPalSpec(char const *arg) { return; } - size_t size = strspn(&arg[1], "0123456789ABCDEFabcdef"); + size_t size = strspn(&arg[1], hexDigits); switch (size) { case 3: options.bgColor = Rgba(singleToHex(arg[1]), singleToHex(arg[2]), singleToHex(arg[3]), 0xFF); diff --git a/src/gfx/reverse.cpp b/src/gfx/reverse.cpp index e78849f4..ceebc6e7 100644 --- a/src/gfx/reverse.cpp +++ b/src/gfx/reverse.cpp @@ -68,12 +68,12 @@ static void pngWarning(png_structp png, char const *msg) { } static void writePng(png_structp png, png_bytep data, size_t length) { - auto &pngFile = *static_cast(png_get_io_ptr(png)); + File &pngFile = *static_cast(png_get_io_ptr(png)); pngFile->sputn(reinterpret_cast(data), length); } static void flushPng(png_structp png) { - auto &pngFile = *static_cast(png_get_io_ptr(png)); + File &pngFile = *static_cast(png_get_io_ptr(png)); pngFile->pubsync(); } @@ -128,7 +128,7 @@ void reverse() { } options.verbosePrint(Options::VERB_LOG_ACT, "Reading tiles...\n"); - auto const tiles = readInto(options.output); + std::vector const tiles = readInto(options.output); uint8_t tileSize = 8 * options.bitDepth; if (tiles.size() % tileSize != 0) { fatal( diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 1acc95b4..4fd9b1be 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -351,9 +351,9 @@ static void categorizeSection(Section §ion) { } std::deque
§ions = unassignedSections[constraints]; - auto pos = sections.begin(); // Insert section while keeping the list sorted by decreasing size + auto pos = sections.begin(); while (pos != sections.end() && (*pos)->size > section.size) { pos++; } diff --git a/src/link/output.cpp b/src/link/output.cpp index 65250f25..7c1e1d8b 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -86,12 +86,12 @@ void out_AddSection(Section const §ion) { std::deque
&bankSections = section.size ? sections[section.type][targetBank].sections : sections[section.type][targetBank].zeroLenSections; - auto pos = bankSections.begin(); + // Insert section while keeping the list sorted by increasing org + auto pos = bankSections.begin(); while (pos != bankSections.end() && (*pos)->org < section.org) { pos++; } - bankSections.insert(pos, §ion); } @@ -347,7 +347,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u std::string parentName = sym->name.substr(0, pos); if (Symbol const *parentSym = sym_GetSymbol(parentName); parentSym && std::holds_alternative