@@ -1526,48 +1526,70 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15261526 MemberLookupTable &table,
15271527 DeclName name,
15281528 NominalTypeDecl *dc) {
1529- auto expandAndPopulate = [&](MacroExpansionDecl *med) {
1530- auto expanded = evaluateOrDefault (med->getASTContext ().evaluator ,
1531- ExpandMacroExpansionDeclRequest{med},
1532- nullptr );
1533- for (auto *decl : expanded)
1534- table.addMember (decl);
1535- };
1536-
1529+ auto *moduleScopeCtx = dc->getModuleScopeContext ();
1530+ auto *module = dc->getModuleContext ();
15371531 for (auto *member : dc->getCurrentMembersWithoutLoading ()) {
1538- auto *med = dyn_cast<MacroExpansionDecl>(member);
1539- if (!med)
1540- continue ;
1541- auto declRef = evaluateOrDefault (
1542- ctx.evaluator , ResolveMacroRequest{med, dc},
1543- nullptr );
1544- auto *macro = dyn_cast_or_null<MacroDecl>(declRef.getDecl ());
1545- if (!macro)
1546- continue ;
1547- auto *attr = macro->getMacroRoleAttr (MacroRole::Declaration);
1548- // If a macro produces arbitrary names, we have to expand it to factor its
1549- // expansion results into name lookup.
1550- if (attr->hasNameKind (MacroIntroducedDeclNameKind::Arbitrary)) {
1551- expandAndPopulate (med);
1552- }
1553- // Otherwise, we expand the macro if it has the same decl base name being
1554- // looked for.
1555- else {
1556- auto it = llvm::find_if (attr->getNames (),
1557- [&](const MacroIntroducedDeclName &introName) {
1558- // FIXME: The `Named` kind of `MacroIntroducedDeclName` should store a
1559- // `DeclName` instead of `Identifier`. This is so that we can compare
1560- // base identifiers when the macro specifies a compound name.
1561- // Currently only simple names are allowed in a `MacroRoleAttr`.
1562- if (!name.isSpecial ())
1563- return introName.getIdentifier () == name.getBaseIdentifier ();
1564- else
1565- return introName.getIdentifier ().str () ==
1566- name.getBaseName ().userFacingName ();
1567- });
1568- if (it != attr->getNames ().end ())
1569- expandAndPopulate (med);
1532+ // Collect all macro introduced names, along with its corresponding macro
1533+ // reference. We need the macro reference to prevent adding auxiliary decls
1534+ // that weren't introduced by the macro.
1535+ llvm::SmallSet<DeclName, 4 > allIntroducedNames;
1536+ bool introducesArbitraryNames = false ;
1537+ if (auto *med = dyn_cast<MacroExpansionDecl>(member)) {
1538+ auto declRef = evaluateOrDefault (
1539+ ctx.evaluator , ResolveMacroRequest{med, dc},
1540+ nullptr );
1541+ if (!declRef)
1542+ continue ;
1543+ auto *macro = dyn_cast<MacroDecl>(declRef.getDecl ());
1544+ if (macro->getMacroRoleAttr (MacroRole::Declaration)
1545+ ->hasNameKind (MacroIntroducedDeclNameKind::Arbitrary))
1546+ introducesArbitraryNames = true ;
1547+ else {
1548+ SmallVector<DeclName, 4 > introducedNames;
1549+ macro->getIntroducedNames (MacroRole::Declaration, nullptr ,
1550+ introducedNames);
1551+ for (auto name : introducedNames)
1552+ allIntroducedNames.insert (name);
1553+ }
1554+ } else if (auto *vd = dyn_cast<ValueDecl>(member)) {
1555+ // We intentionally avoid calling `forEachAttachedMacro` in order to avoid
1556+ // a request cycle.
1557+ for (auto attrConst : member->getSemanticAttrs ().getAttributes <CustomAttr>()) {
1558+ auto *attr = const_cast <CustomAttr *>(attrConst);
1559+ UnresolvedMacroReference macroRef (attr);
1560+ auto macroName = macroRef.getMacroName ();
1561+ UnqualifiedLookupDescriptor lookupDesc{macroName, moduleScopeCtx};
1562+ auto lookup = evaluateOrDefault (
1563+ ctx.evaluator , UnqualifiedLookupRequest{lookupDesc}, {});
1564+ for (auto result : lookup.allResults ()) {
1565+ auto *vd = result.getValueDecl ();
1566+ auto *macro = dyn_cast<MacroDecl>(vd);
1567+ if (!macro)
1568+ continue ;
1569+ auto *macroRoleAttr = macro->getMacroRoleAttr (MacroRole::Peer);
1570+ if (!macroRoleAttr)
1571+ continue ;
1572+ if (macroRoleAttr->hasNameKind (
1573+ MacroIntroducedDeclNameKind::Arbitrary))
1574+ introducesArbitraryNames = true ;
1575+ else {
1576+ SmallVector<DeclName, 4 > introducedNames;
1577+ macro->getIntroducedNames (
1578+ MacroRole::Peer, dyn_cast<ValueDecl>(member), introducedNames);
1579+ for (auto name : introducedNames)
1580+ allIntroducedNames.insert (name);
1581+ }
1582+ }
1583+ }
15701584 }
1585+ // Expand macros based on the name.
1586+ if (introducesArbitraryNames || allIntroducedNames.contains (name))
1587+ member->visitAuxiliaryDecls ([&](Decl *decl) {
1588+ auto *sf = module ->getSourceFileContainingLocation (decl->getLoc ());
1589+ // Bail out if the auxiliary decl was not produced by a macro.
1590+ if (!sf || sf->Kind != SourceFileKind::MacroExpansion) return ;
1591+ table.addMember (decl);
1592+ });
15711593 }
15721594}
15731595
0 commit comments