From cb5911988183a371c8e82b02af6d90e6b240a2dc Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Mon, 25 Mar 2024 11:33:17 -0400 Subject: [PATCH] Use automatically-allocated `std::string_view` for macros --- include/asm/symbol.hpp | 4 ++-- src/asm/fstack.cpp | 6 ++---- src/asm/parser.y | 1 - src/asm/symbol.cpp | 16 ++++------------ 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/include/asm/symbol.hpp b/include/asm/symbol.hpp index ba32c748..1adf46e9 100644 --- a/include/asm/symbol.hpp +++ b/include/asm/symbol.hpp @@ -38,7 +38,7 @@ struct Symbol { std::variant< int32_t, // If isNumeric() int32_t (*)(), // If isNumeric() and has a callback - std::string_view *, // For SYM_MACRO + std::string_view, // For SYM_MACRO std::shared_ptr // For SYM_EQUS > data; @@ -61,7 +61,7 @@ struct Symbol { int32_t getValue() const; int32_t getOutputValue() const; - std::string_view *getMacro() const; + std::string_view getMacro() const; std::shared_ptr getEqus() const; uint32_t getConstantValue() const; }; diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 1cdc5979..07833cd8 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -282,10 +282,8 @@ static void newMacroContext(Symbol const ¯o, std::shared_ptr macr .macroArgs = macroArgs, }); - std::string_view *macroView = macro.getMacro(); - context.lexerState.setViewAsNextState( - "MACRO", macroView->data(), macroView->size(), macro.fileLine - ); + std::string_view view = macro.getMacro(); + context.lexerState.setViewAsNextState("MACRO", view.data(), view.size(), macro.fileLine); } static Context &newReptContext(int32_t reptLineNo, char const *body, size_t size, uint32_t count) { diff --git a/src/asm/parser.y b/src/asm/parser.y index 0734dcd6..1ee16346 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -47,7 +47,6 @@ #include #include #include - #include #include #include #include diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index b6ac6e05..fed1fc89 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -75,9 +74,9 @@ int32_t Symbol::getOutputValue() const { } } -std::string_view *Symbol::getMacro() const { - assert(std::holds_alternative(data)); - return std::get(data); +std::string_view Symbol::getMacro() const { + assert(std::holds_alternative(data)); + return std::get(data); } std::shared_ptr Symbol::getEqus() const { @@ -172,11 +171,7 @@ void sym_Purge(std::string const &symName) { // Do not keep a reference to the label's name after purging it if (sym->name == labelScope) labelScope = std::nullopt; - - // FIXME: this leaks `sym->getEqus()` for SYM_EQUS and `sym->getMacro()` for SYM_MACRO, - // but this can't delete either of them because the expansion may be purging itself. symbols.erase(sym->name); - // TODO: ideally, also unref the file stack nodes } } @@ -500,11 +495,8 @@ Symbol *sym_AddMacro(std::string const &symName, int32_t defLineNo, char const * if (!sym) return nullptr; - std::string_view *macro = new (std::nothrow) std::string_view(body, size); - if (!macro) - fatalerror("No memory for macro: %s\n", strerror(errno)); sym->type = SYM_MACRO; - sym->data = macro; + sym->data = std::string_view(body, size); sym->src = fstk_GetFileStack(); // The symbol is created at the line after the `ENDM`,