From 27257a49fa40923066e500aac90ee9206e51ed10 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:55:54 +0200 Subject: [PATCH 1/5] Update token.cpp --- lib/token.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 1bb62dd8c5d..7b5e2464103 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -114,8 +114,15 @@ void Token::update_property_info() isStandardType(false); if (!mStr.empty()) { - if (mStr == "true" || mStr == "false") - tokType(eBoolean); + if (mStr == "true" || mStr == "false") { + if (mImpl->mVarId) { + if (mIsCpp) + throw InternalError(this, "Internal error. VarId set for bool literal."); + tokType(eVariable); + } + else + tokType(eBoolean); + } else if (isStringLiteral(mStr)) { tokType(eString); isLong(isPrefixStringCharLiteral(mStr, '"', "L")); From 42cf9bb850f6340fd04018f729970d0d8c780eb4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:56:33 +0200 Subject: [PATCH 2/5] Update testvarid.cpp --- test/testvarid.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 8c83853b994..a12ae8fdfcc 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -108,6 +108,7 @@ class TestVarID : public TestFixture { TEST_CASE(varid_cpp_keywords_in_c_code); TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete" TEST_CASE(varid_cpp_keywords_in_c_code3); + TEST_CASE(varid_cpp_keywords_in_c_code4); TEST_CASE(varidFunctionCall1); TEST_CASE(varidFunctionCall2); TEST_CASE(varidFunctionCall3); @@ -1468,6 +1469,17 @@ class TestVarID : public TestFixture { ASSERT_EQUALS(expected, tokenize(code, dinit(TokenizeOptions, $.cpp = false))); } + void varid_cpp_keywords_in_c_code4() { // #12120 + { + const char code[] = "int false = 0;"; + ASSERT_THROW_INTERNAL(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL); + } + { + const char code[] = "int false = 0;"; + ASSERT_EQUALS("1: int false@1 ; false@1 = 0 ;\n", tokenize(code, dinit(TokenizeOptions, $.cpp = false))); + } + } + void varidFunctionCall1() { const char code[] ="void f() {\n" " int x;\n" From 57b651b225e257a9cc0acdf903be74a02567f8a8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:25:52 +0200 Subject: [PATCH 3/5] Update valueflow.cpp --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6079fc07c9e..44aca2357e0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1208,7 +1208,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett for (Token* tok = tokenList.front(); tok; tok = tok->next()) { if (tok->hasKnownIntValue()) continue; - if (Token::Match(tok, "true|false")) + if (tok->tokType() == Token::Type::eBoolean) continue; if (astIsBool(tok) || Token::Match(tok, "%comp%")) { ValueFlow::Value lower{-1}; From 1f1e82bd392ec1031abe35a5d0b9880743bb5b07 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 11 Apr 2025 20:18:47 +0200 Subject: [PATCH 4/5] Address reviewer comments --- lib/valueflow.cpp | 4 ++-- test/testvarid.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 44aca2357e0..dee1b35802d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -562,7 +562,7 @@ static void valueFlowNumber(TokenList &tokenlist, const Settings& settings) if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) { for (Token *tok = tokenlist.front(); tok; tok = tok->next()) { - if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) { + if (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%")) { ValueFlow::Value value(tok->str() == "true"); if (!tok->isTemplateArg()) value.setKnown(); @@ -1208,7 +1208,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett for (Token* tok = tokenList.front(); tok; tok = tok->next()) { if (tok->hasKnownIntValue()) continue; - if (tok->tokType() == Token::Type::eBoolean) + if (Token::Match(tok, "%bool%")) continue; if (astIsBool(tok) || Token::Match(tok, "%comp%")) { ValueFlow::Value lower{-1}; diff --git a/test/testvarid.cpp b/test/testvarid.cpp index a12ae8fdfcc..7d35d2fb2e2 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1472,7 +1472,7 @@ class TestVarID : public TestFixture { void varid_cpp_keywords_in_c_code4() { // #12120 { const char code[] = "int false = 0;"; - ASSERT_THROW_INTERNAL(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL); + ASSERT_THROW_INTERNAL_EQUALS(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL, "Internal error. VarId assigned to bool literal."); } { const char code[] = "int false = 0;"; From 5316f4e586dfcfbe241d4c80659264225314c8c9 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 11 Apr 2025 20:23:53 +0200 Subject: [PATCH 5/5] Fix --- test/testvarid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 7d35d2fb2e2..61f570c9f76 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1472,7 +1472,7 @@ class TestVarID : public TestFixture { void varid_cpp_keywords_in_c_code4() { // #12120 { const char code[] = "int false = 0;"; - ASSERT_THROW_INTERNAL_EQUALS(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL, "Internal error. VarId assigned to bool literal."); + ASSERT_THROW_INTERNAL_EQUALS(tokenize(code, dinit(TokenizeOptions, $.cpp = true)), INTERNAL, "Internal error. VarId set for bool literal."); } { const char code[] = "int false = 0;";