From 50ba5061c722b72c972bd9b03111a99e1b92bc18 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 15 Sep 2023 10:00:00 +0200 Subject: [PATCH] Fix #11438 MathLib error on user defined literals (#5448) Based on #4701, #5418 A helper function for the `isdigit()` test should be introduced on the simplecpp side. Co-authored-by: gerboengels --- lib/token.cpp | 11 +++++++---- test/testtoken.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 0ad96387d..f047cb456 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -143,10 +143,13 @@ 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]))) { + if (MathLib::isInt(mStr) || MathLib::isFloat(mStr)) + tokType(eNumber); + else + tokType(eName); // assume it is a user defined literal + } 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 60f03db1a..64097d096 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -637,8 +637,11 @@ private: givenACodeSampleToTokenize nonNumeric("abc", true); ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%")); - givenACodeSampleToTokenize binary("101010b", true); - ASSERT_EQUALS(true, Token::Match(binary.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 octal("0123", true); ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%")); @@ -652,9 +655,6 @@ private: givenACodeSampleToTokenize floatingPoint("0.0f", true); ASSERT_EQUALS(true, Token::Match(floatingPoint.tokens(), "%num%")); - givenACodeSampleToTokenize doublePrecision("0.0d", true); - ASSERT_EQUALS(true, Token::Match(doublePrecision.tokens(), "%num%")); - givenACodeSampleToTokenize signedLong("0L", true); ASSERT_EQUALS(true, Token::Match(signedLong.tokens(), "%num%"));