@@ -762,7 +762,7 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
762
762
break ;
763
763
764
764
// Add this entry.
765
- auto name = importMacroName (macro.first , info);
765
+ auto name = importMacroName (macro.first , info, clangCtx );
766
766
if (name.empty ()) continue ;
767
767
table.addEntry (name, info, clangCtx.getTranslationUnitDecl ());
768
768
}
@@ -2088,13 +2088,42 @@ bool ClangImporter::shouldIgnoreMacro(StringRef Name,
2088
2088
2089
2089
Identifier ClangImporter::Implementation::importMacroName (
2090
2090
const clang::IdentifierInfo *clangIdentifier,
2091
- const clang::MacroInfo *macro) {
2091
+ const clang::MacroInfo *macro,
2092
+ clang::ASTContext &clangCtx) {
2092
2093
// If we're supposed to ignore this macro, return an empty identifier.
2093
2094
if (::shouldIgnoreMacro (clangIdentifier->getName (), macro))
2094
2095
return Identifier ();
2095
2096
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);
2098
2127
}
2099
2128
2100
2129
auto ClangImporter::Implementation::importFullName (
@@ -3516,11 +3545,13 @@ void ClangImporter::lookupBridgingHeaderDecls(
3516
3545
}
3517
3546
}
3518
3547
}
3548
+
3549
+ auto &ClangCtx = Impl.getClangASTContext ();
3519
3550
auto &ClangPP = Impl.getClangPreprocessor ();
3520
3551
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros ) {
3521
3552
if (auto *MI = ClangPP.getMacroInfo (II)) {
3522
3553
if (filter (MI)) {
3523
- Identifier Name = Impl.importMacroName (II, MI);
3554
+ Identifier Name = Impl.importMacroName (II, MI, ClangCtx );
3524
3555
if (Decl *imported = Impl.importMacro (Name, MI))
3525
3556
receiver (imported);
3526
3557
}
@@ -3597,7 +3628,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
3597
3628
auto *II = const_cast <clang::IdentifierInfo*>(MD->getName ());
3598
3629
if (auto *MI = ClangPP.getMacroInfo (II)) {
3599
3630
if (filter (MI)) {
3600
- Identifier Name = Impl.importMacroName (II, MI);
3631
+ Identifier Name = Impl.importMacroName (II, MI, ClangCtx );
3601
3632
if (Decl *imported = Impl.importMacro (Name, MI))
3602
3633
receiver (imported);
3603
3634
}
@@ -4360,7 +4391,7 @@ SwiftLookupTable *ClangImporter::Implementation::findLookupTable(
4360
4391
// /
4361
4392
// / FIXME: this is an elaborate hack to badly reflect Clang's
4362
4393
// / submodule visibility into Swift.
4363
- static bool isVisibleClangEntry (clang::Preprocessor &pp ,
4394
+ static bool isVisibleClangEntry (clang::ASTContext &ctx ,
4364
4395
StringRef name,
4365
4396
SwiftLookupTable::SingleEntry entry) {
4366
4397
if (auto clangDecl = entry.dyn_cast <clang::NamedDecl *>()) {
@@ -4376,20 +4407,25 @@ static bool isVisibleClangEntry(clang::Preprocessor &pp,
4376
4407
}
4377
4408
4378
4409
// 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 ;
4381
4417
}
4382
4418
4383
4419
void ClangImporter::Implementation::lookupValue (
4384
4420
SwiftLookupTable &table, DeclName name,
4385
4421
VisibleDeclConsumer &consumer) {
4386
- auto clangTU = getClangASTContext (). getTranslationUnitDecl ();
4387
- auto &clangPP = getClangPreprocessor ();
4422
+ auto &clangCtx = getClangASTContext ();
4423
+ auto clangTU = clangCtx. getTranslationUnitDecl ();
4388
4424
auto baseName = name.getBaseName ().str ();
4389
4425
4390
4426
for (auto entry : table.lookup (name.getBaseName ().str (), clangTU)) {
4391
4427
// If the entry is not visible, skip it.
4392
- if (!isVisibleClangEntry (clangPP , baseName, entry)) continue ;
4428
+ if (!isVisibleClangEntry (clangCtx , baseName, entry)) continue ;
4393
4429
4394
4430
ValueDecl *decl;
4395
4431
@@ -4442,12 +4478,12 @@ void ClangImporter::Implementation::lookupObjCMembers(
4442
4478
SwiftLookupTable &table,
4443
4479
DeclName name,
4444
4480
VisibleDeclConsumer &consumer) {
4445
- auto &clangPP = getClangPreprocessor ();
4481
+ auto &clangCtx = getClangASTContext ();
4446
4482
auto baseName = name.getBaseName ().str ();
4447
4483
4448
4484
for (auto clangDecl : table.lookupObjCMembers (baseName)) {
4449
4485
// If the entry is not visible, skip it.
4450
- if (!isVisibleClangEntry (clangPP , baseName, clangDecl)) continue ;
4486
+ if (!isVisibleClangEntry (clangCtx , baseName, clangDecl)) continue ;
4451
4487
4452
4488
// Import the declaration.
4453
4489
auto decl = cast_or_null<ValueDecl>(importDeclReal (clangDecl));
0 commit comments