[clang] [clang][Sema] Improve error recovery for id-expressions referencing invalid decls (PR #81662)
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 13 13:11:00 PST 2024
https://github.com/HighCommander4 created https://github.com/llvm/llvm-project/pull/81662
Passing AcceptInvalidDecl=true to BuildDeclarationNameExpr() allows the RecoveryExpr that's constructed to retain a DeclRefExpr pointing to the invalid decl as a child, preserving information about the reference for use by tools such as clangd.
Fixes https://github.com/clangd/clangd/issues/1820
>From 81f473e30ea8eed48687c1cdfba20522fe20388d Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Tue, 13 Feb 2024 12:26:17 -0500
Subject: [PATCH] [clang][Sema] Improve error recovery for id-expressions
referencing invalid decls
Passing AcceptInvalidDecl=true to BuildDeclarationNameExpr() allows
the RecoveryExpr that's constructed to retain a DeclRefExpr pointing
to the invalid decl as a child, preserving information about the
reference for use by tools such as clangd.
Fixes https://github.com/clangd/clangd/issues/1820
---
clang/lib/Sema/SemaExpr.cpp | 2 +-
clang/test/AST/ast-dump-recovery.cpp | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4049ab3bf6cafb..3030d388147b24 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2927,7 +2927,7 @@ Sema::ActOnIdExpression(Scope *S, CXXScopeSpec &SS,
return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs);
}
- return BuildDeclarationNameExpr(SS, R, ADL);
+ return BuildDeclarationNameExpr(SS, R, ADL, /*AcceptInvalidDecl=*/true);
}
/// BuildQualifiedDeclarationNameExpr - Build a C++ qualified
diff --git a/clang/test/AST/ast-dump-recovery.cpp b/clang/test/AST/ast-dump-recovery.cpp
index cfb013585ad744..f628fa913da1e6 100644
--- a/clang/test/AST/ast-dump-recovery.cpp
+++ b/clang/test/AST/ast-dump-recovery.cpp
@@ -402,6 +402,7 @@ void returnInitListFromVoid() {
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 8
}
+void FuncTakingUnknown(Unknown);
void RecoveryExprForInvalidDecls(Unknown InvalidDecl) {
InvalidDecl + 1;
// CHECK: BinaryOperator {{.*}}
@@ -411,6 +412,11 @@ void RecoveryExprForInvalidDecls(Unknown InvalidDecl) {
InvalidDecl();
// CHECK: CallExpr {{.*}}
// CHECK-NEXT: `-RecoveryExpr {{.*}} '<dependent type>'
+ FuncTakingUnknown(InvalidDecl);
+ // CHECK: CallExpr {{.*}} '<dependent type>'
+ // CHECK-NEXT: |-UnresolvedLookupExpr {{.*}} '<overloaded function type>'
+ // CHECK-NEXT: `-RecoveryExpr {{.*}} '<dependent type>'
+ // CHECK-NEXT: `-DeclRefExpr {{.*}} 'InvalidDecl' 'int'
}
void RecoverToAnInvalidDecl() {
More information about the cfe-commits
mailing list