Skip to content

[Clang Importer] Import ns_error_domain attribute with _BridgedNSError #1192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ClangImporter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_swift_library(swiftClangImporter
ClangDiagnosticConsumer.cpp
ClangImporter.cpp
ImportDecl.cpp
ImportEnumInfo.cpp
ImportMacro.cpp
ImportType.cpp
SwiftLookupTable.cpp
Expand Down
239 changes: 6 additions & 233 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <memory>

using namespace swift;
using namespace importer;

// Commonly-used Clang classes.
using clang::CompilerInstance;
Expand Down Expand Up @@ -1359,235 +1360,6 @@ ClangImporter::Implementation::exportName(Identifier name) {
return ident;
}

/// \brief Returns the common prefix of two strings at camel-case word
/// granularity.
///
/// For example, given "NSFooBar" and "NSFooBas", returns "NSFoo"
/// (not "NSFooBa"). The returned StringRef is a slice of the "a" argument.
///
/// If either string has a non-identifier character immediately after the
/// prefix, \p followedByNonIdentifier will be set to \c true. If both strings
/// have identifier characters after the prefix, \p followedByNonIdentifier will
/// be set to \c false. Otherwise, \p followedByNonIdentifier will not be
/// changed from its initial value.
///
/// This is used to derive the common prefix of enum constants so we can elide
/// it from the Swift interface.
static StringRef getCommonWordPrefix(StringRef a, StringRef b,
bool &followedByNonIdentifier) {
auto aWords = camel_case::getWords(a), bWords = camel_case::getWords(b);
auto aI = aWords.begin(), aE = aWords.end(),
bI = bWords.begin(), bE = bWords.end();

unsigned prevLength = 0;
unsigned prefixLength = 0;
for ( ; aI != aE && bI != bE; ++aI, ++bI) {
if (*aI != *bI) {
followedByNonIdentifier = false;
break;
}

prevLength = prefixLength;
prefixLength = aI.getPosition() + aI->size();
}

// Avoid creating a prefix where the rest of the string starts with a number.
if ((aI != aE && !Lexer::isIdentifier(*aI)) ||
(bI != bE && !Lexer::isIdentifier(*bI))) {
followedByNonIdentifier = true;
prefixLength = prevLength;
}

return a.slice(0, prefixLength);
}

/// Returns the common word-prefix of two strings, allowing the second string
/// to be a common English plural form of the first.
///
/// For example, given "NSProperty" and "NSProperties", the full "NSProperty"
/// is returned. Given "NSMagicArmor" and "NSMagicArmory", only
/// "NSMagic" is returned.
///
/// The "-s", "-es", and "-ies" patterns cover every plural NS_OPTIONS name
/// in Cocoa and Cocoa Touch.
///
/// \see getCommonWordPrefix
static StringRef getCommonPluralPrefix(StringRef singular, StringRef plural) {
assert(!plural.empty());

if (singular.empty())
return singular;

bool ignored;
StringRef commonPrefix = getCommonWordPrefix(singular, plural, ignored);
if (commonPrefix.size() == singular.size() || plural.back() != 's')
return commonPrefix;

StringRef leftover = singular.substr(commonPrefix.size());
StringRef firstLeftoverWord = camel_case::getFirstWord(leftover);
StringRef commonPrefixPlusWord =
singular.substr(0, commonPrefix.size() + firstLeftoverWord.size());

// Is the plural string just "[singular]s"?
plural = plural.drop_back();
if (plural.endswith(firstLeftoverWord))
return commonPrefixPlusWord;

if (plural.empty() || plural.back() != 'e')
return commonPrefix;

// Is the plural string "[singular]es"?
plural = plural.drop_back();
if (plural.endswith(firstLeftoverWord))
return commonPrefixPlusWord;

if (plural.empty() || !(plural.back() == 'i' && singular.back() == 'y'))
return commonPrefix;

// Is the plural string "[prefix]ies" and the singular "[prefix]y"?
plural = plural.drop_back();
firstLeftoverWord = firstLeftoverWord.drop_back();
if (plural.endswith(firstLeftoverWord))
return commonPrefixPlusWord;

return commonPrefix;
}

