Use std::optional<std::string> for lexer expansion names

This commit is contained in:
Rangi42
2024-03-02 04:34:42 -05:00
parent 701b926288
commit b488d3a90f
2 changed files with 7 additions and 4 deletions

View File

@@ -4,6 +4,8 @@
#define RGBDS_ASM_LEXER_H #define RGBDS_ASM_LEXER_H
#include <deque> #include <deque>
#include <optional>
#include <string>
#include "platform.hpp" // SSIZE_MAX #include "platform.hpp" // SSIZE_MAX
@@ -25,7 +27,7 @@ enum LexerMode {
}; };
struct Expansion { struct Expansion {
char *name; std::optional<std::string> name;
union { union {
char const *unowned; char const *unowned;
char *owned; char *owned;

View File

@@ -10,9 +10,11 @@
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <new> #include <new>
#include <optional>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <string.h> #include <string.h>
#include <unordered_map> #include <unordered_map>
#ifndef _MSC_VER #ifndef _MSC_VER
@@ -509,7 +511,7 @@ static void beginExpansion(char const *str, bool owned, char const *name)
lexer_CheckRecursionDepth(); lexer_CheckRecursionDepth();
lexerState->expansions.push_front({ lexerState->expansions.push_front({
.name = name ? strdup(name) : nullptr, .name = name ? std::optional<std::string>(name) : std::nullopt,
.contents = { .unowned = str }, .contents = { .unowned = str },
.size = size, .size = size,
.offset = 0, .offset = 0,
@@ -525,7 +527,6 @@ void lexer_CheckRecursionDepth()
static void freeExpansion(Expansion &expansion) static void freeExpansion(Expansion &expansion)
{ {
free(expansion.name);
if (expansion.owned) if (expansion.owned)
free(expansion.contents.owned); free(expansion.contents.owned);
} }
@@ -843,7 +844,7 @@ void lexer_DumpStringExpansions()
for (Expansion &exp : lexerState->expansions) { for (Expansion &exp : lexerState->expansions) {
// Only register EQUS expansions, not string args // Only register EQUS expansions, not string args
if (exp.name) if (exp.name)
fprintf(stderr, "while expanding symbol \"%s\"\n", exp.name); fprintf(stderr, "while expanding symbol \"%s\"\n", exp.name->c_str());
} }
} }