Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/asm/fstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ MacroArgs *fstk_GetCurrentMacroArgs();
void fstk_AddIncludePath(std::string const &path);
void fstk_AddPreIncludeFile(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 *description);
bool fstk_FailedOnMissingInclude();

bool yywrap();
Expand All @@ -84,6 +84,6 @@ void fstk_RunFor(
bool fstk_Break();

void fstk_NewRecursionDepth(size_t newDepth);
void fstk_Init(std::string const &mainPath);
bool fstk_Init(std::string const &mainPath);

#endif // RGBDS_ASM_FSTACK_HPP
2 changes: 1 addition & 1 deletion src/asm/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 15 additions & 9 deletions src/asm/fstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
);
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
7 changes: 2 additions & 5 deletions src/asm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/asm/section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }};

Expand Down Expand Up @@ -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); }};

Expand Down
8 changes: 3 additions & 5 deletions src/link/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,9 @@ int main(int argc, char *argv[]) {
if (localOptions.linkerScriptName) {
verbosePrint(VERB_NOTICE, "Reading linker script...\n");

if (lexer_Init(*localOptions.linkerScriptName)) {
if (yy::parser parser; parser.parse() != 0) {
// Exited due to YYABORT or YYNOMEM
fatal("Unrecoverable error while reading linker script"); // LCOV_EXCL_LINE
}
if (yy::parser parser; lexer_Init(*localOptions.linkerScriptName) && parser.parse() != 0) {
// Exited due to YYABORT or YYNOMEM
fatal("Unrecoverable error while reading linker script"); // LCOV_EXCL_LINE
}

// If the linker script produced any errors, some sections may be in an invalid state
Expand Down
1 change: 1 addition & 0 deletions test/asm/continues-after-missing-include/a.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-MG -MC
13 changes: 13 additions & 0 deletions test/asm/continues-after-missing-preinclude/a.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PUSHC
PUSHO
PUSHS
SECTION "test", WRAM0
UNION
INCLUDE "nonexistent1.inc"
WARN "still going!"
INCLUDE "nonexistent2.inc"
WARN "and going!"
ENDU
POPS
POPO
POPC
4 changes: 4 additions & 0 deletions test/asm/continues-after-missing-preinclude/a.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
warning: still going! [-Wuser]
at continues-after-missing-preinclude/a.asm(7)
warning: and going! [-Wuser]
at continues-after-missing-preinclude/a.asm(9)
1 change: 1 addition & 0 deletions test/asm/continues-after-missing-preinclude/a.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-MC -P nonexistent-pre.inc
4 changes: 4 additions & 0 deletions test/asm/continues-after-missing-preinclude/a.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a.o: continues-after-missing-preinclude/a.asm
a.o: nonexistent-pre.inc
a.o: nonexistent1.inc
a.o: nonexistent2.inc
2 changes: 0 additions & 2 deletions test/asm/errors-after-missing-include/a.out

This file was deleted.

1 change: 1 addition & 0 deletions test/asm/exits-after-missing-include/a.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-MG
2 changes: 2 additions & 0 deletions test/asm/exits-after-missing-include/a.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a.o: exits-after-missing-include/a.asm
a.o: does not exist
12 changes: 12 additions & 0 deletions test/asm/exits-after-missing-preinclude/a.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PUSHC
PUSHO
PUSHS
SECTION "test", WRAM0
UNION
INCLUDE "never reaches here"
/*
ENDU
POPS
POPO
POPC
*/
Empty file.
1 change: 1 addition & 0 deletions test/asm/exits-after-missing-preinclude/a.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-MG -P nonexistent-pre.inc
2 changes: 2 additions & 0 deletions test/asm/exits-after-missing-preinclude/a.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a.o: exits-after-missing-preinclude/a.asm
a.o: nonexistent-pre.inc
2 changes: 1 addition & 1 deletion test/asm/include-slash.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: Error reading pre-included file "include-slash-nonexist.inc": No such file or directory
error: Error opening pre-included file "include-slash-nonexist.inc": No such file or directory
at include-slash.asm(0)
Assembly aborted with 1 error!
1 change: 1 addition & 0 deletions test/asm/nonexist-preinclude.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INCLUDE "nonexist-include.inc"
5 changes: 5 additions & 0 deletions test/asm/nonexist-preinclude.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Error opening pre-included file "nonexistent-pre.inc": No such file or directory
at nonexist-preinclude.asm(0)
error: Error opening `INCLUDE` file "nonexist-include.inc": No such file or directory
at nonexist-preinclude.asm(1)
Assembly aborted with 2 errors!
1 change: 1 addition & 0 deletions test/asm/nonexist-preinclude.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-P nonexistent-pre.inc
13 changes: 9 additions & 4 deletions test/asm/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,14 @@ done

evaluateDepTest () {
i="$1"
RGBASMFLAGS="-Weverything -Bcollapse -M - $2"
RGBASMFLAGS="-Weverything -Bcollapse -M -"
if [ -f "$i/a.flags" ]; then
RGBASMFLAGS="$RGBASMFLAGS @$i/a.flags"
fi
# Piping the .asm file to rgbasm would not make sense for dependency generation,
# so just test the normal variant
(( tests++ ))
echo "${bold}${green}${i%.asm}...${rescolors}${resbold}"
echo "${bold}${green}${i}...${rescolors}${resbold}"
"$RGBASM" $RGBASMFLAGS -o "$o" "$i"/a.asm >"$output" 2>"$errput"

fixed_output="$input"
Expand All @@ -192,8 +195,10 @@ evaluateDepTest () {
(( failed++ ))
fi
}
evaluateDepTest "continues-after-missing-include" "-MG -MC"
evaluateDepTest "errors-after-missing-include" "-MG"
evaluateDepTest "continues-after-missing-include"
evaluateDepTest "exits-after-missing-include"
evaluateDepTest "continues-after-missing-preinclude"
evaluateDepTest "exits-after-missing-preinclude"

i="state-file"
if which cygpath &>/dev/null; then
Expand Down
Loading