Skip to content

Commit ae060de

Browse files
Fix #14395 FP syntaxError for lambda in for loop (danmar#8125)
1 parent d329ee9 commit ae060de

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/tokenize.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8830,17 +8830,19 @@ void Tokenizer::findGarbageCode() const
88308830
continue;
88318831
// count number of semicolons
88328832
int semicolons = 0, colons = 0;
8833-
const Token* const startTok = tok;
8834-
tok = tok->linkAt(1)->previous(); // find ")" of the for-loop
8835-
// walk backwards until we find the beginning (startTok) of the for() again
8836-
for (; tok != startTok; tok = tok->previous()) {
8833+
const Token* const endTok = tok->linkAt(1);
8834+
for (tok = tok->tokAt(2); tok != endTok; tok = tok->next()) {
8835+
if (const Token* lam = findLambdaEndTokenWithoutAST(tok)) {
8836+
tok = lam;
8837+
continue;
8838+
}
88378839
if (tok->str() == ";") { // do the counting
88388840
semicolons++;
88398841
} else if (tok->str() == ":") {
88408842
if (tok->strAt(-1) == ",")
88418843
syntaxError(tok);
88428844
colons++;
8843-
} else if (tok->str() == ")") { // skip pairs of ( )
8845+
} else if (tok->str() == "(") { // skip pairs of ( )
88448846
tok = tok->link();
88458847
}
88468848
}

test/testtokenize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7689,6 +7689,10 @@ class TestTokenizer : public TestFixture {
76897689
" return {};\n"
76907690
"};\n"));
76917691

7692+
ASSERT_NO_THROW(tokenizeAndStringify("void f() {\n" // #14395
7693+
" for (int i : [](int a, int b) { ++a; ++b; return std::vector<int>{a, b}; }(1, 2)) {}\n"
7694+
"}\n"));
7695+
76927696
ignore_errout();
76937697
}
76947698

0 commit comments

Comments
 (0)