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