@@ -1789,6 +1789,47 @@ class DeferredDiagnosticsEmitter
17891789 Inherited::visitUsedDecl (Loc, D);
17901790 }
17911791
1792+ // Visitor member and parent dtors called by this dtor.
1793+ void VisitCalledDestructors (CXXDestructorDecl *DD) {
1794+ const CXXRecordDecl *RD = DD->getParent ();
1795+
1796+ // Visit the dtors of all members
1797+ for (const FieldDecl *FD : RD->fields ()) {
1798+ QualType FT = FD->getType ();
1799+ if (const auto *RT = FT->getAs <RecordType>())
1800+ if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl ()))
1801+ if (ClassDecl->hasDefinition ())
1802+ if (CXXDestructorDecl *MemberDtor = ClassDecl->getDestructor ())
1803+ asImpl ().visitUsedDecl (MemberDtor->getLocation (), MemberDtor);
1804+ }
1805+
1806+ // Also visit base class dtors
1807+ for (const auto &Base : RD->bases ()) {
1808+ QualType BaseType = Base.getType ();
1809+ if (const auto *RT = BaseType->getAs <RecordType>())
1810+ if (const auto *BaseDecl = dyn_cast<CXXRecordDecl>(RT->getDecl ()))
1811+ if (BaseDecl->hasDefinition ())
1812+ if (CXXDestructorDecl *BaseDtor = BaseDecl->getDestructor ())
1813+ asImpl ().visitUsedDecl (BaseDtor->getLocation (), BaseDtor);
1814+ }
1815+ }
1816+
1817+ void VisitDeclStmt (DeclStmt *DS) {
1818+ // Visit dtors called by variables that need destruction
1819+ for (auto *D : DS->decls ())
1820+ if (auto *VD = dyn_cast<VarDecl>(D))
1821+ if (VD->isThisDeclarationADefinition () &&
1822+ VD->needsDestruction (S.Context )) {
1823+ QualType VT = VD->getType ();
1824+ if (const auto *RT = VT->getAs <RecordType>())
1825+ if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl ()))
1826+ if (ClassDecl->hasDefinition ())
1827+ if (CXXDestructorDecl *Dtor = ClassDecl->getDestructor ())
1828+ asImpl ().visitUsedDecl (Dtor->getLocation (), Dtor);
1829+ }
1830+
1831+ Inherited::VisitDeclStmt (DS);
1832+ }
17921833 void checkVar (VarDecl *VD) {
17931834 assert (VD->isFileVarDecl () &&
17941835 " Should only check file-scope variables" );
@@ -1830,6 +1871,8 @@ class DeferredDiagnosticsEmitter
18301871 if (auto *S = FD->getBody ()) {
18311872 this ->Visit (S);
18321873 }
1874+ if (CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(FD))
1875+ asImpl ().VisitCalledDestructors (Dtor);
18331876 UsePath.pop_back ();
18341877 InUsePath.erase (FD);
18351878 }
0 commit comments