Skip to content

Commit 1023d9b

Browse files
Fix #14218 Missing varid after brace-initialized array member (danmar#7907)
1 parent 19faaee commit 1023d9b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/tokenize.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7450,9 +7450,9 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
74507450
else if (Token::Match(varName, "%name% [")) {
74517451
tok2 = varName->next();
74527452

7453-
while (Token::Match(tok2->link(), "] ,|=|["))
7453+
while (Token::Match(tok2->link(), "] [,=[{]"))
74547454
tok2 = tok2->link()->next();
7455-
if (!Token::Match(tok2, "=|,"))
7455+
if (!Token::Match(tok2, "[=,{]"))
74567456
tok2 = nullptr;
74577457
if (tok2 && tok2->str() == "=") {
74587458
while (tok2 && tok2->str() != "," && tok2->str() != ";") {
@@ -7522,9 +7522,11 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
75227522
varTok = varTok->next();
75237523
if (!varTok)
75247524
syntaxError(tok2); // invalid code
7525-
TokenList::insertTokens(eq, varTok, 2);
7526-
eq->str(";");
7527-
eq->isSplittedVarDeclEq(true);
7525+
if (eq->str() == "=") {
7526+
TokenList::insertTokens(eq, varTok, 2);
7527+
eq->str(";");
7528+
eq->isSplittedVarDeclEq(true);
7529+
}
75287530

75297531
// "= x, " => "= x; type "
75307532
if (tok2->str() == ",") {

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ class TestTokenizer : public TestFixture {
295295
TEST_CASE(simplifyInitVar2);
296296
TEST_CASE(simplifyInitVar3);
297297
TEST_CASE(simplifyInitVar4);
298+
TEST_CASE(simplifyInitVar5);
298299

299300
TEST_CASE(bitfields1);
300301
TEST_CASE(bitfields2);
@@ -4678,6 +4679,11 @@ class TestTokenizer : public TestFixture {
46784679
"}", tokenizeAndStringify(code));
46794680
}
46804681

4682+
void simplifyInitVar5() { // #14218
4683+
const char code[] = "int c[1]{}, b;";
4684+
ASSERT_EQUALS("int c [ 1 ] { } ; int b ;", tokenizeAndStringify(code));
4685+
}
4686+
46814687
void bitfields1() {
46824688
const char code1[] = "struct A { bool x : 1; };";
46834689
ASSERT_EQUALS("struct A { bool x ; } ;", tokenizeAndStringify(code1));

0 commit comments

Comments
 (0)