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
27 changes: 24 additions & 3 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ static void compileTerm(Token *&tok, AST_state& state)
prev = prev->link()->previous();
if (Token::simpleMatch(tok->link(),"} [")) {
tok = tok->next();
} else if (state.cpp && iscpp11init(tok)) {
} else if ((state.cpp && iscpp11init(tok)) || Token::simpleMatch(tok->previous(), "] {")) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the new check be inside iscpp11init()?

Copy link
Copy Markdown
Collaborator Author

@danmar danmar Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not c++11 initialization

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int a[2] { 0, 0 }; isn't valid in either C or C++03, unless I'm missing something.

Copy link
Copy Markdown
Collaborator Author

@danmar danmar Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrchr-github Your example is not valid that is correct. The code we want to handle is for example:

auto p = new int*[2]{new int, new int};

So the code we want to handle is not a variable initialisation at all. There is no variable initialization in the RHS..

Token *const end = tok->link();
if (state.op.empty() || Token::Match(tok->previous(), "[{,]") || Token::Match(tok->tokAt(-2), "%name% (")) {
if (Token::Match(tok, "{ . %name% =|{")) {
Expand Down Expand Up @@ -849,6 +849,17 @@ static void compileScope(Token *&tok, AST_state& state)

static bool isPrefixUnary(const Token* tok, bool cpp)
{
if (cpp && Token::simpleMatch(tok->previous(), "* [") && Token::simpleMatch(tok->link(), "] {")) {
for (const Token* prev = tok->previous(); Token::Match(prev, "%name%|::|*|&|>|>>"); prev = prev->previous()) {
if (Token::Match(prev, ">|>>")) {
if (!prev->link())
break;
prev = prev->link();
}
if (prev->str() == "new")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a check for cpp?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I'll fix

return false;
}
}
if (!tok->previous()
|| ((Token::Match(tok->previous(), "(|[|{|%op%|;|?|:|,|.|return|::") || (cpp && tok->strAt(-1) == "throw"))
&& (tok->previous()->tokType() != Token::eIncDecOp || tok->tokType() == Token::eIncDecOp)))
Expand Down Expand Up @@ -944,8 +955,18 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
}

Token* const tok2 = tok;
if (tok->strAt(1) != "]")
if (tok->strAt(1) != "]") {
compileBinOp(tok, state, compileExpression);
if (Token::Match(tok2->previous(), "%type%|* [") && Token::Match(tok, "] { !!}")) {
tok = tok->next();
Token* const tok3 = tok;
compileBinOp(tok, state, compileExpression);
if (tok != tok3->link())
throw InternalError(tok, "Syntax error in {..}", InternalError::AST);
tok = tok->next();
continue;
}
}
else
compileUnaryOp(tok, state, compileExpression);
tok = tok2->link()->next();
Expand Down Expand Up @@ -1072,7 +1093,7 @@ static void compilePrecedence3(Token *&tok, AST_state& state)
state.op.push(tok->next());
tok = tok->link()->next();
compileBinOp(tok, state, compilePrecedence2);
} else if (tok && (tok->str() == "[" || tok->str() == "(" || tok->str() == "{"))
} else if (Token::Match(tok, "(|{|["))
compilePrecedence2(tok, state);
else if (innertype && Token::simpleMatch(tok, ") [")) {
tok = tok->next();
Expand Down
1 change: 1 addition & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6166,6 +6166,7 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("intnewdelete", testAst("delete new int;")); // #11039
ASSERT_EQUALS("intnewdelete", testAst("void f() { delete new int; }"));
ASSERT_EQUALS("pint3[new1+=", testAst("p = (new int[3]) + 1;")); // #11327
ASSERT_EQUALS("aType2[T1T2,{new=", testAst("a = new Type *[2] {T1, T2};")); // #11745

// placement new
ASSERT_EQUALS("X12,3,(new ab,c,", testAst("new (a,b,c) X(1,2,3);"));
Expand Down