Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4826,6 +4826,9 @@ void Tokenizer::setVarIdPass1()
const bool cpp = isCPP();
// Variable declarations can't start with "return" etc.
const std::unordered_set<std::string>& notstart = (isC()) ? notstart_c : notstart_cpp;
const auto isNotstart = [&notstart](const Token* t) {
return notstart.find(t->str()) != notstart.end() || (t->str() == "requires" && t->isKeyword());
};

VariableMap variableMap;
std::map<nonneg int, std::map<std::string, nonneg int>> structMembers;
Expand Down Expand Up @@ -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 (") ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The same #15314 error still happens with noexcept:

struct S { static const bool v = false; };
void k() noexcept(S::v && true);

cppcheck -q n.cpp → n.cpp:2:27: error: Internal error. VarId set for bool literal. [internalError]. It also happens with a member declaration (struct A { void k() noexcept(S::v && true); };). Base has the same error, so this is not a regression, but it is the same bug through another keyword. Should noexcept ( be skipped here as well, like sizeof ( and static_assert (?

(Token::simpleMatch(tok->previous(), "static_assert (") && tok->previous()->isKeyword())) {
continue;
}

Expand All @@ -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"))
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
55 changes: 55 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<class T>\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"
Expand Down
Loading