Skip to content

[stable/21.x] [API notes] Allow SwiftConformsTo on Typedefs #11078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions clang/include/clang/APINotes/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class CommonTypeInfo : public CommonEntityInfo {
/// The NS error domain for this type.
std::optional<std::string> NSErrorDomain;

/// The Swift protocol that this type should be automatically conformed to.
std::optional<std::string> SwiftConformance;

public:
CommonTypeInfo() {}

Expand All @@ -165,6 +168,14 @@ class CommonTypeInfo : public CommonEntityInfo {
: std::nullopt;
}

std::optional<std::string> getSwiftConformance() const {
return SwiftConformance;
}

void setSwiftConformance(std::optional<std::string> conformance) {
SwiftConformance = conformance;
}

friend bool operator==(const CommonTypeInfo &, const CommonTypeInfo &);

CommonTypeInfo &operator|=(const CommonTypeInfo &RHS) {
Expand All @@ -175,6 +186,8 @@ class CommonTypeInfo : public CommonEntityInfo {
setSwiftBridge(RHS.getSwiftBridge());
if (!NSErrorDomain)
setNSErrorDomain(RHS.getNSErrorDomain());
if (SwiftConformance)
setSwiftConformance(RHS.getSwiftConformance());

return *this;
}
Expand All @@ -185,7 +198,8 @@ class CommonTypeInfo : public CommonEntityInfo {
inline bool operator==(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
return static_cast<const CommonEntityInfo &>(LHS) == RHS &&
LHS.SwiftBridge == RHS.SwiftBridge &&
LHS.NSErrorDomain == RHS.NSErrorDomain;
LHS.NSErrorDomain == RHS.NSErrorDomain &&
LHS.SwiftConformance == RHS.SwiftConformance;
}

inline bool operator!=(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
Expand Down Expand Up @@ -825,9 +839,6 @@ class TagInfo : public CommonTypeInfo {
std::optional<std::string> SwiftReleaseOp;
std::optional<std::string> SwiftDefaultOwnership;

/// The Swift protocol that this type should be automatically conformed to.
std::optional<std::string> SwiftConformance;

std::optional<EnumExtensibilityKind> EnumExtensibility;

TagInfo()
Expand Down Expand Up @@ -876,9 +887,6 @@ class TagInfo : public CommonTypeInfo {
if (!SwiftDefaultOwnership)
SwiftDefaultOwnership = RHS.SwiftDefaultOwnership;

if (!SwiftConformance)
SwiftConformance = RHS.SwiftConformance;

if (!HasFlagEnum)
setFlagEnum(RHS.isFlagEnum());

Expand All @@ -905,7 +913,6 @@ inline bool operator==(const TagInfo &LHS, const TagInfo &RHS) {
LHS.SwiftRetainOp == RHS.SwiftRetainOp &&
LHS.SwiftReleaseOp == RHS.SwiftReleaseOp &&
LHS.SwiftDefaultOwnership == RHS.SwiftDefaultOwnership &&
LHS.SwiftConformance == RHS.SwiftConformance &&
LHS.isFlagEnum() == RHS.isFlagEnum() &&
LHS.isSwiftCopyable() == RHS.isSwiftCopyable() &&
LHS.isSwiftEscapable() == RHS.isSwiftEscapable() &&
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/APINotes/APINotesFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const uint16_t VERSION_MAJOR = 0;
/// API notes file minor version number.
///
/// When the format changes IN ANY WAY, this number should be incremented.
const uint16_t VERSION_MINOR = 36; // TO_UPSTREAM(BoundsSafety)
const uint16_t VERSION_MINOR = 37; // Typedef SwiftConformsTo

const uint8_t kSwiftConforms = 1;
const uint8_t kSwiftDoesNotConform = 2;
Expand Down
13 changes: 7 additions & 6 deletions clang/lib/APINotes/APINotesReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ void ReadCommonTypeInfo(const uint8_t *&Data, CommonTypeInfo &Info) {
reinterpret_cast<const char *>(Data), ErrorDomainLength - 1)));
Data += ErrorDomainLength - 1;
}

if (unsigned ConformanceLength =
endian::readNext<uint16_t, llvm::endianness::little>(Data)) {
Info.setSwiftConformance(std::string(reinterpret_cast<const char *>(Data),
ConformanceLength - 1));
Data += ConformanceLength - 1;
}
}

/// Used to deserialize the on-disk identifier table.
Expand Down Expand Up @@ -668,12 +675,6 @@ class TagTableInfo
reinterpret_cast<const char *>(Data), DefaultOwnershipLength - 1);
Data += DefaultOwnershipLength - 1;
}
if (unsigned ConformanceLength =
endian::readNext<uint16_t, llvm::endianness::little>(Data)) {
Info.SwiftConformance = std::string(reinterpret_cast<const char *>(Data),
ConformanceLength - 1);
Data += ConformanceLength - 1;
}

