From 804db4e073d8a925fac295323bc6a94410ad4d16 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Thu, 22 May 2025 04:55:58 -0400 Subject: [PATCH] Handle missing newline at EOF for linkerscript `INCLUDE`d files (#1691) --- src/link/script.y | 12 +++++++++--- test/link/script-include/a.asm | 2 ++ test/link/script-include/a.inc | 3 +++ test/link/script-include/b.asm | 2 ++ test/link/script-include/b.inc | 3 +++ test/link/script-include/out.err | 0 test/link/script-include/ref.out.bin | 1 + test/link/script-include/script.link | 2 ++ test/link/test.sh | 10 ++++++++++ 9 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/link/script-include/a.asm create mode 100644 test/link/script-include/a.inc create mode 100644 test/link/script-include/b.asm create mode 100644 test/link/script-include/b.inc create mode 100644 test/link/script-include/out.err create mode 100644 test/link/script-include/ref.out.bin create mode 100644 test/link/script-include/script.link diff --git a/src/link/script.y b/src/link/script.y index 7a299640..5e18c62c 100644 --- a/src/link/script.y +++ b/src/link/script.y @@ -251,10 +251,16 @@ yy::parser::symbol_type yylex() { if (c == EOF) { // Basically yywrap(). if (lexerStack.size() != 1) { - lexerStack.pop_back(); - return yylex(); + if (!atEof) { + // Inject a newline at EOF to simplify parsing. + atEof = true; + return yy::parser::make_newline(); + } else { + lexerStack.pop_back(); + return yylex(); + } } else if (!atEof) { - // Inject a newline at EOF, to avoid errors for files that don't end with one. + // Inject a newline at EOF to simplify parsing. atEof = true; return yy::parser::make_newline(); } else { diff --git a/test/link/script-include/a.asm b/test/link/script-include/a.asm new file mode 100644 index 00000000..f6e4bab5 --- /dev/null +++ b/test/link/script-include/a.asm @@ -0,0 +1,2 @@ +section "a", rom0 +db $11 diff --git a/test/link/script-include/a.inc b/test/link/script-include/a.inc new file mode 100644 index 00000000..c37cfb54 --- /dev/null +++ b/test/link/script-include/a.inc @@ -0,0 +1,3 @@ +rom0 + org 0 + "a" ; no newline \ No newline at end of file diff --git a/test/link/script-include/b.asm b/test/link/script-include/b.asm new file mode 100644 index 00000000..8f3f779a --- /dev/null +++ b/test/link/script-include/b.asm @@ -0,0 +1,2 @@ +section "b", rom0 +db $22 diff --git a/test/link/script-include/b.inc b/test/link/script-include/b.inc new file mode 100644 index 00000000..05b6fb4e --- /dev/null +++ b/test/link/script-include/b.inc @@ -0,0 +1,3 @@ +rom0 + org 1 + "b" ; yes newline diff --git a/test/link/script-include/out.err b/test/link/script-include/out.err new file mode 100644 index 00000000..e69de29b diff --git a/test/link/script-include/ref.out.bin b/test/link/script-include/ref.out.bin new file mode 100644 index 00000000..3a1ce181 --- /dev/null +++ b/test/link/script-include/ref.out.bin @@ -0,0 +1 @@ +" \ No newline at end of file diff --git a/test/link/script-include/script.link b/test/link/script-include/script.link new file mode 100644 index 00000000..8bfeab52 --- /dev/null +++ b/test/link/script-include/script.link @@ -0,0 +1,2 @@ +include "script-include/a.inc" +include "script-include/b.inc" diff --git a/test/link/test.sh b/test/link/test.sh index 4012b1de..0013ad69 100755 --- a/test/link/test.sh +++ b/test/link/test.sh @@ -262,6 +262,16 @@ tryDiff "$test"/out.err "$outtemp" tryCmpRomSize "$gbtemp" 65536 evaluateTest +test="script-include" +startTest +"$RGBASM" -o "$otemp" "$test"/a.asm +"$RGBASM" -o "$gbtemp2" "$test"/b.asm +continueTest +rgblinkQuiet -o "$gbtemp" -l "$test"/script.link "$otemp" "$gbtemp2" 2>"$outtemp" +tryDiff "$test"/out.err "$outtemp" +tryCmpRom "$test"/ref.out.bin +evaluateTest + test="sdcc/good" startTest "$RGBASM" -o "$otemp" "$test"/a.asm