[clang] 1a0d466 - [AST] Preserve the invalid initializer for auto VarDecl.

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


Author: Haojian Wu
Date: 2020-04-27T10:25:36+02:00
New Revision: 1a0d466081318adfc356917fccc5116f9031ef7e

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

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

Fixes https://github.com/clangd/clangd/issues/330

Reviewers: sammccall

Tags: #clang

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 99644ff4f46f..5f11038d589e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11847,10 +11847,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     // 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))

diff  --git a/clang/test/AST/ast-dump-expr-errors.cpp b/clang/test/AST/ast-dump-expr-errors.cpp
index 9334b73a4354..5661a41465eb 100644
--- a/clang/test/AST/ast-dump-expr-errors.cpp
+++ b/clang/test/AST/ast-dump-expr-errors.cpp
@@ -40,10 +40,6 @@ int c = &(bar() + baz()) * 10;
 // 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))'

diff  --git a/clang/test/AST/ast-dump-recovery.cpp b/clang/test/AST/ast-dump-recovery.cpp
index 86511181f0a6..f0325b32dc14 100644
--- a/clang/test/AST/ast-dump-recovery.cpp
+++ b/clang/test/AST/ast-dump-recovery.cpp
@@ -156,3 +156,22 @@ void InvalidInitalizer(int x) {
   // 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.*[] {};
+}


        


More information about the cfe-commits mailing list