Skip to content

Commit 8499548

Browse files
committed
VarId: fixed varids for 'for (auto [x,y]: xy)'
1 parent 5ddc1af commit 8499548

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,15 @@ void Tokenizer::setVarIdPass1()
34293429
continue;
34303430

34313431
bool decl;
3432+
if (isCPP() && Token::Match(tok->previous(), "for ( const| auto &|&&| [")) {
3433+
tok2 = Token::findsimplematch(tok, "[");
3434+
while (tok2 && tok2->str() != "]") {
3435+
if (Token::Match(tok2, "%name% [,]]"))
3436+
variableMap.addVariable(tok2->str());
3437+
tok2 = tok2->next();
3438+
}
3439+
continue;
3440+
}
34323441
try { /* Ticket #8151 */
34333442
decl = setVarIdParseDeclaration(&tok2, variableMap.map(), scopeStack.top().isExecutable, isCPP(), isC());
34343443
} catch (const Token * errTok) {

test/testvarid.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ class TestVarID : public TestFixture {
166166
TEST_CASE(varid_trailing_return2); // #9066
167167
TEST_CASE(varid_parameter_pack); // #9383
168168

169+
TEST_CASE(varid_for_auto_cpp17);
170+
169171
TEST_CASE(varidclass1);
170172
TEST_CASE(varidclass2);
171173
TEST_CASE(varidclass3);
@@ -2550,6 +2552,22 @@ class TestVarID : public TestFixture {
25502552
ASSERT_EQUALS(exp1, tokenize(code1));
25512553
}
25522554

2555+
void varid_for_auto_cpp17() {
2556+
const char code[] = "void f() {\n"
2557+
" for (auto [x,y,z]: xyz) {\n"
2558+
" x+y+z;\n"
2559+
" }\n"
2560+
" x+y+z;\n"
2561+
"}";
2562+
const char exp1[] = "1: void f ( ) {\n"
2563+
"2: for ( auto [ x@1 , y@2 , z@3 ] : xyz ) {\n"
2564+
"3: x@1 + y@2 + z@3 ;\n"
2565+
"4: }\n"
2566+
"5: x + y + z ;\n"
2567+
"6: }\n";
2568+
ASSERT_EQUALS(exp1, tokenize(code));
2569+
}
2570+
25532571
void varidclass1() {
25542572
const std::string actual = tokenize(
25552573
"class Fred\n"

0 commit comments

Comments
 (0)