From b2fd2d2f4d4dd115b1696934f99e911d4ff65213 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 8 Sep 2023 23:09:40 +0200 Subject: [PATCH 1/2] Don't handle non-standard literals --- lib/token.cpp | 14 ++++++++++---- test/testtoken.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 0ad96387d61..50d8b9f8189 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -143,10 +143,16 @@ void Token::update_property_info() tokType(eKeyword); else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword) tokType(eName); - } else if (std::isdigit((unsigned char)mStr[0]) || (mStr.length() > 1 && mStr[0] == '-' && std::isdigit((unsigned char)mStr[1]))) - tokType(eNumber); - else if (mStr == "=" || mStr == "<<=" || mStr == ">>=" || - (mStr.size() == 2U && mStr[1] == '=' && std::strchr("+-*/%&^|", mStr[0]))) + } else if (std::isdigit((unsigned char)mStr[0]) || (mStr.length() > 1 && mStr[0] == '-' && std::isdigit((unsigned char)mStr[1]))) { + try { + if (!MathLib::isFloat(mStr)) + (void)MathLib::toLongNumber(mStr); + tokType(eNumber); + } catch (const InternalError& /*e*/) { + tokType(eName); + } + } else if (mStr == "=" || mStr == "<<=" || mStr == ">>=" || + (mStr.size() == 2U && mStr[1] == '=' && std::strchr("+-*/%&^|", mStr[0]))) tokType(eAssignmentOp); else if (mStr.size() == 1 && mStr.find_first_of(",[]()?:") != std::string::npos) tokType(eExtendedOp); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 60f03db1a57..890ef17b2ad 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -637,6 +637,12 @@ class TestToken : public TestFixture { givenACodeSampleToTokenize nonNumeric("abc", true); ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%")); + givenACodeSampleToTokenize msLiteral("5ms", true); // #11438 + ASSERT_EQUALS(false, Token::Match(msLiteral.tokens(), "%num%")); + + givenACodeSampleToTokenize sLiteral("3s", true); + ASSERT_EQUALS(false, Token::Match(sLiteral.tokens(), "%num%")); + givenACodeSampleToTokenize binary("101010b", true); ASSERT_EQUALS(true, Token::Match(binary.tokens(), "%num%")); From e1c2745088f939ee92b01f040fbb7920494f2e45 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 8 Sep 2023 23:10:03 +0200 Subject: [PATCH 2/2] Adjust tests --- test/testtoken.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 890ef17b2ad..25cd5d64a88 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -644,7 +644,7 @@ class TestToken : public TestFixture { ASSERT_EQUALS(false, Token::Match(sLiteral.tokens(), "%num%")); givenACodeSampleToTokenize binary("101010b", true); - ASSERT_EQUALS(true, Token::Match(binary.tokens(), "%num%")); + ASSERT_EQUALS(false, Token::Match(binary.tokens(), "%num%")); givenACodeSampleToTokenize octal("0123", true); ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%")); @@ -659,7 +659,7 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, Token::Match(floatingPoint.tokens(), "%num%")); givenACodeSampleToTokenize doublePrecision("0.0d", true); - ASSERT_EQUALS(true, Token::Match(doublePrecision.tokens(), "%num%")); + ASSERT_EQUALS(false, Token::Match(doublePrecision.tokens(), "%num%")); givenACodeSampleToTokenize signedLong("0L", true); ASSERT_EQUALS(true, Token::Match(signedLong.tokens(), "%num%"));