Use std::string for macro args

This commit is contained in:
Rangi42
2024-03-02 05:23:15 -05:00
parent b130c2e27c
commit 2069a95e0f
5 changed files with 14 additions and 26 deletions

View File

@@ -5,6 +5,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <vector> #include <vector>
#include "asm/warning.hpp" #include "asm/warning.hpp"
@@ -13,10 +14,9 @@
struct MacroArgs { struct MacroArgs {
unsigned int shift; unsigned int shift;
std::vector<char *> args; std::vector<std::string> args;
void append(char *s); void append(std::string s);
void clear();
}; };
MacroArgs *macro_GetCurrentArgs(); MacroArgs *macro_GetCurrentArgs();

View File

@@ -239,10 +239,8 @@ bool yywrap()
lexer_CleanupState(oldContext.lexerState); lexer_CleanupState(oldContext.lexerState);
// Restore args if a macro (not REPT) saved them // Restore args if a macro (not REPT) saved them
if (oldContext.fileInfo->type == NODE_MACRO) { if (oldContext.fileInfo->type == NODE_MACRO)
macro_GetCurrentArgs()->clear();
macro_UseNewArgs(contextStack.top().macroArgs); macro_UseNewArgs(contextStack.top().macroArgs);
}
// Free the file stack node // Free the file stack node
if (!oldContext.fileInfo->referenced) if (!oldContext.fileInfo->referenced)
delete oldContext.fileInfo; delete oldContext.fileInfo;

View File

@@ -16,27 +16,18 @@
static MacroArgs *macroArgs = nullptr; static MacroArgs *macroArgs = nullptr;
static uint32_t uniqueID = 0; static uint32_t uniqueID = 0;
static uint32_t maxUniqueID = 0; static uint32_t maxUniqueID = 0;
// The initialization is somewhat harmful, since it is never used, but it static char uniqueIDBuf[sizeof("_u4294967295")] = {}; // UINT32_MAX
// guarantees the size of the buffer will be correct. I was unable to find a
// better solution, but if you have one, please feel free!
static char uniqueIDBuf[] = "_u4294967295"; // UINT32_MAX
static char *uniqueIDPtr = nullptr; static char *uniqueIDPtr = nullptr;
void MacroArgs::append(char *s) void MacroArgs::append(std::string s)
{ {
if (s[0] == '\0') if (s.empty())
warning(WARNING_EMPTY_MACRO_ARG, "Empty macro argument\n"); warning(WARNING_EMPTY_MACRO_ARG, "Empty macro argument\n");
if (args.size() == MAXMACROARGS) if (args.size() == MAXMACROARGS)
error("A maximum of " EXPAND_AND_STR(MAXMACROARGS) " arguments is allowed\n"); error("A maximum of " EXPAND_AND_STR(MAXMACROARGS) " arguments is allowed\n");
args.push_back(s); args.push_back(s);
} }
void MacroArgs::clear()
{
for (char *arg : args)
free(arg);
}
MacroArgs *macro_GetCurrentArgs() MacroArgs *macro_GetCurrentArgs()
{ {
return macroArgs; return macroArgs;
@@ -54,7 +45,7 @@ char const *macro_GetArg(uint32_t i)
uint32_t realIndex = i + macroArgs->shift - 1; uint32_t realIndex = i + macroArgs->shift - 1;
return realIndex >= macroArgs->args.size() ? nullptr : macroArgs->args[realIndex]; return realIndex >= macroArgs->args.size() ? nullptr : macroArgs->args[realIndex].c_str();
} }
char const *macro_GetAllArgs() char const *macro_GetAllArgs()
@@ -70,7 +61,7 @@ char const *macro_GetAllArgs()
size_t len = 0; size_t len = 0;
for (uint32_t i = macroArgs->shift; i < nbArgs; i++) for (uint32_t i = macroArgs->shift; i < nbArgs; i++)
len += strlen(macroArgs->args[i]) + 1; // 1 for comma len += macroArgs->args[i].length() + 1; // 1 for comma
char *str = (char *)malloc(len + 1); // 1 for '\0' char *str = (char *)malloc(len + 1); // 1 for '\0'
char *ptr = str; char *ptr = str;
@@ -79,10 +70,10 @@ char const *macro_GetAllArgs()
fatalerror("Failed to allocate memory for expanding '\\#': %s\n", strerror(errno)); fatalerror("Failed to allocate memory for expanding '\\#': %s\n", strerror(errno));
for (uint32_t i = macroArgs->shift; i < nbArgs; i++) { for (uint32_t i = macroArgs->shift; i < nbArgs; i++) {
char *arg = macroArgs->args[i]; std::string const &arg = macroArgs->args[i];
size_t n = strlen(arg); size_t n = arg.length();
memcpy(ptr, arg, n); memcpy(ptr, arg.c_str(), n);
ptr += n; ptr += n;
// Commas go between args and after a last empty arg // Commas go between args and after a last empty arg

View File

@@ -52,7 +52,6 @@
#include "extern/utf8decoder.hpp" #include "extern/utf8decoder.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "platform.hpp" // strncasecmp, strdup
static CaptureBody captureBody; // Captures a REPT/FOR or MACRO static CaptureBody captureBody; // Captures a REPT/FOR or MACRO
@@ -507,10 +506,9 @@ macroargs : %empty {
if (!$$) if (!$$)
fatalerror("Failed to allocate memory for macro arguments: %s\n", fatalerror("Failed to allocate memory for macro arguments: %s\n",
strerror(errno)); strerror(errno));
$$->shift = 0;
} }
| macroargs T_STRING { | macroargs T_STRING {
$$->append(strdup($2)); $$->append($2);
} }
; ;

View File

@@ -25,6 +25,7 @@
#include "error.hpp" #include "error.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "platform.hpp" // strdup
#include "version.hpp" #include "version.hpp"
std::map<std::string, Symbol> symbols; std::map<std::string, Symbol> symbols;