Skip to content

Commit 2d765dc

Browse files
committed
exclude
1 parent 7a33ca6 commit 2d765dc

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,7 +4783,7 @@ void Tokenizer::setVarId()
47834783
#define NOTSTART_C "NOT", "case", "default", "goto", "not", "return", "sizeof", "typedef"
47844784
static const std::unordered_set<std::string> notstart_c = { NOTSTART_C };
47854785
static const std::unordered_set<std::string> notstart_cpp = { NOTSTART_C,
4786-
"delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template", "requires"
4786+
"delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template"
47874787
};
47884788

47894789
// Returns the end of the lambda that starts at tok in a constructor initializer list, or nullptr
@@ -4826,6 +4826,9 @@ void Tokenizer::setVarIdPass1()
48264826
const bool cpp = isCPP();
48274827
// Variable declarations can't start with "return" etc.
48284828
const std::unordered_set<std::string>& notstart = (isC()) ? notstart_c : notstart_cpp;
4829+
const auto isNotstart = [&notstart](const Token* t) {
4830+
return notstart.find(t->str()) != notstart.end() || (t->str() == "requires" && t->isKeyword());
4831+
};
48294832

48304833
VariableMap variableMap;
48314834
std::map<nonneg int, std::map<std::string, nonneg int>> structMembers;
@@ -4989,7 +4992,8 @@ void Tokenizer::setVarIdPass1()
49894992
(tok->str() == "(" && TokenList::isFunctionHead(tok, "{"))) {
49904993

49914994
// No variable declarations in sizeof and static_assert
4992-
if (Token::Match(tok->previous(), "sizeof|static_assert|_Static_assert (")) {
4995+
if (Token::Match(tok->previous(), "sizeof|_Static_assert (") ||
4996+
(Token::simpleMatch(tok->previous(), "static_assert (") && tok->previous()->isKeyword())) {
49934997
continue;
49944998
}
49954999

@@ -5007,7 +5011,7 @@ void Tokenizer::setVarIdPass1()
50075011
break;
50085012

50095013
// Variable declaration can't start with "return", etc
5010-
if (notstart.find(tok2->str()) != notstart.end())
5014+
if (isNotstart(tok2))
50115015
continue;
50125016

50135017
if (!isC() && Token::simpleMatch(tok2, "const new"))
@@ -5104,7 +5108,7 @@ void Tokenizer::setVarIdPass1()
51045108
!isPointerToArrayDeclarator(tok3->next(), variableMap)))) ||
51055109
tok3->isOp() ||
51065110
tok3->str() == "(" ||
5107-
notstart.find(tok3->str()) != notstart.end()) {
5111+
isNotstart(tok3)) {
51085112
decl = true;
51095113
break;
51105114
}
@@ -5205,7 +5209,7 @@ void Tokenizer::setVarIdPass1()
52055209
for (end = tok->next(); Token::Match(end, "%name%|*|&|,|[|]|%num%"); end = end->next()) {}
52065210

52075211
// there are tokens which can't appear at the begin of a function declaration such as "return"
5208-
const bool isNotstartKeyword = start->next() && notstart.find(start->strAt(1)) != notstart.end();
5212+
const bool isNotstartKeyword = start->next() && isNotstart(start->next());
52095213

52105214
// now check if it is a function (pointer) declaration
52115215
if ((Token::simpleMatch(start, ") (") || Token::Match(start, "[;{}] %type% %name%|*")) && par && Token::Match(end, ") [;=]") && !isNotstartKeyword) {

‎test/testvarid.cpp‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,10 +1466,10 @@ class TestVarID : public TestFixture {
14661466

14671467
void varid_for_4() {
14681468
const char code[] = "void f(int x) {\n"
1469-
" for (V v{ 1, x, true }; ;) { x = 1; }\n"
1469+
" for (V v{ 1, x, true }, w{ 2, x, false }; ;) { x = 1; }\n"
14701470
"}\n";
14711471
const char expected[] = "1: void f ( int x@1 ) {\n"
1472-
"2: for ( V v@2 { 1 , x@1 , true } ; ; ) { x@1 = 1 ; }\n"
1472+
"2: for ( V v@2 { 1 , x@1 , true } , w@3 { 2 , x@1 , false } ; ; ) { x@1 = 1 ; }\n"
14731473
"3: }\n";
14741474
ASSERT_EQUALS(expected, tokenize(code));
14751475
}
@@ -1480,6 +1480,18 @@ class TestVarID : public TestFixture {
14801480
const char expected[] = "1: struct S { static const bool v@1 = false ; } ;\n"
14811481
"2: static_assert ( S :: v@1 && true , \"\" ) ;\n";
14821482
ASSERT_EQUALS(expected, tokenize(code));
1483+
1484+
// static_assert is not a keyword in C17 and C++03
1485+
const char code2[] = "int static_assert(int x) {\n"
1486+
" return x;\n"
1487+
"}\n";
1488+
const char expected2[] = "1: int static_assert ( int x@1 ) {\n"
1489+
"2: return x@1 ;\n"
1490+
"3: }\n";
1491+
const Settings s1 = settingsBuilder(settings).c(Standards::C17).build();
1492+
ASSERT_EQUALS(expected2, tokenize(code2, s1, false));
1493+
const Settings s2 = settingsBuilder(settings).cpp(Standards::CPP03).build();
1494+
ASSERT_EQUALS(expected2, tokenize(code2, s2));
14831495
}
14841496

14851497
void varid_requires() {
@@ -1488,6 +1500,20 @@ class TestVarID : public TestFixture {
14881500
const char expected[] = "1: template < class T >\n"
14891501
"2: concept C = requires ( T x@1 ) { requires true ; } ;\n";
14901502
ASSERT_EQUALS(expected, tokenize(code));
1503+
1504+
// requires is not a keyword in C++17
1505+
const char code2[] = "struct requires {};\n"
1506+
"void f() {\n"
1507+
" requires r;\n"
1508+
" (void)r;\n"
1509+
"}\n";
1510+
const char expected2[] = "1: struct requires { } ;\n"
1511+
"2: void f ( ) {\n"
1512+
"3: requires r@1 ;\n"
1513+
"4: ( void ) r@1 ;\n"
1514+
"5: }\n";
1515+
const Settings s = settingsBuilder(settings).cpp(Standards::CPP17).build();
1516+
ASSERT_EQUALS(expected2, tokenize(code2, s));
14911517
}
14921518

14931519
void varid_cpp_keywords_in_c_code() {

0 commit comments

Comments
 (0)