Skip to content

Commit b5aca66

Browse files
jrose-appletkremenek
authored andcommitted
[ClangImporter] Remove importer-based NS stripping. (#3880)
* [ClangImporter] Remove importer-based NS stripping. As Tony puts it, in the end we wound up with more Foundation declarations imported as members or keeping "NS" than those that dropped it, and any further decisions will be made on a case-by-case basis. Move all of the existing cases of prefix-stripping into Foundation's API notes and drop the logic from the compiler. Tested by dumping the generated interface for Foundation and its submodules for both macOS and the iOS simulator, and comparing the results. A few cases did slip through here because of the interaction between "SwiftName" and "Availability: nonswift". The next commit will re-add "NS" to some stragglers that we missed. rdar://problem/26880017 * APINotes: Add "NS" back to a few types. NSKeyedUnarchiverDelegate NSKeyedArchiverDelegate NSTextCheckingTypes NSBinarySearchingOptions NSEnumerationOptions NSSortOptions More rdar://problem/26880017 * Remove now-redundant SwiftNames from API notes. No change observed in the generated interface of Foundation and its submodules. Finishes rdar://problem/26880017.
1 parent 654c523 commit b5aca66

34 files changed

+419
-683
lines changed

apinotes/Foundation.apinotes

Lines changed: 249 additions & 444 deletions
Large diffs are not rendered by default.

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ enum class KnownFoundationEntity {
120120
/// entity name.
121121
Optional<KnownFoundationEntity> getKnownFoundationEntity(StringRef name);
122122

123-
/// Determine with the non-prefixed name of the given known Foundation
124-
/// entity conflicts with the Swift standard library.
125-
bool nameConflictsWithStandardLibrary(KnownFoundationEntity entity);
126-
127123
/// Callback function used when referring to a type member of a given
128124
/// type variable.
129125
typedef std::function<Type(TypeVariableType *, AssociatedTypeDecl *)>

include/swift/AST/KnownFoundationEntities.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ FOUNDATION_ENTITY(NSObject)
3030
FOUNDATION_ENTITY(NSRange)
3131
FOUNDATION_ENTITY(NSSet)
3232
FOUNDATION_ENTITY(NSString)
33-
FOUNDATION_ENTITY(NSStringEncoding)
3433
FOUNDATION_ENTITY(NSUInteger)
3534
FOUNDATION_ENTITY(NSURL)
3635
FOUNDATION_ENTITY(NSZone)

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ namespace swift {
161161
/// and methods.
162162
bool InferImportAsMember = false;
163163

164-
/// Whether we are stripping the "NS" prefix from Foundation et al.
165-
bool StripNSPrefix = true;
166-
167164
/// Should 'id' in Objective-C be imported as 'Any' in Swift?
168165
bool EnableIdAsAny = true;
169166

include/swift/Option/FrontendOptions.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ def enable_swift_newtype :
247247
Flag<["-"], "enable-swift-newtype">,
248248
HelpText<"Enable the swift_newtype attribute">;
249249

250-
def enable_strip_ns_prefix :
251-
Flag<["-"], "enable-strip-ns-prefix">,
252-
HelpText<"Strip 'NS' prefix from Foundation entities">;
253-
254250
def swift3_migration :
255251
Flag<["-"], "swift3-migration">,
256252
HelpText<"Enable Fix-It based migration aids for Swift 3">;

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,29 +2578,6 @@ Optional<KnownFoundationEntity> swift::getKnownFoundationEntity(StringRef name){
25782578
.Default(None);
25792579
}
25802580

2581-
bool swift::nameConflictsWithStandardLibrary(KnownFoundationEntity entity) {
2582-
switch (entity) {
2583-
case KnownFoundationEntity::NSArray:
2584-
case KnownFoundationEntity::NSDictionary:
2585-
case KnownFoundationEntity::NSInteger:
2586-
case KnownFoundationEntity::NSRange:
2587-
case KnownFoundationEntity::NSSet:
2588-
case KnownFoundationEntity::NSString:
2589-
case KnownFoundationEntity::NSCopying:
2590-
case KnownFoundationEntity::NSError:
2591-
case KnownFoundationEntity::NSErrorPointer:
2592-
case KnownFoundationEntity::NSNumber:
2593-
case KnownFoundationEntity::NSObject:
2594-
case KnownFoundationEntity::NSUInteger:
2595-
case KnownFoundationEntity::NSURL:
2596-
case KnownFoundationEntity::NSZone:
2597-
return true;
2598-
2599-
case KnownFoundationEntity::NSStringEncoding:
2600-
return false;
2601-
}
2602-
}
2603-
26042581
StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
26052582
StringRef objcName;
26062583
switch (kind) {
@@ -2610,13 +2587,6 @@ StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
26102587
#include "swift/AST/KnownFoundationEntities.def"
26112588
}
26122589

2613-
// If we're omitting needless words and the name won't conflict with
2614-
// something in the standard library, strip the prefix off the Swift
2615-
// name.
2616-
if (LangOpts.StripNSPrefix &&
2617-
!nameConflictsWithStandardLibrary(kind))
2618-
return objcName.substr(2);
2619-
26202590
return objcName;
26212591
}
26222592

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,13 +1309,6 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
13091309
DeprecatedAsUnavailableMessage =
13101310
"APIs deprecated as of OS X 10.9 and earlier are unavailable in Swift";
13111311
}
1312-
1313-
// Prepopulate the set of module prefixes.
1314-
// FIXME: Hard-coded list should move into the module map language.
1315-
if (ctx.LangOpts.StripNSPrefix) {
1316-
ModulePrefixes["Foundation"] = "NS";
1317-
ModulePrefixes["ObjectiveC"] = "NS";
1318-
}
13191312
}
13201313

