Skip to content

[stable/21.x] [APINotes] Add SwiftDestroyOp API note to map to the "destroy" function #11188

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
Aug 16, 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
11 changes: 11 additions & 0 deletions clang/docs/APINotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ declaration kind), all of which are optional:
- Name: tzdb
SwiftCopyable: false

A non-copyable type can have a "destroy" operation, specified with
`SwiftDestroyOp`, which will be invoked on the instance when it is no
longer in use to free up resources.

::

Tags:
- Name: WGPUAdapterInfo
SwiftCopyable: false
SwiftDestroyOp: wgpuAdapterInfoFreeMembers

:SwiftConformsTo:

Allows annotating a C++ class as conforming to a Swift protocol. Equivalent
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/APINotes/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ class TagInfo : public CommonTypeInfo {
std::optional<std::string> SwiftRetainOp;
std::optional<std::string> SwiftReleaseOp;
std::optional<std::string> SwiftDefaultOwnership;
std::optional<std::string> SwiftDestroyOp;

std::optional<EnumExtensibilityKind> EnumExtensibility;

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

if (!HasFlagEnum)
setFlagEnum(RHS.isFlagEnum());
Expand Down Expand Up @@ -913,6 +916,7 @@ inline bool operator==(const TagInfo &LHS, const TagInfo &RHS) {
LHS.SwiftRetainOp == RHS.SwiftRetainOp &&
LHS.SwiftReleaseOp == RHS.SwiftReleaseOp &&
LHS.SwiftDefaultOwnership == RHS.SwiftDefaultOwnership &&
LHS.SwiftDestroyOp == RHS.SwiftDestroyOp &&
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 = 37; // Typedef SwiftConformsTo
const uint16_t VERSION_MINOR = 38; // SwiftDestroyOp

const uint8_t kSwiftConforms = 1;
const uint8_t kSwiftDoesNotConform = 2;
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/APINotes/APINotesReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@ class TagTableInfo
reinterpret_cast<const char *>(Data), DefaultOwnershipLength - 1);
Data += DefaultOwnershipLength - 1;
}
unsigned DestroyOpLength =
endian::readNext<uint16_t, llvm::endianness::little>(Data);
if (DestroyOpLength > 0) {
Info.SwiftDestroyOp = std::string(reinterpret_cast<const char *>(Data),
DestroyOpLength - 1);
Data += DestroyOpLength - 1;
}

ReadCommonTypeInfo(Data, Info);
return Info;
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/APINotes/APINotesWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ 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.SwiftDestroyOp ? TI.SwiftDestroyOp->size() : 0) +
3 + getCommonTypeInfoSize(TI);
// clang-format on
}
Expand Down Expand Up @@ -1391,6 +1392,12 @@ class TagTableInfo : public CommonTypeTableInfo<TagTableInfo, TagInfo> {
} else {
writer.write<uint16_t>(0);
}
if (auto DestroyOp = TI.SwiftDestroyOp) {
writer.write<uint16_t>(DestroyOp->size() + 1);
OS.write(DestroyOp->c_str(), DestroyOp->size());
} else {
writer.write<uint16_t>(0);
}

