From 14f5e16ae8d028aa319281ec6a8f5d764bc33596 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sat, 19 Jul 2025 15:04:08 -0400 Subject: [PATCH] Prefer pre-inc/dec unless post-inc/dec are necessary --- include/helpers.hpp | 4 +-- src/asm/charmap.cpp | 6 ++--- src/asm/fstack.cpp | 2 +- src/asm/lexer.cpp | 35 +++++++++++++------------ src/asm/main.cpp | 2 +- src/asm/opt.cpp | 2 +- src/asm/parser.y | 26 +++++++++---------- src/asm/section.cpp | 2 +- src/asm/symbol.cpp | 6 ++--- src/asm/warning.cpp | 3 +-- src/diagnostics.cpp | 2 +- src/extern/getopt.cpp | 26 +++++++++---------- src/fix/main.cpp | 59 ++++++++++++++++++++----------------------- src/gfx/warning.cpp | 19 +++++++------- src/link/assign.cpp | 21 ++++++++------- src/link/main.cpp | 2 +- src/link/object.cpp | 2 +- src/link/output.cpp | 8 +++--- src/link/warning.cpp | 2 +- 19 files changed, 111 insertions(+), 118 deletions(-) diff --git a/include/helpers.hpp b/include/helpers.hpp index 63b75c57..19f9ff6f 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -71,7 +71,7 @@ static inline int ctz(unsigned int x) { while (!(x & 1)) { x >>= 1; - cnt++; + ++cnt; } return cnt; } @@ -81,7 +81,7 @@ static inline int clz(unsigned int x) { while (x <= UINT_MAX / 2) { x <<= 1; - cnt++; + ++cnt; } return cnt; } diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index 124267b6..65ed513c 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -238,13 +238,13 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector *output break; } - inputIdx++; // Consume that char + ++inputIdx; // Consume that char if (charmap.nodes[nodeIdx].isTerminal()) { matchIdx = nodeIdx; // This node matches, register it rewindDistance = 0; // If no longer match is found, rewind here } else { - rewindDistance++; + ++rewindDistance; } } @@ -271,7 +271,7 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector *output codepointLen = 1; break; } - codepointLen++; + ++codepointLen; if (state == UTF8_ACCEPT) { break; } diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 045ab930..7a8d5645 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -189,7 +189,7 @@ bool yywrap() { } } // Advance to the next iteration - fileInfoIters.front()++; + ++fileInfoIters.front(); // If this wasn't the last iteration, wrap instead of popping if (fileInfoIters.front() <= context.nbReptIters) { lexer_RestartRept(context.fileInfo->lineNo); diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 38a191fe..2b825ec3 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -373,7 +373,7 @@ void LexerState::clear(uint32_t lineNo_) { } static void nextLine() { - lexerState->lineNo++; + ++lexerState->lineNo; } uint32_t lexer_GetIFDepth() { @@ -501,7 +501,7 @@ LexerState::~LexerState() { bool Expansion::advance() { assume(offset <= size()); - offset++; + ++offset; return offset > size(); } @@ -511,12 +511,11 @@ BufferedContent::~BufferedContent() { void BufferedContent::advance() { assume(offset < std::size(buf)); - offset++; - if (offset == std::size(buf)) { + if (++offset == std::size(buf)) { offset = 0; // Wrap around if necessary } if (size > 0) { - size--; + --size; } } @@ -810,11 +809,11 @@ static int peek() { return c; } - lexerState->macroArgScanDistance++; // Do not consider again + ++lexerState->macroArgScanDistance; // Do not consider again if (c == '\\' && !lexerState->disableMacroArgs) { // If character is a backslash, check for a macro arg - lexerState->macroArgScanDistance++; + ++lexerState->macroArgScanDistance; c = lexerState->peekCharAhead(); if (isMacroChar(c)) { shiftChar(); @@ -854,10 +853,10 @@ static void shiftChar() { if (lexerState->captureBuf) { lexerState->captureBuf->push_back(peek()); } - lexerState->captureSize++; + ++lexerState->captureSize; } - lexerState->macroArgScanDistance--; + --lexerState->macroArgScanDistance; for (;;) { if (!lexerState->expansions.empty()) { @@ -871,7 +870,7 @@ static void shiftChar() { } else { // Advance within the file contents if (std::holds_alternative(lexerState->content)) { - std::get(lexerState->content).offset++; + ++std::get(lexerState->content).offset; } else { std::get(lexerState->content).advance(); } @@ -1008,7 +1007,7 @@ static std::string readAnonLabelRef(char c) { // We come here having already peeked at one char, so no need to do it again do { shiftChar(); - n++; + ++n; } while (peek() == c); return sym_MakeAnonLabelName(n, c == '-'); @@ -1273,7 +1272,7 @@ static uint32_t readGfxConstant() { bitPlaneUpper = bitPlaneUpper << 1 | (pixel >> 1); } if (width < 9) { - width++; + ++width; } } @@ -2124,13 +2123,13 @@ static Token yylex_RAW() { case '(': // Open parentheses inside macro args if (parenDepth < UINT_MAX) { - parenDepth++; + ++parenDepth; } goto append; case ')': // Close parentheses inside macro args if (parenDepth > 0) { - parenDepth--; + --parenDepth; } goto append; @@ -2358,11 +2357,11 @@ static Token yylex_SKIP_TO_ENDR() { switch (readIdentifier(c, false).type) { case T_(POP_FOR): case T_(POP_REPT): - depth++; + ++depth; break; case T_(POP_ENDR): - depth--; + --depth; // `lexer_CaptureRept` has already guaranteed that the `ENDR`s are balanced assume(depth > 0); break; @@ -2483,12 +2482,12 @@ Capture lexer_CaptureRept() { switch (readIdentifier(c, false).type) { case T_(POP_REPT): case T_(POP_FOR): - depth++; + ++depth; break; // Ignore the rest of that line case T_(POP_ENDR): if (depth) { - depth--; + --depth; break; // Ignore the rest of that line } endCapture(capture); diff --git a/src/asm/main.cpp b/src/asm/main.cpp index 4fcac9d4..df00fc25 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -293,7 +293,7 @@ int main(int argc, char *argv[]) { case 'Q': precisionArg = musl_optarg; if (precisionArg[0] == '.') { - precisionArg++; + ++precisionArg; } precision = strtoul(precisionArg, &endptr, 0); diff --git a/src/asm/opt.cpp b/src/asm/opt.cpp index ce7e17c3..8cecf181 100644 --- a/src/asm/opt.cpp +++ b/src/asm/opt.cpp @@ -91,7 +91,7 @@ void opt_Parse(char const *s) { case 'Q': precisionArg = &s[1]; if (precisionArg[0] == '.') { - precisionArg++; + ++precisionArg; } if (strlen(precisionArg) <= 2) { int result; diff --git a/src/asm/parser.y b/src/asm/parser.y index 8b20159f..28c59e95 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -2830,7 +2830,7 @@ static size_t strlenUTF8(std::string const &str, bool printErrors) { state = UTF8_ACCEPT; // fallthrough case UTF8_ACCEPT: - len++; + ++len; break; } } @@ -2840,7 +2840,7 @@ static size_t strlenUTF8(std::string const &str, bool printErrors) { if (printErrors) { error("STRLEN: Incomplete UTF-8 character"); } - len++; + ++len; } return len; @@ -2861,10 +2861,10 @@ static std::string strsliceUTF8(std::string const &str, uint32_t start, uint32_t state = UTF8_ACCEPT; // fallthrough case UTF8_ACCEPT: - curIdx++; + ++curIdx; break; } - index++; + ++index; } // An index 1 past the end of the string is allowed, but will trigger the @@ -2887,16 +2887,16 @@ static std::string strsliceUTF8(std::string const &str, uint32_t start, uint32_t state = UTF8_ACCEPT; // fallthrough case UTF8_ACCEPT: - curIdx++; + ++curIdx; break; } - index++; + ++index; } // Check for partial code point. if (state != UTF8_ACCEPT) { error("STRSLICE: Incomplete UTF-8 character"); - curIdx++; + ++curIdx; } if (curIdx < stop) { @@ -2925,10 +2925,10 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len state = UTF8_ACCEPT; // fallthrough case UTF8_ACCEPT: - curPos++; + ++curPos; break; } - index++; + ++index; } // A position 1 past the end of the string is allowed, but will trigger the @@ -2950,16 +2950,16 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len state = UTF8_ACCEPT; // fallthrough case UTF8_ACCEPT: - curLen++; + ++curLen; break; } - index++; + ++index; } // Check for partial code point. if (state != UTF8_ACCEPT) { error("STRSUB: Incomplete UTF-8 character"); - curLen++; + ++curLen; } if (curLen < len) { @@ -3147,7 +3147,7 @@ static std::string fmt.appendString(str, std::get(args[argIndex])); } - argIndex++; + ++argIndex; } if (argIndex < args.size()) { diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 146226e0..f5eb8aae 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -98,7 +98,7 @@ Section *sect_FindSectionByName(std::string const &name) { #define sectError(...) \ do { \ error(__VA_ARGS__); \ - nbSectErrors++; \ + ++nbSectErrors; \ } while (0) static unsigned int mergeSectUnion( diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index 1314c0e2..6a9b7a7f 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -545,7 +545,7 @@ Symbol *sym_AddAnonLabel() { } std::string anon = sym_MakeAnonLabelName(0, true); // The direction is important! - anonLabelID++; + ++anonLabelID; return addLabel(anon); } @@ -565,8 +565,8 @@ std::string sym_MakeAnonLabelName(uint32_t ofs, bool neg) { id = anonLabelID - ofs; } } else { - ofs--; // We're referencing symbols that haven't been created yet... - if (ofs > UINT32_MAX - anonLabelID) { + // We're referencing symbols that haven't been created yet... + if (--ofs > UINT32_MAX - anonLabelID) { // LCOV_EXCL_START error( "Reference to anonymous label %" PRIu32 " after, when only %" PRIu32 diff --git a/src/asm/warning.cpp b/src/asm/warning.cpp index daa66ab0..0b933f86 100644 --- a/src/asm/warning.cpp +++ b/src/asm/warning.cpp @@ -81,8 +81,7 @@ static void printDiag( static void incrementErrors() { // This intentionally makes 0 act as "unlimited" (or at least "limited to sizeof(unsigned)") - nbErrors++; - if (nbErrors == maxErrors) { + if (++nbErrors == maxErrors) { fprintf( stderr, "Assembly aborted after the maximum of %u error%s! (configure with " diff --git a/src/diagnostics.cpp b/src/diagnostics.cpp index ec99be1c..f0df7cf0 100644 --- a/src/diagnostics.cpp +++ b/src/diagnostics.cpp @@ -71,7 +71,7 @@ std::pair> getInitialWarningState(std::stri } param = param * 10 + (*ptr - '0'); - ptr++; + ++ptr; } while (*ptr); // If we reached the end of the string, truncate it at the '=' diff --git a/src/extern/getopt.cpp b/src/extern/getopt.cpp index ade5f079..78106181 100644 --- a/src/extern/getopt.cpp +++ b/src/extern/getopt.cpp @@ -57,7 +57,7 @@ static int getopt(int argc, char *argv[], char const *optstring) { } if (!musl_optpos) { - musl_optpos++; + ++musl_optpos; } k = mbtowc(&c, argv[musl_optind] + musl_optpos, MB_LEN_MAX); if (k < 0) { @@ -68,12 +68,12 @@ static int getopt(int argc, char *argv[], char const *optstring) { musl_optpos += k; if (!argv[musl_optind][musl_optpos]) { - musl_optind++; + ++musl_optind; musl_optpos = 0; } if (optstring[0] == '-' || optstring[0] == '+') { - optstring++; + ++optstring; } i = 0; @@ -83,7 +83,7 @@ static int getopt(int argc, char *argv[], char const *optstring) { if (l > 0) { i += l; } else { - i++; + ++i; } } while (l && d != c); @@ -185,11 +185,11 @@ static int musl_getopt_long_core( opt = start; if (*opt == '-') { - opt++; + ++opt; } while (*opt && *opt != '=' && *opt == *name) { - name++; - opt++; + ++name; + ++opt; } if (*opt && *opt != '=') { continue; @@ -200,7 +200,7 @@ static int musl_getopt_long_core( cnt = 1; break; } - cnt++; + ++cnt; } if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) { int l = arg - start; @@ -209,10 +209,10 @@ static int musl_getopt_long_core( int j = 0; while (j < l && start[j] == optstring[i + j]) { - j++; + ++j; } if (j == l) { - cnt++; + ++cnt; break; } } @@ -220,7 +220,7 @@ static int musl_getopt_long_core( if (cnt == 1) { i = match; opt = arg; - musl_optind++; + ++musl_optind; if (*opt == '=') { if (!longopts[i].has_arg) { musl_optopt = longopts[i].val; @@ -254,7 +254,7 @@ static int musl_getopt_long_core( ); return '?'; } - musl_optind++; + ++musl_optind; } if (idx) { *idx = i; @@ -275,7 +275,7 @@ static int musl_getopt_long_core( strlen(argv[musl_optind] + 2) ); } - musl_optind++; + ++musl_optind; return '?'; } } diff --git a/src/fix/main.cpp b/src/fix/main.cpp index e94c2330..5ee7667e 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -90,7 +90,7 @@ static void error(char const *fmt, ...) { putc('\n', stderr); if (nbErrors != UINT32_MAX) { - nbErrors++; + ++nbErrors; } } @@ -104,7 +104,7 @@ static void fatal(char const *fmt, ...) { putc('\n', stderr); if (nbErrors != UINT32_MAX) { - nbErrors++; + ++nbErrors; } } @@ -206,6 +206,18 @@ static void printAcceptedMBCNames() { static uint8_t tpp1Rev[2]; +static void skipWhitespace(char const *&ptr) { + while (*ptr == ' ' || *ptr == '\t') { + ++ptr; + } +} + +static void skipMBCSpace(char const *&ptr) { + while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') { + ++ptr; + } +} + static bool readMBCSlice(char const *&name, char const *expected) { while (*expected) { char c = *name++; @@ -237,7 +249,7 @@ static MbcType parseMBC(char const *name) { int base = 0; if (name[0] == '$') { - name++; + ++name; base = 16; } // Parse number, and return it as-is (unless it's too large) @@ -257,10 +269,7 @@ static MbcType parseMBC(char const *name) { uint16_t mbc; char const *ptr = name; - // Trim off leading whitespace - while (*ptr == ' ' || *ptr == '\t') { - ptr++; - } + skipWhitespace(ptr); // Trim off leading whitespace #define tryReadSlice(expected) \ do { \ @@ -274,11 +283,9 @@ static MbcType parseMBC(char const *name) { case 'r': tryReadSlice("OM"); // Handle optional " ONLY" - while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') { - ptr++; - } + skipMBCSpace(ptr); if (*ptr == 'O' || *ptr == 'o') { - ptr++; + ++ptr; tryReadSlice("NLY"); } mbc = ROM; @@ -351,9 +358,7 @@ static MbcType parseMBC(char const *name) { case 'P': { tryReadSlice("P1"); // Parse version - while (*ptr == ' ' || *ptr == '_') { - ptr++; - } + skipMBCSpace(ptr); // Major char *endptr; unsigned long val = strtoul(ptr, &endptr, 10); @@ -420,23 +425,18 @@ static MbcType parseMBC(char const *name) { // clang-format on for (;;) { - // Trim off trailing whitespace - while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') { - ptr++; - } + skipWhitespace(ptr); // Trim off trailing whitespace // If done, start processing "features" if (!*ptr) { break; } // We expect a '+' at this point + skipMBCSpace(ptr); if (*ptr++ != '+') { return MBC_BAD; } - // Trim off leading whitespace - while (*ptr == ' ' || *ptr == '\t' || *ptr == '_') { - ptr++; - } + skipMBCSpace(ptr); switch (*ptr++) { case 'B': // BATTERY @@ -502,7 +502,7 @@ static MbcType parseMBC(char const *name) { case MBC1: case MMM01: if (features == RAM) { - mbc++; + ++mbc; } else if (features == (RAM | BATTERY)) { mbc += 2; } else if (features) { @@ -534,7 +534,7 @@ static MbcType parseMBC(char const *name) { MBC3_TIMER_BATTERY + 1 == MBC3_TIMER_RAM_BATTERY, "Enum sanity check failed!" ); if (features == RAM) { - mbc++; + ++mbc; } else if (features == (RAM | BATTERY)) { mbc += 2; } else if (features) { @@ -552,7 +552,7 @@ static MbcType parseMBC(char const *name) { static_assert(MBC5_RUMBLE + 1 == MBC5_RUMBLE_RAM, "Enum sanity check failed!"); static_assert(MBC5_RUMBLE + 2 == MBC5_RUMBLE_RAM_BATTERY, "Enum sanity check failed!"); if (features == RAM) { - mbc++; + ++mbc; } else if (features == (RAM | BATTERY)) { mbc += 2; } else if (features) { @@ -604,10 +604,7 @@ static MbcType parseMBC(char const *name) { break; } - // Trim off trailing whitespace - while (*ptr == ' ' || *ptr == '\t') { - ptr++; - } + skipWhitespace(ptr); // Trim off trailing whitespace // If there is still something past the whitespace, error out if (*ptr) { @@ -1050,7 +1047,7 @@ static void fatal("\"%s\" has more than 65536 banks", name); return; } - nbBanks++; + ++nbBanks; // Update global checksum, too for (uint16_t i = 0; i < bankLen; i++) { @@ -1375,7 +1372,7 @@ int main(int argc, char *argv[]) { default: warnx("Ignoring '%c' in fix spec", *musl_optarg); } - musl_optarg++; + ++musl_optarg; } break; diff --git a/src/gfx/warning.cpp b/src/gfx/warning.cpp index d7c6aaf8..f3a2e952 100644 --- a/src/gfx/warning.cpp +++ b/src/gfx/warning.cpp @@ -37,6 +37,12 @@ void requireZeroErrors() { } } +static void incrementErrors() { + if (nbErrors != std::numeric_limits::max()) { + ++nbErrors; + } +} + void error(char const *fmt, ...) { va_list ap; fputs("error: ", stderr); @@ -45,9 +51,7 @@ void error(char const *fmt, ...) { va_end(ap); putc('\n', stderr); - if (nbErrors != std::numeric_limits::max()) { - nbErrors++; - } + incrementErrors(); } [[noreturn]] @@ -59,10 +63,7 @@ void fatal(char const *fmt, ...) { va_end(ap); putc('\n', stderr); - if (nbErrors != std::numeric_limits::max()) { - nbErrors++; - } - + incrementErrors(); giveUp(); } @@ -89,9 +90,7 @@ void warning(WarningID id, char const *fmt, ...) { va_end(ap); putc('\n', stderr); - if (nbErrors != std::numeric_limits::max()) { - nbErrors++; - } + incrementErrors(); break; } } diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 4fd9b1be..6cb2b766 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -58,7 +58,7 @@ static void assignSection(Section §ion, MemoryLocation const &location) { next->bank = location.bank; } - nbSectionsToAssign--; + --nbSectionsToAssign; out_AddSection(section); } @@ -151,7 +151,7 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { location.address += section.alignMask + 1 + section.alignOfs; } else { // Any location is fine, so, next free block - spaceIdx++; + ++spaceIdx; if (spaceIdx < bankMem.size()) { location.address = bankMem[spaceIdx].address; } @@ -161,7 +161,7 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { // go forwards until that is no longer the case. while (spaceIdx < bankMem.size() && location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size) { - spaceIdx++; + ++spaceIdx; } // Try again with the new location/free space combo @@ -174,7 +174,7 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { return -1; } else if (scrambleROMX && section.type == SECTTYPE_ROMX && location.bank <= scrambleROMX) { if (location.bank > typeInfo.firstBank) { - location.bank--; + --location.bank; } else if (scrambleROMX < typeInfo.lastBank) { location.bank = scrambleROMX + 1; } else { @@ -183,7 +183,7 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { } else if (scrambleWRAMX && section.type == SECTTYPE_WRAMX && location.bank <= scrambleWRAMX) { if (location.bank > typeInfo.firstBank) { - location.bank--; + --location.bank; } else if (scrambleWRAMX < typeInfo.lastBank) { location.bank = scrambleWRAMX + 1; } else { @@ -191,14 +191,14 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { } } else if (scrambleSRAM && section.type == SECTTYPE_SRAM && location.bank <= scrambleSRAM) { if (location.bank > typeInfo.firstBank) { - location.bank--; + --location.bank; } else if (scrambleSRAM < typeInfo.lastBank) { location.bank = scrambleSRAM + 1; } else { return -1; } } else if (location.bank < typeInfo.lastBank) { - location.bank++; + ++location.bank; } else { return -1; } @@ -355,11 +355,11 @@ static void categorizeSection(Section §ion) { // Insert section while keeping the list sorted by decreasing size auto pos = sections.begin(); while (pos != sections.end() && (*pos)->size > section.size) { - pos++; + ++pos; } sections.insert(pos, §ion); - nbSectionsToAssign++; + ++nbSectionsToAssign; } void assign_AssignSections() { @@ -395,8 +395,7 @@ void assign_AssignSections() { constraints--) { for (Section *section : unassignedSections[constraints]) { fprintf(stderr, "%c \"%s\"", nbSections == 0 ? ';' : ',', section->name.c_str()); - nbSections++; - if (nbSections == 10) { + if (++nbSections == 10) { goto max_out; // Can't `break` out of a nested loop } } diff --git a/src/link/main.cpp b/src/link/main.cpp index afb8842e..9e6390dc 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -209,7 +209,7 @@ static void parseScrambleSpec(char const *spec) { } if (*spec == '=') { - spec++; // `strtoul` will skip the whitespace on its own + ++spec; // `strtoul` will skip the whitespace on its own unsigned long limit; char *endptr; diff --git a/src/link/object.cpp b/src/link/object.cpp index 5b5aeaeb..543438b4 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -527,7 +527,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { sym_AddSymbol(symbol); if (std::holds_alternative