Parser; Set varid for structured binding variables

This commit is contained in:
Daniel Marjamäki 2021-04-26 18:04:27 +02:00
parent 3a7979171c
commit 8aa9e448f5
2 changed files with 22 additions and 9 deletions

View File

@ -3857,15 +3857,20 @@ void Tokenizer::setVarIdPass1()
continue;
bool decl;
if (isCPP() && Token::Match(tok->previous(), "for ( const| auto &|&&| [")) {
if (isCPP() && mSettings->standards.cpp >= Standards::CPP17 && Token::Match(tok, "[(;{}] const| auto &|&&| [")) {
// Structured bindings
tok2 = Token::findsimplematch(tok, "[");
while (tok2 && tok2->str() != "]") {
if (Token::Match(tok2, "%name% [,]]"))
variableMap.addVariable(tok2->str());
tok2 = tok2->next();
if ((Token::simpleMatch(tok->previous(), "for (") && Token::simpleMatch(tok2->link(), "] :")) ||
Token::simpleMatch(tok2->link(), "] =")) {
while (tok2 && tok2->str() != "]") {
if (Token::Match(tok2, "%name% [,]]"))
variableMap.addVariable(tok2->str());
tok2 = tok2->next();
}
continue;
}
continue;
}
try { /* Ticket #8151 */
decl = setVarIdParseDeclaration(&tok2, variableMap.map(), scopeStack.top().isExecutable, isCPP(), isC());
} catch (const Token * errTok) {

View File

@ -216,6 +216,8 @@ private:
TEST_CASE(decltype2);
TEST_CASE(exprid1);
TEST_CASE(structuredBindings);
}
std::string tokenize(const char code[], const char filename[] = "test.cpp") {
@ -224,7 +226,7 @@ private:
Settings settings;
settings.platform(Settings::Unix64);
settings.standards.c = Standards::C89;
settings.standards.cpp = Standards::CPP11;
settings.standards.cpp = Standards::CPPLatest;
settings.checkUnusedTemplates = true;
Tokenizer tokenizer(&settings, this);
@ -243,7 +245,7 @@ private:
Settings settings;
settings.platform(Settings::Unix64);
settings.standards.c = Standards::C89;
settings.standards.cpp = Standards::CPP11;
settings.standards.cpp = Standards::CPPLatest;
settings.checkUnusedTemplates = true;
Tokenizer tokenizer(&settings, this);
@ -1285,7 +1287,7 @@ private:
"2: list < int > :: iterator it@2 ;\n"
"3: std :: vector < std :: string > dirs@3 ;\n"
"4: std :: map < int , int > coords@4 ;\n"
"5: std :: unordered_map < int , int > xy@5 ;\n"
"5: std :: tr1 :: unordered_map < int , int > xy@5 ;\n"
"6: std :: list < boost :: wave :: token_id > tokens@6 ;\n"
"7: static std :: vector < CvsProcess * > ex1@7 ;\n"
"8: extern std :: vector < CvsProcess * > ex2@8 ;\n"
@ -3429,6 +3431,12 @@ private:
ASSERT_EQUALS(expected, actual);
}
void structuredBindings() {
const char code[] = "int foo() { auto [x,y] = xy(); return x+y; }";
ASSERT_EQUALS("1: int foo ( ) { auto [ x@1 , y@2 ] = xy ( ) ; return x@1 + y@2 ; }\n",
tokenize(code));
}
};
REGISTER_TEST(TestVarID)