mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::string for macro args
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "asm/warning.hpp"
|
||||
@@ -13,10 +14,9 @@
|
||||
|
||||
struct MacroArgs {
|
||||
unsigned int shift;
|
||||
std::vector<char *> args;
|
||||
std::vector<std::string> args;
|
||||
|
||||
void append(char *s);
|
||||
void clear();
|
||||
void append(std::string s);
|
||||
};
|
||||
|
||||
MacroArgs *macro_GetCurrentArgs();
|
||||
|
||||
@@ -239,10 +239,8 @@ bool yywrap()
|
||||
|
||||
lexer_CleanupState(oldContext.lexerState);
|
||||
// Restore args if a macro (not REPT) saved them
|
||||
if (oldContext.fileInfo->type == NODE_MACRO) {
|
||||
macro_GetCurrentArgs()->clear();
|
||||
if (oldContext.fileInfo->type == NODE_MACRO)
|
||||
macro_UseNewArgs(contextStack.top().macroArgs);
|
||||
}
|
||||
// Free the file stack node
|
||||
if (!oldContext.fileInfo->referenced)
|
||||
delete oldContext.fileInfo;
|
||||
|
||||
@@ -16,27 +16,18 @@
|
||||
static MacroArgs *macroArgs = nullptr;
|
||||
static uint32_t uniqueID = 0;
|
||||
static uint32_t maxUniqueID = 0;
|
||||
// The initialization is somewhat harmful, since it is never used, but it
|
||||
// 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 uniqueIDBuf[sizeof("_u4294967295")] = {}; // UINT32_MAX
|
||||
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");
|
||||
if (args.size() == MAXMACROARGS)
|
||||
error("A maximum of " EXPAND_AND_STR(MAXMACROARGS) " arguments is allowed\n");
|
||||
args.push_back(s);
|
||||
}
|
||||
|
||||
void MacroArgs::clear()
|
||||
{
|
||||
for (char *arg : args)
|
||||
free(arg);
|
||||
}
|
||||
|
||||
MacroArgs *macro_GetCurrentArgs()
|
||||
{
|
||||
return macroArgs;
|
||||
@@ -54,7 +45,7 @@ char const *macro_GetArg(uint32_t i)
|
||||
|
||||
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()
|
||||
@@ -70,7 +61,7 @@ char const *macro_GetAllArgs()
|
||||
size_t len = 0;
|
||||
|
||||
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 *ptr = str;
|
||||
@@ -79,10 +70,10 @@ char const *macro_GetAllArgs()
|
||||
fatalerror("Failed to allocate memory for expanding '\\#': %s\n", strerror(errno));
|
||||
|
||||
for (uint32_t i = macroArgs->shift; i < nbArgs; i++) {
|
||||
char *arg = macroArgs->args[i];
|
||||
size_t n = strlen(arg);
|
||||
std::string const &arg = macroArgs->args[i];
|
||||
size_t n = arg.length();
|
||||
|
||||
memcpy(ptr, arg, n);
|
||||
memcpy(ptr, arg.c_str(), n);
|
||||
ptr += n;
|
||||
|
||||
// Commas go between args and after a last empty arg
|
||||
|
||||
@@ -52,7 +52,6 @@
|
||||
#include "extern/utf8decoder.hpp"
|
||||
|
||||
#include "helpers.hpp"
|
||||
#include "platform.hpp" // strncasecmp, strdup
|
||||
|
||||
static CaptureBody captureBody; // Captures a REPT/FOR or MACRO
|
||||
|
||||
@@ -507,10 +506,9 @@ macroargs : %empty {
|
||||
if (!$$)
|
||||
fatalerror("Failed to allocate memory for macro arguments: %s\n",
|
||||
strerror(errno));
|
||||
$$->shift = 0;
|
||||
}
|
||||
| macroargs T_STRING {
|
||||
$$->append(strdup($2));
|
||||
$$->append($2);
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "error.hpp"
|
||||
#include "helpers.hpp"
|
||||
#include "platform.hpp" // strdup
|
||||
#include "version.hpp"
|
||||
|
||||
std::map<std::string, Symbol> symbols;
|
||||
|
||||
Reference in New Issue
Block a user