diff --git a/include/asm/macro.hpp b/include/asm/macro.hpp index 67b74fb6..b90007a7 100644 --- a/include/asm/macro.hpp +++ b/include/asm/macro.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "asm/warning.hpp" @@ -13,10 +14,9 @@ struct MacroArgs { unsigned int shift; - std::vector args; + std::vector args; - void append(char *s); - void clear(); + void append(std::string s); }; MacroArgs *macro_GetCurrentArgs(); diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index fab459c9..4597d8e7 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -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; diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index 4b0aa867..19be7182 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -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 diff --git a/src/asm/parser.y b/src/asm/parser.y index 61e90ced..cf98119f 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -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); } ; diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index ca280333..023d6d65 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -25,6 +25,7 @@ #include "error.hpp" #include "helpers.hpp" +#include "platform.hpp" // strdup #include "version.hpp" std::map symbols;