CppEditor: Revert changes to parseExpressionStatement()

Amends c8f29b9e01.
It turns out that there are contexts where we want to parse an
expression statement even with the semicolon missing (e.g. completion on
incomplete code).
So leave the existing functions unchanged and do the thorough check
afterwards in parseIfStatement().

Change-Id: Id6209ef1abfe9d155c5b9381e6ae655cc721feb2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2023-05-26 14:10:38 +02:00
parent 809f7c6cfd
commit fb59c70dcb
6 changed files with 23 additions and 24 deletions

View File

@@ -3511,14 +3511,12 @@ bool Parser::parseExpressionStatement(StatementAST *&node)
ExpressionAST *expression = nullptr;
if (parseExpression(expression)) {
if (LA() == T_SEMICOLON) {
ExpressionStatementAST *ast = new (previousPool) ExpressionStatementAST;
ast->semicolon_token = consumeToken();
if (expression)
ast->expression = expression->clone(previousPool);
node = ast;
parsed = true;
}
ExpressionStatementAST *ast = new (previousPool) ExpressionStatementAST;
if (expression)
ast->expression = expression->clone(previousPool);
match(T_SEMICOLON, &ast->semicolon_token);
node = ast;
parsed = true;
}
_inExpressionStatement = wasInExpressionStatement;
@@ -4079,14 +4077,26 @@ bool Parser::parseIfStatement(StatementAST *&node)
if (_languageFeatures.cxx17Enabled) {
const int savedCursor = cursor();
const bool savedBlockErrors = _translationUnit->blockErrors(true);
if (!parseSimpleDeclaration(ast->initDecl)) {
bool foundInitStmt = parseExpressionOrDeclarationStatement(ast->initStmt);
if (foundInitStmt)
foundInitStmt = ast->initStmt;
if (foundInitStmt) {
if (const auto exprStmt = ast->initStmt->asExpressionStatement()) {
foundInitStmt = exprStmt->semicolon_token;
} else if (const auto declStmt = ast->initStmt->asDeclarationStatement()) {
foundInitStmt = declStmt->declaration
&& declStmt->declaration->asSimpleDeclaration()
&& declStmt->declaration->asSimpleDeclaration()->semicolon_token;
} else {
foundInitStmt = false;
}
}
if (!foundInitStmt) {
ast->initStmt = nullptr;
rewind(savedCursor);
if (!parseExpressionStatement(ast->initStmt))
rewind(savedCursor);
}
_translationUnit->blockErrors(savedBlockErrors);
}
parseCondition(ast->condition);
match(T_RPAREN, &ast->rparen_token);
if (! parseStatement(ast->statement))