Use std::string for section names

This commit is contained in:
Rangi42
2024-03-02 04:47:02 -05:00
parent b488d3a90f
commit ba00cf5684
4 changed files with 11 additions and 16 deletions

View File

@@ -26,7 +26,7 @@ struct Patch {
}; };
struct Section { struct Section {
char *name; std::string name;
enum SectionType type; enum SectionType type;
enum SectionModifier modifier; enum SectionModifier modifier;
FileStackNode const *src; // Where the section was defined FileStackNode const *src; // Where the section was defined

View File

@@ -25,7 +25,6 @@
#include "error.hpp" #include "error.hpp"
#include "linkdefs.hpp" #include "linkdefs.hpp"
#include "platform.hpp" // strdup
struct Assertion { struct Assertion {
Patch patch; Patch patch;
@@ -95,7 +94,7 @@ static uint32_t getSectIDIfAny(Section *sect)
return it - sectionList.begin(); return it - sectionList.begin();
} }
fatalerror("Unknown section '%s'\n", sect->name); fatalerror("Unknown section '%s'\n", sect->name.c_str());
} }
// Write a patch to a file // Write a patch to a file
@@ -115,7 +114,7 @@ static void writepatch(Patch const &patch, FILE *f)
// Write a section to a file // Write a section to a file
static void writesection(Section const &sect, FILE *f) static void writesection(Section const &sect, FILE *f)
{ {
putstring(sect.name, f); putstring(sect.name.c_str(), f);
putlong(sect.size, f); putlong(sect.size, f);

View File

@@ -1329,7 +1329,7 @@ string : T_STRING
fatalerror("\"%s\" does not belong to any section\n", sym->name); fatalerror("\"%s\" does not belong to any section\n", sym->name);
// Section names are capped by rgbasm's maximum string length, // Section names are capped by rgbasm's maximum string length,
// so this currently can't overflow. // so this currently can't overflow.
strcpy($$, section->name); strcpy($$, section->name.c_str());
} }
; ;

View File

@@ -23,7 +23,6 @@
#include "error.hpp" #include "error.hpp"
#include "linkdefs.hpp" #include "linkdefs.hpp"
#include "platform.hpp" // strdup
uint8_t fillByte; uint8_t fillByte;
@@ -71,7 +70,7 @@ attr_(warn_unused_result) static bool checkcodesection()
return true; return true;
error("Section '%s' cannot contain code or data (not ROM0 or ROMX)\n", error("Section '%s' cannot contain code or data (not ROM0 or ROMX)\n",
currentSection->name); currentSection->name.c_str());
return false; return false;
} }
@@ -84,7 +83,7 @@ attr_(warn_unused_result) static bool checkSectionSize(Section const *sect, uint
return true; return true;
error("Section '%s' grew too big (max size = 0x%" PRIX32 error("Section '%s' grew too big (max size = 0x%" PRIX32
" bytes, reached 0x%" PRIX32 ").\n", sect->name, maxSize, size); " bytes, reached 0x%" PRIX32 ").\n", sect->name.c_str(), maxSize, size);
return false; return false;
} }
@@ -113,7 +112,7 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size)
Section *sect_FindSectionByName(char const *name) Section *sect_FindSectionByName(char const *name)
{ {
for (Section &sect : sectionList) { for (Section &sect : sectionList) {
if (strcmp(name, sect.name) == 0) if (sect.name == name)
return § return §
} }
return nullptr; return nullptr;
@@ -258,7 +257,7 @@ static void mergeSections(Section *sect, enum SectionType type, uint32_t org, ui
if (nbSectErrors) if (nbSectErrors)
fatalerror("Cannot create section \"%s\" (%u error%s)\n", fatalerror("Cannot create section \"%s\" (%u error%s)\n",
sect->name, nbSectErrors, nbSectErrors == 1 ? "" : "s"); sect->name.c_str(), nbSectErrors, nbSectErrors == 1 ? "" : "s");
} }
#undef fail #undef fail
@@ -270,10 +269,7 @@ static Section *createSection(char const *name, enum SectionType type, uint32_t
// Add the new section to the list (order doesn't matter) // Add the new section to the list (order doesn't matter)
Section &sect = sectionList.emplace_front(); Section &sect = sectionList.emplace_front();
sect.name = strdup(name); sect.name = name;
if (sect.name == nullptr)
fatalerror("Not enough memory for section name: %s\n", strerror(errno));
sect.type = type; sect.type = type;
sect.modifier = mod; sect.modifier = mod;
sect.src = fstk_GetFileStack(); sect.src = fstk_GetFileStack();
@@ -387,7 +383,7 @@ bool Section::isSizeKnown() const
// Any section on the stack is still growing // Any section on the stack is still growing
for (SectionStackEntry &entry : sectionStack) { for (SectionStackEntry &entry : sectionStack) {
if (entry.section && !strcmp(name, entry.section->name)) if (entry.section && entry.section->name == name)
return false; return false;
} }
@@ -402,7 +398,7 @@ void sect_NewSection(char const *name, enum SectionType type, uint32_t org,
fatalerror("Cannot change the section within a `LOAD` block\n"); fatalerror("Cannot change the section within a `LOAD` block\n");
for (SectionStackEntry &entry : sectionStack) { for (SectionStackEntry &entry : sectionStack) {
if (entry.section && !strcmp(name, entry.section->name)) if (entry.section && entry.section->name == name)
fatalerror("Section '%s' is already on the stack\n", name); fatalerror("Section '%s' is already on the stack\n", name);
} }