Fixed #9533 (Syntax Error: AST broken, 'for' doesn't have two operands incrementing pointer in initializer)

This commit is contained in:
Daniel Marjamäki 2020-06-09 20:57:00 +02:00
parent 0c659a1499
commit d31d778bf4
2 changed files with 20 additions and 13 deletions

View File

@ -1372,6 +1372,11 @@ static Token * createAstAtToken(Token *tok, bool cpp)
Token *tok2 = skipDecl(tok->tokAt(2)); Token *tok2 = skipDecl(tok->tokAt(2));
Token *init1 = nullptr; Token *init1 = nullptr;
Token * const endPar = tok->next()->link(); Token * const endPar = tok->next()->link();
if (tok2 == tok->tokAt(2) && Token::Match(tok2, "%op%|(")) {
init1 = tok2;
AST_state state1(cpp);
compileExpression(tok2, state1);
} else {
while (tok2 && tok2 != endPar && tok2->str() != ";") { while (tok2 && tok2 != endPar && tok2->str() != ";") {
if (tok2->str() == "<" && tok2->link()) { if (tok2->str() == "<" && tok2->link()) {
tok2 = tok2->link(); tok2 = tok2->link();
@ -1387,6 +1392,7 @@ static Token * createAstAtToken(Token *tok, bool cpp)
return nullptr; return nullptr;
tok2 = tok2->next(); tok2 = tok2->next();
} }
}
if (!tok2 || tok2->str() != ";") { if (!tok2 || tok2->str() != ";") {
if (tok2 == endPar && init1) { if (tok2 == endPar && init1) {
tok->next()->astOperand2(init1); tok->next()->astOperand2(init1);

View File

@ -7524,6 +7524,7 @@ private:
ASSERT_EQUALS("forx*0=yz;;(", testAst("for(*x=0;y;z)")); ASSERT_EQUALS("forx*0=yz;;(", testAst("for(*x=0;y;z)"));
ASSERT_EQUALS("forx0=y(8<z;;(", testAst("for (x=0;(int)y<8;z);")); ASSERT_EQUALS("forx0=y(8<z;;(", testAst("for (x=0;(int)y<8;z);"));
ASSERT_EQUALS("forab,c:(", testAst("for (auto [a,b]: c);")); ASSERT_EQUALS("forab,c:(", testAst("for (auto [a,b]: c);"));
ASSERT_EQUALS("fora*++;;(", testAst("for (++(*a);;);"));
// problems with multiple expressions // problems with multiple expressions
ASSERT_EQUALS("ax( whilex(", testAst("a(x) while (x)")); ASSERT_EQUALS("ax( whilex(", testAst("a(x) while (x)"));