AST: Do not hang for code 'foo({ for (a;b;c) {} });'

This commit is contained in:
Daniel Marjamäki 2019-11-03 11:02:59 +01:00
parent fadf209e18
commit 03ae0ccef3
2 changed files with 7 additions and 3 deletions

View File

@ -1222,8 +1222,11 @@ static void createAstAtTokenInner(Token * const tok1, const Token *endToken, boo
static Token * findAstTop(Token *tok1, Token *tok2) static Token * findAstTop(Token *tok1, Token *tok2)
{ {
for (Token *tok = tok1; tok && (tok != tok2); tok = tok->next()) { for (Token *tok = tok1; tok && (tok != tok2); tok = tok->next()) {
if (tok->astParent() || tok->astOperand1() || tok->astOperand2()) if (tok->astParent() || tok->astOperand1() || tok->astOperand2()) {
return tok->astTop(); while (tok->astParent() && tok->astParent()->index() >= tok1->index() && tok->astParent()->index() <= tok2->index())
tok = tok->astParent();
return tok;
}
if (Token::simpleMatch(tok, "( {")) if (Token::simpleMatch(tok, "( {"))
tok = tok->link(); tok = tok->link();
} }

View File

@ -7473,7 +7473,6 @@ private:
ASSERT_EQUALS("Abc({newreturn", testAst("return new A {b(c)};")); ASSERT_EQUALS("Abc({newreturn", testAst("return new A {b(c)};"));
ASSERT_EQUALS("a{{return", testAst("return{{a}};")); ASSERT_EQUALS("a{{return", testAst("return{{a}};"));
ASSERT_EQUALS("a{b{,{return", testAst("return{{a},{b}};")); ASSERT_EQUALS("a{b{,{return", testAst("return{{a},{b}};"));
ASSERT_EQUALS("abc{d{,{(=", testAst("a = b({ c{}, d{} });"));
} }
void astbrackets() { // [] void astbrackets() { // []
@ -7530,7 +7529,9 @@ private:
ASSERT_EQUALS("for_each_commit_graftint((void,(", testAst("extern int for_each_commit_graft(int (*)(int*), void *);")); ASSERT_EQUALS("for_each_commit_graftint((void,(", testAst("extern int for_each_commit_graft(int (*)(int*), void *);"));
ASSERT_EQUALS("for;;(", testAst("for (;;) {}")); ASSERT_EQUALS("for;;(", testAst("for (;;) {}"));
ASSERT_EQUALS("xsizeofvoid(=", testAst("x=sizeof(void*)")); ASSERT_EQUALS("xsizeofvoid(=", testAst("x=sizeof(void*)"));
ASSERT_EQUALS("abc{d{,{(=", testAst("a = b({ c{}, d{} });"));
ASSERT_EQUALS("abc;(", testAst("a(b;c)")); ASSERT_EQUALS("abc;(", testAst("a(b;c)"));
ASSERT_THROW(testAst("a({ for(a;b;c){} });"), InternalError);
} }
void asttemplate() { // uninstantiated templates will have <,>,etc.. void asttemplate() { // uninstantiated templates will have <,>,etc..