From e41ce496989ed8a2c3b8f158b27411b1ac5d2e76 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:58:38 -0400 Subject: [PATCH] Only print one warning for too-large integer constants, not one per digit (#1781) This also makes all too-large integer constants evaluate to 0. --- src/asm/lexer.cpp | 21 ++++++++++- test/asm/fixed-point-magnitude.out | 58 +++++++++++++++--------------- test/asm/invalid-numbers.asm | 35 +++++++++++------- test/asm/invalid-numbers.err | 26 +++++++------- test/asm/overflow.err | 2 +- test/fetch-test-deps.sh | 2 +- test/run-tests.sh | 2 +- 7 files changed, 88 insertions(+), 58 deletions(-) diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 65e099f7..15223a51 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -978,6 +978,7 @@ static uint32_t readFractionalPart(uint32_t integer) { if (integer >= (1ULL << (32 - precision))) { warning(WARNING_LARGE_CONSTANT, "Magnitude of fixed-point constant is too large"); + return 0; } // Cast to unsigned avoids undefined overflow behavior @@ -1047,6 +1048,12 @@ static uint32_t readBinaryNumber() { } if (value > (UINT32_MAX - bit) / 2) { warning(WARNING_LARGE_CONSTANT, "Integer constant is too large"); + // Discard any additional digits + skipChars([](int d) { + return d == '0' || d == '1' || d == options.binDigits[0] + || d == options.binDigits[1] || d == '_'; + }); + return 0; } value = value * 2 + bit; @@ -1075,6 +1082,9 @@ static uint32_t readOctalNumber() { if (value > (UINT32_MAX - c) / 8) { warning(WARNING_LARGE_CONSTANT, "Integer constant is too large"); + // Discard any additional digits + skipChars([](int d) { return (d >= '0' && d <= '7') || d == '_'; }); + return 0; } value = value * 8 + c; @@ -1103,6 +1113,9 @@ static uint32_t readDecimalNumber(int initial) { if (value > (UINT32_MAX - c) / 10) { warning(WARNING_LARGE_CONSTANT, "Integer constant is too large"); + // Discard any additional digits + skipChars([](int d) { return (d >= '0' && d <= '9') || d == '_'; }); + return 0; } value = value * 10 + c; } @@ -1129,6 +1142,12 @@ static uint32_t readHexNumber() { if (value > (UINT32_MAX - c) / 16) { warning(WARNING_LARGE_CONSTANT, "Integer constant is too large"); + // Discard any additional digits + skipChars([](int d) { + return (d >= '0' && d <= '9') || (d >= 'a' && d <= 'f') || (d >= 'A' && d <= 'f') + || d == '_'; + }); + return 0; } value = value * 16 + c; @@ -1176,7 +1195,7 @@ static uint32_t readGfxConstant() { error("Invalid graphics constant, no digits after '`'"); } else if (width == 9) { warning( - WARNING_LARGE_CONSTANT, "Graphics constant is too long, only first 8 pixels considered" + WARNING_LARGE_CONSTANT, "Graphics constant is too large; only first 8 pixels considered" ); } diff --git a/test/asm/fixed-point-magnitude.out b/test/asm/fixed-point-magnitude.out index a1afa8c8..951c5d2b 100644 --- a/test/asm/fixed-point-magnitude.out +++ b/test/asm/fixed-point-magnitude.out @@ -3,91 +3,91 @@ Q. 1: min bad = 1 << 31 = -2147483648.0 = $00000000 Q. 1: worse = 1 << 31 + 42 = -2147483606.0 = $00000054 Q. 2: max ok = 1 << 30 - 1 = 1073741823.0 = $fffffffc Q. 2: min bad = 1 << 30 = 1073741824.0 = $00000000 -Q. 2: worse = 1 << 30 + 42 = 1073741866.0 = $000000a8 +Q. 2: worse = 1 << 30 + 42 = 1073741866.0 = $00000000 Q. 3: max ok = 1 << 29 - 1 = 536870911.0 = $fffffff8 Q. 3: min bad = 1 << 29 = 536870912.0 = $00000000 -Q. 3: worse = 1 << 29 + 42 = 536870954.0 = $00000150 +Q. 3: worse = 1 << 29 + 42 = 536870954.0 = $00000000 Q. 4: max ok = 1 << 28 - 1 = 268435455.0 = $fffffff0 Q. 4: min bad = 1 << 28 = 268435456.0 = $00000000 -Q. 4: worse = 1 << 28 + 42 = 268435498.0 = $000002a0 +Q. 4: worse = 1 << 28 + 42 = 268435498.0 = $00000000 Q. 5: max ok = 1 << 27 - 1 = 134217727.0 = $ffffffe0 Q. 5: min bad = 1 << 27 = 134217728.0 = $00000000 -Q. 5: worse = 1 << 27 + 42 = 134217770.0 = $00000540 +Q. 5: worse = 1 << 27 + 42 = 134217770.0 = $00000000 Q. 6: max ok = 1 << 26 - 1 = 67108863.0 = $ffffffc0 Q. 6: min bad = 1 << 26 = 67108864.0 = $00000000 -Q. 6: worse = 1 << 26 + 42 = 67108906.0 = $00000a80 +Q. 6: worse = 1 << 26 + 42 = 67108906.0 = $00000000 Q. 7: max ok = 1 << 25 - 1 = 33554431.0 = $ffffff80 Q. 7: min bad = 1 << 25 = 33554432.0 = $00000000 -Q. 7: worse = 1 << 25 + 42 = 33554474.0 = $00001500 +Q. 7: worse = 1 << 25 + 42 = 33554474.0 = $00000000 Q. 8: max ok = 1 << 24 - 1 = 16777215.0 = $ffffff00 Q. 8: min bad = 1 << 24 = 16777216.0 = $00000000 -Q. 8: worse = 1 << 24 + 42 = 16777258.0 = $00002a00 +Q. 8: worse = 1 << 24 + 42 = 16777258.0 = $00000000 Q. 9: max ok = 1 << 23 - 1 = 8388607.0 = $fffffe00 Q. 9: min bad = 1 << 23 = 8388608.0 = $00000000 -Q. 9: worse = 1 << 23 + 42 = 8388650.0 = $00005400 +Q. 9: worse = 1 << 23 + 42 = 8388650.0 = $00000000 Q.10: max ok = 1 << 22 - 1 = 4194303.0 = $fffffc00 Q.10: min bad = 1 << 22 = 4194304.0 = $00000000 -Q.10: worse = 1 << 22 + 42 = 4194346.0 = $0000a800 +Q.10: worse = 1 << 22 + 42 = 4194346.0 = $00000000 Q.11: max ok = 1 << 21 - 1 = 2097151.0 = $fffff800 Q.11: min bad = 1 << 21 = 2097152.0 = $00000000 -Q.11: worse = 1 << 21 + 42 = 2097194.0 = $00015000 +Q.11: worse = 1 << 21 + 42 = 2097194.0 = $00000000 Q.12: max ok = 1 << 20 - 1 = 1048575.0 = $fffff000 Q.12: min bad = 1 << 20 = 1048576.0 = $00000000 -Q.12: worse = 1 << 20 + 42 = 1048618.0 = $0002a000 +Q.12: worse = 1 << 20 + 42 = 1048618.0 = $00000000 Q.13: max ok = 1 << 19 - 1 = 524287.0 = $ffffe000 Q.13: min bad = 1 << 19 = 524288.0 = $00000000 -Q.13: worse = 1 << 19 + 42 = 524330.0 = $00054000 +Q.13: worse = 1 << 19 + 42 = 524330.0 = $00000000 Q.14: max ok = 1 << 18 - 1 = 262143.0 = $ffffc000 Q.14: min bad = 1 << 18 = 262144.0 = $00000000 -Q.14: worse = 1 << 18 + 42 = 262186.0 = $000a8000 +Q.14: worse = 1 << 18 + 42 = 262186.0 = $00000000 Q.15: max ok = 1 << 17 - 1 = 131071.0 = $ffff8000 Q.15: min bad = 1 << 17 = 131072.0 = $00000000 -Q.15: worse = 1 << 17 + 42 = 131114.0 = $00150000 +Q.15: worse = 1 << 17 + 42 = 131114.0 = $00000000 Q.16: max ok = 1 << 16 - 1 = 65535.0 = $ffff0000 Q.16: min bad = 1 << 16 = 65536.0 = $00000000 -Q.16: worse = 1 << 16 + 42 = 65578.0 = $002a0000 +Q.16: worse = 1 << 16 + 42 = 65578.0 = $00000000 Q.17: max ok = 1 << 15 - 1 = 32767.0 = $fffe0000 Q.17: min bad = 1 << 15 = 32768.0 = $00000000 -Q.17: worse = 1 << 15 + 42 = 32810.0 = $00540000 +Q.17: worse = 1 << 15 + 42 = 32810.0 = $00000000 Q.18: max ok = 1 << 14 - 1 = 16383.0 = $fffc0000 Q.18: min bad = 1 << 14 = 16384.0 = $00000000 -Q.18: worse = 1 << 14 + 42 = 16426.0 = $00a80000 +Q.18: worse = 1 << 14 + 42 = 16426.0 = $00000000 Q.19: max ok = 1 << 13 - 1 = 8191.0 = $fff80000 Q.19: min bad = 1 << 13 = 8192.0 = $00000000 -Q.19: worse = 1 << 13 + 42 = 8234.0 = $01500000 +Q.19: worse = 1 << 13 + 42 = 8234.0 = $00000000 Q.20: max ok = 1 << 12 - 1 = 4095.0 = $fff00000 Q.20: min bad = 1 << 12 = 4096.0 = $00000000 -Q.20: worse = 1 << 12 + 42 = 4138.0 = $02a00000 +Q.20: worse = 1 << 12 + 42 = 4138.0 = $00000000 Q.21: max ok = 1 << 11 - 1 = 2047.0 = $ffe00000 Q.21: min bad = 1 << 11 = 2048.0 = $00000000 -Q.21: worse = 1 << 11 + 42 = 2090.0 = $05400000 +Q.21: worse = 1 << 11 + 42 = 2090.0 = $00000000 Q.22: max ok = 1 << 10 - 1 = 1023.0 = $ffc00000 Q.22: min bad = 1 << 10 = 1024.0 = $00000000 -Q.22: worse = 1 << 10 + 42 = 1066.0 = $0a800000 +Q.22: worse = 1 << 10 + 42 = 1066.0 = $00000000 Q.23: max ok = 1 << 9 - 1 = 511.0 = $ff800000 Q.23: min bad = 1 << 9 = 512.0 = $00000000 -Q.23: worse = 1 << 9 + 42 = 554.0 = $15000000 +Q.23: worse = 1 << 9 + 42 = 554.0 = $00000000 Q.24: max ok = 1 << 8 - 1 = 255.0 = $ff000000 Q.24: min bad = 1 << 8 = 256.0 = $00000000 -Q.24: worse = 1 << 8 + 42 = 298.0 = $2a000000 +Q.24: worse = 1 << 8 + 42 = 298.0 = $00000000 Q.25: max ok = 1 << 7 - 1 = 127.0 = $fe000000 Q.25: min bad = 1 << 7 = 128.0 = $00000000 -Q.25: worse = 1 << 7 + 42 = 170.0 = $54000000 +Q.25: worse = 1 << 7 + 42 = 170.0 = $00000000 Q.26: max ok = 1 << 6 - 1 = 63.0 = $fc000000 Q.26: min bad = 1 << 6 = 64.0 = $00000000 -Q.26: worse = 1 << 6 + 42 = 106.0 = $a8000000 +Q.26: worse = 1 << 6 + 42 = 106.0 = $00000000 Q.27: max ok = 1 << 5 - 1 = 31.0 = $f8000000 Q.27: min bad = 1 << 5 = 32.0 = $00000000 -Q.27: worse = 1 << 5 + 42 = 74.0 = $50000000 +Q.27: worse = 1 << 5 + 42 = 74.0 = $00000000 Q.28: max ok = 1 << 4 - 1 = 15.0 = $f0000000 Q.28: min bad = 1 << 4 = 16.0 = $00000000 -Q.28: worse = 1 << 4 + 42 = 58.0 = $a0000000 +Q.28: worse = 1 << 4 + 42 = 58.0 = $00000000 Q.29: max ok = 1 << 3 - 1 = 7.0 = $e0000000 Q.29: min bad = 1 << 3 = 8.0 = $00000000 -Q.29: worse = 1 << 3 + 42 = 50.0 = $40000000 +Q.29: worse = 1 << 3 + 42 = 50.0 = $00000000 Q.30: max ok = 1 << 2 - 1 = 3.0 = $c0000000 Q.30: min bad = 1 << 2 = 4.0 = $00000000 -Q.30: worse = 1 << 2 + 42 = 46.0 = $80000000 +Q.30: worse = 1 << 2 + 42 = 46.0 = $00000000 Q.31: max ok = 1 << 1 - 1 = 1.0 = $80000000 Q.31: min bad = 1 << 1 = 2.0 = $00000000 Q.31: worse = 1 << 1 + 42 = 44.0 = $00000000 diff --git a/test/asm/invalid-numbers.asm b/test/asm/invalid-numbers.asm index 382d792f..cda1091d 100644 --- a/test/asm/invalid-numbers.asm +++ b/test/asm/invalid-numbers.asm @@ -1,20 +1,29 @@ +macro try + def x = \1 + if _NARG > 1 + assert x == \2 + else + assert x == 0 + endc +endm + ; no digits -def x = $ -def x = ` -def x = 0b -def x = 0o -def x = 0x +try $ +try ` +try 0b +try 0o +try 0x ; too large -def x = 9_876_543_210 -def x = $f_0000_0000 -def x = &400_0000_0000 -def x = %1_00000000_00000000_00000000_00000000 -def x = 65537.0q16 +try 999_876_543_210 +try $ffff_0000_0000 +try &7777_0000_0000_0000 +try %1111_00000000_00000000_00000000_00000000 +try `0123_3210_0123, `0123_3210 +try 99999.0q16 ; no precision suffix -def x = 3.14q +try 3.14q, 3.14 ; invalid precision suffix -def x = 3.14q40 - +try 3.14q40, 3.14 diff --git a/test/asm/invalid-numbers.err b/test/asm/invalid-numbers.err index d455e30b..8e05eb06 100644 --- a/test/asm/invalid-numbers.err +++ b/test/asm/invalid-numbers.err @@ -1,25 +1,27 @@ -error: invalid-numbers.asm(2): +error: invalid-numbers.asm(11) -> invalid-numbers.asm::try(2): Invalid integer constant, no digits after '$' -error: invalid-numbers.asm(3): +error: invalid-numbers.asm(12) -> invalid-numbers.asm::try(2): Invalid graphics constant, no digits after '`' -error: invalid-numbers.asm(4): +error: invalid-numbers.asm(13) -> invalid-numbers.asm::try(2): Invalid integer constant, no digits after '%' -error: invalid-numbers.asm(5): +error: invalid-numbers.asm(14) -> invalid-numbers.asm::try(2): Invalid integer constant, no digits after '&' -error: invalid-numbers.asm(6): +error: invalid-numbers.asm(15) -> invalid-numbers.asm::try(2): Invalid integer constant, no digits after '$' -warning: invalid-numbers.asm(9): [-Wlarge-constant] +warning: invalid-numbers.asm(18) -> invalid-numbers.asm::try(2): [-Wlarge-constant] Integer constant is too large -warning: invalid-numbers.asm(10): [-Wlarge-constant] +warning: invalid-numbers.asm(19) -> invalid-numbers.asm::try(2): [-Wlarge-constant] Integer constant is too large -warning: invalid-numbers.asm(11): [-Wlarge-constant] +warning: invalid-numbers.asm(20) -> invalid-numbers.asm::try(2): [-Wlarge-constant] Integer constant is too large -warning: invalid-numbers.asm(12): [-Wlarge-constant] +warning: invalid-numbers.asm(21) -> invalid-numbers.asm::try(2): [-Wlarge-constant] Integer constant is too large -warning: invalid-numbers.asm(13): [-Wlarge-constant] +warning: invalid-numbers.asm(22) -> invalid-numbers.asm::try(2): [-Wlarge-constant] + Graphics constant is too large; only first 8 pixels considered +warning: invalid-numbers.asm(23) -> invalid-numbers.asm::try(2): [-Wlarge-constant] Magnitude of fixed-point constant is too large -error: invalid-numbers.asm(16): +error: invalid-numbers.asm(26) -> invalid-numbers.asm::try(2): Invalid fixed-point constant, no significant digits after 'q' -error: invalid-numbers.asm(19): +error: invalid-numbers.asm(29) -> invalid-numbers.asm::try(2): Fixed-point constant precision must be between 1 and 31 Assembly aborted with 7 errors! diff --git a/test/asm/overflow.err b/test/asm/overflow.err index ca0c0501..569ea2b8 100644 --- a/test/asm/overflow.err +++ b/test/asm/overflow.err @@ -5,4 +5,4 @@ warning: overflow.asm(24): [-Wdiv] warning: overflow.asm(44): [-Wlarge-constant] Integer constant is too large warning: overflow.asm(47): [-Wlarge-constant] - Graphics constant is too long, only first 8 pixels considered + Graphics constant is too large; only first 8 pixels considered diff --git a/test/fetch-test-deps.sh b/test/fetch-test-deps.sh index cd8bc8b2..ce25ae26 100755 --- a/test/fetch-test-deps.sh +++ b/test/fetch-test-deps.sh @@ -111,7 +111,7 @@ if "$nonfree"; then action pret pokered 2025-07-21 fd86bcaa06fd81911e88eb6d0253715ad79866a8 action zladx LADX-Disassembly 2025-07-26 3ab7b582f67b4302a4f6371f9309f0de167e78ee fi -action AntonioND ucity 2025-05-30 83e5c697cbd9e10a0bc72b02bcb6146c35e2c328 +action AntonioND ucity 2025-08-07 d1880a2a112d7c26f16c0fc06a15b6c32fdc9137 action pinobatch libbet 2025-05-20 bb6cfc026644aa1034eee6d9c49bb4705601c9f6 action LIJI32 SameBoy 2025-07-26 410719854885b60df8566a444c81bc767965f477 action ISSOtm gb-starter-kit 2025-06-13 3cce3215b0bcb559a5cf4ca5916e2d817d53ff29 diff --git a/test/run-tests.sh b/test/run-tests.sh index 5f119023..2323b795 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -129,7 +129,7 @@ if "$nonfree"; then test_downstream pret pokered compare pokered.gbc ea9bcae617fdf159b045185467ae58b2e4a48b9a test_downstream zladx LADX-Disassembly default azle.gbc d90ac17e9bf17b6c61624ad9f05447bdb5efc01a fi -test_downstream AntonioND ucity all ucity.gbc d2f4a7db48ee208b1bd69a78bd492a1c9ac4a030 +test_downstream AntonioND ucity all ucity.gbc 5f026649611c9606ce0bf70dc1552e054e7df5bc test_downstream pinobatch libbet all libbet.gb f117089aa056600e2d404bbcbac96b016fc64611 test_downstream LIJI32 SameBoy bootroms build/bin/BootROMs/cgb_boot.bin 113903775a9d34b798c2f8076672da6626815a91 # gb-starter kit make fails on Windows: https://github.com/ISSOtm/gb-starter-kit/issues/1