Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8973,7 +8973,7 @@ void Tokenizer::findGarbageCode() const
if (tok->strAt(-1) == ",")
syntaxError(tok);
colons++;
} else if (tok->str() == "(") { // skip pairs of ( )
} else if (tok->str() == "(" || tok->str() == "{") { // skip pairs of ( )
tok = tok->link();
}
}
Expand Down Expand Up @@ -9417,6 +9417,7 @@ void Tokenizer::simplifyStructDecl()
if (Token::Match(after->next(), "const|static|volatile| *|&| const| (| %type% )| ,|;|[|=|(|{")) {
after->insertToken(";");
after = after->next();
Token *declEnd = after;
while (!Token::Match(start, "struct|class|union|enum")) {
after->insertToken(start->str());
after = after->next();
Expand Down Expand Up @@ -9459,6 +9460,16 @@ void Tokenizer::simplifyStructDecl()
}
}
}

// pull declaration out of for loop
if (Token::simpleMatch(start->tokAt(-2), "for ( struct")) {
Token *link = start->linkAt(-1);
start->deletePrevious(2);
declEnd->insertToken("(");
declEnd->next()->link(link);
link->link(declEnd->next());
declEnd->insertToken("for");
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,11 @@ class TestTokenizer : public TestFixture {
const char code4[] = "union U { struct { int a; int b; }; int ab[2]; };";
const char expected4[] = "union U { struct { int a ; int b ; } ; int ab [ 2 ] ; } ;";
ASSERT_EQUALS(expected4, tokenizeAndStringify(code4));

// #14836: FP syntaxError for anonymous struct in for loop
const char code5[] = "void f(void) { for (struct { int a; } it = {0}; it.a < 10; it.a++) {} }";
const char expected5[] = "void f ( ) { struct Anonymous0 { int a ; } ; for ( struct Anonymous0 it = { 0 } ; it . a < 10 ; it . a ++ ) { } }";
ASSERT_EQUALS(expected5, tokenizeAndStringify(code5));
}

void vardecl1() {
Expand Down
Loading