Use std::string for symbol/section/node names and assertion messages

This commit is contained in:
Rangi42
2024-02-27 18:59:38 -05:00
committed by Sylvie
parent a24df27cd8
commit 48b2e94aa3
12 changed files with 172 additions and 201 deletions

View File

@@ -27,14 +27,13 @@ static void checkSectUnionCompat(struct Section *target, struct Section *other)
if (target->isAddressFixed) {
if (target->org != other->org)
errx("Section \"%s\" is defined with conflicting addresses $%04"
PRIx16 " and $%04" PRIx16,
other->name, target->org, other->org);
PRIx16 " and $%04" PRIx16, other->name->c_str(), target->org,
other->org);
} else if (target->isAlignFixed) {
if ((other->org - target->alignOfs) & target->alignMask)
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and address $%04" PRIx16,
other->name, target->alignMask + 1,
target->alignOfs, other->org);
PRIu16 ") and address $%04" PRIx16, other->name->c_str(),
target->alignMask + 1, target->alignOfs, other->org);
}
target->isAddressFixed = true;
target->org = other->org;
@@ -44,14 +43,14 @@ static void checkSectUnionCompat(struct Section *target, struct Section *other)
if ((target->org - other->alignOfs) & other->alignMask)
errx("Section \"%s\" is defined with conflicting address $%04"
PRIx16 " and %d-byte alignment (offset %" PRIu16 ")",
other->name, target->org,
other->alignMask + 1, other->alignOfs);
other->name->c_str(), target->org, other->alignMask + 1,
other->alignOfs);
} else if (target->isAlignFixed
&& (other->alignMask & target->alignOfs)
!= (target->alignMask & other->alignOfs)) {
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")",
other->name, target->alignMask + 1, target->alignOfs,
other->name->c_str(), target->alignMask + 1, target->alignOfs,
other->alignMask + 1, other->alignOfs);
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
target->isAlignFixed = true;
@@ -68,15 +67,14 @@ static void checkFragmentCompat(struct Section *target, struct Section *other)
if (target->isAddressFixed) {
if (target->org != org)
errx("Section \"%s\" is defined with conflicting addresses $%04"
PRIx16 " and $%04" PRIx16,
other->name, target->org, other->org);
PRIx16 " and $%04" PRIx16, other->name->c_str(), target->org,
other->org);
} else if (target->isAlignFixed) {
if ((org - target->alignOfs) & target->alignMask)
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and address $%04" PRIx16,
other->name, target->alignMask + 1,
target->alignOfs, other->org);
PRIu16 ") and address $%04" PRIx16, other->name->c_str(),
target->alignMask + 1, target->alignOfs, other->org);
}
target->isAddressFixed = true;
target->org = org;
@@ -91,14 +89,14 @@ static void checkFragmentCompat(struct Section *target, struct Section *other)
if ((target->org - ofs) & other->alignMask)
errx("Section \"%s\" is defined with conflicting address $%04"
PRIx16 " and %d-byte alignment (offset %" PRIu16 ")",
other->name, target->org,
other->alignMask + 1, other->alignOfs);
other->name->c_str(), target->org, other->alignMask + 1,
other->alignOfs);
} else if (target->isAlignFixed
&& (other->alignMask & target->alignOfs) != (target->alignMask & ofs)) {
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")",
other->name, target->alignMask + 1, target->alignOfs,
other->name->c_str(), target->alignMask + 1, target->alignOfs,
other->alignMask + 1, other->alignOfs);
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
@@ -115,7 +113,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
if (target->type != other->type)
errx("Section \"%s\" is defined with conflicting types %s and %s",
other->name, sectionTypeInfo[target->type].name.c_str(),
other->name->c_str(), sectionTypeInfo[target->type].name.c_str(),
sectionTypeInfo[other->type].name.c_str());
if (other->isBankFixed) {
@@ -124,7 +122,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
target->bank = other->bank;
} else if (target->bank != other->bank) {
errx("Section \"%s\" is defined with conflicting banks %" PRIu32 " and %"
PRIu32, other->name, target->bank, other->bank);
PRIu32, other->name->c_str(), target->bank, other->bank);
}
}
@@ -173,24 +171,24 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
void sect_AddSection(struct Section *section)
{
// Check if the section already exists
if (struct Section *other = sect_GetSection(section->name); other) {
if (struct Section *other = sect_GetSection(*section->name); other) {
if (section->modifier != other->modifier)
errx("Section \"%s\" defined as %s and %s", section->name,
errx("Section \"%s\" defined as %s and %s", section->name->c_str(),
sectionModNames[section->modifier], sectionModNames[other->modifier]);
else if (section->modifier == SECTION_NORMAL)
errx("Section name \"%s\" is already in use", section->name);
errx("Section name \"%s\" is already in use", section->name->c_str());
else
mergeSections(other, section, section->modifier);
} else if (section->modifier == SECTION_UNION && sect_HasData(section->type)) {
errx("Section \"%s\" is of type %s, which cannot be unionized",
section->name, sectionTypeInfo[section->type].name.c_str());
section->name->c_str(), sectionTypeInfo[section->type].name.c_str());
} else {
// If not, add it
sections[section->name] = section;
sections[*section->name] = section;
}
}
struct Section *sect_GetSection(char const *name)
struct Section *sect_GetSection(std::string const &name)
{
auto search = sections.find(name);
return search != sections.end() ? search->second : NULL;
@@ -205,27 +203,27 @@ static void doSanityChecks(struct Section *section)
{
// Sanity check the section's type
if (section->type < 0 || section->type >= SECTTYPE_INVALID) {
error(NULL, 0, "Section \"%s\" has an invalid type", section->name);
error(NULL, 0, "Section \"%s\" has an invalid type", section->name->c_str());
return;
}
if (is32kMode && section->type == SECTTYPE_ROMX) {
if (section->isBankFixed && section->bank != 1)
error(NULL, 0, "%s: ROMX sections must be in bank 1 (if any) with option -t",
section->name);
section->name->c_str());
else
section->type = SECTTYPE_ROM0;
}
if (isWRA0Mode && section->type == SECTTYPE_WRAMX) {
if (section->isBankFixed && section->bank != 1)
error(NULL, 0, "%s: WRAMX sections must be in bank 1 with options -w or -d",
section->name);
section->name->c_str());
else
section->type = SECTTYPE_WRAM0;
}
if (isDmgMode && section->type == SECTTYPE_VRAM && section->bank == 1)
error(NULL, 0, "%s: VRAM bank 1 can't be used with option -d",
section->name);
section->name->c_str());
// Check if alignment is reasonable, this is important to avoid UB
// An alignment of zero is equivalent to no alignment, basically
@@ -235,7 +233,8 @@ static void doSanityChecks(struct Section *section)
// Too large an alignment may not be satisfiable
if (section->isAlignFixed && (section->alignMask & sectionTypeInfo[section->type].startAddr))
error(NULL, 0, "%s: %s sections cannot be aligned to $%04x bytes",
section->name, sectionTypeInfo[section->type].name.c_str(), section->alignMask + 1);
section->name->c_str(), sectionTypeInfo[section->type].name.c_str(),
section->alignMask + 1);
uint32_t minbank = sectionTypeInfo[section->type].firstBank, maxbank = sectionTypeInfo[section->type].lastBank;
@@ -243,13 +242,13 @@ static void doSanityChecks(struct Section *section)
error(NULL, 0, minbank == maxbank
? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32
: "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32,
section->name, section->bank, minbank, maxbank);
section->name->c_str(), section->bank, minbank, maxbank);
// Check if section has a chance to be placed
if (section->size > sectionTypeInfo[section->type].size)
error(NULL, 0, "Section \"%s\" is bigger than the max size for that type: $%"
PRIx16 " > $%" PRIx16,
section->name, section->size, sectionTypeInfo[section->type].size);
section->name->c_str(), section->size, sectionTypeInfo[section->type].size);
// Translate loose constraints to strong ones when they're equivalent
@@ -263,7 +262,7 @@ static void doSanityChecks(struct Section *section)
if (section->isAlignFixed) {
if ((section->org & section->alignMask) != section->alignOfs)
error(NULL, 0, "Section \"%s\"'s fixed address doesn't match its alignment",
section->name);
section->name->c_str());
section->isAlignFixed = false;
}
@@ -271,12 +270,12 @@ static void doSanityChecks(struct Section *section)
if (section->org < sectionTypeInfo[section->type].startAddr
|| section->org > endaddr(section->type))
error(NULL, 0, "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04"
PRIx16 "; $%04" PRIx16 "]", section->name, section->org,
PRIx16 "; $%04" PRIx16 "]", section->name->c_str(), section->org,
sectionTypeInfo[section->type].startAddr, endaddr(section->type));
if (section->org + section->size > endaddr(section->type) + 1)
error(NULL, 0, "Section \"%s\"'s end address $%04x is greater than last address $%04x",
section->name, section->org + section->size,
section->name->c_str(), section->org + section->size,
endaddr(section->type) + 1);
}
}