Use an iterator template for enum sequence loops (#1228)

This commit is contained in:
Rangi
2023-11-12 03:19:19 -05:00
committed by GitHub
parent d390db5c57
commit e824e34526
9 changed files with 60 additions and 17 deletions

View File

@@ -13,6 +13,7 @@
#include "asm/warning.hpp"
#include "error.hpp"
#include "itertools.hpp"
unsigned int nbErrors = 0;
@@ -214,7 +215,7 @@ void processWarningFlag(char *flag)
static bool setError = false;
// First, try to match against a "meta" warning
for (enum WarningID id = META_WARNINGS_START; id < NB_WARNINGS; id = (enum WarningID)(id + 1)) {
for (enum WarningID id : EnumSeq(META_WARNINGS_START, NB_WARNINGS)) {
// TODO: improve the matching performance?
if (!strcmp(flag, warningFlags[id])) {
// We got a match!
@@ -309,8 +310,7 @@ void processWarningFlag(char *flag)
}
// Try to match the flag against a "normal" flag
for (enum WarningID id = (enum WarningID)0; id < NB_PLAIN_WARNINGS;
id = (enum WarningID)(id + 1)) {
for (enum WarningID id : EnumSeq(NB_PLAIN_WARNINGS)) {
if (!strcmp(rootFlag, warningFlags[id])) {
// We got a match!
warningStates[id] = state;

View File

@@ -15,6 +15,7 @@
#include "error.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
struct MemoryLocation {
@@ -36,7 +37,7 @@ uint64_t nbSectionsToAssign;
// Init the free space-modelling structs
static void initFreeSpace(void)
{
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (enum SectionType type : EnumSeq(SECTTYPE_INVALID)) {
memory[type] = (struct FreeSpace *)malloc(sizeof(*memory[type]) * nbbanks(type));
if (!memory[type])
err("Failed to init free space for region %d", type);
@@ -428,7 +429,7 @@ max_out:
void assign_Cleanup(void)
{
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (enum SectionType type : EnumSeq(SECTTYPE_INVALID)) {
for (uint32_t bank = 0; bank < nbbanks(type); bank++) {
struct FreeSpace *ptr =
memory[type][bank].next;

View File

@@ -23,6 +23,7 @@
#include "extern/getopt.hpp"
#include "error.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "version.hpp"
@@ -249,7 +250,7 @@ static void parseScrambleSpec(char const *spec)
size_t regionNameLen = strcspn(spec, "=, \t");
// Length of region name string slice for printing, truncated if too long
int regionNamePrintLen = regionNameLen > INT_MAX ? INT_MAX : (int)regionNameLen;
enum ScrambledRegion region = (enum ScrambledRegion)0;
enum ScrambledRegion region = SCRAMBLE_UNK;
// If this trips, `spec` must be pointing at a ',' or '=' (or NUL) due to the assert
if (regionNameLen == 0) {
@@ -272,12 +273,14 @@ static void parseScrambleSpec(char const *spec)
}
// Now, determine which region type this is
for (; region < SCRAMBLE_UNK; region = (enum ScrambledRegion)(region + 1)) {
for (enum ScrambledRegion r : EnumSeq(SCRAMBLE_UNK)) {
// If the strings match (case-insensitively), we got it!
// `strncasecmp` must be used here since `regionName` points
// to the entire remaining argument.
if (!strncasecmp(scrambleSpecs[region].name, regionName, regionNameLen))
if (!strncasecmp(scrambleSpecs[r].name, regionName, regionNameLen)) {
region = r;
break;
}
}
if (region == SCRAMBLE_UNK)

View File

@@ -15,6 +15,7 @@
#include "extern/utf8decoder.hpp"
#include "error.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
#include "platform.hpp" // For `MIN_NB_ELMS` and `AT`
@@ -604,7 +605,7 @@ static void cleanupSections(struct SortedSection *section)
static void cleanup(void)
{
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (enum SectionType type : EnumSeq(SECTTYPE_INVALID)) {
for (uint32_t i = 0; i < sections[type].nbBanks; i++) {
struct SortedSections *bank = &sections[type].banks[i];

View File

@@ -12,6 +12,7 @@
#include "link/section.hpp"
#include "error.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
@@ -292,7 +293,7 @@ static struct LinkerScriptToken *nextToken(void)
token.type = TOKEN_INVALID;
// Try to match a command
for (enum LinkerScriptCommand i = (enum LinkerScriptCommand)0; i < COMMAND_INVALID; i = (enum LinkerScriptCommand)(i + 1)) {
for (enum LinkerScriptCommand i : EnumSeq(COMMAND_INVALID)) {
if (!strcmp(commands[i], str)) {
token.type = TOKEN_COMMAND;
token.attr.command = i;
@@ -302,7 +303,7 @@ static struct LinkerScriptToken *nextToken(void)
if (token.type == TOKEN_INVALID) {
// Try to match a bank specifier
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (enum SectionType type : EnumSeq(SECTTYPE_INVALID)) {
if (!strcmp(sectionTypeInfo[type].name, str)) {
token.type = TOKEN_BANK;
token.attr.secttype = type;
@@ -384,7 +385,7 @@ struct SectionPlacement *script_NextSection(void)
lineNo = 1;
// Init PC for all banks
for (enum SectionType i = (enum SectionType)0; i < SECTTYPE_INVALID; i = (enum SectionType)(i + 1)) {
for (enum SectionType i : EnumSeq(SECTTYPE_INVALID)) {
curaddr[i] = (uint16_t *)malloc(sizeof(*curaddr[i]) * nbbanks(i));
for (uint32_t b = 0; b < nbbanks(i); b++)
curaddr[i][b] = sectionTypeInfo[i].startAddr;
@@ -544,6 +545,6 @@ lineend:
void script_Cleanup(void)
{
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1))
for (enum SectionType type : EnumSeq(SECTTYPE_INVALID))
free(curaddr[type]);
}