ReadCommonTypeInfo(Data, Info);
return Info;
Expand Down
16 changes: 8 additions & 8 deletions clang/lib/APINotes/APINotesWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ unsigned getCommonEntityInfoSize(const CommonEntityInfo &CEI) {
// in on-disk hash tables.
unsigned getCommonTypeInfoSize(const CommonTypeInfo &CTI) {
return 2 + (CTI.getSwiftBridge() ? CTI.getSwiftBridge()->size() : 0) + 2 +
(CTI.getNSErrorDomain() ? CTI.getNSErrorDomain()->size() : 0) +
(CTI.getNSErrorDomain() ? CTI.getNSErrorDomain()->size() : 0) + 2 +
(CTI.getSwiftConformance() ? CTI.getSwiftConformance()->size() : 0) +
getCommonEntityInfoSize(CTI);
}

Expand All @@ -564,6 +565,12 @@ void emitCommonTypeInfo(raw_ostream &OS, const CommonTypeInfo &CTI) {
} else {
writer.write<uint16_t>(0);
}
if (auto conformance = CTI.getSwiftConformance()) {
writer.write<uint16_t>(conformance->size() + 1);
OS.write(conformance->c_str(), conformance->size());
} else {
writer.write<uint16_t>(0);
}
}

/// Used to serialize the on-disk Objective-C property table.
Expand Down Expand Up @@ -1331,7 +1338,6 @@ class TagTableInfo : public CommonTypeTableInfo<TagTableInfo, TagInfo> {
2 + (TI.SwiftRetainOp ? TI.SwiftRetainOp->size() : 0) +
2 + (TI.SwiftReleaseOp ? TI.SwiftReleaseOp->size() : 0) +
2 + (TI.SwiftDefaultOwnership ? TI.SwiftDefaultOwnership->size() : 0) +
2 + (TI.SwiftConformance ? TI.SwiftConformance->size() : 0) +
3 + getCommonTypeInfoSize(TI);
// clang-format on
}
Expand Down Expand Up @@ -1385,12 +1391,6 @@ class TagTableInfo : public CommonTypeTableInfo<TagTableInfo, TagInfo> {
} else {
writer.write<uint16_t>(0);
}
if (auto Conformance = TI.SwiftConformance) {
writer.write<uint16_t>(Conformance->size() + 1);
OS.write(Conformance->c_str(), Conformance->size());
} else {
writer.write<uint16_t>(0);
}

emitCommonTypeInfo(OS, TI);
}
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/APINotes/APINotesYAMLCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ struct Class {
std::optional<StringRef> NSErrorDomain;
std::optional<bool> SwiftImportAsNonGeneric;
std::optional<bool> SwiftObjCMembers;
std::optional<std::string> SwiftConformance;
MethodsSeq Methods;
PropertiesSeq Properties;
};
Expand All @@ -437,6 +438,7 @@ template <> struct MappingTraits<Class> {
IO.mapOptional("NSErrorDomain", C.NSErrorDomain);
IO.mapOptional("SwiftImportAsNonGeneric", C.SwiftImportAsNonGeneric);
IO.mapOptional("SwiftObjCMembers", C.SwiftObjCMembers);
IO.mapOptional("SwiftConformsTo", C.SwiftConformance);
IO.mapOptional("Methods", C.Methods);
IO.mapOptional("Properties", C.Properties);
}
Expand Down Expand Up @@ -693,6 +695,7 @@ struct Typedef {
std::optional<StringRef> SwiftBridge;
std::optional<StringRef> NSErrorDomain;
std::optional<SwiftNewTypeKind> SwiftType;
std::optional<std::string> SwiftConformance;
};

