Reuse startsIdentifier and continuesIdentifier functions (#1695)

This commit is contained in:
Rangi
2025-05-19 15:31:26 -04:00
committed by GitHub
parent 4f2400c15b
commit 126b1e5726
6 changed files with 32 additions and 48 deletions

View File

@@ -15,6 +15,7 @@
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "util.hpp"
#include "link/main.hpp"
#include "link/section.hpp"
@@ -260,29 +261,13 @@ static void writeROM() {
}
}
// Checks whether a symbol is legal for a sym file or map file.
// Eliminates anonymous labels, which start with a '!'.
static bool isLegalSymbol(Symbol const &sym) {
if (sym.name.empty()) {
return false;
}
char c = sym.name[0];
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
}
// Checks whether this character is legal in a symbol's name in a sym file
static bool isLegalForSymName(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'
|| c == '@' || c == '#' || c == '$' || c == '.';
}
// Prints a symbol's name to a file, assuming that the first character is legal.
// Illegal characters are UTF-8-decoded (errors are replaced by U+FFFD) and emitted as '\u'/'\U'.
static void printSymName(std::string const &name, FILE *file) {
for (char const *ptr = name.c_str(); *ptr != '\0';) {
char c = *ptr;
if (isLegalForSymName(c)) {
if (continuesIdentifier(c)) {
// Output legal ASCII characters as-is
putc(c, file);
++ptr;
@@ -369,7 +354,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u
forEachSortedSection(sect, {
for (Symbol const *sym : sect->symbols) {
// Don't output symbols that begin with an illegal character
if (isLegalSymbol(*sym)) {
if (!sym->name.empty() && startsIdentifier(sym->name[0])) {
uint16_t addr = static_cast<uint16_t>(sym->label().offset + sect->org);
uint16_t parentAddr = addr;
if (auto pos = sym->name.find('.'); pos != std::string::npos) {
@@ -482,7 +467,7 @@ static void writeMapBank(SortedSections const &sectList, SectionType type, uint3
for (uint16_t org = sect->org; sect; sect = sect->nextu.get()) {
for (Symbol *sym : sect->symbols) {
// Don't output symbols that begin with an illegal character
if (isLegalSymbol(*sym)) {
if (!sym->name.empty() && startsIdentifier(sym->name[0])) {
// Space matches "\tSECTION: $xxxx ..."
fprintf(mapFile, "\t $%04" PRIx32 " = ", sym->label().offset + org);
printSymName(sym->name, mapFile);