[clang] [clang][Sema] Improve error recovery for id-expressions referencing invalid decls (PR #81662)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 13 13:11:30 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Nathan Ridge (HighCommander4)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/81662.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaExpr.cpp (+1-1)
- (modified) clang/test/AST/ast-dump-recovery.cpp (+6)
``````````diff
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() {
``````````
</details>
https://github.com/llvm/llvm-project/pull/81662
More information about the cfe-commits
mailing list