Fix internalAstError with new (#4508)

* Fix internalAstError with new

* Format

* nullptr check
This commit is contained in:
chrchr-github 2022-09-26 22:05:58 +02:00 committed by GitHub
parent ff4eb586ea
commit 0f79f8bd70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View File

@ -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)) {
@ -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);

View File

@ -6071,10 +6071,13 @@ private:
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