Use std::string for PURGE args

This commit is contained in:
Rangi42
2024-02-27 19:55:37 -05:00
committed by Sylvie
parent 0bed84174b
commit 962398969b
3 changed files with 14 additions and 20 deletions

View File

@@ -4,6 +4,7 @@
#define RGBDS_SYMBOL_H #define RGBDS_SYMBOL_H
#include <stdint.h> #include <stdint.h>
#include <string>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@@ -128,7 +129,7 @@ struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body,
struct Symbol *sym_Ref(char const *symName); struct Symbol *sym_Ref(char const *symName);
struct Symbol *sym_AddString(char const *symName, char const *value); struct Symbol *sym_AddString(char const *symName, char const *value);
struct Symbol *sym_RedefString(char const *symName, char const *value); struct Symbol *sym_RedefString(char const *symName, char const *value);
void sym_Purge(char const *symName); void sym_Purge(std::string const &symName);
void sym_Init(time_t now); void sym_Init(time_t now);
// Functions to save and restore the current symbol scope. // Functions to save and restore the current symbol scope.

View File

@@ -361,20 +361,13 @@ static void initDsArgList(std::vector<struct Expression> *&args)
fatalerror("Failed to allocate memory for ds arg list: %s\n", strerror(errno)); fatalerror("Failed to allocate memory for ds arg list: %s\n", strerror(errno));
} }
static void initPurgeArgList(std::vector<char *> *&args) static void initPurgeArgList(std::vector<std::string> *&args)
{ {
args = new(std::nothrow) std::vector<char *>(); args = new(std::nothrow) std::vector<std::string>();
if (!args) if (!args)
fatalerror("Failed to allocate memory for purge arg list: %s\n", strerror(errno)); fatalerror("Failed to allocate memory for purge arg list: %s\n", strerror(errno));
} }
static void freePurgeArgList(std::vector<char *> *&args)
{
for (char *arg : *args)
free(arg);
delete args;
}
static void failAssert(enum AssertionType type) static void failAssert(enum AssertionType type)
{ {
switch (type) { switch (type) {
@@ -468,7 +461,7 @@ enum {
enum AssertionType assertType; enum AssertionType assertType;
struct AlignmentSpec alignSpec; struct AlignmentSpec alignSpec;
std::vector<struct Expression> *dsArgs; std::vector<struct Expression> *dsArgs;
std::vector<char *> *purgeArgs; std::vector<std::string> *purgeArgs;
struct ForArgs forArgs; struct ForArgs forArgs;
struct StrFmtArgList strfmtArgs; struct StrFmtArgList strfmtArgs;
bool captureTerminated; bool captureTerminated;
@@ -1235,19 +1228,19 @@ redef_equs : redef_id T_POP_EQUS string { sym_RedefString($1, $3); }
purge : T_POP_PURGE { purge : T_POP_PURGE {
lexer_ToggleStringExpansion(false); lexer_ToggleStringExpansion(false);
} purge_args trailing_comma { } purge_args trailing_comma {
for (char *arg : *$3) for (std::string &arg : *$3)
sym_Purge(arg); sym_Purge(arg);
freePurgeArgList($3); delete $3;
lexer_ToggleStringExpansion(true); lexer_ToggleStringExpansion(true);
} }
; ;
purge_args : scoped_id { purge_args : scoped_id {
initPurgeArgList($$); initPurgeArgList($$);
$$->push_back(strdup($1)); $$->push_back($1);
} }
| purge_args T_COMMA scoped_id { | purge_args T_COMMA scoped_id {
$1->push_back(strdup($3)); $1->push_back($3);
$$ = $1; $$ = $1;
} }
; ;

View File

@@ -199,16 +199,16 @@ static bool isReferenced(struct Symbol const *sym)
} }
// Purge a symbol // Purge a symbol
void sym_Purge(char const *symName) void sym_Purge(std::string const &symName)
{ {
struct Symbol *sym = sym_FindScopedValidSymbol(symName); struct Symbol *sym = sym_FindScopedValidSymbol(symName.c_str());
if (!sym) { if (!sym) {
error("'%s' not defined\n", symName); error("'%s' not defined\n", symName.c_str());
} else if (sym->isBuiltin) { } else if (sym->isBuiltin) {
error("Built-in symbol '%s' cannot be purged\n", symName); error("Built-in symbol '%s' cannot be purged\n", symName.c_str());
} else if (isReferenced(sym)) { } else if (isReferenced(sym)) {
error("Symbol \"%s\" is referenced and thus cannot be purged\n", symName); error("Symbol \"%s\" is referenced and thus cannot be purged\n", symName.c_str());
} else { } else {
// Do not keep a reference to the label's name after purging it // Do not keep a reference to the label's name after purging it
if (sym->name == labelScope) if (sym->name == labelScope)