Skip to content

Commit 319e2a7

Browse files
committed
[Clang importer] Strip "NS" prefix from macro names, too
1 parent 06d25f7 commit 319e2a7

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

20892089
Identifier 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

21002129
auto 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

43834419
void 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));

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
874874

875875
/// Imports the name of the given Clang macro into Swift.
876876
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
877-
const clang::MacroInfo *macro);
877+
const clang::MacroInfo *macro,
878+
clang::ASTContext &clangCtx);
878879

879880
/// \brief Import the given Clang identifier into Swift.
880881
///

lib/ClangImporter/SwiftLookupTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const uint16_t SWIFT_LOOKUP_TABLE_VERSION_MAJOR = 1;
5151
/// Lookup table minor version number.
5252
///
5353
/// When the format changes IN ANY WAY, this number should be incremented.
54-
const uint16_t SWIFT_LOOKUP_TABLE_VERSION_MINOR = 2;
54+
const uint16_t SWIFT_LOOKUP_TABLE_VERSION_MINOR = 6;
5555

5656
/// A lookup table that maps Swift names to the set of Clang
5757
/// declarations with that particular name.

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// REQUIRES: objc_interop
88

99
// CHECK-LABEL: <<Foundation lookup table>>
10+
// CHECK: NSTimeIntervalSince1970:
11+
// CHECK: TU: Macro
1012
// CHECK: Categories:{{.*}}NSValue(NSValueCreation){{.*}}
1113

1214
// CHECK-LABEL: <<ObjectiveC lookup table>>
@@ -80,6 +82,10 @@
8082

8183
// CHECK: Categories: SNSomeClass(), SNSomeClass(Category1)
8284

85+
// CHECK-OMIT-NEEDLESS-WORDS-LABEL: <<Foundation lookup table>>
86+
// CHECK-OMIT-NEEDLESS-WORDS: timeIntervalSince1970:
87+
// CHECK-OMIT-NEEDLESS-WORDS: TU: Macro
88+
8389
// CHECK-OMIT-NEEDLESS-WORDS: <<ObjectiveC lookup table>>
8490
// CHECK-OMIT-NEEDLESS-WORDS-NOT: lookup table
8591
// CHECK-OMIT-NEEDLESS-WORDS: respondsTo:

test/IDE/print_omit_needless_words.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@
178178
// Lowercasing initialisms with plurals.
179179
// CHECK-FOUNDATION: var urlsInText: [URL] { get }
180180

181+
// Prefix stripping for macro names.
182+
// CHECK-FOUNDATION: var timeIntervalSince1970: Double { get }
183+
// CHECK-FOUNDATION: var DO_SOMETHING: Int
184+
181185
// Note: class method name stripping context type.
182186
// CHECK-APPKIT: class func red() -> NSColor
183187

test/Inputs/clang-importer-sdk/usr/include/Foundation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,3 +1005,6 @@ extern NSString *NSHTTPRequestKey;
10051005
@interface NSObject (Selectors)
10061006
-(void)messageSomeObject:(nonnull id)object selector:(SEL)selector;
10071007
@end
1008+
1009+
#define NSTimeIntervalSince1970 978307200.0
1010+
#define NS_DO_SOMETHING 17

0 commit comments

Comments
 (0)