Skip to content

Commit a9ad4e2

Browse files
committed
[AST] Avoid walking macro expanded bodies in non-expanded mode
Make sure we don't walk into the expansion of a body macro if the AST walker is configured not to walk macro expansions.
1 parent f0d4572 commit a9ad4e2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8065,6 +8065,11 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
80658065
/// type-checked.
80668066
BraceStmt *getMacroExpandedBody() const;
80678067

8068+
/// Whether the body of the function has been expanded from a body macro.
8069+
bool isBodyMacroExpanded() const {
8070+
return getBodyExpandedStatus() == BodyExpandedStatus::Expanded;
8071+
}
8072+
80688073
/// Retrieve the type-checked body of the given function, or \c nullptr if
80698074
/// there's no body available.
80708075
BraceStmt *getTypecheckedBody() const;

lib/AST/ASTWalker.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,12 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
558558
if (WalkGenerics && visitTrailingRequirements(AFD))
559559
return true;
560560

561-
if (AFD->getBody(/*canSynthesize=*/false)) {
561+
// If we're not walking macro expansions, avoid walking into a body if it
562+
// was expanded from a macro.
563+
auto SkipBody = !Walker.shouldWalkMacroArgumentsAndExpansion().second &&
564+
AFD->isBodyMacroExpanded();
565+
566+
if (!SkipBody && AFD->getBody(/*canSynthesize=*/false)) {
562567
AbstractFunctionDecl::BodyKind PreservedKind = AFD->getBodyKind();
563568
if (BraceStmt *S = cast_or_null<BraceStmt>(doIt(AFD->getBody())))
564569
AFD->setBody(S, PreservedKind);

0 commit comments

Comments
 (0)