From 5055de0bb1cf3f9aac63a60217f2dbde5533c557 Mon Sep 17 00:00:00 2001 From: jarcherNV Date: Fri, 5 Sep 2025 16:03:32 -0700 Subject: Add warnings for overflows of integer types (#8281) The code int x4 = 0xFFFFFFFFFFFFFFFF previously did not produce a warning due to the value being too large for the type. This patch now checks for this and similar issues during parsing. --- source/slang/slang-parser.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'source') diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index e71b6162c..5b6f30477 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -7249,7 +7249,7 @@ static IntegerLiteralValue _fixIntegerLiteral( // If the masked value is 0 or equal to the mask, we 'assume' no information is // lost // This allows for example -1u, to give 0xffffffff - // It also means 0xfffffffffffffffffu will give 0xffffffff, without a warning. + // It also means 0xffffffffffffffffu will give 0xffffffff, without a warning. if ((!(maskedValue == 0 || maskedValue == mask)) && sink && token) { // Output a warning that number has been altered @@ -7306,20 +7306,19 @@ static BaseType _determineNonSuffixedIntegerLiteralType( { baseType = BaseType::UInt64; - if (isDecimalBase) - { - // There is an edge case here where 9223372036854775808 or INT64_MAX + 1 - // brings us here, but the complete literal is -9223372036854775808 or INT64_MIN and is - // valid. Unfortunately because the lexer handles the negative(-) part of the literal - // separately it is impossible to know whether the literal has a negative sign or not. - // We emit the warning and initially process it as a uint64 anyways, and the negative - // sign will be properly parsed and the value will still be properly stored as a - // negative INT64_MIN. + // Emit warning if the value is too large for signed 64-bit, regardless of base - // Decimal integer is too large to be represented as signed. - // Output warning that it is represented as unsigned instead. - sink->diagnose(*token, Diagnostics::integerLiteralTooLarge); - } + // There is an edge case here where 9223372036854775808 or INT64_MAX + 1 + // brings us here, but the complete literal is -9223372036854775808 or INT64_MIN and is + // valid. Unfortunately because the lexer handles the negative(-) part of the literal + // separately it is impossible to know whether the literal has a negative sign or not. + // We emit the warning and initially process it as a uint64 anyways, and the negative + // sign will be properly parsed and the value will still be properly stored as a + // negative INT64_MIN. + + // Decimal integer is too large to be represented as signed. + // Output warning that it is represented as unsigned instead. + sink->diagnose(*token, Diagnostics::integerLiteralTooLarge); } return baseType; -- cgit v1.2.3