Skip to content

Commit 1f1188c

Browse files
committed
[Macros] Always visit macro-produced decls as auxiliary decls
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros.
1 parent b3b90a8 commit 1f1188c

13 files changed

+75
-87
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ enum class MacroWalking {
9595

9696
/// Walk into both the arguments of the macro as written in the source code
9797
/// and also the macro expansion.
98-
ArgumentsAndExpansion
98+
ArgumentsAndExpansion,
99+
100+
/// Don't walk into macros.
101+
None
99102
};
100103

101104
/// An abstract class used to traverse an AST.
@@ -545,6 +548,9 @@ class ASTWalker {
545548

546549
case MacroWalking::ArgumentsAndExpansion:
547550
return std::make_pair(true, true);
551+
552+
case MacroWalking::None:
553+
return std::make_pair(false, false);
548554
}
549555
}
550556

include/swift/AST/Decl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8497,7 +8497,6 @@ class MacroExpansionDecl : public Decl {
84978497
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
84988498
DeclNameRef getMacroName() const { return MacroName; }
84998499
ArgumentList *getArgs() const { return ArgList; }
8500-
ArrayRef<Decl *> getRewritten() const;
85018500
ConcreteDeclRef getMacroRef() const { return macroRef; }
85028501
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
85038502

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,16 +3835,16 @@ class MacroDefinitionRequest
38353835
/// Find the definition of a given macro.
38363836
class ExpandMacroExpansionDeclRequest
38373837
: public SimpleRequest<ExpandMacroExpansionDeclRequest,
3838-
ArrayRef<Decl *>(MacroExpansionDecl *),
3838+
Optional<unsigned>(MacroExpansionDecl *),
38393839
RequestFlags::Cached> {
38403840
public:
38413841
using SimpleRequest::SimpleRequest;
38423842

38433843
private:
38443844
friend SimpleRequest;
38453845

3846-
ArrayRef<Decl *> evaluate(Evaluator &evaluator,
3847-
MacroExpansionDecl *med) const;
3846+
Optional<unsigned>
3847+
evaluate(Evaluator &evaluator, MacroExpansionDecl *med) const;
38483848

38493849
public:
38503850
bool isCached() const { return true; }

include/swift/AST/TypeMemberVisitor.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
5454
return RetTy();
5555
}
5656

57+
RetTy visitMacroExpansionDecl(MacroExpansionDecl *D) {
58+
// Expansion already visited as auxiliary decls.
59+
return RetTy();
60+
}
61+
5762
/// A convenience method to visit all the members.
5863
void visitMembers(NominalTypeDecl *D) {
5964
for (Decl *member : D->getMembers()) {
65+
member->visitAuxiliaryDecls([&](Decl *decl) {
66+
asImpl().visit(decl);
67+
});
6068
asImpl().visit(member);
6169
}
6270
}
@@ -78,12 +86,6 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
7886
asImpl().visit(dd);
7987
}
8088
}
81-
82-
/// Visit expanded macros.
83-
void visitMacroExpansionDecl(MacroExpansionDecl *D) {
84-
for (auto *decl : D->getRewritten())
85-
asImpl().visit(decl);
86-
}
8789
};
8890

8991
template<typename ImplClass, typename RetTy = void>

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class Verifier : public ASTWalker {
272272
}
273273

274274
MacroWalking getMacroWalkingBehavior() const override {
275-
return MacroWalking::Expansion;
275+
return MacroWalking::None;
276276
}
277277

278278
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {

lib/AST/ASTWalker.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
115115
LLVM_NODISCARD
116116
bool visit(Decl *D) {
117117
SetParentRAII SetParent(Walker, D);
118-
return inherited::visit(D);
118+
// Visit auxiliary decls, which may be decls from macro expansions.
119+
bool alreadyFailed = false;
120+
if (Walker.shouldWalkMacroArgumentsAndExpansion().second) {
121+
D->visitAuxiliaryDecls([&](Decl *decl) {
122+
if (alreadyFailed) return;
123+
alreadyFailed = inherited::visit(decl);
124+
});
125+
}
126+
return alreadyFailed || inherited::visit(D);
119127
}
120128

121129
LLVM_NODISCARD
@@ -430,18 +438,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
430438
}
431439

432440
bool visitMacroExpansionDecl(MacroExpansionDecl *MED) {
433-
bool shouldWalkArguments, shouldWalkExpansion;
434-
std::tie(shouldWalkArguments, shouldWalkExpansion) =
435-
Walker.shouldWalkMacroArgumentsAndExpansion();
436-
if (shouldWalkArguments && MED->getArgs() && doIt(MED->getArgs()))
437-
return true;
438-
439-
if (shouldWalkExpansion) {
440-
for (auto *decl : MED->getRewritten())
441-
if (doIt(decl))
442-
return true;
443-
}
444-
return false;
441+
bool shouldWalkArguments =
442+
Walker.shouldWalkMacroArgumentsAndExpansion().first;
443+
return shouldWalkArguments && MED->getArgs() && !doIt(MED->getArgs());
445444
}
446445

447446
bool visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {

lib/AST/Decl.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
413413
}
414414
}
415415

416+
else if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
417+
if (auto bufferID = evaluateOrDefault(
418+
ctx.evaluator, ExpandMacroExpansionDeclRequest{med}, {})) {
419+
auto startLoc = sourceMgr.getLocForBufferStart(*bufferID);
420+
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
421+
for (auto *decl : sourceFile->getTopLevelDecls())
422+
callback(decl);
423+
}
424+
}
425+
416426
// FIXME: fold VarDecl::visitAuxiliaryDecls into this.
417427
}
418428

@@ -10073,13 +10083,6 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
1007310083
return getRawDiscriminator();
1007410084
}
1007510085

