Skip to content

Commit 8e000fb

Browse files
committed
[Clang importer] Factor mapping of macro names into a single location. NFC
1 parent 58e4340 commit 8e000fb

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ class ClangImporter final : public ClangModuleLoader {
231231
/// Otherwise, return nullptr.
232232
Decl *importDeclCached(const clang::NamedDecl *ClangDecl);
233233

234-
/// Returns true if it is expected that the macro is ignored.
235-
bool shouldIgnoreMacro(StringRef Name, const clang::MacroInfo *Macro);
236-
237234
/// Returns the name of the given enum element as it would be imported into
238235
/// Swift.
239236
///

lib/ClangImporter/ClangImporter.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,6 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
740740
clang::MacroDirective *MD = pp.getLocalMacroDirectiveHistory(macro.first);
741741
if (!MD) continue;
742742

743-
// Import the name.
744-
auto name = importIdentifier(macro.first);
745-
if (name.empty()) continue;
746-
747743
// Walk the history.
748744
for (; MD; MD = MD->getPrevious()) {
749745
// Check whether we have a macro defined in this module.
@@ -766,6 +762,8 @@ void ClangImporter::Implementation::addMacrosToLookupTable(
766762
break;
767763

768764
// Add this entry.
765+
auto name = importMacroName(macro.first, info);
766+
if (name.empty()) continue;
769767
table.addEntry(name, info, clangCtx.getTranslationUnitDecl());
770768
}
771769
}
@@ -2020,6 +2018,44 @@ static bool canStripModulePrefix(StringRef name) {
20202018
return true;
20212019
}
20222020

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+
20232059
auto ClangImporter::Implementation::importFullName(
20242060
const clang::NamedDecl *D,
20252061
ImportNameOptions options,
@@ -3439,12 +3475,11 @@ void ClangImporter::lookupBridgingHeaderDecls(
34393475
}
34403476
}
34413477
}
3442-
ASTContext &Ctx = Impl.SwiftContext;
34433478
auto &ClangPP = Impl.getClangPreprocessor();
34443479
for (clang::IdentifierInfo *II : Impl.BridgeHeaderMacros) {
34453480
if (auto *MI = ClangPP.getMacroInfo(II)) {
34463481
if (filter(MI)) {
3447-
Identifier Name = Ctx.getIdentifier(II->getName());
3482+
Identifier Name = Impl.importMacroName(II, MI);
34483483
if (Decl *imported = Impl.importMacro(Name, MI))
34493484
receiver(imported);
34503485
}
@@ -3460,7 +3495,6 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
34603495
if (!File)
34613496
return true;
34623497

3463-
ASTContext &Ctx = Impl.SwiftContext;
34643498
auto &ClangCtx = getClangASTContext();
34653499
auto &ClangSM = ClangCtx.getSourceManager();
34663500
auto &ClangPP = getClangPreprocessor();
@@ -3522,7 +3556,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
35223556
auto *II = const_cast<clang::IdentifierInfo*>(MD->getName());
35233557
if (auto *MI = ClangPP.getMacroInfo(II)) {
35243558
if (filter(MI)) {
3525-
Identifier Name = Ctx.getIdentifier(II->getName());
3559+
Identifier Name = Impl.importMacroName(II, MI);
35263560
if (Decl *imported = Impl.importMacro(Name, MI))
35273561
receiver(imported);
35283562
}
@@ -3937,11 +3971,6 @@ Decl *ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
39373971
return Impl.importDeclCached(ClangDecl);
39383972
}
39393973

3940-
bool ClangImporter::shouldIgnoreMacro(StringRef Name,
3941-
const clang::MacroInfo *Macro) {
3942-
return Impl.shouldIgnoreMacro(Name, Macro);
3943-
}
3944-
39453974
void ClangImporter::printStatistics() const {
39463975
Impl.Instance->getModuleManager()->PrintStats();
39473976
}

lib/ClangImporter/ImportMacro.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -225,40 +225,12 @@ static bool isSignToken(const clang::Token &tok) {
225225
tok.is(clang::tok::tilde);
226226
}
227227

228-
/// Returns true if it is expected that the macro is ignored.
229-
bool ClangImporter::Implementation::shouldIgnoreMacro(StringRef name,
230-
const clang::MacroInfo *macro) {
231-
// Ignore include guards.
232-
if (macro->isUsedForHeaderGuard())
233-
return true;
234-
235-
if (macro->tokens_empty())
236-
return true;
237-
238-
// Consult the blacklist of macros to suppress.
239-
auto suppressMacro =
240-
llvm::StringSwitch<bool>(name)
241-
#define SUPPRESS_MACRO(NAME) .Case(#NAME, true)
242-
#include "MacroTable.def"
243-
.Default(false);
244-
245-
if (suppressMacro)
246-
return true;
247-
248-
return false;
249-
}
250-
251228
static ValueDecl *importMacro(ClangImporter::Implementation &impl,
252229
DeclContext *DC,
253230
Identifier name,
254231
const clang::MacroInfo *macro,
255232
const clang::MacroInfo *ClangN) {
256-
if (impl.shouldIgnoreMacro(name.str(), macro))
257-
return nullptr;
258-
259-
// Currently we only convert non-function-like macros.
260-
if (macro->isFunctionLike())
261-
return nullptr;
233+
if (name.empty()) return nullptr;
262234

263235
auto numTokens = macro->getNumTokens();
264236
auto tokenI = macro->tokens_begin(), tokenE = macro->tokens_end();

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
872872
clang::DeclContext **effectiveContext = nullptr,
873873
clang::Sema *clangSemaOverride = nullptr);
874874

875+
/// Imports the name of the given Clang macro into Swift.
876+
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
877+
const clang::MacroInfo *macro);
878+
875879
/// \brief Import the given Clang identifier into Swift.
876880
///
877881
/// \param identifier The Clang identifier to map into Swift.
@@ -906,9 +910,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
906910
/// translated into Swift.
907911
ValueDecl *importMacro(Identifier name, clang::MacroInfo *macro);
908912

909-
/// Returns true if it is expected that the macro is ignored.
910-
bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro);
911-
912913
/// \brief Classify the given Clang enumeration type to describe how it
913914
/// should be imported
914915
static EnumKind classifyEnum(clang::Preprocessor &pp,

0 commit comments

Comments
 (0)