[clang] c673d67 - [AST] Dont invalidate a ref-type var decl if it has no initializer.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed May 25 06:14:46 PDT 2022


Author: Haojian Wu
Date: 2022-05-25T15:14:35+02:00
New Revision: c673d67bc76bfa31b78e3eee23899b9f262158ea

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

LOG: [AST] Dont invalidate a ref-type var decl if it has no initializer.

This would allow more AST nodes being preserved for broken code, and
have a more consistent valid bit for ref-type var decl (currently, a
ref-type var decl with a broken initializer is valid).

Per https://reviews.llvm.org/D76831#1973053, the initializer of a variable
should play no part in its "invalid" bit.

Reviewed By: sammccall

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/AST/ast-dump-invalid-initialized.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b150a88e0aa21..c5d4075a49e2f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13199,7 +13199,6 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
     if (Type->isReferenceType()) {
       Diag(Var->getLocation(), diag::err_reference_var_requires_init)
           << Var << SourceRange(Var->getLocation(), Var->getLocation());
-      Var->setInvalidDecl();
       return;
     }
 

diff  --git a/clang/test/AST/ast-dump-invalid-initialized.cpp b/clang/test/AST/ast-dump-invalid-initialized.cpp
index 1b3c8581a94fb..1c374ae716a9d 100644
--- a/clang/test/AST/ast-dump-invalid-initialized.cpp
+++ b/clang/test/AST/ast-dump-invalid-initialized.cpp
@@ -12,10 +12,10 @@ void test() {
   const A a2;
   // CHECK: `-VarDecl {{.*}} a3 'A'
   A a3 = garbage();
+  // CHECK: `-VarDecl {{.*}} a4 'const A &'
+  const A& a4;
 
 
-  // CHECK: `-VarDecl {{.*}} invalid b1 'const A &'
-  const A& b1;
   // CHECK: `-VarDecl {{.*}} invalid b2 'ForwardDecl'
   ForwardDecl b2;
   // CHECK: `-VarDecl {{.*}} invalid b3 'auto'


        


More information about the cfe-commits mailing list