13211314

@@ -1817,8 +1810,7 @@ static unsigned stripModulePrefixLength(
18171810
// Check whether this is a known Foundation entity that conflicts with the
18181811
// standard library.
18191812
if (auto known = getKnownFoundationEntity(baseName))
1820-
if (nameConflictsWithStandardLibrary(*known))
1821-
return 0;
1813+
return 0;
18221814

18231815
// If the character following the prefix is a '_', eat that, too.
18241816
unsigned prefixLen = prefixPos->second.size();
@@ -4436,7 +4428,6 @@ ClangImporter::Implementation::SwiftNameLookupExtension::hashExtension(
44364428
SWIFT_LOOKUP_TABLE_VERSION_MAJOR,
44374429
SWIFT_LOOKUP_TABLE_VERSION_MINOR,
44384430
Impl.InferImportAsMember,
4439-
Impl.SwiftContext.LangOpts.StripNSPrefix,
44404431
Impl.HonorSwiftNewtypeAttr);
44414432
}
44424433

lib/ClangImporter/MappedTypes.def

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ MAP_STDLIB_TYPE("CFIndex", SignedWord, 0, "Int", false, DefineAndUse)
165165

166166
// Foundation types.
167167
// FIXME: <rdar://problem/16074941> NSStringEncoding doesn't work on 32-bit
168-
MAP_STDLIB_TYPE(
169-
Impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSStringEncoding),
170-
UnsignedWord, 0, "UInt", false, DoNothing)
168+
MAP_STDLIB_TYPE("NSStringEncoding", UnsignedWord, 0, "UInt", false, DoNothing)
171169

172170
#undef MAP_STDLIB_TYPE
173171
#undef MAP_TYPE

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
771771
Opts.Playground |= Args.hasArg(OPT_playground);
772772
Opts.Swift3Migration |= Args.hasArg(OPT_swift3_migration);
773773
Opts.WarnOmitNeedlessWords = Args.hasArg(OPT_warn_omit_needless_words);
774-
Opts.StripNSPrefix |= Args.hasArg(OPT_enable_strip_ns_prefix);
775774
Opts.InferImportAsMember |= Args.hasArg(OPT_enable_infer_import_as_member);
776775

777776
Opts.EnableThrowWithoutTry |= Args.hasArg(OPT_enable_throw_without_try);

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,13 +1100,12 @@ Decl *ModuleFile::resolveCrossReference(Module *M, uint32_t pathLen) {
11001100
// has to go through this path, but it's an option we toggle for
11011101
// testing.
11021102
if (values.empty() && !retrying &&
1103-
getContext().LangOpts.StripNSPrefix &&
11041103
(M->getName().str() == "ObjectiveC" ||
11051104
M->getName().str() == "Foundation")) {
11061105
if (name.str().startswith("NS")) {
11071106
if (name.str().size() > 2 && name.str() != "NSCocoaError") {
11081107
auto known = getKnownFoundationEntity(name.str());
1109-
if (!known || !nameConflictsWithStandardLibrary(*known)) {
1108+
if (!known) {
11101109
name = getContext().getIdentifier(name.str().substr(2));
11111110
retrying = true;
11121111
goto retry;

0 commit comments

Comments
 (0)