From fc2bf2ef0807f9caf1fd04be3345005975aab2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 20 Oct 2015 23:55:29 +0200 Subject: [PATCH] Fixed #7065 (AST: More explicit handling of lambda functions, generate ast for ';new Fred;') --- lib/tokenlist.cpp | 24 ++++++++++++++++-------- test/testtokenize.cpp | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index e16272168..78a403660 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -638,6 +638,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state) } else compileBinOp(tok, state, compileScope); } else if (tok->str() == "[") { + bool lambda = false; if (state.cpp && isPrefixUnary(tok, state.cpp) && tok->link()->strAt(1) == "(") { // Lambda // What we do here: // - Nest the round bracket under the square bracket. @@ -645,14 +646,18 @@ static void compilePrecedence2(Token *&tok, AST_state& state) // - Compile the content of the lambda function as separate tree (this is done later) Token* squareBracket = tok; Token* roundBracket = squareBracket->link()->next(); - Token* curlyBracket = Token::findsimplematch(roundBracket->link()->next(), "{"); - if (!curlyBracket) - break; - squareBracket->astOperand1(roundBracket); - roundBracket->astOperand1(curlyBracket); - state.op.push(squareBracket); - tok = curlyBracket->link()->next(); - } else { + Token* curlyBracket = roundBracket->link()->next(); + while (Token::Match(curlyBracket, "%name%|.|::")) + curlyBracket = curlyBracket->next(); + if (Token::simpleMatch(curlyBracket, "{")) { + lambda = true; + squareBracket->astOperand1(roundBracket); + roundBracket->astOperand1(curlyBracket); + state.op.push(squareBracket); + tok = curlyBracket->link()->next(); + } + } + if (!lambda) { Token* tok2 = tok; if (tok->strAt(1) != "]") compileBinOp(tok, state, compileExpression); @@ -1005,6 +1010,9 @@ static Token * createAstAtToken(Token *tok, bool cpp) return tok->linkAt(1); if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<|?") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) { + if (cpp && Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%")) + tok = tok->previous(); + Token * const tok1 = tok; AST_state state(cpp); compileExpression(tok, state); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 305004427..d650f98d7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8105,6 +8105,7 @@ private: ASSERT_EQUALS("ab.3c-(delete", testAst("delete[] a.b(3 - c);")); ASSERT_EQUALS("a::new=", testAst("a = new (b) ::X;")); ASSERT_EQUALS("aA1(new(bB2(new(,", testAst("a(new A(1)), b(new B(2))")); + ASSERT_EQUALS("Fred10[new", testAst(";new Fred[10];")); // invalid code (libreoffice), don't hang // #define SlideSorterViewShell