VarId: fixed varids for 'for (auto [x,y]: xy)'

This commit is contained in:
Daniel Marjamäki 2020-02-27 07:18:07 +01:00
parent 5ddc1af5e6
commit 84995485ea
2 changed files with 27 additions and 0 deletions

View File

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

View File

@ -166,6 +166,8 @@ private:
TEST_CASE(varid_trailing_return2); // #9066
TEST_CASE(varid_parameter_pack); // #9383
TEST_CASE(varid_for_auto_cpp17);
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
TEST_CASE(varidclass3);
@ -2550,6 +2552,22 @@ private:
ASSERT_EQUALS(exp1, tokenize(code1));
}
void varid_for_auto_cpp17() {
const char code[] = "void f() {\n"
" for (auto [x,y,z]: xyz) {\n"
" x+y+z;\n"
" }\n"
" x+y+z;\n"
"}";
const char exp1[] = "1: void f ( ) {\n"
"2: for ( auto [ x@1 , y@2 , z@3 ] : xyz ) {\n"
"3: x@1 + y@2 + z@3 ;\n"
"4: }\n"
"5: x + y + z ;\n"
"6: }\n";
ASSERT_EQUALS(exp1, tokenize(code));
}
void varidclass1() {
const std::string actual = tokenize(
"class Fred\n"