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,20 +1372,26 @@ static Token * createAstAtToken(Token *tok, bool cpp)
Token *tok2 = skipDecl(tok->tokAt(2));
Token *init1 = nullptr;
Token * const endPar = tok->next()->link();
while (tok2 && tok2 != endPar && tok2->str() != ";") {
if (tok2->str() == "<" && tok2->link()) {
tok2 = tok2->link();
} else if (Token::Match(tok2, "%name% %op%|(|[|.|:|::") || Token::Match(tok2->previous(), "[(;{}] %cop%|(")) {
init1 = tok2;
AST_state state1(cpp);
compileExpression(tok2, state1);
if (Token::Match(tok2, ";|)"))
break;
init1 = nullptr;
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() != ";") {
if (tok2->str() == "<" && tok2->link()) {
tok2 = tok2->link();
} else if (Token::Match(tok2, "%name% %op%|(|[|.|:|::") || Token::Match(tok2->previous(), "[(;{}] %cop%|(")) {
init1 = tok2;
AST_state state1(cpp);
compileExpression(tok2, state1);
if (Token::Match(tok2, ";|)"))
break;
init1 = nullptr;
}
if (!tok2) // #7109 invalid code
return nullptr;
tok2 = tok2->next();
}
if (!tok2) // #7109 invalid code
return nullptr;
tok2 = tok2->next();
}
if (!tok2 || tok2->str() != ";") {
if (tok2 == endPar && init1) {

View File

@ -7524,6 +7524,7 @@ private:
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("forab,c:(", testAst("for (auto [a,b]: c);"));
ASSERT_EQUALS("fora*++;;(", testAst("for (++(*a);;);"));
// problems with multiple expressions
ASSERT_EQUALS("ax( whilex(", testAst("a(x) while (x)"));