Skip to content

Commit eb9f047

Browse files
authored
[PrintAsObjC] Handle the importer's compatibility typealiases. (#10042)
These are TypeAliasDecls whose Clang nodes are not TypedefNameDecls. This worked all right for classes, but dropped the tag keyword (e.g. 'struct') for tag decls with names of their own, and didn't print any name at all for C types that used the typedef-for-anonymous-tag pattern. rdar://problem/32514335
1 parent 3e2bbfe commit eb9f047

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

lib/AST/SwiftNameTranslation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ using namespace swift;
2828
StringRef swift::objc_translation::
2929
getNameForObjC(const ValueDecl *VD, CustomNamesOnly_t customNamesOnly) {
3030
assert(isa<ClassDecl>(VD) || isa<ProtocolDecl>(VD) || isa<StructDecl>(VD) ||
31-
isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD));
31+
isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD) ||
32+
isa<TypeAliasDecl>(VD));
3233
if (auto objc = VD->getAttrs().getAttribute<ObjCAttr>()) {
3334
if (auto name = objc->getName()) {
3435
assert(name->getNumSelectorPieces() == 1);

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
13661366
if (alias->hasClangNode()) {
13671367
if (auto *clangTypeDecl =
13681368
dyn_cast<clang::TypeDecl>(alias->getClangDecl())) {
1369-
os << clangTypeDecl->getName();
1369+
maybePrintTagKeyword(alias);
1370+
os << getNameForObjC(alias);
13701371

13711372
if (isClangPointerType(clangTypeDecl))
13721373
printNullability(optionalKind);
@@ -1392,7 +1393,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
13921393
visitPart(alias->getUnderlyingTypeLoc().getType(), optionalKind);
13931394
}
13941395

1395-
void maybePrintTagKeyword(const NominalTypeDecl *NTD) {
1396+
void maybePrintTagKeyword(const TypeDecl *NTD) {
13961397
if (isa<EnumDecl>(NTD) && !NTD->hasClangNode()) {
13971398
os << "enum ";
13981399
return;

test/PrintAsObjC/Inputs/custom-modules/NestedClass.apinotes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ SwiftVersions:
44
Classes:
55
- Name: InnerClass
66
SwiftName: InnerClass
7+
Tags:
8+
- Name: InnerStruct
9+
SwiftName: InnerStruct
10+
Typedefs:
11+
- Name: InnerAlias
12+
SwiftName: InnerAlias
13+
- Name: InnerAnonStruct
14+
SwiftName: InnerAnonStruct

test/PrintAsObjC/Inputs/custom-modules/NestedClass.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@
88
__attribute__((swift_name("Outer.Inner")))
99
@interface InnerClass : NSObject
1010
@end
11+
12+
struct __attribute__((swift_name("Outer.InnerV"))) InnerStruct {
13+
int value;
14+
};
15+
16+
typedef struct {
17+
int value;
18+
} InnerAnonStruct __attribute__((swift_name("Outer.InnerAS")));
19+
20+
typedef int InnerAlias __attribute__((swift_name("Outer.InnerA")));
21+

test/PrintAsObjC/versioned.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import NestedClass
2222
@objc class UsesNestedClass : NSObject {
2323
// CHECK-NEXT: - (InnerClass * _Nullable)foo SWIFT_WARN_UNUSED_RESULT;
2424
@objc func foo() -> InnerClass? { return nil }
25+
// CHECK-NEXT: - (void)fooStruct:(struct InnerStruct)_;
26+
@objc func fooStruct(_: InnerStruct) {}
27+
// CHECK-NEXT: - (void)fooAnonStruct:(InnerAnonStruct)_;
28+
@objc func fooAnonStruct(_: InnerAnonStruct) {}
29+
// CHECK-NEXT: - (void)fooAlias:(InnerAlias)_;
30+
@objc func fooAlias(_: InnerAlias) {}
2531

2632
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
2733
}

0 commit comments

Comments
 (0)