Skip to content

Commit 28757ab

Browse files
committed
[ClangImporter] Don't put forward-declared structs in the lookup table. (#2610)
Merge pull request #2610 from jrose-apple/cg-import-as-member
2 parents 24c212d + 24030eb commit 28757ab

File tree

7 files changed

+198
-171
lines changed

7 files changed

+198
-171
lines changed

apinotes/CoreGraphics.apinotes

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

lib/ClangImporter/ClangImporter.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,24 @@ void ClangImporter::Implementation::addEntryToLookupTable(
775775
// Determine whether this declaration is suppressed in Swift.
776776
if (shouldSuppressDeclImport(named)) return;
777777

778+
// Leave incomplete struct/enum/union types out of the table; Swift only
779+
// handles pointers to them.
780+
// FIXME: At some point we probably want to be importing incomplete types,
781+
// so that pointers to different incomplete types themselves have distinct
782+
// types. At that time it will be necessary to make the decision of whether
783+
// or not to import an incomplete type declaration based on whether it's
784+
// actually the struct backing a CF type:
785+
//
786+
// typedef struct CGColor *CGColorRef;
787+
//
788+
// The best way to do this is probably to change CFDatabase.def to include
789+
// struct names when relevant, not just pointer names. That way we can check
790+
// both CFDatabase.def and the objc_bridge attribute and cover all our bases.
791+
if (auto *tagDecl = dyn_cast<clang::TagDecl>(named)) {
792+
if (!tagDecl->getDefinition())
793+
return;
794+
}
795+
778796
// If we have a name to import as, add this entry to the table.
779797
if (auto importedName = importFullName(named, None, &clangSema)) {
780798
table.addEntry(importedName.Imported, named, importedName.EffectiveContext);

test/ClangModules/availability.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ func test_dispatch(_ object: dispatch_object_t) {
110110
dispatch_retain(object); // expected-error {{'dispatch_retain' is unavailable}}
111111
dispatch_release(object); // expected-error {{'dispatch_release' is unavailable}}
112112
}
113+
114+
func testImportAsMember() {
115+
_ = CGColorCreateGenericGray(0.5, 1.0) // expected-error {{'CGColorCreateGenericGray' has been replaced by 'CGColor.init(gray:alpha:)'}} {{7-31=CGColor}} {{32-32=gray: }} {{37-37=alpha: }}
116+
_ = CGColor(gray: 0.5, alpha: 1.0)
117+
}

test/IDE/Inputs/custom-modules/ImportAsMemberC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
typedef const void *CFTypeRef __attribute__((objc_bridge(id)));
22

3-
typedef const struct __attribute__((objc_bridge(id))) __CCPowerSupply *CCPowerSupplyRef;
3+
typedef const struct __attribute__((objc_bridge(id))) CCPowerSupply *CCPowerSupplyRef;
44
typedef const struct __attribute__((objc_bridge(id))) __CCRefrigerator *CCRefrigeratorRef;
55
typedef struct __CCRefrigerator *CCMutableRefrigeratorRef;
66

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
// CHECK-NEXT: TU: SNSomeProtocol
4545
// CHECK: UIActionSheet:
4646
// CHECK-NEXT: TU: UIActionSheet
47-
// CHECK-NEXT: __CCItem:
48-
// CHECK-NEXT: TU: __CCItem
4947
// CHECK-NEXT: __swift:
5048
// CHECK-NEXT: TU: __swift
5149
// CHECK-NEXT: accessibilityFloat:

test/Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ public func ==(lhs: CGFloat, rhs: CGFloat) -> Bool {
3535
return lhs.value == rhs.value
3636
}
3737

38-
extension CGFloat : IntegerLiteralConvertible, Equatable {
38+
extension CGFloat : IntegerLiteralConvertible, FloatLiteralConvertible, Equatable {
3939
public init(integerLiteral value: UnderlyingType) {
4040
self.value = value
4141
}
42+
43+
public init(floatLiteral value: UnderlyingType) {
44+
self.value = value
45+
}
4246
}
4347

4448
public extension Double {

test/Inputs/clang-importer-sdk/usr/include/CoreGraphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ typedef struct CGRect CGRect;
3434
typedef CGRect CGRectTy;
3535

3636
typedef struct CGColor *CGColorRef;
37+
38+
CGColorRef CGColorCreateGenericGray(CGFloat gray, CGFloat alpha);

0 commit comments

Comments
 (0)