Skip to content

Commit

Permalink
Clang importer: omit non-accessibility property getters/setters from …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
DougGregor committed Dec 3, 2015
1 parent 5776fa5 commit 9ee502b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
32 changes: 27 additions & 5 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,31 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
void ClangImporter::Implementation::addEntryToLookupTable(
SwiftLookupTable &table, clang::NamedDecl *named)
{
// If we have a name to import as, add this entry to the table.
clang::DeclContext *effectiveContext;
if (DeclName name = importFullName(named, nullptr, &effectiveContext)) {
table.addEntry(name, named, effectiveContext);
// Determine whether this declaration is suppressed in Swift.
bool suppressDecl = false;
if (auto objcMethod = dyn_cast<clang::ObjCMethodDecl>(named)) {
// If this member is a method that is a getter or setter for a
// property, don't add it into the table. property names and
// getter names (by choosing to only have a property).
//
// Note that this is suppressed for certain accessibility declarations,
// which are imported as getter/setter pairs and not properties.
if (objcMethod->isPropertyAccessor() && !isAccessibilityDecl(objcMethod)) {
suppressDecl = true;
}
} else if (auto objcProperty = dyn_cast<clang::ObjCPropertyDecl>(named)) {
// Suppress certain accessibility properties; they're imported as
// getter/setter pairs instead.
if (isAccessibilityDecl(objcProperty))
suppressDecl = true;
}

if (!suppressDecl) {
// If we have a name to import as, add this entry to the table.
clang::DeclContext *effectiveContext;
if (DeclName name = importFullName(named, nullptr, &effectiveContext)) {
table.addEntry(name, named, effectiveContext);
}
}

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

Expand Down
3 changes: 3 additions & 0 deletions test/IDE/Inputs/swift_name_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ SWIFT_NAME(SomeProtocol)
@protocol SNCollision
@end

@protocol NSAccessibility
@property (nonatomic) float accessibilityFloat;
@end
20 changes: 11 additions & 9 deletions test/IDE/dump_swift_lookup_tables_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
// REQUIRES: objc_interop

// CHECK: Base -> full name mappings:
// CHECK-NEXT: NSAccessibility --> NSAccessibility
// CHECK-NEXT: SNCollision --> SNCollision
// CHECK-NEXT: SNCollisionProtocol --> SNCollisionProtocol
// CHECK-NEXT: SomeClass --> SomeClass
// CHECK-NEXT: SomeProtocol --> SomeProtocol
// CHECK-NEXT: accessibilityFloat --> accessibilityFloat()
// CHECK-NEXT: categoryMethodWithX --> categoryMethodWithX(_:y:), categoryMethodWithX(_:y:z:)
// CHECK-NEXT: doubleProperty --> doubleProperty, doubleProperty()
// CHECK-NEXT: doubleProperty --> doubleProperty{{$}}
// CHECK-NEXT: extensionMethodWithX --> extensionMethodWithX(_:y:)
// CHECK-NEXT: floatProperty --> floatProperty, floatProperty()
// CHECK-NEXT: floatProperty --> floatProperty{{$}}
// CHECK-NEXT: initWithFloat --> initWithFloat(_:)
// CHECK-NEXT: instanceMethodWithX --> instanceMethodWithX(_:y:z:)
// CHECK-NEXT: protoInstanceMethodWithX --> protoInstanceMethodWithX(_:y:)
// CHECK-NEXT: setDoubleProperty --> setDoubleProperty(_:)
// CHECK-NEXT: setAccessibilityFloat --> setAccessibilityFloat(_:)
// CHECK-NEXT: someClassWithDouble --> someClassWithDouble(_:)

// CHECK: Full name -> entry mappings:
// CHECK-NEXT: NSAccessibility:
// CHECK-NEXT: TU: NSAccessibility{{$}}
// CHECK-NEXT: SNCollision:
// CHECK-NEXT: TU: SNCollision{{$}}
// CHECK-NEXT: SNCollisionProtocol:
Expand All @@ -27,27 +31,25 @@
// CHECK-NEXT: TU: SNSomeClass
// CHECK-NEXT: SomeProtocol:
// CHECK-NEXT: TU: SNSomeProtocol
// CHECK-NEXT: accessibilityFloat():
// CHECK-NEXT: NSAccessibility: -[NSAccessibility accessibilityFloat]
// CHECK-NEXT: categoryMethodWithX(_:y:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass categoryMethodWithX:y:]
// CHECK-NEXT: categoryMethodWithX(_:y:z:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass categoryMethodWithX:y:z:]
// CHECK-NEXT: doubleProperty:
// CHECK-NEXT: SNSomeClass: SNSomeClass.doubleProperty
// CHECK-NEXT: doubleProperty():
// CHECK-NEXT: SNSomeClass: -[SNSomeClass doubleProperty]
// CHECK-NEXT: extensionMethodWithX(_:y:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass extensionMethodWithX:y:]
// CHECK-NEXT: floatProperty:
// CHECK-NEXT: SNSomeClass: SNSomeClass.floatProperty
// CHECK-NEXT: floatProperty():
// CHECK-NEXT: SNSomeClass: -[SNSomeClass floatProperty]
// CHECK-NEXT: initWithFloat(_:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass initWithFloat:]
// CHECK-NEXT: instanceMethodWithX(_:y:z:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass instanceMethodWithX:y:z:]
// CHECK-NEXT: protoInstanceMethodWithX(_:y:):
// CHECK-NEXT: SNSomeProtocol: -[SNSomeProtocol protoInstanceMethodWithX:y:]
// CHECK-NEXT: setDoubleProperty(_:):
// CHECK-NEXT: SNSomeClass: -[SNSomeClass setDoubleProperty:]
// CHECK-NEXT: setAccessibilityFloat(_:):
// CHECK-NEXT: NSAccessibility: -[NSAccessibility setAccessibilityFloat:]
// CHECK-NEXT: someClassWithDouble(_:):
// CHECK-NEXT: SNSomeClass: +[SNSomeClass someClassWithDouble:]

0 comments on commit 9ee502b

Please sign in to comment.