Allow multiple preinclude files (#1763)

This commit is contained in:
Rangi
2025-07-19 17:07:15 -04:00
committed by GitHub
parent 9a4593e823
commit 2935942667
9 changed files with 20 additions and 17 deletions

View File

@@ -56,7 +56,7 @@ std::shared_ptr<std::string> fstk_GetUniqueIDStr();
MacroArgs *fstk_GetCurrentMacroArgs(); MacroArgs *fstk_GetCurrentMacroArgs();
void fstk_AddIncludePath(std::string const &path); void fstk_AddIncludePath(std::string const &path);
void fstk_SetPreIncludeFile(std::string const &path); void fstk_AddPreIncludeFile(std::string const &path);
std::optional<std::string> fstk_FindFile(std::string const &path); std::optional<std::string> fstk_FindFile(std::string const &path);
bool fstk_FileError(std::string const &path, char const *functionName); bool fstk_FileError(std::string const &path, char const *functionName);

View File

@@ -176,6 +176,7 @@ This acts as if a
.Ql Ic INCLUDE Qq Ar include_file .Ql Ic INCLUDE Qq Ar include_file
was read before the input was read before the input
.Ar asmfile . .Ar asmfile .
Multiple files can be pre-included in the order they were provided.
.It Fl p Ar pad_value , Fl \-pad-value Ar pad_value .It Fl p Ar pad_value , Fl \-pad-value Ar pad_value
Use this as the value for Use this as the value for
.Ic DS .Ic DS

View File

@@ -3,6 +3,7 @@
#include "asm/fstack.hpp" #include "asm/fstack.hpp"
#include <sys/stat.h> #include <sys/stat.h>
#include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <memory> #include <memory>
@@ -46,7 +47,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 std::string preIncludeName; static std::deque<std::string> preIncludeNames;
std::string const &FileStackNode::dump(uint32_t curLineNo) const { std::string const &FileStackNode::dump(uint32_t curLineNo) const {
if (std::holds_alternative<std::vector<uint32_t>>(data)) { if (std::holds_alternative<std::vector<uint32_t>>(data)) {
@@ -115,12 +116,9 @@ void fstk_AddIncludePath(std::string const &path) {
} }
} }
void fstk_SetPreIncludeFile(std::string const &path) { void fstk_AddPreIncludeFile(std::string const &path) {
if (!preIncludeName.empty()) { preIncludeNames.emplace_front(path);
warnx("Overriding pre-included filename %s", preIncludeName.c_str()); verbosePrint("Pre-included filename %s\n", path.c_str()); // LCOV_EXCL_LINE
}
preIncludeName = path;
verbosePrint("Pre-included filename %s\n", preIncludeName.c_str()); // LCOV_EXCL_LINE
} }
static bool isValidFilePath(std::string const &path) { static bool isValidFilePath(std::string const &path) {
@@ -413,13 +411,11 @@ void fstk_Init(std::string const &mainPath, size_t maxDepth) {
maxRecursionDepth = maxDepth; maxRecursionDepth = maxDepth;
if (!preIncludeName.empty()) { for (std::string const &name : preIncludeNames) {
if (std::optional<std::string> fullPath = fstk_FindFile(preIncludeName); fullPath) { if (std::optional<std::string> fullPath = fstk_FindFile(name); fullPath) {
newFileContext(*fullPath, false); newFileContext(*fullPath, false);
} else { } else {
error( error("Error reading pre-included file '%s': %s", name.c_str(), strerror(errno));
"Error reading pre-included file '%s': %s", preIncludeName.c_str(), strerror(errno)
);
} }
} }
} }

View File

@@ -270,7 +270,7 @@ int main(int argc, char *argv[]) {
break; break;
case 'P': case 'P':
fstk_SetPreIncludeFile(musl_optarg); fstk_AddPreIncludeFile(musl_optarg);
break; break;
unsigned long padByte; unsigned long padByte;

View File

@@ -0,0 +1,4 @@
warn "another pre-included file"
def v1 += 10
def v2 -= 10

View File

@@ -1,4 +1,6 @@
warning: preinclude.asm(0) -> preinclude.inc(1): [-Wuser] warning: preinclude.asm(0) -> preinclude-2.inc(0) -> preinclude-1.inc(1): [-Wuser]
pre-included file pre-included file
warning: preinclude.asm(0) -> preinclude-2.inc(1): [-Wuser]
another pre-included file
warning: preinclude.asm(1): [-Wuser] warning: preinclude.asm(1): [-Wuser]
main file main file

View File

@@ -1 +1 @@
-Weverything -P preinclude.inc -Weverything -P preinclude-1.inc -P preinclude-2.inc

View File

@@ -4,4 +4,4 @@ rept 3
for 0/3 for 0/3
for 1/3 for 1/3
for 2/3 for 2/3
12 + 34 = 46 22 + 24 = 46