Use automatically-allocated std::string_view for macros

This commit is contained in:
Rangi42
2024-03-25 11:33:17 -04:00
parent d9e5e57e27
commit cb59119881
4 changed files with 8 additions and 19 deletions

View File

@@ -38,7 +38,7 @@ struct Symbol {
std::variant< std::variant<
int32_t, // If isNumeric() int32_t, // If isNumeric()
int32_t (*)(), // If isNumeric() and has a callback int32_t (*)(), // If isNumeric() and has a callback
std::string_view *, // For SYM_MACRO std::string_view, // For SYM_MACRO
std::shared_ptr<std::string> // For SYM_EQUS std::shared_ptr<std::string> // For SYM_EQUS
> >
data; data;
@@ -61,7 +61,7 @@ struct Symbol {
int32_t getValue() const; int32_t getValue() const;
int32_t getOutputValue() const; int32_t getOutputValue() const;
std::string_view *getMacro() const; std::string_view getMacro() const;
std::shared_ptr<std::string> getEqus() const; std::shared_ptr<std::string> getEqus() const;
uint32_t getConstantValue() const; uint32_t getConstantValue() const;
}; };

View File

@@ -282,10 +282,8 @@ static void newMacroContext(Symbol const &macro, std::shared_ptr<MacroArgs> macr
.macroArgs = macroArgs, .macroArgs = macroArgs,
}); });
std::string_view *macroView = macro.getMacro(); std::string_view view = macro.getMacro();
context.lexerState.setViewAsNextState( context.lexerState.setViewAsNextState("MACRO", view.data(), view.size(), macro.fileLine);
"MACRO", macroView->data(), macroView->size(), macro.fileLine
);
} }
static Context &newReptContext(int32_t reptLineNo, char const *body, size_t size, uint32_t count) { static Context &newReptContext(int32_t reptLineNo, char const *body, size_t size, uint32_t count) {

View File

@@ -47,7 +47,6 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <new>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@@ -5,7 +5,6 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <new>
#include <stdio.h> #include <stdio.h>
#include <unordered_map> #include <unordered_map>
@@ -75,9 +74,9 @@ int32_t Symbol::getOutputValue() const {
} }
} }
std::string_view *Symbol::getMacro() const { std::string_view Symbol::getMacro() const {
assert(std::holds_alternative<std::string_view *>(data)); assert(std::holds_alternative<std::string_view>(data));
return std::get<std::string_view *>(data); return std::get<std::string_view>(data);
} }
std::shared_ptr<std::string> Symbol::getEqus() const { std::shared_ptr<std::string> 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 // Do not keep a reference to the label's name after purging it
if (sym->name == labelScope) if (sym->name == labelScope)
labelScope = std::nullopt; 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); 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) if (!sym)
return nullptr; 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->type = SYM_MACRO;
sym->data = macro; sym->data = std::string_view(body, size);
sym->src = fstk_GetFileStack(); sym->src = fstk_GetFileStack();
// The symbol is created at the line after the `ENDM`, // The symbol is created at the line after the `ENDM`,