Skip to content

Commit

Permalink
Handle invalid empty c++17 fold expression early
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
i-garrison authored and jonahgraham committed Mar 16, 2023
1 parent 6ea3d70 commit 0923e66
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,14 @@ public void testFoldExpressionRecognitionSuccess() throws Exception {
// (... + 1 * vals);
// (vals + ... + 1 * 2);
// (1 * 2 + ... + vals);
// (...);
// }
public void testFoldExpressionErrors() throws Exception {
final String code = getAboveComment();
IASTTranslationUnit tu = parse(code, CPP, ScannerKind.STD, false);
ICPPASTTemplateDeclaration tdef = getDeclaration(tu, 0);
IASTFunctionDefinition fdef = (IASTFunctionDefinition) tdef.getDeclaration();
for (int i = 0; i < 13; ++i) {
for (int i = 0; i < 14; ++i) {
IASTProblemExpression e = getExpressionOfStatement(fdef, i);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ private int calculateFirstOffset(BinaryOperator leftChain, IASTInitializerClause

@Override
public final IASTExpression buildExpression(BinaryOperator leftChain, IASTInitializerClause expr) {
if (supportFoldExpression && leftChain != null && expr != null) {
if (supportFoldExpression && expr != null) {
int foldCount = 0;
int foldOpToken = 0;

Expand Down Expand Up @@ -1321,7 +1321,7 @@ public final IASTExpression buildExpression(BinaryOperator leftChain, IASTInitia

// Valid fold-expression has single ellipsis.

if (foldCount == 1) {
if (foldCount == 1 && leftChain != null) {
BinaryOperator rightChain;
if (foldOp == null) {
// unary right fold, remove expression and use left chain as is
Expand Down

0 comments on commit 0923e66

Please sign in to comment.