[clang] 706a435 - [AST][RecoveryExpr] Error-dependent expression should not be treat as a nullptr pointer constant.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 22 01:04:24 PDT 2020


Author: Haojian Wu
Date: 2020-07-22T10:03:51+02:00
New Revision: 706a4353e87b1127446db7daf5e3e95fcb408924

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

LOG: [AST][RecoveryExpr] Error-dependent expression should not be treat as a nullptr pointer constant.

If an expression is contains-error and its type is unknown (dependent), we
don't treat it as a null pointer constant.

Fix a recovery-ast crash on C.

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

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/lib/Sema/Sema.cpp
    clang/test/AST/ast-dump-recovery.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 399e7e13c445..213a6d1d1caa 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3740,6 +3740,9 @@ Expr::isNullPointerConstant(ASTContext &Ctx,
                             NullPointerConstantValueDependence NPC) const {
   if (isValueDependent() &&
       (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
+    // Error-dependent expr should never be a null pointer.
+    if (containsErrors())
+      return NPCK_NotNull;
     switch (NPC) {
     case NPC_NeverValueDependent:
       llvm_unreachable("Unexpected value dependent expression!");

diff  --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 735349c3de62..9c8f3fdcda4a 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -539,8 +539,10 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
   if (VK == VK_RValue && !E->isRValue()) {
     switch (Kind) {
     default:
-      llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast "
-                       "kind");
+      llvm_unreachable(("can't implicitly cast lvalue to rvalue with this cast "
+                        "kind: " +
+                        std::string(CastExpr::getCastKindName(Kind)))
+                           .c_str());
     case CK_Dependent:
     case CK_LValueToRValue:
     case CK_ArrayToPointerDecay:

diff  --git a/clang/test/AST/ast-dump-recovery.c b/clang/test/AST/ast-dump-recovery.c
index b49c0103dbaf..f3a33fdac49b 100644
--- a/clang/test/AST/ast-dump-recovery.c
+++ b/clang/test/AST/ast-dump-recovery.c
@@ -39,3 +39,14 @@ void test1() {
   // CHECK-NEXT:  `-DeclRefExpr {{.*}} 'a' 'const int'
   static int foo = a++; // verify no crash on local static var decl.
 }
+
+void test2() {
+  int* ptr;
+  // FIXME: the top-level expr should be a binary operator.
+  // CHECK:      ImplicitCastExpr {{.*}} contains-errors <LValueToRValue>
+  // CHECK-NEXT: `-RecoveryExpr {{.*}} contains-errors lvalue
+  // CHECK-NEXT:   |-DeclRefExpr {{.*}} 'ptr' 'int *'
+  // CHECK-NEXT:   `-RecoveryExpr {{.*}}
+  // CHECK-NEXT:     `-DeclRefExpr {{.*}} 'some_func'
+  ptr = some_func(); // should not crash
+}


        


More information about the cfe-commits mailing list