[clang] a2132d7 - [Clang] Fix the do while statement disappearing in AST when an error occurs in the conditional expression of the do while statement

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 8 04:35:01 PDT 2023


Author: yrong
Date: 2023-08-08T19:34:43+08:00
New Revision: a2132d72bed153a3347763dfaa7e699de7128647

URL: https://github.com/llvm/llvm-project/commit/a2132d72bed153a3347763dfaa7e699de7128647
DIFF: https://github.com/llvm/llvm-project/commit/a2132d72bed153a3347763dfaa7e699de7128647.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Parse/ParseStmt.cpp
    clang/test/AST/ast-dump-recovery.cpp
    clang/test/SemaCXX/constexpr-function-recovery-crash.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 2346470dbdb73d..bf295b8a0cb8a2 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -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);

diff  --git a/clang/test/AST/ast-dump-recovery.cpp b/clang/test/AST/ast-dump-recovery.cpp
index cc4f8afbfc4441..c882b4659a7310 100644
--- a/clang/test/AST/ast-dump-recovery.cpp
+++ b/clang/test/AST/ast-dump-recovery.cpp
@@ -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);
+}

diff  --git a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
index 8bada7c0b6df3e..16654104a9177e 100644
--- a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
+++ b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
@@ -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 + {{}}


        


More information about the cfe-commits mailing list