@@ -213,19 +213,32 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
213213
214214 // Register all previous declarations as well. For extern blocks, just replace
215215 // the index with the new variable.
216- if (auto Idx =
217- createGlobal (VD, VD->getType (), IsStatic, IsExtern, IsWeak, Init)) {
218- for (const Decl *P = VD; P; P = P->getPreviousDecl ()) {
219- unsigned &PIdx = GlobalIndices[P];
220- if (P != VD) {
221- if (Globals[PIdx]->block ()->isExtern ())
222- Globals[PIdx] = Globals[*Idx];
216+ std::optional<unsigned > Idx =
217+ createGlobal (VD, VD->getType (), IsStatic, IsExtern, IsWeak, Init);
218+ if (!Idx)
219+ return std::nullopt ;
220+
221+ Global *NewGlobal = Globals[*Idx];
222+ for (const Decl *Redecl : VD->redecls ()) {
223+ unsigned &PIdx = GlobalIndices[Redecl];
224+ if (Redecl != VD) {
225+ if (Block *RedeclBlock = Globals[PIdx]->block ();
226+ RedeclBlock->isExtern ()) {
227+ Globals[PIdx] = NewGlobal;
228+ // All pointers pointing to the previous extern decl now point to the
229+ // new decl.
230+ for (Pointer *Ptr = RedeclBlock->Pointers ; Ptr;
231+ Ptr = Ptr->PointeeStorage .BS .Next ) {
232+ RedeclBlock->removePointer (Ptr);
233+ Ptr->PointeeStorage .BS .Pointee = NewGlobal->block ();
234+ NewGlobal->block ()->addPointer (Ptr);
235+ }
223236 }
224- PIdx = *Idx;
225237 }
226- return *Idx;
238+ PIdx = *Idx;
227239 }
228- return std::nullopt ;
240+
241+ return *Idx;
229242}
230243
231244std::optional<unsigned > Program::createGlobal (const Expr *E) {
@@ -264,7 +277,7 @@ std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,
264277 Ctx.getEvalID (), getCurrentDecl (), Desc, IsStatic, IsExtern, IsWeak);
265278 G->block ()->invokeCtor ();
266279
267- // Initialize InlineDescriptor fields.
280+ // Initialize GlobalInlineDescriptor fields.
268281 auto *GD = new (G->block ()->rawData ()) GlobalInlineDescriptor ();
269282 if (!Init)
270283 GD->InitState = GlobalInitState::NoInitializer;
0 commit comments