Skip to content

Commit a2132d7

Browse files
committed
[Clang] Fix the do while statement disappearing in AST when an error 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
1 parent 50a76a7 commit a2132d7

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/lib/Parse/ParseStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,8 @@ StmtResult Parser::ParseDoStatement() {
18941894
ExprResult Cond = ParseExpression();
18951895
// Correct the typos in condition before closing the scope.
18961896
if (Cond.isUsable())
1897-
Cond = Actions.CorrectDelayedTyposInExpr(Cond);
1897+
Cond = Actions.CorrectDelayedTyposInExpr(Cond, /*InitDecl=*/nullptr,
1898+
/*RecoverUncorrectedTypos=*/true);
18981899
else {
18991900
if (!Tok.isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
19001901
SkipUntil(tok::semi);

clang/test/AST/ast-dump-recovery.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,15 @@ void RecoverToAnInvalidDecl() {
420420
// CHECK: RecoveryExpr {{.*}} '<dependent type>' contains-errors lvalue
421421
// CHECK-NEXT: `-DeclRefExpr {{.*}} 'foo' 'int *'
422422
}
423+
424+
void RecoveryToDoWhileStmtCond() {
425+
// CHECK: FunctionDecl {{.*}} RecoveryToDoWhileStmtCond
426+
// CHECK: `-DoStmt {{.*}}
427+
// CHECK-NEXT: |-CompoundStmt {{.*}}
428+
// CHECK-NEXT: `-BinaryOperator {{.*}} '<dependent type>' contains-errors '<'
429+
// CHECK-NEXT: |-BinaryOperator {{.*}} '<dependent type>' contains-errors '+'
430+
// CHECK-NEXT: | |-RecoveryExpr {{.*}} '<dependent type>' contains-errors lvalue
431+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 1
432+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 10
433+
do {} while (some_invalid_val + 1 < 10);
434+
}

clang/test/SemaCXX/constexpr-function-recovery-crash.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ TEST_EVALUATE(For, for (!!){}); // expected-error + {{}}
9595
TEST_EVALUATE(ForRange, for (auto x : !!){}); // expected-error + {{}}
9696
TEST_EVALUATE(While, while (!!){}); // expected-error + {{}}
9797
TEST_EVALUATE(DoWhile, do {} while (!!);); // expected-error + {{}}
98+
TEST_EVALUATE(DoWhileCond, do {} while (some_cond < 10);); // expected-error {{use of undeclared identifier}} \
99+
// expected-error {{constexpr variable 'forceEvaluateDoWhileCond' must be initialized by a constant expression}}
98100
TEST_EVALUATE(If, if (!!){};); // expected-error + {{}}
99101
TEST_EVALUATE(IfInit, if (auto x = !!; 1){};);// expected-error + {{}}
100102
TEST_EVALUATE(ForInit, if (!!;;){};); // expected-error + {{}}

0 commit comments

Comments
 (0)