Prefer pre-increment/decrement operators in for loops

This commit is contained in:
Rangi42
2025-07-24 18:08:17 -04:00
parent c6d0e8de63
commit d6a28a6259
15 changed files with 48 additions and 48 deletions

View File

@@ -160,7 +160,7 @@ std::string Diagnostics<L, W>::processWarningFlag(char const *flag) {
} }
// Set the first <param> to enabled/error, and disable the rest // Set the first <param> 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) { if (WarningState &warning = state.flagStates[baseID + ofs]; ofs < *param) {
warning.update(flagState); warning.update(flagState);
} else { } else {

View File

@@ -112,7 +112,7 @@ struct Palette {
// Flipping tends to happen fairly often, so take a bite out of dcache to speed it up // Flipping tends to happen fairly often, so take a bite out of dcache to speed it up
static std::array<uint16_t, 256> flipTable = ([]() constexpr { static std::array<uint16_t, 256> flipTable = ([]() constexpr {
std::array<uint16_t, 256> table{}; std::array<uint16_t, 256> 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. // To flip all the bits, we'll flip both nibbles, then each nibble half, etc.
uint16_t byte = i; uint16_t byte = i;
byte = (byte & 0b0000'1111) << 4 | (byte & 0b1111'0000) >> 4; byte = (byte & 0b0000'1111) << 4 | (byte & 0b1111'0000) >> 4;

View File

@@ -45,7 +45,7 @@ bool forEachChar(Charmap const &charmap, F callback) {
if (node.isTerminal() && !callback(nodeIdx, mapping)) { if (node.isTerminal() && !callback(nodeIdx, mapping)) {
return false; 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) { if (size_t nextIdx = node.next[c]; nextIdx) {
prefixes.push({nextIdx, mapping + static_cast<char>(c)}); prefixes.push({nextIdx, mapping + static_cast<char>(c)});
} }

View File

@@ -1068,7 +1068,7 @@ static bool isValidDigit(char c) {
} }
static bool checkDigitErrors(char const *digits, size_t n, char const *type) { 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]; char c = digits[i];
if (!isValidDigit(c)) { if (!isValidDigit(c)) {
@@ -1081,7 +1081,7 @@ static bool checkDigitErrors(char const *digits, size_t n, char const *type) {
return false; return false;
} }
for (size_t j = i + 1; j < n; j++) { for (size_t j = i + 1; j < n; ++j) {
if (c == digits[j]) { if (c == digits[j]) {
error("Repeated digit for %s constant %s", type, printChar(c)); error("Repeated digit for %s constant %s", type, printChar(c));
return false; return false;

View File

@@ -29,14 +29,14 @@ std::shared_ptr<std::string> MacroArgs::getAllArgs() const {
size_t len = 0; 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 len += args[i]->length() + 1; // 1 for comma
} }
auto str = std::make_shared<std::string>(); auto str = std::make_shared<std::string>();
str->reserve(len + 1); // 1 for comma 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<std::string> const &arg = args[i]; std::shared_ptr<std::string> const &arg = args[i];
str->append(*arg); str->append(*arg);

View File

@@ -332,7 +332,7 @@ void out_WriteObject() {
putLong(sectionList.size(), file); putLong(sectionList.size(), file);
putLong(fileStackNodes.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; FileStackNode const &node = **it;
writeFileStackNode(node, file); writeFileStackNode(node, file);

View File

@@ -2802,7 +2802,7 @@ static uint32_t strToNum(std::vector<int32_t> const &s) {
uint32_t r = 0; 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 <<= 8;
r |= static_cast<uint8_t>(s[i]); r |= static_cast<uint8_t>(s[i]);
} }
@@ -2973,7 +2973,7 @@ static size_t charlenUTF8(std::string const &str) {
std::string_view view = str; std::string_view view = str;
size_t len; size_t len;
for (len = 0; charmap_ConvertNext(view, nullptr); len++) {} for (len = 0; charmap_ConvertNext(view, nullptr); ++len) {}
return len; return len;
} }
@@ -2983,7 +2983,7 @@ static std::string strcharUTF8(std::string const &str, uint32_t idx) {
size_t charLen = 1; size_t charLen = 1;
// Advance to starting index in source string. // 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); charLen = charmap_ConvertNext(view, nullptr);
} }
@@ -3006,7 +3006,7 @@ static std::string charsubUTF8(std::string const &str, uint32_t pos) {
size_t charLen = 1; size_t charLen = 1;
// Advance to starting position in source string. // 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); charLen = charmap_ConvertNext(view, nullptr);
} }

View File

@@ -807,7 +807,7 @@ void sect_RelBytes(uint32_t n, std::vector<Expression> const &exprs) {
return; 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()) { if (Expression const &expr = exprs[i % exprs.size()]; !expr.isKnown()) {
createPatch(PATCHTYPE_BYTE, expr, i); createPatch(PATCHTYPE_BYTE, expr, i);
writeByte(0); writeByte(0);

10
src/extern/getopt.cpp vendored
View File

@@ -118,7 +118,7 @@ static void permute(char **argv, int dest, int src) {
char *tmp = argv[src]; char *tmp = argv[src];
int i; int i;
for (i = src; i > dest; i--) { for (i = src; i > dest; --i) {
argv[i] = argv[i - 1]; argv[i] = argv[i - 1];
} }
argv[dest] = tmp; argv[dest] = tmp;
@@ -146,7 +146,7 @@ static int musl_getopt_long(
skipped = musl_optind; skipped = musl_optind;
if (optstring[0] != '+' && optstring[0] != '-') { if (optstring[0] != '+' && optstring[0] != '-') {
int i; int i;
for (i = musl_optind;; i++) { for (i = musl_optind;; ++i) {
if (i >= argc || !argv[i]) { if (i >= argc || !argv[i]) {
return -1; return -1;
} }
@@ -161,7 +161,7 @@ static int musl_getopt_long(
if (resumed > skipped) { if (resumed > skipped) {
int i, cnt = musl_optind - resumed; int i, cnt = musl_optind - resumed;
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; ++i) {
permute(argv, skipped, musl_optind - 1); permute(argv, skipped, musl_optind - 1);
} }
musl_optind = skipped + cnt; musl_optind = skipped + cnt;
@@ -180,7 +180,7 @@ static int musl_getopt_long_core(
int i, cnt, match = 0; int i, cnt, match = 0;
char *arg = 0, *opt, *start = argv[musl_optind] + 1; 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; char const *name = longopts[i].name;
opt = start; opt = start;
@@ -205,7 +205,7 @@ static int musl_getopt_long_core(
if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) { if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) {
int l = arg - start; int l = arg - start;
for (i = 0; optstring[i]; i++) { for (i = 0; optstring[i]; ++i) {
int j = 0; int j = 0;
while (j < l && start[j] == optstring[i + j]) { while (j < l && start[j] == optstring[i + j]) {

View File

@@ -881,7 +881,7 @@ static void overwriteBytes(
uint8_t *rom0, uint16_t startAddr, uint8_t const *fixed, uint8_t size, char const *areaName uint8_t *rom0, uint16_t startAddr, uint8_t const *fixed, uint8_t size, char const *areaName
) { ) {
if (!overwriteRom) { if (!overwriteRom) {
for (uint8_t i = 0; i < size; i++) { for (uint8_t i = 0; i < size; ++i) {
uint8_t origByte = rom0[i + startAddr]; uint8_t origByte = rom0[i + startAddr];
if (origByte != 0 && origByte != fixed[i]) { if (origByte != 0 && origByte != fixed[i]) {
@@ -1049,7 +1049,7 @@ static void
++nbBanks; ++nbBanks;
// Update global checksum, too // Update global checksum, too
for (uint16_t i = 0; i < bankLen; i++) { for (uint16_t i = 0; i < bankLen; ++i) {
globalSum += romx[totalRomxLen + i]; globalSum += romx[totalRomxLen + i];
} }
totalRomxLen += bankLen; totalRomxLen += bankLen;
@@ -1097,7 +1097,7 @@ static void
if (fixSpec & (FIX_HEADER_SUM | TRASH_HEADER_SUM)) { if (fixSpec & (FIX_HEADER_SUM | TRASH_HEADER_SUM)) {
uint8_t sum = 0; uint8_t sum = 0;
for (uint16_t i = 0x134; i < 0x14D; i++) { for (uint16_t i = 0x134; i < 0x14D; ++i) {
sum -= rom0[i] + 1; sum -= rom0[i] + 1;
} }
@@ -1107,10 +1107,10 @@ static void
if (fixSpec & (FIX_GLOBAL_SUM | TRASH_GLOBAL_SUM)) { if (fixSpec & (FIX_GLOBAL_SUM | TRASH_GLOBAL_SUM)) {
// Computation of the global checksum does not include the checksum bytes // Computation of the global checksum does not include the checksum bytes
assume(rom0Len >= 0x14E); assume(rom0Len >= 0x14E);
for (uint16_t i = 0; i < 0x14E; i++) { for (uint16_t i = 0; i < 0x14E; ++i) {
globalSum += rom0[i]; globalSum += rom0[i];
} }
for (uint16_t i = 0x150; i < rom0Len; i++) { for (uint16_t i = 0x150; i < rom0Len; ++i) {
globalSum += rom0[i]; globalSum += rom0[i];
} }
// Pipes have already read ROMX and updated globalSum, but not regular files // Pipes have already read ROMX and updated globalSum, but not regular files
@@ -1118,7 +1118,7 @@ static void
for (;;) { for (;;) {
ssize_t bankLen = readBytes(input, bank, sizeof(bank)); 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]; globalSum += bank[i];
} }
if (bankLen != sizeof(bank)) { if (bankLen != sizeof(bank)) {
@@ -1558,7 +1558,7 @@ int main(int argc, char *argv[]) {
memcpy(logo, nintendoLogo, sizeof(nintendoLogo)); memcpy(logo, nintendoLogo, sizeof(nintendoLogo));
} }
if (fixSpec & TRASH_LOGO) { 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]; logo[i] = 0xFF ^ logo[i];
} }
} }

View File

@@ -580,10 +580,10 @@ static void parseGBCFile(char const *, std::filebuf &file) {
} }
static bool checkPngSwatch(std::vector<Rgba> const &pixels, uint32_t base, uint32_t swatchSize) { static bool checkPngSwatch(std::vector<Rgba> 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; 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) { if (x == 0 && y == 0) {
continue; continue;
} }

View File

@@ -370,7 +370,7 @@ int main(int argc, char *argv[]) {
} }
// Read all object files first, // 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); obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1);
} }

View File

@@ -126,7 +126,7 @@ static void readFileStackNode(
depth, file, "%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, nodeID depth, file, "%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, nodeID
); );
node.data = std::vector<uint32_t>(depth); node.data = std::vector<uint32_t>(depth);
for (uint32_t i = 0; i < depth; i++) { for (uint32_t i = 0; i < depth; ++i) {
tryReadLong( tryReadLong(
node.iters()[i], node.iters()[i],
file, file,
@@ -389,7 +389,7 @@ static void readSection(
); );
section.patches.resize(nbPatches); 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); 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<uint32_t> nbSymPerSect(nbSections, 0); std::vector<uint32_t> nbSymPerSect(nbSections, 0);
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols); verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
for (uint32_t i = 0; i < nbSymbols; i++) { for (uint32_t i = 0; i < nbSymbols; ++i) {
// Read symbol // Read symbol
Symbol &symbol = fileSymbols[i]; Symbol &symbol = fileSymbols[i];
@@ -532,7 +532,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
std::vector<std::unique_ptr<Section>> fileSections(nbSections); std::vector<std::unique_ptr<Section>> fileSections(nbSections);
verbosePrint("Reading %" PRIu32 " sections...\n", 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 // Read section
fileSections[i] = std::make_unique<Section>(); fileSections[i] = std::make_unique<Section>();
fileSections[i]->nextu = nullptr; fileSections[i]->nextu = nullptr;
@@ -544,7 +544,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
uint32_t nbAsserts; uint32_t nbAsserts;
tryReadLong(nbAsserts, file, "%s: Cannot read number of assertions: %s", fileName); tryReadLong(nbAsserts, file, "%s: Cannot read number of assertions: %s", fileName);
verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts); 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(); Assertion &assertion = patch_AddAssertion();
readAssertion(file, assertion, fileName, i, nodes[fileID]); 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 // 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)) { if (sect_HasData(fileSections[i]->type)) {
for (Patch &patch : fileSections[i]->patches) { for (Patch &patch : fileSections[i]->patches) {
linkPatchToPCSect(patch, fileSections); linkPatchToPCSect(patch, fileSections);
@@ -562,7 +562,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
} }
// Give symbols' section pointers to their sections // 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<Label>(fileSymbols[i].data)) { if (std::holds_alternative<Label>(fileSymbols[i].data)) {
Label &label = std::get<Label>(fileSymbols[i].data); Label &label = std::get<Label>(fileSymbols[i].data);
label.section = fileSections[label.sectionID].get(); label.section = fileSections[label.sectionID].get();
@@ -572,14 +572,14 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
} }
// Calling `sect_AddSection` invalidates the contents of `fileSections`! // Calling `sect_AddSection` invalidates the contents of `fileSections`!
for (uint32_t i = 0; i < nbSections; i++) { for (uint32_t i = 0; i < nbSections; ++i) {
sect_AddSection(std::move(fileSections[i])); sect_AddSection(std::move(fileSections[i]));
} }
// Fix symbols' section pointers to component sections // Fix symbols' section pointers to component sections
// This has to run **after** all the `sect_AddSection()` calls, // This has to run **after** all the `sect_AddSection()` calls,
// so that `sect_GetSection()` will work // so that `sect_GetSection()` will work
for (uint32_t i = 0; i < nbSymbols; i++) { for (uint32_t i = 0; i < nbSymbols; ++i) {
if (std::holds_alternative<Label>(fileSymbols[i].data)) { if (std::holds_alternative<Label>(fileSymbols[i].data)) {
Label &label = std::get<Label>(fileSymbols[i].data); Label &label = std::get<Label>(fileSymbols[i].data);
Section *section = label.section; Section *section = label.section;

View File

@@ -79,7 +79,7 @@ void out_AddSection(Section const &section) {
); );
} }
for (uint32_t i = sections[section.type].size(); i < minNbBanks; i++) { for (uint32_t i = sections[section.type].size(); i < minNbBanks; ++i) {
sections[section.type].emplace_back(); sections[section.type].emplace_back();
} }
@@ -146,7 +146,7 @@ static void coverOverlayBanks(uint32_t nbOverlayBanks) {
: 0; : 0;
if (nbUncoveredBanks > sections[SECTTYPE_ROMX].size()) { if (nbUncoveredBanks > sections[SECTTYPE_ROMX].size()) {
for (uint32_t i = sections[SECTTYPE_ROMX].size(); i < nbUncoveredBanks; i++) { for (uint32_t i = sections[SECTTYPE_ROMX].size(); i < nbUncoveredBanks; ++i) {
sections[SECTTYPE_ROMX].emplace_back(); sections[SECTTYPE_ROMX].emplace_back();
} }
} }
@@ -189,7 +189,7 @@ static void
continue; continue;
} }
// Skip bytes even with pipes // Skip bytes even with pipes
for (uint16_t i = 0; i < section->size; i++) { for (uint16_t i = 0; i < section->size; ++i) {
getc(overlayFile); getc(overlayFile);
} }
} }
@@ -255,7 +255,7 @@ static void writeROM() {
sectionTypeInfo[SECTTYPE_ROM0].size sectionTypeInfo[SECTTYPE_ROM0].size
); );
for (uint32_t i = 0; i < sections[SECTTYPE_ROMX].size(); i++) { for (uint32_t i = 0; i < sections[SECTTYPE_ROMX].size(); ++i) {
writeBank( writeBank(
&sections[SECTTYPE_ROMX][i].sections, &sections[SECTTYPE_ROMX][i].sections,
sectionTypeInfo[SECTTYPE_ROMX].startAddr, sectionTypeInfo[SECTTYPE_ROMX].startAddr,
@@ -498,7 +498,7 @@ static void writeMapBank(SortedSections const &sectList, SectionType type, uint3
static void writeMapSummary() { static void writeMapSummary() {
fputs("SUMMARY:\n", mapFile); fputs("SUMMARY:\n", mapFile);
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) { for (uint8_t i = 0; i < SECTTYPE_INVALID; ++i) {
SectionType type = typeMap[i]; SectionType type = typeMap[i];
uint32_t nbBanks = sections[type].size(); uint32_t nbBanks = sections[type].size();
@@ -514,7 +514,7 @@ static void writeMapSummary() {
uint32_t usedTotal = 0; uint32_t usedTotal = 0;
for (uint32_t bank = 0; bank < nbBanks; bank++) { for (uint32_t bank = 0; bank < nbBanks; ++bank) {
usedTotal += forEachSection(sections[type][bank], [](Section const &) {}); usedTotal += forEachSection(sections[type][bank], [](Section const &) {});
} }
@@ -552,10 +552,10 @@ static void writeSym() {
fputs("; File generated by rgblink\n", symFile); fputs("; File generated by rgblink\n", symFile);
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) { for (uint8_t i = 0; i < SECTTYPE_INVALID; ++i) {
SectionType type = typeMap[i]; SectionType type = typeMap[i];
for (uint32_t bank = 0; bank < sections[type].size(); bank++) { for (uint32_t bank = 0; bank < sections[type].size(); ++bank) {
writeSymBank(sections[type][bank], type, bank); writeSymBank(sections[type][bank], type, bank);
} }
} }
@@ -602,10 +602,10 @@ static void writeMap() {
writeMapSummary(); writeMapSummary();
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) { for (uint8_t i = 0; i < SECTTYPE_INVALID; ++i) {
SectionType type = typeMap[i]; SectionType type = typeMap[i];
for (uint32_t bank = 0; bank < sections[type].size(); bank++) { for (uint32_t bank = 0; bank < sections[type].size(); ++bank) {
writeMapBank(sections[type][bank], type, bank); writeMapBank(sections[type][bank], type, bank);
} }
} }

View File

@@ -557,7 +557,7 @@ static void applyFilePatches(Section &section, Section &dataSection) {
type.size * 8U type.size * 8U
); );
} }
for (uint8_t i = 0; i < type.size; i++) { for (uint8_t i = 0; i < type.size; ++i) {
dataSection.data[offset + i] = value & 0xFF; dataSection.data[offset + i] = value & 0xFF;
value >>= 8; value >>= 8;
} }