[Macros] Improve visitation of auxiliary decls

Use the same pattern as 'getAllMembers()'. This supports nested macro
expansion:
```
std::function<void(Decl *)> visit;
visit = [&](Decl *d) {
  doIt(d);
  d->visitAuxiliaryDecls(visit);
};
for (auto *d : decls)
  visit(d);
```

Don't visit auxiliary decls in `PrintAST::visit(Decl *)` this function
is only intended for single decl printing. The caller should visit them
separately. For that, add
`ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls()`
This commit is contained in:
Rintaro Ishizaki
2023-10-23 12:54:13 -07:00
parent 6f5283ebfc
commit 03bf349778
5 changed files with 33 additions and 21 deletions

View File

@@ -1331,6 +1331,11 @@ void ModuleDecl::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
FORWARD(getTopLevelDecls, (Results));
}
void ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls(
SmallVectorImpl<Decl *> &Results) const {
FORWARD(getTopLevelDeclsWithAuxiliaryDecls, (Results));
}
void ModuleDecl::dumpDisplayDecls() const {
SmallVector<Decl *, 32> Decls;
getDisplayDecls(Decls);
@@ -3095,7 +3100,9 @@ void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
std::set<DeclKind> MajorDeclKinds = {DeclKind::Class, DeclKind::Enum,
DeclKind::Extension, DeclKind::Protocol, DeclKind::Struct};
for (auto decl : getTopLevelDecls()) {
SmallVector<Decl *> topLevelDecls;
getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
for (auto decl : topLevelDecls) {
if (!decl->shouldPrintInContext(PO))
continue;
// For a major decl, we print an empty line before it.
@@ -4190,14 +4197,18 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
SmallVectorImpl<Decl*> &results) const {
std::function<void(Decl *)> addResult;
addResult = [&](Decl *decl) {
results.push_back(decl);
decl->visitAuxiliaryDecls(addResult);
};
SmallVector<Decl *, 32> nonExpandedDecls;
nonExpandedDecls.reserve(results.capacity());
getTopLevelDecls(nonExpandedDecls);
for (auto *decl : nonExpandedDecls) {
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
results.push_back(auxDecl);
});
results.push_back(decl);
addResult(decl);
}
}