Skip to content

Commit 9ee502b

Browse files
committed
Clang importer: omit non-accessibility property getters/setters from lookup table.
The getters and setters for Objective-C @Property declarations are never found by name lookup, so don't introduce them into the Swift lookup tables. Note that we exclude some of the accessibility declarations for unrelated reasons, as we do elsewhere in the importer.
1 parent 5776fa5 commit 9ee502b

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,31 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
705705
void ClangImporter::Implementation::addEntryToLookupTable(
706706
SwiftLookupTable &table, clang::NamedDecl *named)
707707
{
708-
// If we have a name to import as, add this entry to the table.
709-
clang::DeclContext *effectiveContext;
710-
if (DeclName name = importFullName(named, nullptr, &effectiveContext)) {
711-
table.addEntry(name, named, effectiveContext);
708+
// Determine whether this declaration is suppressed in Swift.
709+
bool suppressDecl = false;
710+
if (auto objcMethod = dyn_cast<clang::ObjCMethodDecl>(named)) {
711+
// If this member is a method that is a getter or setter for a
712+
// property, don't add it into the table. property names and
713+
// getter names (by choosing to only have a property).
714+
//
715+
// Note that this is suppressed for certain accessibility declarations,
716+
// which are imported as getter/setter pairs and not properties.
717+
if (objcMethod->isPropertyAccessor() && !isAccessibilityDecl(objcMethod)) {
718+
suppressDecl = true;
719+
}
720+
} else if (auto objcProperty = dyn_cast<clang::ObjCPropertyDecl>(named)) {
721+
// Suppress certain accessibility properties; they're imported as
722+
// getter/setter pairs instead.
723+
if (isAccessibilityDecl(objcProperty))
724+
suppressDecl = true;
725+
}
726+
727+
if (!suppressDecl) {
728+
// If we have a name to import as, add this entry to the table.
729+
clang::DeclContext *effectiveContext;
730+
if (DeclName name = importFullName(named, nullptr, &effectiveContext)) {
731+
table.addEntry(name, named, effectiveContext);
732+
}
712733
}
713734

714735
// Walk the members of any context that can have nested members.
@@ -1579,7 +1600,8 @@ DeclName ClangImporter::Implementation::importFullName(
15791600
const clang::NamedDecl *D,
15801601
bool *hasCustomName,
15811602
clang::DeclContext **effectiveContext) {
1582-
// Objective-C categories and extensions don't have names.
1603+
// Objective-C categories and extensions don't have names, despite
1604+
// being "named" declarations.
15831605
if (isa<clang::ObjCCategoryDecl>(D))
15841606
return { };
15851607

test/IDE/Inputs/swift_name_objc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ SWIFT_NAME(SomeProtocol)
4343
@protocol SNCollision
4444
@end
4545

46+
@protocol NSAccessibility
47+
@property (nonatomic) float accessibilityFloat;
48+
@end

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
// REQUIRES: objc_interop
55

66
// CHECK: Base -> full name mappings:
7+
// CHECK-NEXT: NSAccessibility --> NSAccessibility
78
// CHECK-NEXT: SNCollision --> SNCollision
89
// CHECK-NEXT: SNCollisionProtocol --> SNCollisionProtocol
910
// CHECK-NEXT: SomeClass --> SomeClass
1011
// CHECK-NEXT: SomeProtocol --> SomeProtocol
12+
// CHECK-NEXT: accessibilityFloat --> accessibilityFloat()
1113
// CHECK-NEXT: categoryMethodWithX --> categoryMethodWithX(_:y:), categoryMethodWithX(_:y:z:)
12-
// CHECK-NEXT: doubleProperty --> doubleProperty, doubleProperty()
14+
// CHECK-NEXT: doubleProperty --> doubleProperty{{$}}
1315
// CHECK-NEXT: extensionMethodWithX --> extensionMethodWithX(_:y:)
14-
// CHECK-NEXT: floatProperty --> floatProperty, floatProperty()
16+
// CHECK-NEXT: floatProperty --> floatProperty{{$}}
1517
// CHECK-NEXT: initWithFloat --> initWithFloat(_:)
1618
// CHECK-NEXT: instanceMethodWithX --> instanceMethodWithX(_:y:z:)
1719
// CHECK-NEXT: protoInstanceMethodWithX --> protoInstanceMethodWithX(_:y:)
18-
// CHECK-NEXT: setDoubleProperty --> setDoubleProperty(_:)
20+
// CHECK-NEXT: setAccessibilityFloat --> setAccessibilityFloat(_:)
1921
// CHECK-NEXT: someClassWithDouble --> someClassWithDouble(_:)
2022

2123
// CHECK: Full name -> entry mappings:
24+
// CHECK-NEXT: NSAccessibility:
25+
// CHECK-NEXT: TU: NSAccessibility{{$}}
2226
// CHECK-NEXT: SNCollision:
2327
// CHECK-NEXT: TU: SNCollision{{$}}
2428
// CHECK-NEXT: SNCollisionProtocol:
@@ -27,27 +31,25 @@
2731
// CHECK-NEXT: TU: SNSomeClass
2832
// CHECK-NEXT: SomeProtocol:
2933
// CHECK-NEXT: TU: SNSomeProtocol
34+
// CHECK-NEXT: accessibilityFloat():
35+
// CHECK-NEXT: NSAccessibility: -[NSAccessibility accessibilityFloat]
3036
// CHECK-NEXT: categoryMethodWithX(_:y:):
3137
// CHECK-NEXT: SNSomeClass: -[SNSomeClass categoryMethodWithX:y:]
3238
// CHECK-NEXT: categoryMethodWithX(_:y:z:):
3339
// CHECK-NEXT: SNSomeClass: -[SNSomeClass categoryMethodWithX:y:z:]
3440
// CHECK-NEXT: doubleProperty:
3541
// CHECK-NEXT: SNSomeClass: SNSomeClass.doubleProperty
36-
// CHECK-NEXT: doubleProperty():
37-
// CHECK-NEXT: SNSomeClass: -[SNSomeClass doubleProperty]
3842
// CHECK-NEXT: extensionMethodWithX(_:y:):
3943
// CHECK-NEXT: SNSomeClass: -[SNSomeClass extensionMethodWithX:y:]
4044
// CHECK-NEXT: floatProperty:
4145
// CHECK-NEXT: SNSomeClass: SNSomeClass.floatProperty
42-
// CHECK-NEXT: floatProperty():
43-
// CHECK-NEXT: SNSomeClass: -[SNSomeClass floatProperty]
4446
// CHECK-NEXT: initWithFloat(_:):
4547
// CHECK-NEXT: SNSomeClass: -[SNSomeClass initWithFloat:]
4648
// CHECK-NEXT: instanceMethodWithX(_:y:z:):
4749
// CHECK-NEXT: SNSomeClass: -[SNSomeClass instanceMethodWithX:y:z:]
4850
// CHECK-NEXT: protoInstanceMethodWithX(_:y:):
4951
// CHECK-NEXT: SNSomeProtocol: -[SNSomeProtocol protoInstanceMethodWithX:y:]
50-
// CHECK-NEXT: setDoubleProperty(_:):
51-
// CHECK-NEXT: SNSomeClass: -[SNSomeClass setDoubleProperty:]
52+
// CHECK-NEXT: setAccessibilityFloat(_:):
53+
// CHECK-NEXT: NSAccessibility: -[NSAccessibility setAccessibilityFloat:]
5254
// CHECK-NEXT: someClassWithDouble(_:):
5355
// CHECK-NEXT: SNSomeClass: +[SNSomeClass someClassWithDouble:]

0 commit comments

Comments
 (0)