1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Handle invalid empty c++17 fold expression early

One of Qt sample snippets contain (...) as placeholder for actual user code.
Parsing that leads to invalid class cast because buildExpression would return
fold expression token marker and not a proper ICPPASTexpression.

Fix that by handling this error early as invalid fold expression.
This commit is contained in:
Igor V. Kovalenko 2023-03-12 09:50:18 +03:00 committed by Jonah Graham
parent 6ea3d70456
commit 0923e66b7b
2 changed files with 4 additions and 3 deletions

View file

@ -164,13 +164,14 @@ public class FoldExpressionTests extends AST2CPPTestBase {
// (... + 1 * vals); // (... + 1 * vals);
// (vals + ... + 1 * 2); // (vals + ... + 1 * 2);
// (1 * 2 + ... + vals); // (1 * 2 + ... + vals);
// (...);
// } // }
public void testFoldExpressionErrors() throws Exception { public void testFoldExpressionErrors() throws Exception {
final String code = getAboveComment(); final String code = getAboveComment();
IASTTranslationUnit tu = parse(code, CPP, ScannerKind.STD, false); IASTTranslationUnit tu = parse(code, CPP, ScannerKind.STD, false);
ICPPASTTemplateDeclaration tdef = getDeclaration(tu, 0); ICPPASTTemplateDeclaration tdef = getDeclaration(tu, 0);
IASTFunctionDefinition fdef = (IASTFunctionDefinition) tdef.getDeclaration(); IASTFunctionDefinition fdef = (IASTFunctionDefinition) tdef.getDeclaration();
for (int i = 0; i < 13; ++i) { for (int i = 0; i < 14; ++i) {
IASTProblemExpression e = getExpressionOfStatement(fdef, i); IASTProblemExpression e = getExpressionOfStatement(fdef, i);
} }
} }

View file

@ -1287,7 +1287,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
@Override @Override
public final IASTExpression buildExpression(BinaryOperator leftChain, IASTInitializerClause expr) { public final IASTExpression buildExpression(BinaryOperator leftChain, IASTInitializerClause expr) {
if (supportFoldExpression && leftChain != null && expr != null) { if (supportFoldExpression && expr != null) {
int foldCount = 0; int foldCount = 0;
int foldOpToken = 0; int foldOpToken = 0;
@ -1321,7 +1321,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
// Valid fold-expression has single ellipsis. // Valid fold-expression has single ellipsis.
if (foldCount == 1) { if (foldCount == 1 && leftChain != null) {
BinaryOperator rightChain; BinaryOperator rightChain;
if (foldOp == null) { if (foldOp == null) {
// unary right fold, remove expression and use left chain as is // unary right fold, remove expression and use left chain as is