Skip to content

Commit

Permalink
[Clang] Fix the do while statement disappearing in AST when an error …
Browse files Browse the repository at this point in the history
…occurs in the conditional expression of the do while statement

```
constexpr int test() {
    do {} while (a + 1 < 10);
    return 0;
}
```
Before:
```
`-FunctionDecl 0x56512a172650 <./recovery.cpp:1:1, line:4:1> line:1:15 constexpr test 'int ()' implicit-inline
  `-CompoundStmt 0x56512a172860 <col:22, line:4:1>
    `-ReturnStmt 0x56512a172850 <line:3:5, col:12>
      `-IntegerLiteral 0x56512a172830 <col:12> 'int' 0
```
Now:
```
`-FunctionDecl 0x5642c4804650 <./recovery.cpp:1:1, line:4:1> line:1:15 constexpr test 'int ()' implicit-inline
  `-CompoundStmt 0x5642c48048e0 <col:22, line:4:1>
    |-DoStmt 0x5642c4804890 <line:2:5, col:28>
    | |-CompoundStmt 0x5642c4804740 <col:8, col:9>
    | `-BinaryOperator 0x5642c4804870 <col:18, col:26> '<dependent type>' contains-errors '<'
    |   |-BinaryOperator 0x5642c4804850 <col:18, col:22> '<dependent type>' contains-errors '+'
    |   | |-RecoveryExpr 0x5642c4804830 <col:18> '<dependent type>' contains-errors lvalue
    |   | `-IntegerLiteral 0x5642c48047b0 <col:22> 'int' 1
    |   `-IntegerLiteral 0x5642c48047f0 <col:26> 'int' 10
    `-ReturnStmt 0x5642c48048d0 <line:3:5, col:12>
      `-IntegerLiteral 0x5642c48048b0 <col:12> 'int' 0
```

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D157195
  • Loading branch information
yronglin committed Aug 8, 2023
1 parent 50a76a7 commit a2132d7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,8 @@ StmtResult Parser::ParseDoStatement() {
ExprResult Cond = ParseExpression();
// Correct the typos in condition before closing the scope.
if (Cond.isUsable())
Cond = Actions.CorrectDelayedTyposInExpr(Cond);
Cond = Actions.CorrectDelayedTyposInExpr(Cond, /*InitDecl=*/nullptr,
/*RecoverUncorrectedTypos=*/true);
else {
if (!Tok.isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
SkipUntil(tok::semi);
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ast-dump-recovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,15 @@ void RecoverToAnInvalidDecl() {
// CHECK: RecoveryExpr {{.*}} '<dependent type>' contains-errors lvalue
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'foo' 'int *'
}

void RecoveryToDoWhileStmtCond() {
// CHECK: FunctionDecl {{.*}} RecoveryToDoWhileStmtCond
// CHECK: `-DoStmt {{.*}}
// CHECK-NEXT: |-CompoundStmt {{.*}}
// CHECK-NEXT: `-BinaryOperator {{.*}} '<dependent type>' contains-errors '<'
// CHECK-NEXT: |-BinaryOperator {{.*}} '<dependent type>' contains-errors '+'
// CHECK-NEXT: | |-RecoveryExpr {{.*}} '<dependent type>' contains-errors lvalue
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 1
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 10
do {} while (some_invalid_val + 1 < 10);
}
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ TEST_EVALUATE(For, for (!!){}); // expected-error + {{}}
TEST_EVALUATE(ForRange, for (auto x : !!){}); // expected-error + {{}}
TEST_EVALUATE(While, while (!!){}); // expected-error + {{}}
TEST_EVALUATE(DoWhile, do {} while (!!);); // expected-error + {{}}
TEST_EVALUATE(DoWhileCond, do {} while (some_cond < 10);); // expected-error {{use of undeclared identifier}} \
// expected-error {{constexpr variable 'forceEvaluateDoWhileCond' must be initialized by a constant expression}}
TEST_EVALUATE(If, if (!!){};); // expected-error + {{}}
TEST_EVALUATE(IfInit, if (auto x = !!; 1){};);// expected-error + {{}}
TEST_EVALUATE(ForInit, if (!!;;){};); // expected-error + {{}}
Expand Down

0 comments on commit a2132d7

Please sign in to comment.