diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 15879d813..441842ae4 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1222,8 +1222,11 @@ static void createAstAtTokenInner(Token * const tok1, const Token *endToken, boo static Token * findAstTop(Token *tok1, Token *tok2) { for (Token *tok = tok1; tok && (tok != tok2); tok = tok->next()) { - if (tok->astParent() || tok->astOperand1() || tok->astOperand2()) - return tok->astTop(); + if (tok->astParent() || tok->astOperand1() || tok->astOperand2()) { + while (tok->astParent() && tok->astParent()->index() >= tok1->index() && tok->astParent()->index() <= tok2->index()) + tok = tok->astParent(); + return tok; + } if (Token::simpleMatch(tok, "( {")) tok = tok->link(); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ae94bd02b..29cc4f343 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7473,7 +7473,6 @@ private: ASSERT_EQUALS("Abc({newreturn", testAst("return new A {b(c)};")); ASSERT_EQUALS("a{{return", testAst("return{{a}};")); ASSERT_EQUALS("a{b{,{return", testAst("return{{a},{b}};")); - ASSERT_EQUALS("abc{d{,{(=", testAst("a = b({ c{}, d{} });")); } 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;;(", testAst("for (;;) {}")); ASSERT_EQUALS("xsizeofvoid(=", testAst("x=sizeof(void*)")); + ASSERT_EQUALS("abc{d{,{(=", testAst("a = b({ c{}, d{} });")); ASSERT_EQUALS("abc;(", testAst("a(b;c)")); + ASSERT_THROW(testAst("a({ for(a;b;c){} });"), InternalError); } void asttemplate() { // uninstantiated templates will have <,>,etc..