typedef std::vector<Typedef> TypedefsSeq;
Expand Down Expand Up @@ -721,6 +724,7 @@ template <> struct MappingTraits<Typedef> {
IO.mapOptional("SwiftBridge", T.SwiftBridge);
IO.mapOptional("NSErrorDomain", T.NSErrorDomain);
IO.mapOptional("SwiftWrapper", T.SwiftType);
IO.mapOptional("SwiftConformsTo", T.SwiftConformance);
}
};
} // namespace yaml
Expand Down Expand Up @@ -979,6 +983,8 @@ class YAMLConverter {
if (Common.SwiftBridge)
Info.setSwiftBridge(std::string(*Common.SwiftBridge));
Info.setNSErrorDomain(Common.NSErrorDomain);
if (auto conformance = Common.SwiftConformance)
Info.setSwiftConformance(conformance);
}

// Translate from Method into ObjCMethodInfo and write it out.
Expand Down Expand Up @@ -1176,8 +1182,6 @@ class YAMLConverter {
TI.SwiftRetainOp = T.SwiftRetainOp;
if (T.SwiftReleaseOp)
TI.SwiftReleaseOp = T.SwiftReleaseOp;
if (T.SwiftConformance)
TI.SwiftConformance = T.SwiftConformance;
if (T.SwiftDefaultOwnership)
TI.SwiftDefaultOwnership = T.SwiftDefaultOwnership;

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Sema/SemaAPINotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ static void ProcessAPINotes(Sema &S, Decl *D,
});
}

