Skip to content

Commit 346f3f8

Browse files
DougGregorbnbarham
authored andcommitted
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. (cherry picked from commit 954a0db)
1 parent b0ea22c commit 346f3f8

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
@@ -913,7 +913,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
913913
///
914914
/// Auxiliary declarations can be property wrapper backing variables,
915915
/// backing variables for 'lazy' vars, or peer macro expansions.
916-
void visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const;
916+
///
917+
/// When \p visitFreestandingExpanded is true (the default), this will also
918+
/// visit the declarations produced by a freestanding macro expansion.
919+
void visitAuxiliaryDecls(
920+
AuxiliaryDeclCallback callback,
921+
bool visitFreestandingExpanded = true
922+
) const;
917923

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

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
@@ -385,7 +385,10 @@ DeclAttributes Decl::getSemanticAttrs() const {
385385
return getAttrs();
386386
}
387387

388-
void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
388+
void Decl::visitAuxiliaryDecls(
389+
AuxiliaryDeclCallback callback,
390+
bool visitFreestandingExpanded
391+
) const {
389392
auto &ctx = getASTContext();
390393
auto *mutableThis = const_cast<Decl *>(this);
391394
SourceManager &sourceMgr = ctx.SourceMgr;
@@ -418,13 +421,15 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
418421
}
419422
}
420423

421-
else 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);
424+
if (visitFreestandingExpanded) {
425+
if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
426+
if (auto bufferID = evaluateOrDefault(
427+
ctx.evaluator, ExpandMacroExpansionDeclRequest{med}, {})) {
428+
auto startLoc = sourceMgr.getLocForBufferStart(*bufferID);
429+
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
430+
for (auto *decl : sourceFile->getTopLevelDecls())
431+
callback(decl);
432+
}
428433
}
429434
}
430435

lib/Sema/TypeCheckDeclPrimary.cpp

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

18561856
if (auto *Stats = getASTContext().Stats)
@@ -2067,10 +2067,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20672067
// Assign a discriminator.
20682068
(void)MED->getDiscriminator();
20692069
MED->forEachExpandedNode([&](ASTNode node) {
2070-
// Decls in expansion already visited as auxiliary decls.
2071-
if (node.is<Decl *>())
2072-
return;
2073-
20742070
TypeChecker::typeCheckASTNode(node, MED->getDeclContext());
20752071
});
20762072
}

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
@@ -150,13 +150,6 @@ func testFileID(a: Int, b: Int) {
150150

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

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

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)