@@ -740,10 +740,6 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
740
740
clang::MacroDirective *MD = pp.getLocalMacroDirectiveHistory (macro.first );
741
741
if (!MD) continue ;
742
742
743
- // Import the name.
744
- auto name = importIdentifier (macro.first );
745
- if (name.empty ()) continue ;
746
-
747
743
// Walk the history.
748
744
for (; MD; MD = MD->getPrevious ()) {
749
745
// Check whether we have a macro defined in this module.
@@ -766,6 +762,8 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
766
762
break ;
767
763
768
764
// Add this entry.
765
+ auto name = importMacroName (macro.first , info);
766
+ if (name.empty ()) continue ;
769
767
table.addEntry (name, info, clangCtx.getTranslationUnitDecl ());
770
768
}
771
769
}
@@ -2020,6 +2018,44 @@ static bool canStripModulePrefix(StringRef name) {
2020
2018
return true ;
2021
2019
}
2022
2020
2021
+ // / Returns true if it is expected that the macro is ignored.
2022
+ static bool shouldIgnoreMacro (const clang::IdentifierInfo *identifier,
2023
+ const clang::MacroInfo *macro) {
2024
+ // Ignore include guards.
2025
+ if (macro->isUsedForHeaderGuard ())
2026
+ return true ;
2027
+
2028
+ // If there are no tokens, there is nothing to convert.
2029
+ if (macro->tokens_empty ())
2030
+ return true ;
2031
+
2032
+ // Currently we only convert non-function-like macros.
2033
+ if (macro->isFunctionLike ())
2034
+ return nullptr ;
2035
+
2036
+ // Consult the blacklist of macros to suppress.
2037
+ auto suppressMacro =
2038
+ llvm::StringSwitch<bool >(identifier->getName ())
2039
+ #define SUPPRESS_MACRO (NAME ) .Case(#NAME, true )
2040
+ #include " MacroTable.def"
2041
+ .Default (false );
2042
+
2043
+ if (suppressMacro)
2044
+ return true ;
2045
+
2046
+ return false ;
2047
+ }
2048
+
2049
+ Identifier ClangImporter::Implementation::importMacroName (
2050
+ const clang::IdentifierInfo *clangIdentifier,
2051
+ const clang::MacroInfo *macro) {
2052
+ // If we're supposed to ignore this macro, return an empty identifier.
2053
+ if (shouldIgnoreMacro (clangIdentifier, macro)) return Identifier ();
2054
+
2055
+ // Import the identifier.
2056
+ return importIdentifier (clangIdentifier);
2057
+ }
2058
+
2023
2059
auto ClangImporter::Implementation::importFullName (
2024
2060
const clang::NamedDecl *D,
2025
2061
ImportNameOptions options,
@@ -3439,12 +3475,11 @@ void ClangImporter::lookupBridgingHeaderDecls(
3439
3475
}
3440
3476
}
3441
3477
}
3442
- ASTContext &Ctx = Impl.SwiftContext ;
3443
3478
auto &ClangPP = Impl.getClangPreprocessor ();
3444
3479
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros ) {
3445
3480
if (auto *MI = ClangPP.getMacroInfo (II)) {
3446
3481
if (filter (MI)) {
3447
- Identifier Name = Ctx. getIdentifier (II-> getName () );
3482
+ Identifier Name = Impl. importMacroName (II, MI );
3448
3483
if (Decl *imported = Impl.importMacro (Name, MI))
3449
3484
receiver (imported);
3450
3485
}
@@ -3460,7 +3495,6 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
3460
3495
if (!File)
3461
3496
return true ;
3462
3497
3463
- ASTContext &Ctx = Impl.SwiftContext ;
3464
3498
auto &ClangCtx = getClangASTContext ();
3465
3499
auto &ClangSM = ClangCtx.getSourceManager ();
3466
3500
auto &ClangPP = getClangPreprocessor ();
@@ -3522,7 +3556,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
3522
3556
auto *II = const_cast <clang::IdentifierInfo*>(MD->getName ());
3523
3557
if (auto *MI = ClangPP.getMacroInfo (II)) {
3524
3558
if (filter (MI)) {
3525
- Identifier Name = Ctx. getIdentifier (II-> getName () );
3559
+ Identifier Name = Impl. importMacroName (II, MI );
3526
3560
if (Decl *imported = Impl.importMacro (Name, MI))
3527
3561
receiver (imported);
3528
3562
}
@@ -3937,11 +3971,6 @@ Decl *ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
3937
3971
return Impl.importDeclCached (ClangDecl);
3938
3972
}
3939
3973
3940
- bool ClangImporter::shouldIgnoreMacro (StringRef Name,
3941
- const clang::MacroInfo *Macro) {
3942
- return Impl.shouldIgnoreMacro (Name, Macro);
3943
- }
3944
-
3945
3974
void ClangImporter::printStatistics () const {
3946
3975
Impl.Instance ->getModuleManager ()->PrintStats ();
3947
3976
}
0 commit comments