From d6a28a6259400da4ce991816e2a155d4d19eb227 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 24 Jul 2025 18:08:17 -0400 Subject: [PATCH] Prefer pre-increment/decrement operators in `for` loops --- include/diagnostics.hpp | 2 +- include/gfx/main.hpp | 2 +- src/asm/charmap.cpp | 2 +- src/asm/lexer.cpp | 4 ++-- src/asm/macro.cpp | 4 ++-- src/asm/output.cpp | 2 +- src/asm/parser.y | 8 ++++---- src/asm/section.cpp | 2 +- src/extern/getopt.cpp | 10 +++++----- src/fix/main.cpp | 14 +++++++------- src/gfx/pal_spec.cpp | 4 ++-- src/link/main.cpp | 2 +- src/link/object.cpp | 18 +++++++++--------- src/link/output.cpp | 20 ++++++++++---------- src/link/patch.cpp | 2 +- 15 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/diagnostics.hpp b/include/diagnostics.hpp index 6274d93f..1e96fecd 100644 --- a/include/diagnostics.hpp +++ b/include/diagnostics.hpp @@ -160,7 +160,7 @@ std::string Diagnostics::processWarningFlag(char const *flag) { } // Set the first to enabled/error, and disable the rest - for (uint32_t ofs = 0; ofs < maxParam; ofs++) { + for (uint32_t ofs = 0; ofs < maxParam; ++ofs) { if (WarningState &warning = state.flagStates[baseID + ofs]; ofs < *param) { warning.update(flagState); } else { diff --git a/include/gfx/main.hpp b/include/gfx/main.hpp index 6937eb85..9879142e 100644 --- a/include/gfx/main.hpp +++ b/include/gfx/main.hpp @@ -112,7 +112,7 @@ struct Palette { // Flipping tends to happen fairly often, so take a bite out of dcache to speed it up static std::array flipTable = ([]() constexpr { std::array table{}; - for (uint16_t i = 0; i < table.size(); i++) { + for (uint16_t i = 0; i < table.size(); ++i) { // To flip all the bits, we'll flip both nibbles, then each nibble half, etc. uint16_t byte = i; byte = (byte & 0b0000'1111) << 4 | (byte & 0b1111'0000) >> 4; diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index 01ce2862..d427efea 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -45,7 +45,7 @@ bool forEachChar(Charmap const &charmap, F callback) { if (node.isTerminal() && !callback(nodeIdx, mapping)) { return false; } - for (unsigned c = 0; c < std::size(node.next); c++) { + for (unsigned c = 0; c < std::size(node.next); ++c) { if (size_t nextIdx = node.next[c]; nextIdx) { prefixes.push({nextIdx, mapping + static_cast(c)}); } diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 32ccc587..d7685f0b 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -1068,7 +1068,7 @@ static bool isValidDigit(char c) { } static bool checkDigitErrors(char const *digits, size_t n, char const *type) { - for (size_t i = 0; i < n; i++) { + for (size_t i = 0; i < n; ++i) { char c = digits[i]; if (!isValidDigit(c)) { @@ -1081,7 +1081,7 @@ static bool checkDigitErrors(char const *digits, size_t n, char const *type) { return false; } - for (size_t j = i + 1; j < n; j++) { + for (size_t j = i + 1; j < n; ++j) { if (c == digits[j]) { error("Repeated digit for %s constant %s", type, printChar(c)); return false; diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index 90a0eb76..a8f597b6 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -29,14 +29,14 @@ std::shared_ptr MacroArgs::getAllArgs() const { size_t len = 0; - for (uint32_t i = shift; i < nbArgs; i++) { + for (uint32_t i = shift; i < nbArgs; ++i) { len += args[i]->length() + 1; // 1 for comma } auto str = std::make_shared(); str->reserve(len + 1); // 1 for comma - for (uint32_t i = shift; i < nbArgs; i++) { + for (uint32_t i = shift; i < nbArgs; ++i) { std::shared_ptr const &arg = args[i]; str->append(*arg); diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 38c3f489..caf54c52 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -332,7 +332,7 @@ void out_WriteObject() { putLong(sectionList.size(), file); putLong(fileStackNodes.size(), file); - for (auto it = fileStackNodes.begin(); it != fileStackNodes.end(); it++) { + for (auto it = fileStackNodes.begin(); it != fileStackNodes.end(); ++it) { FileStackNode const &node = **it; writeFileStackNode(node, file); diff --git a/src/asm/parser.y b/src/asm/parser.y index c38394da..4416a38e 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -2802,7 +2802,7 @@ static uint32_t strToNum(std::vector const &s) { uint32_t r = 0; - for (uint32_t i = length < 4 ? 0 : length - 4; i < length; i++) { + for (uint32_t i = length < 4 ? 0 : length - 4; i < length; ++i) { r <<= 8; r |= static_cast(s[i]); } @@ -2973,7 +2973,7 @@ static size_t charlenUTF8(std::string const &str) { std::string_view view = str; size_t len; - for (len = 0; charmap_ConvertNext(view, nullptr); len++) {} + for (len = 0; charmap_ConvertNext(view, nullptr); ++len) {} return len; } @@ -2983,7 +2983,7 @@ static std::string strcharUTF8(std::string const &str, uint32_t idx) { size_t charLen = 1; // Advance to starting index in source string. - for (uint32_t curIdx = 0; charLen && curIdx < idx; curIdx++) { + for (uint32_t curIdx = 0; charLen && curIdx < idx; ++curIdx) { charLen = charmap_ConvertNext(view, nullptr); } @@ -3006,7 +3006,7 @@ static std::string charsubUTF8(std::string const &str, uint32_t pos) { size_t charLen = 1; // Advance to starting position in source string. - for (uint32_t curPos = 1; charLen && curPos < pos; curPos++) { + for (uint32_t curPos = 1; charLen && curPos < pos; ++curPos) { charLen = charmap_ConvertNext(view, nullptr); } diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 060efb77..dc7c815a 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -807,7 +807,7 @@ void sect_RelBytes(uint32_t n, std::vector const &exprs) { return; } - for (uint32_t i = 0; i < n; i++) { + for (uint32_t i = 0; i < n; ++i) { if (Expression const &expr = exprs[i % exprs.size()]; !expr.isKnown()) { createPatch(PATCHTYPE_BYTE, expr, i); writeByte(0); diff --git a/src/extern/getopt.cpp b/src/extern/getopt.cpp index 78106181..d04ec5e3 100644 --- a/src/extern/getopt.cpp +++ b/src/extern/getopt.cpp @@ -118,7 +118,7 @@ static void permute(char **argv, int dest, int src) { char *tmp = argv[src]; int i; - for (i = src; i > dest; i--) { + for (i = src; i > dest; --i) { argv[i] = argv[i - 1]; } argv[dest] = tmp; @@ -146,7 +146,7 @@ static int musl_getopt_long( skipped = musl_optind; if (optstring[0] != '+' && optstring[0] != '-') { int i; - for (i = musl_optind;; i++) { + for (i = musl_optind;; ++i) { if (i >= argc || !argv[i]) { return -1; } @@ -161,7 +161,7 @@ static int musl_getopt_long( if (resumed > skipped) { int i, cnt = musl_optind - resumed; - for (i = 0; i < cnt; i++) { + for (i = 0; i < cnt; ++i) { permute(argv, skipped, musl_optind - 1); } musl_optind = skipped + cnt; @@ -180,7 +180,7 @@ static int musl_getopt_long_core( int i, cnt, match = 0; char *arg = 0, *opt, *start = argv[musl_optind] + 1; - for (cnt = i = 0; longopts[i].name; i++) { + for (cnt = i = 0; longopts[i].name; ++i) { char const *name = longopts[i].name; opt = start; @@ -205,7 +205,7 @@ static int musl_getopt_long_core( if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) { int l = arg - start; - for (i = 0; optstring[i]; i++) { + for (i = 0; optstring[i]; ++i) { int j = 0; while (j < l && start[j] == optstring[i + j]) { diff --git a/src/fix/main.cpp b/src/fix/main.cpp index c312effa..a5be7224 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -881,7 +881,7 @@ static void overwriteBytes( uint8_t *rom0, uint16_t startAddr, uint8_t const *fixed, uint8_t size, char const *areaName ) { if (!overwriteRom) { - for (uint8_t i = 0; i < size; i++) { + for (uint8_t i = 0; i < size; ++i) { uint8_t origByte = rom0[i + startAddr]; if (origByte != 0 && origByte != fixed[i]) { @@ -1049,7 +1049,7 @@ static void ++nbBanks; // Update global checksum, too - for (uint16_t i = 0; i < bankLen; i++) { + for (uint16_t i = 0; i < bankLen; ++i) { globalSum += romx[totalRomxLen + i]; } totalRomxLen += bankLen; @@ -1097,7 +1097,7 @@ static void if (fixSpec & (FIX_HEADER_SUM | TRASH_HEADER_SUM)) { uint8_t sum = 0; - for (uint16_t i = 0x134; i < 0x14D; i++) { + for (uint16_t i = 0x134; i < 0x14D; ++i) { sum -= rom0[i] + 1; } @@ -1107,10 +1107,10 @@ static void if (fixSpec & (FIX_GLOBAL_SUM | TRASH_GLOBAL_SUM)) { // Computation of the global checksum does not include the checksum bytes assume(rom0Len >= 0x14E); - for (uint16_t i = 0; i < 0x14E; i++) { + for (uint16_t i = 0; i < 0x14E; ++i) { globalSum += rom0[i]; } - for (uint16_t i = 0x150; i < rom0Len; i++) { + for (uint16_t i = 0x150; i < rom0Len; ++i) { globalSum += rom0[i]; } // Pipes have already read ROMX and updated globalSum, but not regular files @@ -1118,7 +1118,7 @@ static void for (;;) { ssize_t bankLen = readBytes(input, bank, sizeof(bank)); - for (uint16_t i = 0; i < bankLen; i++) { + for (uint16_t i = 0; i < bankLen; ++i) { globalSum += bank[i]; } if (bankLen != sizeof(bank)) { @@ -1558,7 +1558,7 @@ int main(int argc, char *argv[]) { memcpy(logo, nintendoLogo, sizeof(nintendoLogo)); } if (fixSpec & TRASH_LOGO) { - for (uint16_t i = 0; i < sizeof(logo); i++) { + for (uint16_t i = 0; i < sizeof(logo); ++i) { logo[i] = 0xFF ^ logo[i]; } } diff --git a/src/gfx/pal_spec.cpp b/src/gfx/pal_spec.cpp index fa653cdc..3a1ac79b 100644 --- a/src/gfx/pal_spec.cpp +++ b/src/gfx/pal_spec.cpp @@ -580,10 +580,10 @@ static void parseGBCFile(char const *, std::filebuf &file) { } static bool checkPngSwatch(std::vector const &pixels, uint32_t base, uint32_t swatchSize) { - for (uint32_t y = 0; y < swatchSize; y++) { + for (uint32_t y = 0; y < swatchSize; ++y) { uint32_t yOffset = y * swatchSize * options.nbColorsPerPal + base; - for (uint32_t x = 0; x < swatchSize; x++) { + for (uint32_t x = 0; x < swatchSize; ++x) { if (x == 0 && y == 0) { continue; } diff --git a/src/link/main.cpp b/src/link/main.cpp index 8f97a02b..6aefbbae 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -370,7 +370,7 @@ int main(int argc, char *argv[]) { } // Read all object files first, - for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++) { + for (obj_Setup(argc - curArgIndex); curArgIndex < argc; ++curArgIndex) { obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1); } diff --git a/src/link/object.cpp b/src/link/object.cpp index a5ad37f3..ac267d2d 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -126,7 +126,7 @@ static void readFileStackNode( depth, file, "%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, nodeID ); node.data = std::vector(depth); - for (uint32_t i = 0; i < depth; i++) { + for (uint32_t i = 0; i < depth; ++i) { tryReadLong( node.iters()[i], file, @@ -389,7 +389,7 @@ static void readSection( ); section.patches.resize(nbPatches); - for (uint32_t i = 0; i < nbPatches; i++) { + for (uint32_t i = 0; i < nbPatches; ++i) { readPatch(file, section.patches[i], fileName, section.name, i, fileNodes); } } @@ -516,7 +516,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { std::vector nbSymPerSect(nbSections, 0); verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols); - for (uint32_t i = 0; i < nbSymbols; i++) { + for (uint32_t i = 0; i < nbSymbols; ++i) { // Read symbol Symbol &symbol = fileSymbols[i]; @@ -532,7 +532,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { std::vector> fileSections(nbSections); verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); - for (uint32_t i = 0; i < nbSections; i++) { + for (uint32_t i = 0; i < nbSections; ++i) { // Read section fileSections[i] = std::make_unique
(); fileSections[i]->nextu = nullptr; @@ -544,7 +544,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { uint32_t nbAsserts; tryReadLong(nbAsserts, file, "%s: Cannot read number of assertions: %s", fileName); verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts); - for (uint32_t i = 0; i < nbAsserts; i++) { + for (uint32_t i = 0; i < nbAsserts; ++i) { Assertion &assertion = patch_AddAssertion(); readAssertion(file, assertion, fileName, i, nodes[fileID]); @@ -553,7 +553,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { } // Give patches' PC section pointers to their sections - for (uint32_t i = 0; i < nbSections; i++) { + for (uint32_t i = 0; i < nbSections; ++i) { if (sect_HasData(fileSections[i]->type)) { for (Patch &patch : fileSections[i]->patches) { linkPatchToPCSect(patch, fileSections); @@ -562,7 +562,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { } // Give symbols' section pointers to their sections - for (uint32_t i = 0; i < nbSymbols; i++) { + for (uint32_t i = 0; i < nbSymbols; ++i) { if (std::holds_alternative