Fixed #5386 (ast: hang when code is 'for (T a : b)')

This commit is contained in:
Daniel Marjamäki 2014-01-23 06:13:24 +01:00
parent 5b479cc841
commit ec034c1d59
2 changed files with 9 additions and 3 deletions

View File

@ -692,12 +692,13 @@ void TokenList::createAst()
if (Token::simpleMatch(tok,"for (")) {
Token *tok2 = tok->tokAt(2);
Token *init1 = 0;
while (tok2 && tok2->str() != ";") {
const Token * const endPar = tok->next()->link();
while (tok2 && tok2 != endPar && tok2->str() != ";") {
if (tok2->str() == "<" && tok2->link()) {
tok2 = tok2->link();
if (!tok2)
break;
} else if (Token::Match(tok2, "%var% %op%|(|[|.|=|::") || Token::Match(tok2->previous(), "[;{}] %cop%|(")) {
} else if (Token::Match(tok2, "%var% %op%|(|[|.|=|:|::") || Token::Match(tok2->previous(), "[;{}] %cop%|(")) {
init1 = tok2;
std::stack<Token *> operands;
compileExpression(tok2, operands);
@ -708,6 +709,10 @@ void TokenList::createAst()
tok2 = tok2->next();
}
if (!tok2 || tok2->str() != ";") {
if (tok2 == endPar && init1) {
tok->next()->astOperand2(init1);
tok->next()->astOperand1(tok);
}
tok = tok2;
continue;
}

View File

@ -10106,7 +10106,8 @@ private:
ASSERT_EQUALS("for;;(", testAst("for(;;)"));
ASSERT_EQUALS("fora0=a8<a++;;(", testAst("for(a=0;a<8;a++)"));
TODO_ASSERT_EQUALS("fori1=current0=,iNUM<=i++;;(", "fori1=current0=,i<NUM=i++;;(", testAst("for(i = (1), current = 0; i <= (NUM); ++i)"));
ASSERT_EQUALS("eachxy,(", testAst("for(each(x,y)){}")); // it's not well-defined what this ast should be
ASSERT_EQUALS("foreachxy,((", testAst("for(each(x,y)){}")); // it's not well-defined what this ast should be
ASSERT_EQUALS("forab:(", testAst("for (int a : b);"));
}
void astpar() const { // parentheses