Add -Wsingle-union warning for UNION without NEXTU (#2170)

This commit is contained in:
Rangi authored and GitHub committed 2026-09-27 20:02:42 -04:00
1 parent 62a9d8d518
commit ba4e58f4e0
11 files changed
+22 -3

No files matched your search

+1
View File
@@ -193,6 +193,7 @@ _rgbasm_completions() {
purge
shift
shift-amount
single-union
truncation
unmapped-char
unmatched-directive
+1
View File
@@ -25,6 +25,7 @@ _rgbasm_warnings() {
'purge:Warn when purging exported symbols or labels'
'shift:Warn when shifting negative values'
'shift-amount:Warn when a shift'\''s operand is negative or \> 32'
'single-union:Warn on UNION without NEXTU'
'truncation:Warn when implicit truncation loses bits'
'unmapped-char:Warn on unmapped character'
'unmatched-directive:Warn on unmatched directive pair'
+1
View File
@@ -30,6 +30,7 @@ enum WarningID {
WARNING_OBSOLETE, // Obsolete/deprecated things
WARNING_SHIFT, // Undefined `SHIFT` behavior
WARNING_SHIFT_AMOUNT, // Strange `SHIFT` amount
WARNING_SINGLE_UNION, // `UNION` without `NEXTU`
WARNING_UNMATCHED_DIRECTIVE, // `PUSH[C|O|S]` without `POP[C|O|S]`
WARNING_UNTERMINATED_LOAD, // `LOAD` without `ENDL`
WARNING_USER, // User-defined `WARN`ings
+6
View File
@@ -517,6 +517,12 @@ Warn when shifting right a negative value.
Use a division by 2**N instead.
.It Fl Wshift-amount
Warn when a shift's operand is negative or greater than 32.
.It Fl Wsingle-union
Warn when a
.Ic UNION
construct has no
.Ic NEXTU
alternatives.
.It Fl Wtruncation=
Warn when an implicit truncation (for example,
.Ic db
+6 -1
View File
@@ -38,6 +38,7 @@ using namespace std::literals;
struct UnionStackEntry {
uint32_t start;
uint32_t size;
bool hasNextu;
};
struct SectionStackEntry {
@@ -782,7 +783,7 @@ void sect_StartUnion() {
return;
}
currentUnionStack.push({.start = curOffset, .size = 0});
currentUnionStack.push({.start = curOffset, .size = 0, .hasNextu = false});
}
static void endUnionMember() {
@@ -801,6 +802,7 @@ void sect_NextUnionMember() {
return;
}
endUnionMember();
currentUnionStack.top().hasNextu = true;
}
void sect_EndUnion() {
@@ -808,6 +810,9 @@ void sect_EndUnion() {
error("Found `ENDU` outside of a `UNION` construct");
return;
}
if (!currentUnionStack.top().hasNextu) {
warning(WARNING_SINGLE_UNION, "`UNION` construct has no `NEXTU` alternatives");
}
endUnionMember();
curOffset += currentUnionStack.top().size;
currentUnionStack.pop();
+1
View File
@@ -37,6 +37,7 @@ Diagnostics<WarningLevel, WarningID> warnings = {
{"obsolete", LEVEL_DEFAULT },
{"shift", LEVEL_EVERYTHING},
{"shift-amount", LEVEL_EVERYTHING},
{"single-union", LEVEL_EVERYTHING},
{"unmatched-directive", LEVEL_EXTRA },
{"unterminated-load", LEVEL_EXTRA },
{"user", LEVEL_DEFAULT },
@@ -5,6 +5,7 @@ SECTION "test", WRAM0
UNION
INCLUDE "nonexistent1.inc"
WARN "still going!"
NEXTU
INCLUDE "nonexistent2.inc"
WARN "and going!"
ENDU
@@ -1,4 +1,4 @@
warning: still going! [-Wuser]
at continues-after-missing-include/a.asm(7)
warning: and going! [-Wuser]
at continues-after-missing-include/a.asm(9)
at continues-after-missing-include/a.asm(10)
@@ -5,6 +5,7 @@ SECTION "test", WRAM0
UNION
INCLUDE "nonexistent1.inc"
WARN "still going!"
NEXTU
INCLUDE "nonexistent2.inc"
WARN "and going!"
ENDU
@@ -1,4 +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)
at continues-after-missing-preinclude/a.asm(10)
+2
View File
@@ -0,0 +1,2 @@
warning: `UNION` construct has no `NEXTU` alternatives [-Wsingle-union]
at single-union.asm(4)