AST: Better handling of C++11 initializer lists

This commit is contained in:
Daniel Marjamäki 2017-06-09 22:35:46 +02:00
parent 7a0f05aaa5
commit 453058c059
2 changed files with 19 additions and 13 deletions

View File

@ -405,21 +405,19 @@ static Token * findCppTypeInitPar(Token *tok)
// X{} X<Y>{} etc // X{} X<Y>{} etc
static bool iscpp11init(const Token * const tok) static bool iscpp11init(const Token * const tok)
{ {
const Token *nameToken = nullptr; const Token *nameToken = tok;
if (tok->isName()) while (nameToken && nameToken->str() == "{") {
nameToken = tok; nameToken = nameToken->previous();
else if (Token::Match(tok->previous(), "%name% {")) if (nameToken && nameToken->str() == "," && Token::simpleMatch(nameToken->previous(), "} ,"))
nameToken = tok->previous(); nameToken = nameToken->linkAt(-1);
else if (tok->linkAt(-1) && Token::simpleMatch(tok->previous(), "> {") && Token::Match(tok->linkAt(-1)->previous(),"%name% <")) }
nameToken = tok->linkAt(-1)->previous();
if (!nameToken) if (!nameToken)
return false; return false;
if (nameToken->str() == ">" && nameToken->link())
if (Token::Match(nameToken, "%name% { [")) nameToken = nameToken->link()->previous();
return false;
const Token *endtok = nullptr; const Token *endtok = nullptr;
if (Token::Match(nameToken,"%name% {")) if (Token::Match(nameToken, "%name% { !!["))
endtok = nameToken->linkAt(1); endtok = nameToken->linkAt(1);
else if (Token::Match(nameToken,"%name% <") && Token::simpleMatch(nameToken->linkAt(1),"> {")) else if (Token::Match(nameToken,"%name% <") && Token::simpleMatch(nameToken->linkAt(1),"> {"))
endtok = nameToken->linkAt(1)->linkAt(1); endtok = nameToken->linkAt(1)->linkAt(1);
@ -543,8 +541,14 @@ static void compileTerm(Token *&tok, AST_state& state)
prev = prev->link()->previous(); prev = prev->link()->previous();
if (Token::simpleMatch(tok->link(),"} [")) { if (Token::simpleMatch(tok->link(),"} [")) {
tok = tok->next(); tok = tok->next();
} else if (tok->previous() && tok->previous()->isName()) { } else if (state.cpp && iscpp11init(tok)) {
if (state.op.empty() || Token::Match(tok->previous(), "[{,]"))
compileUnaryOp(tok, state, compileExpression);
else
compileBinOp(tok, state, compileExpression); compileBinOp(tok, state, compileExpression);
if (Token::simpleMatch(tok, "} ,")) {
tok = tok->next();
}
} else if (!state.inArrayAssignment && !Token::simpleMatch(prev, "=")) { } else if (!state.inArrayAssignment && !Token::simpleMatch(prev, "=")) {
state.op.push(tok); state.op.push(tok);
tok = tok->link()->next(); tok = tok->link()->next();

View File

@ -8192,6 +8192,8 @@ private:
ASSERT_EQUALS("abc12,{d:?=", testAst("a=b?c<X>{1,2}:d;")); ASSERT_EQUALS("abc12,{d:?=", testAst("a=b?c<X>{1,2}:d;"));
ASSERT_EQUALS("a::12,{", testAst("::a{1,2};")); // operator precedence ASSERT_EQUALS("a::12,{", testAst("::a{1,2};")); // operator precedence
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{b{,{return", testAst("return{{a},{b}};"));
} }
void astbrackets() { // [] void astbrackets() { // []