Skip to content

Commit ad04c7a

Browse files
authored
Fix #14836: FP syntaxError for anonymous struct in for loop (#8710)
1 parent 80ab538 commit ad04c7a

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8973,7 +8973,7 @@ void Tokenizer::findGarbageCode() const
89738973
if (tok->strAt(-1) == ",")
89748974
syntaxError(tok);
89758975
colons++;
8976-
} else if (tok->str() == "(") { // skip pairs of ( )
8976+
} else if (tok->str() == "(" || tok->str() == "{") { // skip pairs of ( )
89778977
tok = tok->link();
89788978
}
89798979
}
@@ -9417,6 +9417,7 @@ void Tokenizer::simplifyStructDecl()
94179417
if (Token::Match(after->next(), "const|static|volatile| *|&| const| (| %type% )| ,|;|[|=|(|{")) {
94189418
after->insertToken(";");
94199419
after = after->next();
9420+
Token *declEnd = after;
94209421
while (!Token::Match(start, "struct|class|union|enum")) {
94219422
after->insertToken(start->str());
94229423
after = after->next();
@@ -9459,6 +9460,16 @@ void Tokenizer::simplifyStructDecl()
94599460
}
94609461
}
94619462
}
9463+
9464+
// pull declaration out of for loop
9465+
if (Token::simpleMatch(start->tokAt(-2), "for ( struct")) {
9466+
Token *link = start->linkAt(-1);
9467+
start->deletePrevious(2);
9468+
declEnd->insertToken("(");
9469+
declEnd->next()->link(link);
9470+
link->link(declEnd->next());
9471+
declEnd->insertToken("for");
9472+
}
94629473
}
94639474
}
94649475

test/testtokenize.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,11 @@ class TestTokenizer : public TestFixture {
22742274
const char code4[] = "union U { struct { int a; int b; }; int ab[2]; };";
22752275
const char expected4[] = "union U { struct { int a ; int b ; } ; int ab [ 2 ] ; } ;";
22762276
ASSERT_EQUALS(expected4, tokenizeAndStringify(code4));
2277+
2278+
// #14836: FP syntaxError for anonymous struct in for loop
2279+
const char code5[] = "void f(void) { for (struct { int a; } it = {0}; it.a < 10; it.a++) {} }";
2280+
const char expected5[] = "void f ( ) { struct Anonymous0 { int a ; } ; for ( struct Anonymous0 it = { 0 } ; it . a < 10 ; it . a ++ ) { } }";
2281+
ASSERT_EQUALS(expected5, tokenizeAndStringify(code5));
22772282
}
22782283

22792284
void vardecl1() {

0 commit comments

Comments
 (0)