[PATCH] D78365: [AST] Preserve the invalid initializer for auto VarDecl.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 27 01:34:12 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG1a0d46608131: [AST] Preserve the invalid initializer for auto VarDecl. (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D78365?vs=258309&id=260241#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78365/new/

https://reviews.llvm.org/D78365

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/AST/ast-dump-expr-errors.cpp
  clang/test/AST/ast-dump-recovery.cpp


Index: clang/test/AST/ast-dump-recovery.cpp
===================================================================
--- clang/test/AST/ast-dump-recovery.cpp
+++ clang/test/AST/ast-dump-recovery.cpp
@@ -156,3 +156,22 @@
   // CHECK-NEXT:     `-UnresolvedLookupExpr {{.*}} 'invalid'
   Bar b6 = Bar{invalid()};
 }
+void InitializerForAuto() {
+  // CHECK:     `-VarDecl {{.*}} invalid a 'auto'
+  // CHECK-NEXT: `-RecoveryExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT:   `-UnresolvedLookupExpr {{.*}} 'invalid'
+  auto a = invalid();
+
+  // CHECK:     `-VarDecl {{.*}} invalid b 'auto'
+  // CHECK-NEXT: `-CallExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT:   |-UnresolvedLookupExpr {{.*}} 'some_func'
+  // CHECK-NEXT:   `-RecoveryExpr {{.*}} '<dependent type>' contains-errors
+  // CHECK-NEXT:     `-UnresolvedLookupExpr {{.*}} 'invalid'
+  auto b = some_func(invalid());
+
+  decltype(ned);
+  // very bad initailizer: there is an unresolved typo expr internally, we just
+  // drop it.
+  // CHECK: `-VarDecl {{.*}} invalid unresolved_typo 'auto'
+  auto unresolved_typo = gned.*[] {};
+}
Index: clang/test/AST/ast-dump-expr-errors.cpp
===================================================================
--- clang/test/AST/ast-dump-expr-errors.cpp
+++ clang/test/AST/ast-dump-expr-errors.cpp
@@ -40,10 +40,6 @@
 // CHECK-NEXT:|     `-IntegerLiteral {{.*}} 1
 int d = static_cast<int>(bar() + 1);
 
-// FIXME: store initializer even when 'auto' could not be deduced.
-// Expressions with errors currently do not keep initializers around.
-// CHECK: -VarDecl {{.*}} invalid e 'auto'
-auto e = bar();
 
 // Error type should result in an invalid decl.
 // CHECK: -VarDecl {{.*}} invalid f 'decltype(<recovery-expr>(bar))'
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -11847,10 +11847,18 @@
     // be deduced based on the chosen correction if the original init contains a
     // TypoExpr.
     ExprResult Res = CorrectDelayedTyposInExpr(Init, VDecl);
-    if (!Res.isUsable() || Res.get()->containsErrors()) {
+    if (!Res.isUsable()) {
+      // There are unresolved typos in Init, just drop them.
+      // FIXME: improve the recovery strategy to preserve the Init.
       RealDecl->setInvalidDecl();
       return;
     }
+    if (Res.get()->containsErrors()) {
+      // Invalidate the decl as we don't know the type for recovery-expr yet.
+      RealDecl->setInvalidDecl();
+      VDecl->setInit(Res.get());
+      return;
+    }
     Init = Res.get();
 
     if (DeduceVariableDeclarationType(VDecl, DirectInit, Init))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78365.260241.patch
Type: text/x-patch
Size: 2684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200427/f0cd6a5d/attachment.bin>


More information about the cfe-commits mailing list