Skip to content

Commit f4f3e6a

Browse files
committed
WIP: Let ASTWalker visit auxiliary decls
1 parent 1fd61a3 commit f4f3e6a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "swift/AST/GenericParamList.h"
5959
#include "swift/AST/ParameterList.h"
6060
#include "swift/AST/PrettyStackTrace.h"
61+
#include "swift/AST/SourceFile.h"
6162
using namespace swift;
6263

6364
void ASTWalker::anchor() {}
@@ -115,7 +116,22 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
115116
LLVM_NODISCARD
116117
bool visit(Decl *D) {
117118
SetParentRAII SetParent(Walker, D);
118-
return inherited::visit(D);
119+
// Visit auxiliary decls, which may be decls from macro expansions.
120+
auto moduleDecl = D->getDeclContext()->getParentModule();
121+
bool alreadyFailed = false;
122+
D->visitAuxiliaryDecls([&](ASTNode node) {
123+
if (alreadyFailed) return;
124+
auto *decl = node.dyn_cast<Decl *>();
125+
if (!decl) return;
126+
// Skip macro expansions if asked.
127+
if (!Walker.shouldWalkMacroExpansions())
128+
if (auto *sf = moduleDecl->getSourceFileContainingLocation(
129+
decl->getLoc()))
130+
if (sf && sf->Kind == SourceFileKind::MacroExpansion)
131+
return;
132+
alreadyFailed = inherited::visit(decl);
133+
});
134+
return alreadyFailed || inherited::visit(D);
119135
}
120136

121137
LLVM_NODISCARD
@@ -439,10 +455,11 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
439455
if (auto *expr = expandedNode.dyn_cast<Expr *>()) {
440456
if (!doIt(expr))
441457
return true;
442-
} else if (auto *decl = expandedNode.dyn_cast<Decl *>()) {
443-
if (doIt(decl))
458+
} else if (auto *stmt = expandedNode.dyn_cast<Stmt *>()) {
459+
if (!doIt(stmt))
444460
return true;
445461
}
462+
// Decl already visited by `visitAuxiliaryDecls`.
446463
}
447464
}
448465
return false;

0 commit comments

Comments
 (0)