[clang] 684e416 - [AST][RecoveryExpr] Preserve the AST for invalid conditions.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 20 05:58:52 PDT 2020


Author: Haojian Wu
Date: 2020-07-20T14:58:36+02:00
New Revision: 684e416ef1361fc3cc93bebbee1466b844751497

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

LOG: [AST][RecoveryExpr] Preserve the AST for invalid conditions.

Adjust an existing diagnostic test, which is an improvement of secondary diagnostic.

Differential Revision: https://reviews.llvm.org/D81163

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/AST/ast-dump-recovery.cpp
    clang/test/SemaTemplate/instantiate-expr-3.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8fd0e2892b14..598aede14dd3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18458,9 +18458,12 @@ Sema::ConditionResult Sema::ActOnCondition(Scope *S, SourceLocation Loc,
     Cond = CheckSwitchCondition(Loc, SubExpr);
     break;
   }
-  if (Cond.isInvalid())
-    return ConditionError();
-
+  if (Cond.isInvalid()) {
+    Cond = CreateRecoveryExpr(SubExpr->getBeginLoc(), SubExpr->getEndLoc(),
+                              {SubExpr});
+    if (!Cond.get())
+      return ConditionError();
+  }
   // FIXME: FullExprArg doesn't have an invalid bit, so check nullness instead.
   FullExprArg FullExpr = MakeFullExpr(Cond.get(), Loc);
   if (!FullExpr.get())

diff  --git a/clang/test/AST/ast-dump-recovery.cpp b/clang/test/AST/ast-dump-recovery.cpp
index cbae3fddf21e..4d11bb29d5bf 100644
--- a/clang/test/AST/ast-dump-recovery.cpp
+++ b/clang/test/AST/ast-dump-recovery.cpp
@@ -229,3 +229,30 @@ void ValueCategory() {
   // CHECK:  RecoveryExpr {{.*}} 'int' contains-errors xvalue
   xvalue(); // call to a function (rvalue reference return type) yields an xvalue.
 }
+
+void InvalidCondition() {
+  // CHECK:      IfStmt {{.*}}
+  // CHECK-NEXT: |-RecoveryExpr {{.*}} <col:7, col:15> '<dependent type>' contains-errors
+  // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} <col:7>
+  if (invalid()) {}
+
+  // CHECK:      WhileStmt {{.*}}
+  // CHECK-NEXT: |-RecoveryExpr {{.*}} <col:10, col:18> '<dependent type>' contains-errors
+  // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} <col:10>
+  while (invalid()) {}
+
+  // CHECK:      SwitchStmt {{.*}}
+  // CHECK-NEXT: |-RecoveryExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} <col:10>
+  switch(invalid()) {
+    case 1:
+      break;
+  }
+  // FIXME: figure out why the type of ConditionalOperator is not int.
+  // CHECK:      ConditionalOperator {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT: |-RecoveryExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}}
+  // CHECK-NEXT: |-IntegerLiteral {{.*}} 'int' 1
+  // CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 2
+  invalid() ? 1 : 2;
+}

diff  --git a/clang/test/SemaTemplate/instantiate-expr-3.cpp b/clang/test/SemaTemplate/instantiate-expr-3.cpp
index 142e4ebcedc6..689fd22ea448 100644
--- a/clang/test/SemaTemplate/instantiate-expr-3.cpp
+++ b/clang/test/SemaTemplate/instantiate-expr-3.cpp
@@ -65,7 +65,7 @@ struct StatementExpr0 {
   void f(T t) {
     (void)({
         if (t) // expected-error{{contextually convertible}}
-          t = t + 17;
+          t = t + 17; // expected-error {{invalid operands to binary expression ('N1::X' and 'int')}}
         t + 12; // expected-error{{invalid operands}}
       });
   }


        


More information about the cfe-commits mailing list