[clang] [clang][Sema] Preserve the initializer of invalid VarDecls (PR #88645)
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 13 23:43:43 PDT 2024
https://github.com/HighCommander4 created https://github.com/llvm/llvm-project/pull/88645
Fixes https://github.com/clangd/clangd/issues/1821
>From 42ee794d3de89241fae7314db2202cac32af469b Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Sun, 14 Apr 2024 02:41:48 -0400
Subject: [PATCH] [clang][Sema] Preserve the initializer of invalid VarDecls
Fixes https://github.com/clangd/clangd/issues/1821
---
clang/lib/Sema/SemaDecl.cpp | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9fdd8eb236d1ee..7e13cb6e8fc59c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13435,8 +13435,7 @@ void Sema::checkNonTrivialCUnion(QualType QT, SourceLocation Loc,
void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
// If there is no declaration, there was an error parsing it. Just ignore
// the initializer.
- if (!RealDecl || RealDecl->isInvalidDecl()) {
- CorrectDelayedTyposInExpr(Init, dyn_cast_or_null<VarDecl>(RealDecl));
+ if (!RealDecl) {
return;
}
@@ -13456,6 +13455,14 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
return;
}
+ if (VDecl->isInvalidDecl()) {
+ CorrectDelayedTyposInExpr(Init, VDecl);
+ VDecl->setInit(
+ CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), {Init})
+ .get());
+ return;
+ }
+
// WebAssembly tables can't be used to initialise a variable.
if (Init && !Init->getType().isNull() &&
Init->getType()->isWebAssemblyTableType()) {
More information about the cfe-commits
mailing list