diff --git a/include/swift/AST/Decl.h b/include/swift/AST/Decl.h index 5c58d90f6ad..7fe35d0afdb 100644 --- a/include/swift/AST/Decl.h +++ b/include/swift/AST/Decl.h @@ -8065,6 +8065,11 @@ public: /// type-checked. BraceStmt *getMacroExpandedBody() const; + /// Whether the body of the function has been expanded from a body macro. + bool isBodyMacroExpanded() const { + return getBodyExpandedStatus() == BodyExpandedStatus::Expanded; + } + /// Retrieve the type-checked body of the given function, or \c nullptr if /// there's no body available. BraceStmt *getTypecheckedBody() const; diff --git a/lib/AST/ASTWalker.cpp b/lib/AST/ASTWalker.cpp index 31920c0ea54..e39f3c4c3c7 100644 --- a/lib/AST/ASTWalker.cpp +++ b/lib/AST/ASTWalker.cpp @@ -558,7 +558,12 @@ class Traversal : public ASTVisitorgetBody(/*canSynthesize=*/false)) { + // If we're not walking macro expansions, avoid walking into a body if it + // was expanded from a macro. + auto SkipBody = !Walker.shouldWalkMacroArgumentsAndExpansion().second && + AFD->isBodyMacroExpanded(); + + if (!SkipBody && AFD->getBody(/*canSynthesize=*/false)) { AbstractFunctionDecl::BodyKind PreservedKind = AFD->getBodyKind(); if (BraceStmt *S = cast_or_null(doIt(AFD->getBody()))) AFD->setBody(S, PreservedKind);