CppEditor: Add support for init statements in if conditions

Fixes: QTCREATORBUG-29182
Change-Id: I9b7969da694b368236246123ad0028d8e754e903
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2023-05-24 15:18:48 +02:00
parent 07764bdf15
commit c8f29b9e01
9 changed files with 68 additions and 6 deletions

View File

@@ -1779,6 +1779,8 @@ public:
int if_token = 0;
int constexpr_token = 0;
int lparen_token = 0;
DeclarationAST *initDecl = nullptr;
StatementAST *initStmt = nullptr;
ExpressionAST *condition = nullptr;
int rparen_token = 0;
StatementAST *statement = nullptr;

View File

@@ -785,6 +785,10 @@ IfStatementAST *IfStatementAST::clone(MemoryPool *pool) const
ast->if_token = if_token;
ast->constexpr_token = constexpr_token;
ast->lparen_token = lparen_token;
if (initDecl)
ast->initDecl = initDecl->clone(pool);
if (initStmt)
ast->initStmt = initStmt->clone(pool);
if (condition)
ast->condition = condition->clone(pool);
ast->rparen_token = rparen_token;

View File

@@ -1318,6 +1318,16 @@ bool ASTMatcher::match(IfStatementAST *node, IfStatementAST *pattern)
pattern->lparen_token = node->lparen_token;
if (!pattern->initDecl)
pattern->initDecl = node->initDecl;
else if (!AST::match(node->initDecl, pattern->initDecl, this))
return false;
if (!pattern->initStmt)
pattern->initStmt = node->initStmt;
else if (!AST::match(node->initStmt, pattern->initStmt, this))
return false;
if (! pattern->condition)
pattern->condition = node->condition;
else if (! AST::match(node->condition, pattern->condition, this))

View File

@@ -563,6 +563,8 @@ void ForStatementAST::accept0(ASTVisitor *visitor)
void IfStatementAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
accept(initDecl, visitor);
accept(initStmt, visitor);
accept(condition, visitor);
accept(statement, visitor);
accept(else_statement, visitor);

View File

@@ -1528,6 +1528,10 @@ bool Bind::visit(IfStatementAST *ast)
ast->symbol = block;
Scope *previousScope = switchScope(block);
if (ast->initDecl)
this->declaration(ast->initDecl);
else if (ast->initStmt)
this->statement(ast->initStmt);
/*ExpressionTy condition =*/ this->expression(ast->condition);
this->statement(ast->statement);
this->statement(ast->else_statement);

View File

@@ -3511,12 +3511,14 @@ bool Parser::parseExpressionStatement(StatementAST *&node)
ExpressionAST *expression = nullptr;
if (parseExpression(expression)) {
ExpressionStatementAST *ast = new (previousPool) ExpressionStatementAST;
if (expression)
ast->expression = expression->clone(previousPool);
match(T_SEMICOLON, &ast->semicolon_token);
node = ast;
parsed = true;
if (LA() == T_SEMICOLON) {
ExpressionStatementAST *ast = new (previousPool) ExpressionStatementAST;
ast->semicolon_token = consumeToken();
if (expression)
ast->expression = expression->clone(previousPool);
node = ast;
parsed = true;
}
}
_inExpressionStatement = wasInExpressionStatement;
@@ -4072,6 +4074,19 @@ bool Parser::parseIfStatement(StatementAST *&node)
ast->constexpr_token = consumeToken();
}
match(T_LPAREN, &ast->lparen_token);
// C++17: init-statement
if (_languageFeatures.cxx17Enabled) {
const int savedCursor = cursor();
const bool savedBlockErrors = _translationUnit->blockErrors(true);
if (!parseSimpleDeclaration(ast->initDecl)) {
rewind(savedCursor);
if (!parseExpressionStatement(ast->initStmt))
rewind(savedCursor);
}
_translationUnit->blockErrors(savedBlockErrors);
}
parseCondition(ast->condition);
match(T_RPAREN, &ast->rparen_token);
if (! parseStatement(ast->statement))

View File

@@ -455,6 +455,7 @@ struct LanguageFeatures
unsigned int cxxEnabled : 1;
unsigned int cxx11Enabled : 1;
unsigned int cxx14Enabled : 1;
unsigned int cxx17Enabled : 1;
unsigned int cxx20Enabled : 1;
unsigned int objCEnabled : 1;
unsigned int c99Enabled : 1;