Pass std::string references to fstack functions

This commit is contained in:
Rangi42
2024-03-18 13:41:07 -04:00
committed by Sylvie
parent 472d1bde06
commit e96675be03
5 changed files with 36 additions and 47 deletions

View File

@@ -52,15 +52,13 @@ struct MacroArgs;
void fstk_DumpCurrent(); void fstk_DumpCurrent();
FileStackNode *fstk_GetFileStack(); FileStackNode *fstk_GetFileStack();
// The lifetime of the returned chars is until reaching the end of that file
char const *fstk_GetFileName();
void fstk_AddIncludePath(char const *s); void fstk_AddIncludePath(std::string const &path);
void fstk_SetPreIncludeFile(char const *s); void fstk_SetPreIncludeFile(std::string const &path);
std::optional<std::string> fstk_FindFile(char const *path); std::optional<std::string> fstk_FindFile(std::string const &path);
bool yywrap(); bool yywrap();
void fstk_RunInclude(char const *path); void fstk_RunInclude(std::string const &path);
void fstk_RunMacro(std::string const &macroName, MacroArgs &args); void fstk_RunMacro(std::string const &macroName, MacroArgs &args);
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size); void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size);
void fstk_RunFor( void fstk_RunFor(
@@ -76,6 +74,6 @@ void fstk_StopRept();
bool fstk_Break(); bool fstk_Break();
void fstk_NewRecursionDepth(size_t newDepth); void fstk_NewRecursionDepth(size_t newDepth);
void fstk_Init(char const *mainPath, size_t maxDepth); void fstk_Init(std::string const &mainPath, size_t maxDepth);
#endif // RGBDS_ASM_FSTACK_H #endif // RGBDS_ASM_FSTACK_H

View File

@@ -39,7 +39,7 @@ size_t maxRecursionDepth;
// The first include path for `fstk_FindFile` to try is none at all // The first include path for `fstk_FindFile` to try is none at all
static std::vector<std::string> includePaths = {""}; static std::vector<std::string> includePaths = {""};
static char const *preIncludeName; static std::string preIncludeName;
std::vector<uint32_t> &FileStackNode::iters() { std::vector<uint32_t> &FileStackNode::iters() {
assert(std::holds_alternative<std::vector<uint32_t>>(data)); assert(std::holds_alternative<std::vector<uint32_t>>(data));
@@ -108,56 +108,47 @@ FileStackNode *fstk_GetFileStack() {
return topNode; return topNode;
} }
char const *fstk_GetFileName() { void fstk_AddIncludePath(std::string const &path) {
// Iterating via the nodes themselves skips nested REPTs if (path.empty())
FileStackNode const *node = contextStack.top().fileInfo;
while (node->type != NODE_FILE)
node = node->parent;
return node->name().c_str();
}
void fstk_AddIncludePath(char const *path) {
if (path[0] == '\0')
return; return;
std::string &str = includePaths.emplace_back(path); std::string &includePath = includePaths.emplace_back(path);
if (str.back() != '/') if (includePath.back() != '/')
str += '/'; includePath += '/';
} }
void fstk_SetPreIncludeFile(char const *path) { void fstk_SetPreIncludeFile(std::string const &path) {
if (preIncludeName) if (!preIncludeName.empty())
warnx("Overriding pre-included filename %s", preIncludeName); warnx("Overriding pre-included filename %s", preIncludeName.c_str());
preIncludeName = path; preIncludeName = path;
if (verbose) if (verbose)
printf("Pre-included filename %s\n", preIncludeName); printf("Pre-included filename %s\n", preIncludeName.c_str());
} }
static void printDep(char const *path) { static void printDep(std::string const &path) {
if (dependfile) { if (dependfile) {
fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), path); fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), path.c_str());
if (generatePhonyDeps) if (generatePhonyDeps)
fprintf(dependfile, "%s:\n", path); fprintf(dependfile, "%s:\n", path.c_str());
} }
} }
static bool isPathValid(char const *path) { static bool isPathValid(std::string const &path) {
struct stat statbuf; struct stat statbuf;
if (stat(path, &statbuf) != 0) if (stat(path.c_str(), &statbuf) != 0)
return false; return false;
// Reject directories // Reject directories
return !S_ISDIR(statbuf.st_mode); return !S_ISDIR(statbuf.st_mode);
} }
std::optional<std::string> fstk_FindFile(char const *path) { std::optional<std::string> fstk_FindFile(std::string const &path) {
for (std::string &str : includePaths) { for (std::string &str : includePaths) {
std::string fullPath = str + path; std::string fullPath = str + path;
if (isPathValid(fullPath.c_str())) { if (isPathValid(fullPath)) {
printDep(fullPath.c_str()); printDep(fullPath);
return fullPath; return fullPath;
} }
} }
@@ -252,15 +243,15 @@ static Context &newContext(FileStackNode &fileInfo) {
return context; return context;
} }
void fstk_RunInclude(char const *path) { void fstk_RunInclude(std::string const &path) {
std::optional<std::string> fullPath = fstk_FindFile(path); std::optional<std::string> fullPath = fstk_FindFile(path);
if (!fullPath) { if (!fullPath) {
if (generatedMissingIncludes) { if (generatedMissingIncludes) {
if (verbose) if (verbose)
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", path, strerror(errno)); printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", path.c_str(), strerror(errno));
failedOnMissingInclude = true; failedOnMissingInclude = true;
} else { } else {
error("Unable to open included file '%s': %s\n", path, strerror(errno)); error("Unable to open included file '%s': %s\n", path.c_str(), strerror(errno));
} }
return; return;
} }
@@ -286,12 +277,12 @@ void fstk_RunInclude(char const *path) {
// Similar to `fstk_RunInclude`, but not subject to `-MG`, and // Similar to `fstk_RunInclude`, but not subject to `-MG`, and
// calling `lexer_SetState` instead of `lexer_SetStateAtEOL`. // calling `lexer_SetState` instead of `lexer_SetStateAtEOL`.
static void runPreIncludeFile() { static void runPreIncludeFile() {
if (!preIncludeName) if (preIncludeName.empty())
return; return;
std::optional<std::string> fullPath = fstk_FindFile(preIncludeName); std::optional<std::string> fullPath = fstk_FindFile(preIncludeName);
if (!fullPath) { if (!fullPath) {
error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno)); error("Unable to open included file '%s': %s\n", preIncludeName.c_str(), strerror(errno));
return; return;
} }
@@ -454,9 +445,9 @@ void fstk_NewRecursionDepth(size_t newDepth) {
maxRecursionDepth = newDepth; maxRecursionDepth = newDepth;
} }
void fstk_Init(char const *mainPath, size_t maxDepth) { void fstk_Init(std::string const &mainPath, size_t maxDepth) {
Context &context = contextStack.emplace(); Context &context = contextStack.emplace();
if (!lexer_OpenFile(context.lexerState, mainPath)) if (!lexer_OpenFile(context.lexerState, mainPath.c_str()))
fatalerror("Failed to open main file\n"); fatalerror("Failed to open main file\n");
lexer_SetState(&context.lexerState); lexer_SetState(&context.lexerState);

View File

@@ -377,17 +377,17 @@ int main(int argc, char *argv[]) {
exit(1); exit(1);
} }
char const *mainFileName = argv[musl_optind]; std::string mainFileName = argv[musl_optind];
if (verbose) if (verbose)
printf("Assembling %s\n", mainFileName); printf("Assembling %s\n", mainFileName.c_str());
if (dependfile) { if (dependfile) {
if (targetFileName.empty()) if (targetFileName.empty())
errx("Dependency files can only be created if a target file is specified with either " errx("Dependency files can only be created if a target file is specified with either "
"-o, -MQ or -MT"); "-o, -MQ or -MT");
fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), mainFileName); fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), mainFileName.c_str());
} }
charmap_New(DEFAULT_CHARMAP_NAME, nullptr); charmap_New(DEFAULT_CHARMAP_NAME, nullptr);