StringRef ClangImporter::Implementation::getEnumConstantNamePrefix(
clang::Sema &sema,
const clang::EnumDecl *decl) {
switch (classifyEnum(sema.getPreprocessor(), decl)) {
case EnumKind::Enum:
case EnumKind::Options:
// Enums are mapped to Swift enums, Options to Swift option sets, both
// of which attempt prefix-stripping.
break;

case EnumKind::Constants:
case EnumKind::Unknown:
// Nothing to do.
return StringRef();
}

// If there are no enumers, there is no prefix to compute.
auto ec = decl->enumerator_begin(), ecEnd = decl->enumerator_end();
if (ec == ecEnd)
return StringRef();

// Determine whether we can cache the result.
// FIXME: Pass in a cache?
bool useCache = &sema == &getClangSema();

// If we've already computed the prefix, return it.
auto known = useCache ? EnumConstantNamePrefixes.find(decl)
: EnumConstantNamePrefixes.end();
if (known != EnumConstantNamePrefixes.end())
return known->second;

// Determine whether the given enumerator is non-deprecated and has no
// specifically-provided name.
auto isNonDeprecatedWithoutCustomName =
[](const clang::EnumConstantDecl *elem) -> bool {
if (elem->hasAttr<clang::SwiftNameAttr>())
return false;

clang::VersionTuple maxVersion{~0U, ~0U, ~0U};
switch (elem->getAvailability(nullptr, maxVersion)) {
case clang::AR_Available:
case clang::AR_NotYetIntroduced:
for (auto attr : elem->attrs()) {
if (auto annotate = dyn_cast<clang::AnnotateAttr>(attr)) {
if (annotate->getAnnotation() == "swift1_unavailable")
return false;
}
if (auto avail = dyn_cast<clang::AvailabilityAttr>(attr)) {
if (avail->getPlatform()->getName() == "swift")
return false;
}
}
return true;

case clang::AR_Deprecated:
case clang::AR_Unavailable:
return false;
}
};

// Move to the first non-deprecated enumerator, or non-swift_name'd
// enumerator, if present.
auto firstNonDeprecated = std::find_if(ec, ecEnd,
isNonDeprecatedWithoutCustomName);
bool hasNonDeprecated = (firstNonDeprecated != ecEnd);
if (hasNonDeprecated) {
ec = firstNonDeprecated;
} else {
// Advance to the first case without a custom name, deprecated or not.
while (ec != ecEnd && (*ec)->hasAttr<clang::SwiftNameAttr>())
++ec;
if (ec == ecEnd) {
if (useCache)
EnumConstantNamePrefixes.insert({decl, StringRef()});
return StringRef();
}
}

// Compute the common prefix.
StringRef commonPrefix = (*ec)->getName();
bool followedByNonIdentifier = false;
for (++ec; ec != ecEnd; ++ec) {
// Skip deprecated or swift_name'd enumerators.
const clang::EnumConstantDecl *elem = *ec;
if (hasNonDeprecated) {
if (!isNonDeprecatedWithoutCustomName(elem))
continue;
} else {
if (elem->hasAttr<clang::SwiftNameAttr>())
continue;
}

commonPrefix = getCommonWordPrefix(commonPrefix, elem->getName(),
followedByNonIdentifier);
if (commonPrefix.empty())
break;
}

if (!commonPrefix.empty()) {
StringRef checkPrefix = commonPrefix;

// Account for the 'kConstant' naming convention on enumerators.
if (checkPrefix[0] == 'k') {
bool canDropK;
if (checkPrefix.size() >= 2)
canDropK = clang::isUppercase(checkPrefix[1]);
else
canDropK = !followedByNonIdentifier;

if (canDropK)
checkPrefix = checkPrefix.drop_front();
}

// Don't use importFullName() here, we want to ignore the swift_name
// and swift_private attributes.
StringRef enumNameStr = decl->getName();
StringRef commonWithEnum = getCommonPluralPrefix(checkPrefix,
enumNameStr);
size_t delta = commonPrefix.size() - checkPrefix.size();

// Account for the 'EnumName_Constant' convention on enumerators.
if (commonWithEnum.size() < checkPrefix.size() &&
checkPrefix[commonWithEnum.size()] == '_' &&
!followedByNonIdentifier) {
delta += 1;
}

commonPrefix = commonPrefix.slice(0, commonWithEnum.size() + delta);
}

if (useCache)
EnumConstantNamePrefixes.insert({decl, commonPrefix});
return commonPrefix;
}

/// Determine whether the given Clang selector matches the given
/// selector pieces.
static bool isNonNullarySelector(clang::Selector selector,
Expand Down Expand Up @@ -2038,7 +1810,7 @@ auto ClangImporter::Implementation::importFullName(
// scope, depending how their enclosing enumeration is imported.
if (isa<clang::EnumConstantDecl>(D)) {
auto enumDecl = cast<clang::EnumDecl>(dc);
switch (classifyEnum(clangSema.getPreprocessor(), enumDecl)) {
switch (getEnumKind(enumDecl, &clangSema.getPreprocessor())) {
case EnumKind::Enum:
case EnumKind::Options:
// Enums are mapped to Swift enums, Options to Swift option sets.
Expand Down Expand Up @@ -2289,7 +2061,8 @@ auto ClangImporter::Implementation::importFullName(
// Enumeration constants may have common prefixes stripped.
if (isa<clang::EnumConstantDecl>(D)) {
auto enumDecl = cast<clang::EnumDecl>(D->getDeclContext());
StringRef removePrefix = getEnumConstantNamePrefix(clangSema, enumDecl);
StringRef removePrefix =
getEnumConstantNamePrefix(enumDecl, &clangSema.getPreprocessor());
if (baseName.startswith(removePrefix))
baseName = baseName.substr(removePrefix.size());
}
Expand Down Expand Up @@ -2382,15 +2155,15 @@ auto ClangImporter::Implementation::importFullName(

// Local function to determine whether the given declaration is subject to
// a swift_private attribute.
auto hasSwiftPrivate = [&clangSema](const clang::NamedDecl *D) {
auto hasSwiftPrivate = [&clangSema, this](const clang::NamedDecl *D) {
if (D->hasAttr<clang::SwiftPrivateAttr>())
return true;

// Enum constants that are not imported as members should be considered
// private if the parent enum is marked private.
if (auto *ECD = dyn_cast<clang::EnumConstantDecl>(D)) {
auto *ED = cast<clang::EnumDecl>(ECD->getDeclContext());
switch (classifyEnum(clangSema.getPreprocessor(), ED)) {
switch (getEnumKind(ED, &clangSema.getPreprocessor())) {
case EnumKind::Constants:
case EnumKind::Unknown:
if (ED->hasAttr<clang::SwiftPrivateAttr>())
Expand Down
Loading