10076-
ArrayRef<Decl *> MacroExpansionDecl::getRewritten() const {
10077-
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
10078-
return evaluateOrDefault(
10079-
getASTContext().evaluator,
10080-
ExpandMacroExpansionDeclRequest{mutableThis}, {});
10081-
}
10082-
1008310086
NominalTypeDecl *
1008410087
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
1008510088
auto &ctx = getASTContext();

lib/AST/NameLookup.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,14 +1510,6 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15101510
MemberLookupTable &table,
15111511
DeclName name,
15121512
NominalTypeDecl *dc) {
1513-
auto expandAndPopulate = [&](MacroExpansionDecl *med) {
1514-
auto expanded = evaluateOrDefault(med->getASTContext().evaluator,
1515-
ExpandMacroExpansionDeclRequest{med},
1516-
nullptr);
1517-
for (auto *decl : expanded)
1518-
table.addMember(decl);
1519-
};
1520-
15211513
for (auto *member : dc->getCurrentMembersWithoutLoading()) {
15221514
auto *med = dyn_cast<MacroExpansionDecl>(member);
15231515
if (!med)
@@ -1532,7 +1524,9 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15321524
// If a macro produces arbitrary names, we have to expand it to factor its
15331525
// expansion results into name lookup.
15341526
if (attr->hasNameKind(MacroIntroducedDeclNameKind::Arbitrary)) {
1535-
expandAndPopulate(med);
1527+
med->visitAuxiliaryDecls([&](Decl *decl) {
1528+
table.addMember(decl);
1529+
});
15361530
}
15371531
// Otherwise, we expand the macro if it has the same decl base name being
15381532
// looked for.
@@ -1550,7 +1544,9 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15501544
name.getBaseName().userFacingName();
15511545
});
15521546
if (it != attr->getNames().end())
1553-
expandAndPopulate(med);
1547+
med->visitAuxiliaryDecls([&](Decl *decl) {
1548+
table.addMember(decl);
1549+
});
15541550
}
15551551
}
15561552
}

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,9 @@ void swift::irgen::disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariabl
24572457

24582458
/// Emit a global declaration.
24592459
void IRGenModule::emitGlobalDecl(Decl *D) {
2460+
D->visitAuxiliaryDecls([&](Decl *decl) {
2461+
emitGlobalDecl(decl);
2462+
});
24602463
switch (D->getKind()) {
24612464
case DeclKind::Extension:
24622465
return emitExtension(cast<ExtensionDecl>(D));
@@ -2542,8 +2545,7 @@ void IRGenModule::emitGlobalDecl(Decl *D) {
25422545
return;
25432546

25442547
case DeclKind::MacroExpansion:
2545-
for (auto *rewritten : cast<MacroExpansionDecl>(D)->getRewritten())
2546-
emitGlobalDecl(rewritten);
2548+
// Expansion already visited as auxiliary decls.
25472549
return;
25482550
}
25492551

@@ -5466,6 +5468,9 @@ Address IRGenModule::getAddrOfEnumCase(EnumElementDecl *Case,
54665468

54675469
void IRGenModule::emitNestedTypeDecls(DeclRange members) {
54685470
for (Decl *member : members) {
5471+
member->visitAuxiliaryDecls([&](Decl *decl) {
5472+
emitNestedTypeDecls({decl, nullptr});
5473+
});
54695474
switch (member->getKind()) {
54705475
case DeclKind::Import:
54715476
case DeclKind::TopLevelCode:
@@ -5526,8 +5531,7 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
55265531
emitClassDecl(cast<ClassDecl>(member));
55275532
continue;
55285533
case DeclKind::MacroExpansion:
5529-
for (auto *decl : cast<MacroExpansionDecl>(member)->getRewritten())
5530-
emitNestedTypeDecls({decl, nullptr});
5534+
// Expansion already visited as auxiliary decls.
55315535
continue;
55325536
}
55335537
}

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3805,7 +3805,7 @@ void VarDeclUsageChecker::handleIfConfig(IfConfigDecl *ICD) {
38053805
: VDUC(VDUC), SF(VDUC.DC->getParentSourceFile()) {}
38063806

38073807
MacroWalking getMacroWalkingBehavior() const override {
3808-
return MacroWalking::ArgumentsAndExpansion;
3808+
return MacroWalking::Arguments;
38093809
}
38103810

38113811
PostWalkResult<Expr *> walkToExprPost(Expr *E) override {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,12 +2063,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20632063
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
20642064
// Assign a discriminator.
20652065
(void)MED->getDiscriminator();
2066-
2067-
auto rewritten = evaluateOrDefault(
2068-
Ctx.evaluator, ExpandMacroExpansionDeclRequest{MED}, {});
2069-
2070-
for (auto *decl : rewritten)
2071-
visit(decl);
2066+
// Expansion already visited as auxiliary decls.
20722067
}
20732068

20742069
void visitBoundVariable(VarDecl *VD) {
@@ -3763,7 +3758,7 @@ void TypeChecker::checkParameterList(ParameterList *params,
37633758
}
37643759
}
37653760

3766-
ArrayRef<Decl *>
3761+
Optional<unsigned>
37673762
ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37683763
MacroExpansionDecl *MED) const {
37693764
auto &ctx = MED->getASTContext();
@@ -3785,8 +3780,5 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37853780
MED->setMacroRef(macro);
37863781

37873782
// Expand the macro.
3788-
SmallVector<Decl *, 2> expandedTemporary;
3789-
if (!expandFreestandingDeclarationMacro(MED, expandedTemporary))
3790-
return {};
3791-
return ctx.AllocateCopy(expandedTemporary);
3783+
return expandFreestandingDeclarationMacro(MED);
37923784
}

0 commit comments

Comments
 (0)