Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,20 @@ static bool isPrefixUnary(const Token* tok, bool cpp)

static void compilePrecedence2(Token *&tok, AST_state& state)
{
const bool isStartOfCpp11Init = state.cpp && tok && tok->str() == "{" && iscpp11init(tok);
if (!(isStartOfCpp11Init && Token::Match(tok->tokAt(-2), "new %type% {")))
auto doCompileScope = [&](const Token* tok) -> bool {
const bool isStartOfCpp11Init = state.cpp && tok && tok->str() == "{" && iscpp11init(tok);
if (isStartOfCpp11Init) {
tok = tok->previous();
while (tok && Token::Match(tok->previous(), ":: %type%"))
tok = tok->tokAt(-2);
if (tok && !tok->isKeyword())
tok = tok->previous();
return !Token::Match(tok, "new ::| %type%");
}
return true;
};

if (doCompileScope(tok))
compileScope(tok, state);
while (tok) {
if (tok->tokType() == Token::eIncDecOp && !isPrefixUnary(tok, state.cpp)) {
Expand Down Expand Up @@ -1069,12 +1081,18 @@ static void compilePrecedence3(Token *&tok, AST_state& state)
}

Token* leftToken = tok;
while (Token::Match(tok->next(), ":: %name%")) {
Token* scopeToken = tok->next(); //The ::
scopeToken->astOperand1(leftToken);
scopeToken->astOperand2(scopeToken->next());
leftToken = scopeToken;
tok = scopeToken->next();
if (Token::simpleMatch(tok, "::")) {
tok->astOperand1(tok->next());
tok = tok->next();
}
else {
while (Token::Match(tok->next(), ":: %name%")) {
Token* scopeToken = tok->next(); //The ::
scopeToken->astOperand1(leftToken);
scopeToken->astOperand2(scopeToken->next());
leftToken = scopeToken;
tok = scopeToken->next();
}
}

state.op.push(tok);
Expand Down
5 changes: 4 additions & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6071,10 +6071,13 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("Aa*A12,{new=", testAst("A* a = new A{ 1, 2 };"));
ASSERT_EQUALS("Sv0[(new", testAst("new S(v[0]);")); // #10929
ASSERT_EQUALS("SS::x(px0>intx[{newint1[{new:?(:", testAst("S::S(int x) : p(x > 0 ? new int[x]{} : new int[1]{}) {}")); // #10793
ASSERT_EQUALS("a0[T{new=", testAst("a[0] = new T{};"));
ASSERT_EQUALS("a0[T::{new=", testAst("a[0] = new ::T{};"));
ASSERT_EQUALS("a0[ST::{new=", testAst("a[0] = new S::T{};"));

// placement new
ASSERT_EQUALS("X12,3,(new ab,c,", testAst("new (a,b,c) X(1,2,3);"));
ASSERT_EQUALS("a::new=", testAst("a = new (b) ::X;"));
ASSERT_EQUALS("aX::new=", testAst("a = new (b) ::X;"));
ASSERT_EQUALS("cCnew= abc:?", testAst("c = new(a ? b : c) C;"));

// invalid code (libreoffice), don't hang
Expand Down