View File

@@ -1123,7 +1123,7 @@ export_list_entry:
include: include:
label POP_INCLUDE string endofline { label POP_INCLUDE string endofline {
fstk_RunInclude($3.c_str()); fstk_RunInclude($3);
if (failedOnMissingInclude) if (failedOnMissingInclude)
YYACCEPT; YYACCEPT;
} }

View File

@@ -834,7 +834,7 @@ void sect_BinaryFile(std::string const &name, int32_t startPos) {
if (!checkcodesection()) if (!checkcodesection())
return; return;
std::optional<std::string> fullPath = fstk_FindFile(name.c_str()); std::optional<std::string> fullPath = fstk_FindFile(name);
FILE *file = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; FILE *file = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
if (!file) { if (!file) {
if (generatedMissingIncludes) { if (generatedMissingIncludes) {
@@ -902,7 +902,7 @@ void sect_BinaryFileSlice(std::string const &name, int32_t startPos, int32_t len
if (!reserveSpace(length)) if (!reserveSpace(length))
return; return;
std::optional<std::string> fullPath = fstk_FindFile(name.c_str()); std::optional<std::string> fullPath = fstk_FindFile(name);
FILE *file = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; FILE *file = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
if (!file) { if (!file) {
if (generatedMissingIncludes) { if (generatedMissingIncludes) {