@@ -1331,6 +1331,11 @@ void ModuleDecl::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
13311331 FORWARD (getTopLevelDecls, (Results));
13321332}
13331333
1334+ void ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls (
1335+ SmallVectorImpl<Decl *> &Results) const {
1336+ FORWARD (getTopLevelDeclsWithAuxiliaryDecls, (Results));
1337+ }
1338+
13341339void ModuleDecl::dumpDisplayDecls () const {
13351340 SmallVector<Decl *, 32 > Decls;
13361341 getDisplayDecls (Decls);
@@ -3095,7 +3100,9 @@ void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
30953100void SourceFile::print (ASTPrinter &Printer, const PrintOptions &PO) {
30963101 std::set<DeclKind> MajorDeclKinds = {DeclKind::Class, DeclKind::Enum,
30973102 DeclKind::Extension, DeclKind::Protocol, DeclKind::Struct};
3098- for (auto decl : getTopLevelDecls ()) {
3103+ SmallVector<Decl *> topLevelDecls;
3104+ getTopLevelDeclsWithAuxiliaryDecls (topLevelDecls);
3105+ for (auto decl : topLevelDecls) {
30993106 if (!decl->shouldPrintInContext (PO))
31003107 continue ;
31013108 // For a major decl, we print an empty line before it.
@@ -4190,14 +4197,18 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
41904197
41914198void FileUnit::getTopLevelDeclsWithAuxiliaryDecls (
41924199 SmallVectorImpl<Decl*> &results) const {
4200+
4201+ std::function<void (Decl *)> addResult;
4202+ addResult = [&](Decl *decl) {
4203+ results.push_back (decl);
4204+ decl->visitAuxiliaryDecls (addResult);
4205+ };
4206+
41934207 SmallVector<Decl *, 32 > nonExpandedDecls;
41944208 nonExpandedDecls.reserve (results.capacity ());
41954209 getTopLevelDecls (nonExpandedDecls);
41964210 for (auto *decl : nonExpandedDecls) {
4197- decl->visitAuxiliaryDecls ([&](Decl *auxDecl) {
4198- results.push_back (auxDecl);
4199- });
4200- results.push_back (decl);
4211+ addResult (decl);
42014212 }
42024213}
42034214
0 commit comments