Use concrete types instead of auto when convenient and not redundant (#1757)

This commit is contained in:
Rangi
2025-07-17 14:59:51 -04:00
committed by GitHub
parent 9dddd87893
commit 0c96234532
16 changed files with 67 additions and 68 deletions

View File

@@ -133,7 +133,7 @@ std::string Diagnostics<L, W>::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 &paramWarning : paramWarnings) {
for (ParamWarning<W> const &paramWarning : paramWarnings) {
W baseID = paramWarning.firstID;
uint8_t maxParam = paramWarning.lastID - baseID + 1;
assume(paramWarning.defaultLevel <= maxParam);

View File

@@ -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<uint16_t, 256> flipTable = ([]() constexpr {
std::array<uint16_t, 256> 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.

View File

@@ -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<uint32_t>(val) << shift; };
constexpr auto shl = [](uint8_t val, unsigned shift) {
return static_cast<uint32_t>(val) << shift;
};
return shl(red, 24) | shl(green, 16) | shl(blue, 8) | shl(alpha, 0);
}

View File

@@ -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; }
};

View File

@@ -685,7 +685,7 @@ static uint32_t readBracketedMacroArgNum() {
static std::shared_ptr<std::string> readMacroArg(char name) {
if (name == '@') {
auto str = fstk_GetUniqueIDStr();
std::shared_ptr<std::string> 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<std::string> readMacroArg(char name) {
return nullptr;
}
auto str = macroArgs->getAllArgs();
std::shared_ptr<std::string> 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<std::string> readMacroArg(char name) {
return nullptr;
}
auto str = macroArgs->getArg(num);
std::shared_ptr<std::string> str = macroArgs->getArg(num);
if (!str) {
error("Macro argument '\\<%" PRId32 ">' not defined", num);
}
@@ -727,7 +727,7 @@ static std::shared_ptr<std::string> readMacroArg(char name) {
return nullptr;
}
auto str = macroArgs->getArg(name - '0');
std::shared_ptr<std::string> 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<std::string> str = readInterpolation(0); str) {
beginExpansion(str, *str);
}
return peek();
@@ -1341,7 +1341,7 @@ static std::shared_ptr<std::string> 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<std::string> 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<std::string> 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<std::string> interpolation = readInterpolation(0); interpolation) {
appendExpandedString(str, *interpolation);
}
lexerState->disableMacroArgs = true;

View File

@@ -37,7 +37,7 @@ std::shared_ptr<std::string> 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<std::string> const &arg = args[i];
str->append(*arg);

View File

@@ -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);

View File

@@ -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<char> &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<size_t> 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<char *> &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",

View File

@@ -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<uint16_t> &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<typename Iter>
auto combinedVolume(Iter &&begin, Iter const &end, std::vector<ProtoPalette> const &protoPals)
size_t combinedVolume(Iter &&begin, Iter const &end, std::vector<ProtoPalette> const &protoPals)
const {
auto &colors = uniqueColors();
std::unordered_set<uint16_t> &colors = uniqueColors();
addUniqueColors(colors, std::forward<Iter>(begin), end, protoPals);
return colors.size();
}
// Computes the "relative size" of a set of colors on this palette
template<typename Iter>
auto combinedVolume(Iter &&begin, Iter &&end) const {
auto &colors = uniqueColors();
size_t combinedVolume(Iter &&begin, Iter &&end) const {
std::unordered_set<uint16_t> &colors = uniqueColors();
colors.insert(std::forward<Iter>(begin), std::forward<Iter>(end));
return colors.size();
}
};
static void verboseOutputAssignments(
std::vector<AssignedProtos> const &assignments, std::vector<ProtoPalette> 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<AssignedProtos> &assignments, std::vector<ProtoPalette> const &protoPalettes
) {
@@ -423,7 +439,7 @@ std::tuple<std::vector<size_t>, 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<std::vector<size_t>, 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<std::vector<size_t>, 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

View File

@@ -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
);

View File

@@ -26,6 +26,8 @@
using namespace std::string_view_literals;
static char const *hexDigits = "0123456789ABCDEFabcdef";
template<typename Str> // 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<char, 772> 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);

View File

@@ -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<File *>(png_get_io_ptr(png));
File &pngFile = *static_cast<File *>(png_get_io_ptr(png));
pngFile->sputn(reinterpret_cast<char *>(data), length);
}
static void flushPng(png_structp png) {
auto &pngFile = *static_cast<File *>(png_get_io_ptr(png));
File &pngFile = *static_cast<File *>(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<uint8_t> const tiles = readInto(options.output);
uint8_t tileSize = 8 * options.bitDepth;
if (tiles.size() % tileSize != 0) {
fatal(

View File

@@ -351,9 +351,9 @@ static void categorizeSection(Section &section) {
}
std::deque<Section *> &sections = 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++;
}

View File

@@ -86,12 +86,12 @@ void out_AddSection(Section const &section) {
std::deque<Section const *> &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, &section);
}
@@ -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<Label>(parentSym->data)) {
auto const &parentLabel = parentSym->label();
Label const &parentLabel = parentSym->label();
Section const &parentSection = *parentLabel.section;
parentAddr = static_cast<uint16_t>(parentLabel.offset + parentSection.org);
}

View File

@@ -857,7 +857,7 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector<Symbol> &f
// RAM sections can have a size, but don't get any data (they shouldn't have any)
if (section->type != SECTTYPE_INVALID) {
auto const &typeInfo = sectionTypeInfo[section->type];
SectionTypeInfo const &typeInfo = sectionTypeInfo[section->type];
// Otherwise, how would the type already be known at this point?
assume(section->isAddressFixed);

View File

@@ -16,7 +16,7 @@ std::vector<std::unique_ptr<Section>> sectionList;
std::unordered_map<std::string, size_t> sectionMap; // Indexes into `sectionList`
void sect_ForEach(void (*callback)(Section &)) {
for (auto &ptr : sectionList) {
for (std::unique_ptr<Section> &ptr : sectionList) {
callback(*ptr);
}
}