mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
-Wmacro-shift warns about shifting macro arguments too far (#741)
Fixes #735
This commit is contained in:
@@ -23,6 +23,7 @@ enum WarningID {
|
||||
WARNING_EMPTY_STRRPL, /* Empty second argument in `STRRPL` */
|
||||
WARNING_LARGE_CONSTANT, /* Constants too large */
|
||||
WARNING_LONG_STR, /* String too long for internal buffers */
|
||||
WARNING_MACRO_SHIFT, /* Shift past available arguments in macro */
|
||||
WARNING_NESTED_COMMENT, /* Comment-start delimiter in a block comment */
|
||||
WARNING_OBSOLETE, /* Obsolete things */
|
||||
WARNING_SHIFT, /* Shifting undefined behavior */
|
||||
|
||||
@@ -168,12 +168,17 @@ void macro_ShiftCurrentArgs(int32_t count)
|
||||
{
|
||||
if (!macroArgs) {
|
||||
error("Cannot shift macro arguments outside of a macro\n");
|
||||
} else if (macroArgs->shift < macroArgs->nbArgs) {
|
||||
macroArgs->shift += count;
|
||||
if (macroArgs->shift > macroArgs->nbArgs)
|
||||
} else if (count > 0 && (count > macroArgs->nbArgs
|
||||
|| macroArgs->shift > macroArgs->nbArgs - count)) {
|
||||
warning(WARNING_MACRO_SHIFT,
|
||||
"Cannot shift macro arguments past their end\n");
|
||||
macroArgs->shift = macroArgs->nbArgs;
|
||||
else if (macroArgs->shift < 0)
|
||||
} else if (count < 0 && macroArgs->shift < -count) {
|
||||
warning(WARNING_MACRO_SHIFT,
|
||||
"Cannot shift macro arguments past their beginning\n");
|
||||
macroArgs->shift = 0;
|
||||
} else {
|
||||
macroArgs->shift += count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -229,6 +229,10 @@ This warning is enabled by
|
||||
Warn when a string too long to fit in internal buffers is encountered.
|
||||
This warning is enabled by
|
||||
.Fl Wall .
|
||||
.It Fl Wmacro-shift
|
||||
Warn when shifting macro arguments past their limits.
|
||||
This warning is enabled by
|
||||
.Fl Wextra .
|
||||
.It Fl Wno-obsolete
|
||||
Warn when obsolete constructs such as the
|
||||
.Ic _PI
|
||||
|
||||
@@ -38,6 +38,7 @@ static enum WarningState const defaultWarnings[NB_WARNINGS] = {
|
||||
[WARNING_EMPTY_STRRPL] = WARNING_DISABLED,
|
||||
[WARNING_LARGE_CONSTANT] = WARNING_DISABLED,
|
||||
[WARNING_LONG_STR] = WARNING_DISABLED,
|
||||
[WARNING_MACRO_SHIFT] = WARNING_DISABLED,
|
||||
[WARNING_NESTED_COMMENT] = WARNING_ENABLED,
|
||||
[WARNING_OBSOLETE] = WARNING_ENABLED,
|
||||
[WARNING_SHIFT] = WARNING_DISABLED,
|
||||
@@ -79,6 +80,7 @@ static char const *warningFlags[NB_WARNINGS_ALL] = {
|
||||
"empty-strrpl",
|
||||
"large-constant",
|
||||
"long-string",
|
||||
"macro-shift",
|
||||
"nested-comment",
|
||||
"obsolete",
|
||||
"shift",
|
||||
@@ -110,6 +112,7 @@ static uint8_t const _wallCommands[] = {
|
||||
/* Warnings that are less likely to indicate an error */
|
||||
static uint8_t const _wextraCommands[] = {
|
||||
WARNING_EMPTY_ENTRY,
|
||||
WARNING_MACRO_SHIFT,
|
||||
WARNING_NESTED_COMMENT,
|
||||
META_WARNING_DONE
|
||||
};
|
||||
@@ -123,6 +126,7 @@ static uint8_t const _weverythingCommands[] = {
|
||||
WARNING_EMPTY_STRRPL,
|
||||
WARNING_LARGE_CONSTANT,
|
||||
WARNING_LONG_STR,
|
||||
WARNING_MACRO_SHIFT,
|
||||
WARNING_NESTED_COMMENT,
|
||||
WARNING_OBSOLETE,
|
||||
WARNING_SHIFT,
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
warning: narg-decreases-after-shift.asm(14) -> narg-decreases-after-shift.asm::testing(9): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their end
|
||||
|
||||
@@ -8,9 +8,7 @@ m: macro
|
||||
; Shifting a little more to check that over-shifting doesn't crash
|
||||
SHIFT
|
||||
SHIFT
|
||||
REPT 256
|
||||
SHIFT
|
||||
ENDR
|
||||
SHIFT 256
|
||||
PRINTLN "\1"
|
||||
endm
|
||||
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
FATAL: rept-shift.asm(17) -> rept-shift.asm::m(14):
|
||||
warning: rept-shift.asm(15) -> rept-shift.asm::m(10): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their end
|
||||
warning: rept-shift.asm(15) -> rept-shift.asm::m(11): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their end
|
||||
FATAL: rept-shift.asm(15) -> rept-shift.asm::m(12):
|
||||
Macro argument '\1' not defined
|
||||
|
||||
@@ -12,5 +12,9 @@ ENDM
|
||||
m: MACRO
|
||||
shift 2
|
||||
shift -3
|
||||
shift 1
|
||||
shift 1
|
||||
shift -1
|
||||
shift -1
|
||||
ENDM
|
||||
m
|
||||
m one
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
warning: shift-negative.asm(20) -> shift-negative.asm::m(13): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their end
|
||||
warning: shift-negative.asm(20) -> shift-negative.asm::m(14): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their beginning
|
||||
warning: shift-negative.asm(20) -> shift-negative.asm::m(16): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their end
|
||||
warning: shift-negative.asm(20) -> shift-negative.asm::m(18): [-Wmacro-shift]
|
||||
Cannot shift macro arguments past their beginning
|
||||
|
||||
Reference in New Issue
Block a user