Skip to content

Commit beb980b

Browse files
committed
AST: Support @cdecl enums without an explicit C name
Accept `@cdecl` enums without an explicit C name. The C name defaults to the Swift one. It is printed using the `SWIFT_ENUM` macro instead of `SWIFT_ENUM_NAMED`.
1 parent dcc7e38 commit beb980b

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

lib/AST/SwiftNameTranslation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ getNameForObjC(const ValueDecl *VD, CustomNamesOnly_t customNamesOnly) {
5050
}
5151

5252
if (auto cdeclAttr = VD->getAttrs().getAttribute<CDeclAttr>())
53-
return cdeclAttr->Name;
53+
if (!customNamesOnly || !cdeclAttr->Name.empty())
54+
return VD->getCDeclName();
5455

5556
if (customNamesOnly)
5657
return StringRef();

test/PrintAsObjC/cdecl-enums.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ import Foundation
3131
// CHECK-NEXT: ObjcEnumNamedHelloDolly = 4,
3232
// CHECK-NEXT: };
3333

34-
@cdecl("ObjcEnumNamed") enum EnumNamed: Int {
34+
@cdecl(ObjcEnumNamed) enum EnumNamed: Int {
3535
case A, B, C, d, helloDolly
3636
}
3737

38-
// CHECK-LABEL: typedef SWIFT_ENUM_NAMED(unsigned int, ExplicitValues, "ExplicitValues", closed) {
38+
// CHECK-LABEL: typedef SWIFT_ENUM(unsigned int, ExplicitValues, closed) {
3939
// CHECK-NEXT: ExplicitValuesZim = 0,
4040
// CHECK-NEXT: ExplicitValuesZang = 219,
4141
// CHECK-NEXT: ExplicitValuesZung = 220,
4242
// CHECK-NEXT: };
4343
// NEGATIVE-NOT: ExplicitValuesDomain
4444

45-
@cdecl("ExplicitValues") enum ExplicitValues: CUnsignedInt {
45+
@cdecl enum ExplicitValues: CUnsignedInt {
4646
case Zim, Zang = 219, Zung
4747

4848
func methodNotExportedToC() {}

test/PrintAsObjC/cdecl-official.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// CHECK: #endif
2929

3030
// CHECK: /// Enums
31-
// CHECK: typedef SWIFT_ENUM_NAMED(int, CEnum, "CEnum", closed) {
31+
// CHECK: typedef SWIFT_ENUM(int, CEnum, closed) {
3232
// CHECK: CEnumA = 0,
3333
// CHECK: CEnumB = 1,
3434
// CHECK: };
@@ -84,7 +84,7 @@ func g_nullablePointers(_ x: UnsafeMutableRawPointer,
8484

8585
/// Enums
8686

87-
@cdecl("CEnum")
87+
@cdecl
8888
enum CEnum: CInt { case A, B }
8989

9090
@cdecl("CEnumRenamed_CName")

test/attr/attr_cdecl_official.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ enum SwiftEnum { case A, B }
4949
@cdecl("CEnum") enum CEnum: Int { case A, B }
5050
#endif
5151

52+
@cdecl enum CEnumDefaultName: CInt { case A, B }
53+
5254
@cdecl("CEnumNoRawType") enum CEnumNoRawType { case A, B }
5355
// expected-error @-1 {{'@cdecl' enum must declare an integer raw type}}
5456

0 commit comments

Comments
 (0)