@@ -1594,6 +1594,60 @@ static DeclName adjustLazyMacroExpansionNameKey(
1594
1594
return name;
1595
1595
}
1596
1596
1597
+ SmallVector<MacroDecl *, 1 >
1598
+ namelookup::lookupMacros (DeclContext *dc, DeclNameRef macroName,
1599
+ MacroRoles roles) {
1600
+ SmallVector<MacroDecl *, 1 > choices;
1601
+ auto moduleScopeDC = dc->getModuleScopeContext ();
1602
+ ASTContext &ctx = moduleScopeDC->getASTContext ();
1603
+
1604
+ // Macro lookup should always exclude macro expansions; macro
1605
+ // expansions cannot introduce new macro declarations. Note that
1606
+ // the source location here doesn't matter.
1607
+ UnqualifiedLookupDescriptor descriptor{
1608
+ macroName, moduleScopeDC, SourceLoc (),
1609
+ UnqualifiedLookupFlags::ExcludeMacroExpansions
1610
+ };
1611
+
1612
+ auto lookup = evaluateOrDefault (
1613
+ ctx.evaluator , UnqualifiedLookupRequest{descriptor}, {});
1614
+ for (const auto &found : lookup.allResults ()) {
1615
+ if (auto macro = dyn_cast<MacroDecl>(found.getValueDecl ())) {
1616
+ auto candidateRoles = macro->getMacroRoles ();
1617
+ if ((candidateRoles && roles.contains (candidateRoles)) ||
1618
+ // FIXME: `externalMacro` should have all roles.
1619
+ macro->getBaseIdentifier ().str () == " externalMacro" ) {
1620
+ choices.push_back (macro);
1621
+ }
1622
+ }
1623
+ }
1624
+ return choices;
1625
+ }
1626
+
1627
+ bool
1628
+ namelookup::isInMacroArgument (SourceFile *sourceFile, SourceLoc loc) {
1629
+ bool inMacroArgument = false ;
1630
+
1631
+ ASTScope::lookupEnclosingMacroScope (
1632
+ sourceFile, loc,
1633
+ [&](auto potentialMacro) -> bool {
1634
+ UnresolvedMacroReference macro (potentialMacro);
1635
+
1636
+ if (macro.getFreestanding ()) {
1637
+ inMacroArgument = true ;
1638
+ } else if (auto *attr = macro.getAttr ()) {
1639
+ auto *moduleScope = sourceFile->getModuleScopeContext ();
1640
+ auto results = lookupMacros (moduleScope, macro.getMacroName (),
1641
+ getAttachedMacroRoles ());
1642
+ inMacroArgument = !results.empty ();
1643
+ }
1644
+
1645
+ return inMacroArgument;
1646
+ });
1647
+
1648
+ return inMacroArgument;
1649
+ }
1650
+
1597
1651
// / Call the given function body with each macro declaration and its associated
1598
1652
// / role attribute for the given role.
1599
1653
// /
@@ -2631,7 +2685,7 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
2631
2685
2632
2686
// Manually exclude macro expansions here since the source location
2633
2687
// is overridden below.
2634
- if (ASTScope ::isInMacroArgument (dc->getParentSourceFile (), loc))
2688
+ if (namelookup ::isInMacroArgument (dc->getParentSourceFile (), loc))
2635
2689
options |= UnqualifiedLookupFlags::ExcludeMacroExpansions;
2636
2690
2637
2691
// In a protocol or protocol extension, the 'where' clause can refer to
0 commit comments