Skip to content

Commit 9f67343

Browse files
committed
[Clang importer] Factor the checking for -enable-swift-newtype more sensibly. NFC
1 parent 857df29 commit 9f67343

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,13 +2034,12 @@ static bool moduleIsInferImportAsMember(const clang::NamedDecl *decl,
20342034

20352035
// If this decl is associated with a swift_newtype typedef, return it, otherwise
20362036
// null
2037-
static clang::TypedefNameDecl *findSwiftNewtype(const clang::Decl *decl,
2038-
bool honorSwiftNewtypeAttr) {
2039-
if (!honorSwiftNewtypeAttr) return nullptr;
2040-
2037+
static clang::TypedefNameDecl *findSwiftNewtype(
2038+
ClangImporter::Implementation &impl,
2039+
const clang::Decl *decl) {
20412040
if (auto varDecl = dyn_cast<clang::VarDecl>(decl))
20422041
if (auto typedefTy = varDecl->getType()->getAs<clang::TypedefType>())
2043-
if (typedefTy->getDecl()->hasAttr<clang::SwiftNewtypeAttr>())
2042+
if (impl.getSwiftNewtypeAttr(typedefTy->getDecl()))
20442043
return typedefTy->getDecl();
20452044

20462045
return nullptr;
@@ -2081,7 +2080,7 @@ auto ClangImporter::Implementation::importFullName(
20812080
break;
20822081
}
20832082
// Import onto a swift_newtype if present
2084-
} else if (auto newtypeDecl = findSwiftNewtype(D, HonorSwiftNewtypeAttr)) {
2083+
} else if (auto newtypeDecl = findSwiftNewtype(*this, D)) {
20852084
result.EffectiveContext = newtypeDecl;
20862085
// Everything else goes into its redeclaration context.
20872086
} else {
@@ -2556,7 +2555,7 @@ auto ClangImporter::Implementation::importFullName(
25562555

25572556
// swift_newtype-ed declarations may have common words with the type name
25582557
// stripped.
2559-
if (auto newtypeDecl = findSwiftNewtype(D, HonorSwiftNewtypeAttr)) {
2558+
if (auto newtypeDecl = findSwiftNewtype(*this, D)) {
25602559
// Skip a leading 'k' in a 'kConstant' pattern
25612560
if (baseName.size() >= 2 && baseName[0] == 'k' &&
25622561
clang::isUppercase(baseName[1]))

lib/ClangImporter/ImportDecl.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,9 +1300,8 @@ namespace {
13001300
return nullptr;
13011301

13021302
// Check for swift_newtype
1303-
if (!SwiftType && Impl.HonorSwiftNewtypeAttr) {
1304-
if (auto newtypeAttr =
1305-
Decl->template getAttr<clang::SwiftNewtypeAttr>()) {
1303+
if (!SwiftType) {
1304+
if (auto newtypeAttr = Impl.getSwiftNewtypeAttr(Decl)) {
13061305
switch (newtypeAttr->getNewtypeKind()) {
13071306
case clang::SwiftNewtypeAttr::NK_Enum:
13081307
// TODO: import as closed enum instead
@@ -5597,6 +5596,17 @@ canSkipOverTypedef(ClangImporter::Implementation &Impl,
55975596
return UnderlyingDecl;
55985597
}
55995598

5599+
clang::SwiftNewtypeAttr *ClangImporter::Implementation::getSwiftNewtypeAttr(
5600+
const clang::TypedefNameDecl *decl) {
5601+
// If we aren't honoring the swift_newtype attribute, don't even
5602+
// bother looking.
5603+
if (!HonorSwiftNewtypeAttr)
5604+
return nullptr;
5605+
5606+
// Retrieve the attribute.
5607+
return decl->getAttr<clang::SwiftNewtypeAttr>();
5608+
}
5609+
56005610
/// Import Clang attributes as Swift attributes.
56015611
void ClangImporter::Implementation::importAttributes(
56025612
const clang::NamedDecl *ClangDecl,

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,7 @@ namespace {
551551
Type mappedType = decl->getDeclaredType();
552552
ImportHint hint = ImportHint::None;
553553

554-
if (Impl.HonorSwiftNewtypeAttr &&
555-
type->getDecl()->hasAttr<clang::SwiftNewtypeAttr>()) {
554+
if (Impl.getSwiftNewtypeAttr(type->getDecl())) {
556555
hint = ImportHint::SwiftNewtype;
557556

558557
// For certain special typedefs, we don't want to use the imported type.

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
973973
/// translated into Swift.
974974
ValueDecl *importMacro(Identifier name, clang::MacroInfo *macro);
975975

976+
/// Find the swift_newtype attribute on the given typedef, if present.
977+
clang::SwiftNewtypeAttr *getSwiftNewtypeAttr(
978+
const clang::TypedefNameDecl *decl);
979+
976980
/// Import attributes from the given Clang declaration to its Swift
977981
/// equivalent.
978982
///

0 commit comments

Comments
 (0)