@@ -762,7 +762,7 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
762762 break ;
763763
764764 // Add this entry.
765- auto name = importMacroName (macro.first , info);
765+ auto name = importMacroName (macro.first , info, clangCtx );
766766 if (name.empty ()) continue ;
767767 table.addEntry (name, info, clangCtx.getTranslationUnitDecl ());
768768 }
@@ -2088,13 +2088,42 @@ bool ClangImporter::shouldIgnoreMacro(StringRef Name,
20882088
20892089Identifier ClangImporter::Implementation::importMacroName (
20902090 const clang::IdentifierInfo *clangIdentifier,
2091- const clang::MacroInfo *macro) {
2091+ const clang::MacroInfo *macro,
2092+ clang::ASTContext &clangCtx) {
20922093 // If we're supposed to ignore this macro, return an empty identifier.
20932094 if (::shouldIgnoreMacro (clangIdentifier->getName (), macro))
20942095 return Identifier ();
20952096
2096- // Import the identifier.
2097- return importIdentifier (clangIdentifier);
2097+ // If we aren't omitting needless words, no transformation is applied to the
2098+ // name.
2099+ StringRef name = clangIdentifier->getName ();
2100+ if (!OmitNeedlessWords) return SwiftContext.getIdentifier (name);
2101+
2102+ // Determine which module defines this macro.
2103+ StringRef moduleName;
2104+ if (auto moduleID = macro->getOwningModuleID ()) {
2105+ if (auto module = clangCtx.getExternalSource ()->getModule (moduleID))
2106+ moduleName = module ->getTopLevelModuleName ();
2107+ }
2108+
2109+ if (moduleName.empty ())
2110+ moduleName = clangCtx.getLangOpts ().CurrentModule ;
2111+
2112+ // Check whether we have a prefix to strip.
2113+ unsigned prefixLen = stripModulePrefixLength (ModulePrefixes, moduleName,
2114+ name);
2115+ if (prefixLen == 0 ) return SwiftContext.getIdentifier (name);
2116+
2117+ // Strip the prefix.
2118+ name = name.substr (prefixLen);
2119+
2120+ // If we should lowercase, do so.
2121+ StringScratchSpace scratch;
2122+ if (shouldLowercaseValueName (name))
2123+ name = camel_case::toLowercaseInitialisms (name, scratch);
2124+
2125+ // We're done.
2126+ return SwiftContext.getIdentifier (name);
20982127}
20992128
21002129auto ClangImporter::Implementation::importFullName (
@@ -3516,11 +3545,13 @@ void ClangImporter::lookupBridgingHeaderDecls(
35163545 }
35173546 }
35183547 }
3548+
3549+ auto &ClangCtx = Impl.getClangASTContext ();
35193550 auto &ClangPP = Impl.getClangPreprocessor ();
35203551 for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros ) {
35213552 if (auto *MI = ClangPP.getMacroInfo (II)) {
35223553 if (filter (MI)) {
3523- Identifier Name = Impl.importMacroName (II, MI);
3554+ Identifier Name = Impl.importMacroName (II, MI, ClangCtx );
35243555 if (Decl *imported = Impl.importMacro (Name, MI))
35253556 receiver (imported);
35263557 }
@@ -3597,7 +3628,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
35973628 auto *II = const_cast <clang::IdentifierInfo*>(MD->getName ());
35983629 if (auto *MI = ClangPP.getMacroInfo (II)) {
35993630 if (filter (MI)) {
3600- Identifier Name = Impl.importMacroName (II, MI);
3631+ Identifier Name = Impl.importMacroName (II, MI, ClangCtx );
36013632 if (Decl *imported = Impl.importMacro (Name, MI))
36023633 receiver (imported);
36033634 }
@@ -4360,7 +4391,7 @@ SwiftLookupTable *ClangImporter::Implementation::findLookupTable(
43604391// /
43614392// / FIXME: this is an elaborate hack to badly reflect Clang's
43624393// / submodule visibility into Swift.
4363- static bool isVisibleClangEntry (clang::Preprocessor &pp ,
4394+ static bool isVisibleClangEntry (clang::ASTContext &ctx ,
43644395 StringRef name,
43654396 SwiftLookupTable::SingleEntry entry) {
43664397 if (auto clangDecl = entry.dyn_cast <clang::NamedDecl *>()) {
@@ -4376,20 +4407,25 @@ static bool isVisibleClangEntry(clang::Preprocessor &pp,
43764407 }
43774408
43784409 // Check whether the macro is defined.
4379- // FIXME: We could get the wrong macro definition here.
4380- return pp.isMacroDefined (name);
4410+ auto clangMacro = entry.get <clang::MacroInfo *>();
4411+ if (auto moduleID = clangMacro->getOwningModuleID ()) {
4412+ if (auto module = ctx.getExternalSource ()->getModule (moduleID))
4413+ return module ->NameVisibility == clang::Module::AllVisible;
4414+ }
4415+
4416+ return true ;
43814417}
43824418
43834419void ClangImporter::Implementation::lookupValue (
43844420 SwiftLookupTable &table, DeclName name,
43854421 VisibleDeclConsumer &consumer) {
4386- auto clangTU = getClangASTContext (). getTranslationUnitDecl ();
4387- auto &clangPP = getClangPreprocessor ();
4422+ auto &clangCtx = getClangASTContext ();
4423+ auto clangTU = clangCtx. getTranslationUnitDecl ();
43884424 auto baseName = name.getBaseName ().str ();
43894425
43904426 for (auto entry : table.lookup (name.getBaseName ().str (), clangTU)) {
43914427 // If the entry is not visible, skip it.
4392- if (!isVisibleClangEntry (clangPP , baseName, entry)) continue ;
4428+ if (!isVisibleClangEntry (clangCtx , baseName, entry)) continue ;
43934429
43944430 ValueDecl *decl;
43954431
@@ -4442,12 +4478,12 @@ void ClangImporter::Implementation::lookupObjCMembers(
44424478 SwiftLookupTable &table,
44434479 DeclName name,
44444480 VisibleDeclConsumer &consumer) {
4445- auto &clangPP = getClangPreprocessor ();
4481+ auto &clangCtx = getClangASTContext ();
44464482 auto baseName = name.getBaseName ().str ();
44474483
44484484 for (auto clangDecl : table.lookupObjCMembers (baseName)) {
44494485 // If the entry is not visible, skip it.
4450- if (!isVisibleClangEntry (clangPP , baseName, clangDecl)) continue ;
4486+ if (!isVisibleClangEntry (clangCtx , baseName, clangDecl)) continue ;
44514487
44524488 // Import the declaration.
44534489 auto decl = cast_or_null<ValueDecl>(importDeclReal (clangDecl));
0 commit comments