emitCommonTypeInfo(OS, TI);
}
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/APINotes/APINotesYAMLCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ struct Tag {
std::optional<std::string> SwiftRetainOp;
std::optional<std::string> SwiftReleaseOp;
std::optional<std::string> SwiftDefaultOwnership;
std::optional<std::string> SwiftDestroyOp;
std::optional<std::string> SwiftConformance;
std::optional<EnumExtensibilityKind> EnumExtensibility;
std::optional<bool> FlagEnum;
Expand Down Expand Up @@ -672,6 +673,7 @@ template <> struct MappingTraits<Tag> {
IO.mapOptional("SwiftReleaseOp", T.SwiftReleaseOp);
IO.mapOptional("SwiftRetainOp", T.SwiftRetainOp);
IO.mapOptional("SwiftDefaultOwnership", T.SwiftDefaultOwnership);
IO.mapOptional("SwiftDestroyOp", T.SwiftDestroyOp);
IO.mapOptional("SwiftConformsTo", T.SwiftConformance);
IO.mapOptional("EnumExtensibility", T.EnumExtensibility);
IO.mapOptional("FlagEnum", T.FlagEnum);
Expand Down Expand Up @@ -1184,6 +1186,8 @@ class YAMLConverter {
TI.SwiftReleaseOp = T.SwiftReleaseOp;
if (T.SwiftDefaultOwnership)
TI.SwiftDefaultOwnership = T.SwiftDefaultOwnership;
if (T.SwiftDestroyOp)
TI.SwiftDestroyOp = T.SwiftDestroyOp;

if (T.SwiftCopyable)
TI.setSwiftCopyable(T.SwiftCopyable);
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaAPINotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@ static void ProcessAPINotes(Sema &S, TagDecl *D, const api_notes::TagInfo &Info,
if (auto DefaultOwnership = Info.SwiftDefaultOwnership)
D->addAttr(SwiftAttrAttr::Create(
S.Context, "returned_as_" + DefaultOwnership.value() + "_by_default"));
if (auto DestroyOp = Info.SwiftDestroyOp)
D->addAttr(
SwiftAttrAttr::Create(S.Context, "destroy:" + DestroyOp.value()));

if (auto Copyable = Info.isSwiftCopyable()) {
if (!*Copyable)
Expand Down
7 changes: 7 additions & 0 deletions clang/test/APINotes/Inputs/Headers/SwiftImportAs.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ Tags:
SwiftEscapable: false
- Name: EscapableType
SwiftEscapable: true
- Name: NoncopyableWithDestroyType
SwiftCopyable: false
SwiftDestroyOp: NCDDestroy

Functions:
- Name: functionReturningFrt__
- Name: functionReturningFrt_returns_unretained
SwiftReturnOwnership: unretained
- Name: functionReturningFrt_returns_retained
SwiftReturnOwnership: retained
Typedefs:
- Name: WrappedOptions
SwiftWrapper: struct
SwiftConformsTo: Swift.OptionSet
7 changes: 7 additions & 0 deletions clang/test/APINotes/Inputs/Headers/SwiftImportAs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ struct OpaqueRefCountedType; // redeclaration

inline void ORCRetain(struct OpaqueRefCountedType *x);
inline void ORCRelease(struct OpaqueRefCountedType *x);

typedef unsigned WrappedOptions;

struct NoncopyableWithDestroyType {
};

void NCDDestroy(NoncopyableWithDestroyType instance);
12 changes: 12 additions & 0 deletions clang/test/APINotes/swift-import-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// 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 methodReturningFrt__ | FileCheck -check-prefix=CHECK-METHOD-RETURNING-FRT %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 methodReturningFrt_returns_unretained | FileCheck -check-prefix=CHECK-METHOD-RETURNING-FRT-UNRETAINED %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 methodReturningFrt_returns_retained | FileCheck -check-prefix=CHECK-METHOD-RETURNING-FRT-RETAINED %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 WrappedOptions | FileCheck -check-prefix=CHECK-WRAPPED-OPTIONS %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 NoncopyableWithDestroyType | FileCheck -check-prefix=CHECK-NONCOPYABLE-WITH-DESTROY %s

#include <SwiftImportAs.h>

Expand Down Expand Up @@ -91,3 +93,13 @@
// CHECK-METHOD-RETURNING-FRT-RETAINED: Dumping ImmortalRefType::methodReturningFrt_returns_retained:
// CHECK-METHOD-RETURNING-FRT-RETAINED: CXXMethodDecl {{.+}} imported in SwiftImportAs methodReturningFrt_returns_retained 'ImmortalRefType *()'
// CHECK-METHOD-RETURNING-FRT-RETAINED: `-SwiftAttrAttr {{.+}} "returns_retained"

// CHECK-WRAPPED-OPTIONS: Dumping WrappedOptions
// CHECK-WRAPPED-OPTIONS: TypedefDecl{{.*}}WrappedOptions 'unsigned int'
// CHECK-WRAPPED-OPTIONS: SwiftNewTypeAttr {{.*}} swift_wrapper NK_Struct
// CHECK-WRAPPED-OPTIONS: SwiftAttrAttr {{.*}} "conforms_to:Swift.OptionSet"

// CHECK-NONCOPYABLE-WITH-DESTROY: Dumping NoncopyableWithDestroyType
// CHECK-NONCOPYABLE-WITH-DESTROY: RecordDecl {{.*}}struct NoncopyableWithDestroyType
// CHECK-NONCOPYABLE-WITH-DESTROY: SwiftAttrAttr {{.+}} "destroy:NCDDestroy"
// CHECK-NONCOPYABLE-WITH-DESTROY: SwiftAttrAttr {{.+}} "~Copyable"