Skip to content

Commit 954a0db

Browse files
committed
Parameterize visitAuxiliaryDecls to account for separate traversal
Some places want to do in-order walks of MacroExpansionDecls, but still visit auxiliary declarations. Rather than force them to specifically filter out declarations from the MacroExpansionDecl, add a parameter to visitAuxiliaryDecls to skip them.
1 parent 0caa453 commit 954a0db

File tree

8 files changed

+36
-29
lines changed

8 files changed

+36
-29
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
884884
///
885885
/// Auxiliary declarations can be property wrapper backing variables,
886886
/// backing variables for 'lazy' vars, or peer macro expansions.
887-
void visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const;
887+
///
888+
/// When \p visitFreestandingExpanded is true (the default), this will also
889+
/// visit the declarations produced by a freestanding macro expansion.
890+
void visitAuxiliaryDecls(
891+
AuxiliaryDeclCallback callback,
892+
bool visitFreestandingExpanded = true
893+
) const;
888894

889895
using MacroCallback = llvm::function_ref<void(CustomAttr *, MacroDecl *)>;
890896

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
455455
else
456456
return true;
457457
}
458-
// Visit auxiliary decls, which may be decls from macro expansions.
459458
bool alreadyFailed = false;
460459
if (shouldWalkExpansion) {
461460
MED->forEachExpandedNode([&](ASTNode expandedNode) {

lib/AST/Decl.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,10 @@ DeclAttributes Decl::getSemanticAttrs() const {
381381
return getAttrs();
382382
}
383383

384-
void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
384+
void Decl::visitAuxiliaryDecls(
385+
AuxiliaryDeclCallback callback,
386+
bool visitFreestandingExpanded
387+
) const {
385388
auto &ctx = getASTContext();
386389
auto *mutableThis = const_cast<Decl *>(this);
387390
SourceManager &sourceMgr = ctx.SourceMgr;
@@ -414,13 +417,15 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
414417
}
415418
}
416419

417-
else if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
418-
if (auto bufferID = evaluateOrDefault(
419-
ctx.evaluator, ExpandMacroExpansionDeclRequest{med}, {})) {
420-
auto startLoc = sourceMgr.getLocForBufferStart(*bufferID);
421-
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
422-
for (auto *decl : sourceFile->getTopLevelDecls())
423-
callback(decl);
420+
if (visitFreestandingExpanded) {
421+
if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
422+
if (auto bufferID = evaluateOrDefault(
423+
ctx.evaluator, ExpandMacroExpansionDeclRequest{med}, {})) {
424+
auto startLoc = sourceMgr.getLocForBufferStart(*bufferID);
425+
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
426+
for (auto *decl : sourceFile->getTopLevelDecls())
427+
callback(decl);
428+
}
424429
}
425430
}
426431

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
18451845
if (!isa<ClassDecl>(decl->getDeclContext())) {
18461846
decl->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
18471847
this->visit(auxiliaryDecl);
1848-
});
1848+
}, /*visitFreestandingExpanded=*/false);
18491849
}
18501850

18511851
if (auto *Stats = getASTContext().Stats)
@@ -2062,10 +2062,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20622062
// Assign a discriminator.
20632063
(void)MED->getDiscriminator();
20642064
MED->forEachExpandedNode([&](ASTNode node) {
2065-
// Decls in expansion already visited as auxiliary decls.
2066-
if (node.is<Decl *>())
2067-
return;
2068-
20692065
TypeChecker::typeCheckASTNode(node, MED->getDeclContext());
20702066
});
20712067
}

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,19 @@ public struct DefineDeclsWithKnownNamesMacro: DeclarationMacro {
299299
}
300300
}
301301

302-
public struct VarDeclMacro: DeclarationMacro {
302+
public struct VarDeclMacro: CodeItemMacro {
303303
public static func expansion(
304304
of node: some FreestandingMacroExpansionSyntax,
305305
in context: some MacroExpansionContext
306-
) throws -> [DeclSyntax] {
306+
) throws -> [CodeBlockItemSyntax] {
307+
let name = context.makeUniqueName("fromMacro")
307308
return [
309+
"let \(name) = 23",
310+
"use(\(name))",
308311
"""
309-
let fromMacro = 23
310-
use(fromMacro)
311312
if true {
312-
let fromMacro = "string"
313-
use(fromMacro)
313+
let \(name) = "string"
314+
use(\(name))
314315
}
315316
"""
316317
]

test/Macros/macro_expand.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,6 @@ func testFileID(a: Int, b: Int) {
151151

152152
testFileID(a: 1, b: 2)
153153

154-
@freestanding(declaration, names: named(fromMacro)) macro varDecl() = #externalMacro(module: "MacroDefinition", type: "VarDeclMacro")
155-
156-
func testVarDecl() {
157-
func use<T>(_ t: T) {}
158-
#varDecl()
159-
}
160-
161154
@freestanding(expression) macro stringifyAndTry<T>(_ value: T) -> (T, String) =
162155
#externalMacro(module: "MacroDefinition", type: "StringifyAndTryMacro")
163156

test/Macros/macro_expand_codeitems.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ func testFreestandingMacroExpansion() {
3333
#codeItems
3434
}
3535
testFreestandingMacroExpansion()
36+
37+
@freestanding(codeItem) macro varDecl() = #externalMacro(module: "MacroDefinition", type: "VarDeclMacro")
38+
39+
func testVarDecl() {
40+
func use<T>(_ t: T) {}
41+
#varDecl()
42+
}

test/Macros/top_level_freestanding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func lookupGlobalFreestandingExpansion() {
5050

5151
#anonymousTypes(public: true) { "hello" }
5252

53-
// CHECK-SIL: sil @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF
53+
// CHECK-SIL: sil @$s9MacroUser03$s9A70User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf_4namefMu_C5helloSSyF
5454

5555
@main
5656
struct Main {

0 commit comments

Comments
 (0)