From 8564df51e5c13b3cc1ecad0c2e0d6c0a633d46f1 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sun, 24 Aug 2025 16:52:20 -0400 Subject: [PATCH] `-Wexport-undefined` warning for exporting undefined symbols --- contrib/bash_compl/_rgbasm.bash | 1 + contrib/zsh_compl/_rgbasm | 1 + include/asm/warning.hpp | 1 + man/rgbasm.1 | 4 ++++ src/asm/symbol.cpp | 4 +++- src/asm/warning.cpp | 1 + test/asm/export.asm | 3 +++ test/asm/export.err | 9 +++++++++ 8 files changed, 23 insertions(+), 1 deletion(-) diff --git a/contrib/bash_compl/_rgbasm.bash b/contrib/bash_compl/_rgbasm.bash index 7ee09752..5377ef00 100755 --- a/contrib/bash_compl/_rgbasm.bash +++ b/contrib/bash_compl/_rgbasm.bash @@ -184,6 +184,7 @@ _rgbasm_completions() { empty-data-directive empty-macro-arg empty-strrpl + export-undefined large-constant macro-shift nested-comment diff --git a/contrib/zsh_compl/_rgbasm b/contrib/zsh_compl/_rgbasm index 8347401c..d65bc88d 100644 --- a/contrib/zsh_compl/_rgbasm +++ b/contrib/zsh_compl/_rgbasm @@ -16,6 +16,7 @@ _rgbasm_warnings() { 'empty-data-directive:Warn on arg-less d[bwl] in ROM' 'empty-macro-arg:Warn on empty macro arg' 'empty-strrpl:Warn on calling STRRPL with empty pattern' + 'export-undefined:Warn on EXPORT of an undefined symbol' 'large-constant:Warn on constants too large for a signed 32-bit int' 'macro-shift:Warn when shifting macro args part their limits' 'nested-comment:Warn on "/*" inside block comments' diff --git a/include/asm/warning.hpp b/include/asm/warning.hpp index 56e2cfff..f2f37c73 100644 --- a/include/asm/warning.hpp +++ b/include/asm/warning.hpp @@ -23,6 +23,7 @@ enum WarningID { WARNING_EMPTY_DATA_DIRECTIVE, // `db`, `dw` or `dl` directive without data in ROM WARNING_EMPTY_MACRO_ARG, // Empty macro argument WARNING_EMPTY_STRRPL, // Empty second argument in `STRRPL` + WARNING_EXPORT_UNDEFINED, // `EXPORT` of an undefined symbol WARNING_LARGE_CONSTANT, // Constants too large WARNING_MACRO_SHIFT, // `SHIFT` past available arguments in macro WARNING_NESTED_COMMENT, // Comment-start delimiter in a block comment diff --git a/man/rgbasm.1 b/man/rgbasm.1 index ea8acafe..85977395 100644 --- a/man/rgbasm.1 +++ b/man/rgbasm.1 @@ -394,6 +394,10 @@ Warn when is called with an empty string as its second argument (the substring to replace). This warning is enabled by .Fl Wall . +.It Fl Wexport-undefined +Warn when exporting an undefined symbol. +This warning is enabled by +.Fl Wall . .It Fl Wno-large-constant Warn when a constant too large to fit in a signed 32-bit integer is encountered. .It Fl Wmacro-shift diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index 1211f09c..f95af7ca 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -611,7 +611,7 @@ void sym_Export(std::string const &symName) { if (symName.starts_with('!')) { // LCOV_EXCL_START // The parser does not accept anonymous labels for an `EXPORT` directive - error("Anonymous labels cannot be exported"); + error("Cannot export anonymous label"); return; // LCOV_EXCL_STOP } @@ -620,8 +620,10 @@ void sym_Export(std::string const &symName) { // If the symbol doesn't exist, create a ref that can be purged if (!sym) { + warning(WARNING_EXPORT_UNDEFINED, "Exporting an undefined symbol `%s`", symName.c_str()); sym = sym_Ref(symName); } + sym->isExported = true; } diff --git a/src/asm/warning.cpp b/src/asm/warning.cpp index ae523bc6..a3772004 100644 --- a/src/asm/warning.cpp +++ b/src/asm/warning.cpp @@ -30,6 +30,7 @@ Diagnostics warnings = { {"empty-data-directive", LEVEL_ALL }, {"empty-macro-arg", LEVEL_EXTRA }, {"empty-strrpl", LEVEL_ALL }, + {"export-undefined", LEVEL_ALL }, {"large-constant", LEVEL_DEFAULT }, {"macro-shift", LEVEL_EXTRA }, {"nested-comment", LEVEL_DEFAULT }, diff --git a/test/asm/export.asm b/test/asm/export.asm index ce66d3b4..7d9dd54d 100644 --- a/test/asm/export.asm +++ b/test/asm/export.asm @@ -16,3 +16,6 @@ EXPORT REDEF variable = 1234 DEF equs_sym EQUS "hello" EXPORT equs_sym ; exports undefined symbol `hello` due to EQUS expansion EXPORT DEF string EQUS "goodbye" ; invalid syntax + +PURGE equ_sym +EXPORT equ_sym diff --git a/test/asm/export.err b/test/asm/export.err index 516e8e07..7f1b85ff 100644 --- a/test/asm/export.err +++ b/test/asm/export.err @@ -1,3 +1,12 @@ +warning: Exporting an undefined symbol `undefined` [-Wexport-undefined] + at export.asm(1) +warning: Exporting an undefined symbol `hello` [-Wexport-undefined] + at export.asm(17) + while expanding symbol `equs_sym` error: syntax error, unexpected EQUS at export.asm(18) +warning: Purging an exported symbol `equ_sym` [-Wpurge] + at export.asm(20) +warning: Exporting an undefined symbol `equ_sym` [-Wexport-undefined] + at export.asm(21) Assembly aborted with 1 error!