Handle a missing -P/--preinclude file the same as an INCLUDE (#1873)

This commit is contained in:
Rangi
2025-12-08 14:39:34 -05:00
committed by GitHub
parent 33475e2c36
commit b0e0dfc56e
25 changed files with 83 additions and 31 deletions

View File

@@ -139,7 +139,7 @@ std::optional<std::string> act_ReadFile(std::string const &name, uint32_t maxLen
file = fopen(fullPath->c_str(), "rb");
}
if (!file) {
if (fstk_FileError(name, "READFILE")) {
if (fstk_FileError(name, "`READFILE`")) {
// If `fstk_FileError` returned true due to `-MG`, we should abort due to a
// missing file, so return `std::nullopt`, which tells the caller to `YYACCEPT`
return std::nullopt;

View File

@@ -191,10 +191,14 @@ std::optional<std::string> fstk_FindFile(std::string const &path) {
}
}
errno = ENOENT;
if (options.missingIncludeState != INC_ERROR) {
printDep(path);
}
// Set `errno` as if `fopen` had failed on a nonexistent file.
// This allows a subsequent `fstk_FileError` to report correctly with `strerror`.
errno = ENOENT;
return std::nullopt;
}
@@ -351,17 +355,17 @@ static Context &
return context;
}
bool fstk_FileError(std::string const &path, char const *functionName) {
bool fstk_FileError(std::string const &path, char const *description) {
if (options.missingIncludeState == INC_ERROR) {
error("Error opening `%s` file \"%s\": %s", functionName, path.c_str(), strerror(errno));
error("Error opening %s file \"%s\": %s", description, path.c_str(), strerror(errno));
} else {
failedOnMissingInclude = true;
// LCOV_EXCL_START
if (options.missingIncludeState == GEN_EXIT) {
verbosePrint(
VERB_NOTICE,
"Aborting due to '-MG' on `%s` file \"%s\": %s\n",
functionName,
"Aborting due to '-MG' on %s file \"%s\": %s\n",
description,
path.c_str(),
strerror(errno)
);
@@ -382,7 +386,7 @@ bool fstk_RunInclude(std::string const &path, bool isQuiet) {
newFileContext(*fullPath, isQuiet, false);
return false;
}
return fstk_FileError(path, "INCLUDE");
return fstk_FileError(path, "`INCLUDE`");
}
void fstk_RunMacro(
@@ -490,14 +494,16 @@ void fstk_NewRecursionDepth(size_t newDepth) {
options.maxRecursionDepth = newDepth;
}
void fstk_Init(std::string const &mainPath) {
bool fstk_Init(std::string const &mainPath) {
newFileContext(mainPath, false, true);
for (std::string const &name : preIncludeNames) {
if (std::optional<std::string> fullPath = fstk_FindFile(name); fullPath) {
newFileContext(*fullPath, false, false);
} else {
error("Error reading pre-included file \"%s\": %s", name.c_str(), strerror(errno));
} else if (fstk_FileError(name, "pre-included")) {
return false;
}
}
return true;
}

View File

@@ -558,11 +558,8 @@ int main(int argc, char *argv[]) {
charmap_New(DEFAULT_CHARMAP_NAME, nullptr);
// Init lexer and file stack, providing file info
fstk_Init(*localOptions.inputFileName);
// Perform parse (`yy::parser` is auto-generated from `parser.y`)
if (yy::parser parser; parser.parse() != 0) {
// Init lexer and file stack, and parse (`yy::parser` is auto-generated from `parser.y`)
if (yy::parser parser; fstk_Init(*localOptions.inputFileName) && parser.parse() != 0) {
// Exited due to YYABORT or YYNOMEM
fatal("Unrecoverable error while parsing"); // LCOV_EXCL_LINE
}

View File

@@ -929,7 +929,7 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) {
file = fopen(fullPath->c_str(), "rb");
}
if (!file) {
return fstk_FileError(name, "INCBIN");
return fstk_FileError(name, "`INCBIN`");
}
Defer closeFile{[&] { fclose(file); }};
@@ -984,7 +984,7 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l
file = fopen(fullPath->c_str(), "rb");
}
if (!file) {
return fstk_FileError(name, "INCBIN");
return fstk_FileError(name, "`INCBIN`");
}
Defer closeFile{[&] { fclose(file); }};