From 7a33ca67ed9a363773f326cd21fd6d5188c956af Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:05:35 +0100 Subject: [PATCH 1/2] fix --- lib/tokenize.cpp | 8 ++++---- test/testvarid.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 349ca6c0ad1..210e31dc632 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4783,7 +4783,7 @@ void Tokenizer::setVarId() #define NOTSTART_C "NOT", "case", "default", "goto", "not", "return", "sizeof", "typedef" static const std::unordered_set notstart_c = { NOTSTART_C }; static const std::unordered_set notstart_cpp = { NOTSTART_C, - "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template" + "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template", "requires" }; // Returns the end of the lambda that starts at tok in a constructor initializer list, or nullptr @@ -4988,8 +4988,8 @@ void Tokenizer::setVarIdPass1() (tok->isName() && endsWith(tok->str(), ':')))) || (tok->str() == "(" && TokenList::isFunctionHead(tok, "{"))) { - // No variable declarations in sizeof - if (Token::simpleMatch(tok->previous(), "sizeof (")) { + // No variable declarations in sizeof and static_assert + if (Token::Match(tok->previous(), "sizeof|static_assert|_Static_assert (")) { continue; } @@ -5126,7 +5126,7 @@ void Tokenizer::setVarIdPass1() if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=[({,]")) { for (const Token *tok3 = prev2->next(); tok3 && tok3->str() != ";"; tok3 = tok3->next()) { - if (Token::Match(tok3, "[([]")) + if (Token::Match(tok3, "[([{]")) tok3 = tok3->link(); if (Token::Match(tok3, ", %name% [=[({,;]")) variableMap.addVariable(tok3->strAt(1), false); diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 0722e698e9e..7a3e1fc3053 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -106,6 +106,9 @@ class TestVarID : public TestFixture { TEST_CASE(varid_for_1); TEST_CASE(varid_for_2); TEST_CASE(varid_for_3); + TEST_CASE(varid_for_4); + TEST_CASE(varid_static_assert); + TEST_CASE(varid_requires); 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); @@ -1461,6 +1464,32 @@ class TestVarID : public TestFixture { ASSERT_EQUALS(expected2, tokenize(code2)); } + void varid_for_4() { + const char code[] = "void f(int x) {\n" + " for (V v{ 1, x, true }; ;) { x = 1; }\n" + "}\n"; + const char expected[] = "1: void f ( int x@1 ) {\n" + "2: for ( V v@2 { 1 , x@1 , true } ; ; ) { x@1 = 1 ; }\n" + "3: }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid_static_assert() { + const char code[] = "struct S { static const bool v = false; };\n" + "static_assert(S::v && true, \"\");\n"; + const char expected[] = "1: struct S { static const bool v@1 = false ; } ;\n" + "2: static_assert ( S :: v@1 && true , \"\" ) ;\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid_requires() { + const char code[] = "template\n" + "concept C = requires (T x) { requires true; };\n"; + const char expected[] = "1: template < class T >\n" + "2: concept C = requires ( T x@1 ) { requires true ; } ;\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + void varid_cpp_keywords_in_c_code() { const char code[] = "void f() {\n" " delete d;\n" From 2d765dc9c96cd0d98ffcccd88e4401c9c9d79895 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:38:32 +0100 Subject: [PATCH 2/2] exclude --- lib/tokenize.cpp | 14 +++++++++----- test/testvarid.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 210e31dc632..a1e7d588046 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4783,7 +4783,7 @@ void Tokenizer::setVarId() #define NOTSTART_C "NOT", "case", "default", "goto", "not", "return", "sizeof", "typedef" static const std::unordered_set notstart_c = { NOTSTART_C }; static const std::unordered_set notstart_cpp = { NOTSTART_C, - "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template", "requires" + "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template" }; // Returns the end of the lambda that starts at tok in a constructor initializer list, or nullptr @@ -4826,6 +4826,9 @@ void Tokenizer::setVarIdPass1() const bool cpp = isCPP(); // Variable declarations can't start with "return" etc. const std::unordered_set& notstart = (isC()) ? notstart_c : notstart_cpp; + const auto isNotstart = [¬start](const Token* t) { + return notstart.find(t->str()) != notstart.end() || (t->str() == "requires" && t->isKeyword()); + }; VariableMap variableMap; std::map> structMembers; @@ -4989,7 +4992,8 @@ void Tokenizer::setVarIdPass1() (tok->str() == "(" && TokenList::isFunctionHead(tok, "{"))) { // No variable declarations in sizeof and static_assert - if (Token::Match(tok->previous(), "sizeof|static_assert|_Static_assert (")) { + if (Token::Match(tok->previous(), "sizeof|_Static_assert (") || + (Token::simpleMatch(tok->previous(), "static_assert (") && tok->previous()->isKeyword())) { continue; } @@ -5007,7 +5011,7 @@ void Tokenizer::setVarIdPass1() break; // Variable declaration can't start with "return", etc - if (notstart.find(tok2->str()) != notstart.end()) + if (isNotstart(tok2)) continue; if (!isC() && Token::simpleMatch(tok2, "const new")) @@ -5104,7 +5108,7 @@ void Tokenizer::setVarIdPass1() !isPointerToArrayDeclarator(tok3->next(), variableMap)))) || tok3->isOp() || tok3->str() == "(" || - notstart.find(tok3->str()) != notstart.end()) { + isNotstart(tok3)) { decl = true; break; } @@ -5205,7 +5209,7 @@ void Tokenizer::setVarIdPass1() for (end = tok->next(); Token::Match(end, "%name%|*|&|,|[|]|%num%"); end = end->next()) {} // there are tokens which can't appear at the begin of a function declaration such as "return" - const bool isNotstartKeyword = start->next() && notstart.find(start->strAt(1)) != notstart.end(); + const bool isNotstartKeyword = start->next() && isNotstart(start->next()); // now check if it is a function (pointer) declaration if ((Token::simpleMatch(start, ") (") || Token::Match(start, "[;{}] %type% %name%|*")) && par && Token::Match(end, ") [;=]") && !isNotstartKeyword) { diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 7a3e1fc3053..8d573197c4a 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1466,10 +1466,10 @@ class TestVarID : public TestFixture { void varid_for_4() { const char code[] = "void f(int x) {\n" - " for (V v{ 1, x, true }; ;) { x = 1; }\n" + " for (V v{ 1, x, true }, w{ 2, x, false }; ;) { x = 1; }\n" "}\n"; const char expected[] = "1: void f ( int x@1 ) {\n" - "2: for ( V v@2 { 1 , x@1 , true } ; ; ) { x@1 = 1 ; }\n" + "2: for ( V v@2 { 1 , x@1 , true } , w@3 { 2 , x@1 , false } ; ; ) { x@1 = 1 ; }\n" "3: }\n"; ASSERT_EQUALS(expected, tokenize(code)); } @@ -1480,6 +1480,18 @@ class TestVarID : public TestFixture { const char expected[] = "1: struct S { static const bool v@1 = false ; } ;\n" "2: static_assert ( S :: v@1 && true , \"\" ) ;\n"; ASSERT_EQUALS(expected, tokenize(code)); + + // static_assert is not a keyword in C17 and C++03 + const char code2[] = "int static_assert(int x) {\n" + " return x;\n" + "}\n"; + const char expected2[] = "1: int static_assert ( int x@1 ) {\n" + "2: return x@1 ;\n" + "3: }\n"; + const Settings s1 = settingsBuilder(settings).c(Standards::C17).build(); + ASSERT_EQUALS(expected2, tokenize(code2, s1, false)); + const Settings s2 = settingsBuilder(settings).cpp(Standards::CPP03).build(); + ASSERT_EQUALS(expected2, tokenize(code2, s2)); } void varid_requires() { @@ -1488,6 +1500,20 @@ class TestVarID : public TestFixture { const char expected[] = "1: template < class T >\n" "2: concept C = requires ( T x@1 ) { requires true ; } ;\n"; ASSERT_EQUALS(expected, tokenize(code)); + + // requires is not a keyword in C++17 + const char code2[] = "struct requires {};\n" + "void f() {\n" + " requires r;\n" + " (void)r;\n" + "}\n"; + const char expected2[] = "1: struct requires { } ;\n" + "2: void f ( ) {\n" + "3: requires r@1 ;\n" + "4: ( void ) r@1 ;\n" + "5: }\n"; + const Settings s = settingsBuilder(settings).cpp(Standards::CPP17).build(); + ASSERT_EQUALS(expected2, tokenize(code2, s)); } void varid_cpp_keywords_in_c_code() {