diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 349ca6c0ad1..a1e7d588046 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; @@ -4988,8 +4991,9 @@ 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 (") || + (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; } @@ -5126,7 +5130,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); @@ -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 0722e698e9e..8d573197c4a 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,58 @@ 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 }, 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 } , w@3 { 2 , x@1 , false } ; ; ) { 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)); + + // 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() { + 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)); + + // 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() { const char code[] = "void f() {\n" " delete d;\n"