diff options
author | Nathan Ridge <zeratul976@hotmail.com> | 2024-04-24 20:31:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-24 20:31:18 -0400 |
commit | d5308949cf884d8e4b971d51a8b4f73584c4adec (patch) | |
tree | 3c2a73300ff60011b2320522e029ecc70958a63a /clang/lib/Sema/SemaDecl.cpp | |
parent | b9f2c16b50f68c978e90190f46a7c0db3f39e98c (diff) | |
download | llvm-d5308949cf884d8e4b971d51a8b4f73584c4adec.zip llvm-d5308949cf884d8e4b971d51a8b4f73584c4adec.tar.gz llvm-d5308949cf884d8e4b971d51a8b4f73584c4adec.tar.bz2 |
[clang][Sema] Preserve the initializer of invalid VarDecls (#88645)
Fixes https://github.com/clangd/clangd/issues/1821
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 37861549..e0745fe9 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13502,16 +13502,18 @@ 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()) { + if (!RealDecl) { CorrectDelayedTyposInExpr(Init, dyn_cast_or_null<VarDecl>(RealDecl)); return; } - if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { - // Pure-specifiers are handled in ActOnPureSpecifier. - Diag(Method->getLocation(), diag::err_member_function_initialization) - << Method->getDeclName() << Init->getSourceRange(); - Method->setInvalidDecl(); + if (auto *Method = dyn_cast<CXXMethodDecl>(RealDecl)) { + if (!Method->isInvalidDecl()) { + // Pure-specifiers are handled in ActOnPureSpecifier. + Diag(Method->getLocation(), diag::err_member_function_initialization) + << Method->getDeclName() << Init->getSourceRange(); + Method->setInvalidDecl(); + } return; } @@ -13523,6 +13525,15 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { return; } + if (VDecl->isInvalidDecl()) { + CorrectDelayedTyposInExpr(Init, VDecl); + ExprResult Recovery = + CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), {Init}); + if (Expr *E = Recovery.get()) + VDecl->setInit(E); + return; + } + // WebAssembly tables can't be used to initialise a variable. if (Init && !Init->getType().isNull() && Init->getType()->isWebAssemblyTableType()) { |