if (auto ConformsTo = Info.getSwiftConformance())
D->addAttr(
SwiftAttrAttr::Create(S.Context, "conforms_to:" + ConformsTo.value()));

ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
Metadata);
}
Expand Down Expand Up @@ -777,10 +781,6 @@ static void ProcessAPINotes(Sema &S, TagDecl *D, const api_notes::TagInfo &Info,
D->addAttr(SwiftAttrAttr::Create(
S.Context, "returned_as_" + DefaultOwnership.value() + "_by_default"));

if (auto ConformsTo = Info.SwiftConformance)
D->addAttr(
SwiftAttrAttr::Create(S.Context, "conforms_to:" + ConformsTo.value()));

if (auto Copyable = Info.isSwiftCopyable()) {
if (!*Copyable)
D->addAttr(SwiftAttrAttr::Create(S.Context, "~Copyable"));
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19610,6 +19610,10 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
// record.
AddPushedVisibilityAttribute(New);

// If this is not a definition, process API notes for it now.
if (TUK != TagUseKind::Definition)
ProcessAPINotes(New);

if (isMemberSpecialization && !New->isInvalidDecl())
CompleteMemberSpecialization(New, Previous);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ Classes:
- Name: scalarNewProperty
PropertyKind: Instance
Nullability: Scalar
Typedefs:
- Name: MyTypedef
SwiftConformsTo: Swift.Equatable
4 changes: 4 additions & 0 deletions clang/test/APINotes/Inputs/Headers/SwiftImportAs.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Tags:
SwiftReleaseOp: release
SwiftRetainOp: retain
SwiftDefaultOwnership: unretained
- Name: OpaqueRefCountedType
SwiftImportAs: reference
SwiftReleaseOp: ORCRelease
SwiftRetainOp: ORCRetain
- Name: NonCopyableType
SwiftCopyable: false
SwiftConformsTo: MySwiftModule.MySwiftNonCopyableProtocol
Expand Down
6 changes: 6 additions & 0 deletions clang/test/APINotes/Inputs/Headers/SwiftImportAs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ struct EscapableType { int value; };
struct RefCountedTypeWithDefaultConvention {};
inline void retain(RefCountedType *x) {}
inline void release(RefCountedType *x) {}

struct OpaqueRefCountedType;
struct OpaqueRefCountedType; // redeclaration

inline void ORCRetain(struct OpaqueRefCountedType *x);
inline void ORCRelease(struct OpaqueRefCountedType *x);
17 changes: 16 additions & 1 deletion clang/test/APINotes/swift-import-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter ImmortalRefType | FileCheck -check-prefix=CHECK-IMMORTAL %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter RefCountedType | FileCheck -check-prefix=CHECK-REF-COUNTED %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter RefCountedTypeWithDefaultConvention | FileCheck -check-prefix=CHECK-REF-COUNTED-DEFAULT %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter OpaqueRefCountedType | FileCheck -check-prefix=CHECK-OPAQUE-REF-COUNTED %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter NonCopyableType | FileCheck -check-prefix=CHECK-NON-COPYABLE %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter CopyableType | FileCheck -check-prefix=CHECK-COPYABLE %s
// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -I %S/Inputs/Headers %s -x c++ -ast-dump -ast-dump-filter NonEscapableType | FileCheck -check-prefix=CHECK-NON-ESCAPABLE %s
Expand Down Expand Up @@ -34,10 +35,24 @@
// CHECK-REF-COUNTED-DEFAULT: SwiftAttrAttr {{.+}} <<invalid sloc>> "release:release"
// CHECK-REF-COUNTED-DEFAULT: SwiftAttrAttr {{.+}} <<invalid sloc>> "returned_as_unretained_by_default"

// CHECK-OPAQUE-REF-COUNTED: Dumping OpaqueRefCountedType:
// CHECK-OPAQUE-REF-COUNTED-NEXT: CXXRecordDecl {{.+}} imported in SwiftImportAs{{.*}}struct OpaqueRefCountedType
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "import_reference"
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "retain:ORCRetain"
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "release:ORCRelease"
// CHECK-OPAQUE-REF-COUNTED-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "release:ORCRelease"

// CHECK-OPAQUE-REF-COUNTED: Dumping OpaqueRefCountedType:
// CHECK-OPAQUE-REF-COUNTED-NEXT: CXXRecordDecl {{.+}} imported in SwiftImportAs{{.*}}struct OpaqueRefCountedType
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "import_reference"
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "retain:ORCRetain"
// CHECK-OPAQUE-REF-COUNTED: SwiftAttrAttr {{.+}} <<invalid sloc>> "release:ORCRelease"

// CHECK-OPAQUE-REF-COUNTED-NOT: SwiftAttrAttr {{.+}} <<invalid sloc>> "release:
// CHECK-NON-COPYABLE: Dumping NonCopyableType:
// CHECK-NON-COPYABLE-NEXT: CXXRecordDecl {{.+}} imported in SwiftImportAs {{.+}} struct NonCopyableType
// CHECK-NON-COPYABLE: SwiftAttrAttr {{.+}} <<invalid sloc>> "conforms_to:MySwiftModule.MySwiftNonCopyableProtocol"
// CHECK-NON-COPYABLE: SwiftAttrAttr {{.+}} <<invalid sloc>> "~Copyable"
// CHECK-NON-COPYABLE: SwiftAttrAttr {{.+}} <<invalid sloc>> "conforms_to:MySwiftModule.MySwiftNonCopyableProtocol"

// CHECK-COPYABLE: Dumping CopyableType:
// CHECK-COPYABLE-NEXT: CXXRecordDecl {{.+}} imported in SwiftImportAs {{.+}} struct CopyableType
Expand Down
2 changes: 1 addition & 1 deletion clang/test/APINotes/yaml-roundtrip.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CHECK-NEXT: 25c26
CHECK-NEXT: < Nullability: S
CHECK-NEXT: ---
CHECK-NEXT: > Nullability: Unspecified
CHECK-NEXT: 28c29,30
CHECK-NEXT: 28c29
CHECK-NEXT: < Nullability: Scalar
CHECK-NEXT: ---
CHECK-NEXT: > Nullability: Unspecified