[clang] [Clang] Fix crash when a local extern's previous declaration is in a discarded if `constexpr` branch (PR #233389)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 08:57:20 PST 2026
================
@@ -6513,13 +6513,31 @@ void Sema::BuildVariableInstantiation(
NewVar->isLocalExternDecl() ? RedeclarationKind::ForExternalRedeclaration
: forRedeclarationInCurContext());
- if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() &&
- (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() ||
- OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) {
+ VarDecl *PatternPrev = nullptr;
+ if (NewVar->isLocalExternDecl()) {
+ for (VarDecl *Prev = OldVar->getPreviousDecl(); Prev;
+ Prev = Prev->getPreviousDecl()) {
+ DeclContext *PrevDC = Prev->getDeclContext();
+ if (!PrevDC->isDependentContext()) {
+ PatternPrev = Prev;
+ break;
+ }
+ if (PrevDC != OldVar->getDeclContext())
+ break;
+ if (CurrentInstantiationScope &&
+ CurrentInstantiationScope->getInstantiationOfIfExists(Prev)) {
----------------
erichkeane wrote:
This whole section seems wrong... Why are we trying to get the instantiation only to re-look it up? Also, searching as it is doing in contexts is wrong.
Also, this is only applying to local-externs, why can't this happen elsewise? There's likely other cases where this sort of thing can happen.
I suspect the real answer here is to make sure we set up the 'previous' decl correctly. I find myself wondering if the discarded-statements should be the actual 'previous' decl here, or if that causes a branch of some sort...
https://github.com/llvm/llvm-project/pull/233389
More information about the cfe-commits
mailing list