Skip to content

Commit cbdeaec

Browse files
authored
Merge pull request swiftlang#84076 from Xazax-hun/os-objects-support-on-6.2
[6.2][cxx-interop] Properly support OS objects in reverse interop
2 parents fdaafd6 + 209feca commit cbdeaec

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ClangSyntaxPrinter.h"
14+
#include "DeclAndTypePrinter.h"
1415
#include "PrimitiveTypeMapping.h"
1516
#include "swift/ABI/MetadataValues.h"
1617
#include "swift/AST/ASTContext.h"
@@ -99,6 +100,12 @@ bool ClangSyntaxPrinter::printNominalTypeOutsideMemberDeclInnerStaticAssert(
99100
}
100101

101102
void ClangSyntaxPrinter::printClangTypeReference(const clang::Decl *typeDecl) {
103+
StringRef osObjectName = DeclAndTypePrinter::maybeGetOSObjectBaseName(
104+
dyn_cast<clang::NamedDecl>(cast<clang::NamedDecl>(typeDecl)));
105+
if (!osObjectName.empty()) {
106+
os << osObjectName << "_t";
107+
return;
108+
}
102109
if (cast<clang::NamedDecl>(typeDecl)->getDeclName().isEmpty() &&
103110
isa<clang::TagDecl>(typeDecl)) {
104111
if (auto *tnd =

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,8 @@ const TypeDecl *DeclAndTypePrinter::getObjCTypeDecl(const TypeDecl* TD) {
30843084

30853085
StringRef
30863086
DeclAndTypePrinter::maybeGetOSObjectBaseName(const clang::NamedDecl *decl) {
3087+
if (!decl)
3088+
return StringRef();
30873089
StringRef name = decl->getName();
30883090
if (!name.consume_front("OS_"))
30893091
return StringRef();

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,16 @@ class CFunctionSignatureTypePrinter
320320
auto *cd = CT->getDecl();
321321
if (cd->hasClangNode()) {
322322
const auto *clangDecl = cd->getClangDecl();
323-
ClangSyntaxPrinter(cd->getASTContext(), os).printClangTypeReference(clangDecl);
323+
ClangSyntaxPrinter(cd->getASTContext(), os)
324+
.printClangTypeReference(clangDecl);
324325
bool alreadyPointer = false;
325326
if (const auto *typedefDecl = dyn_cast<clang::TypedefNameDecl>(clangDecl))
326327
if (importer::isCFTypeDecl(typedefDecl))
327328
alreadyPointer = true;
329+
if (!DeclAndTypePrinter::maybeGetOSObjectBaseName(
330+
dyn_cast<clang::NamedDecl>(clangDecl))
331+
.empty())
332+
alreadyPointer = true;
328333
os << (alreadyPointer ? " " : " *")
329334
<< (!optionalKind || *optionalKind == OTK_None ? "_Nonnull"
330335
: "_Nullable");

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/ClangImporter/ClangImporter.h"
2424
#include "swift/IRGen/IRABIDetailsProvider.h"
2525
#include "swift/IRGen/Linking.h"
26+
#include "clang/AST/Decl.h"
2627
#include "clang/Basic/Module.h"
2728
#include "llvm/ADT/STLExtras.h"
2829
#include "llvm/Support/raw_ostream.h"
@@ -654,8 +655,13 @@ void ClangValueTypePrinter::printTypeGenericTraits(
654655
os << "} // end namespace \n\n";
655656
os << "namespace swift SWIFT_PRIVATE_ATTR {\n";
656657
auto classDecl = dyn_cast<ClassDecl>(typeDecl);
657-
bool addPointer =
658-
typeDecl->isObjC() || (classDecl && classDecl->isForeignReferenceType());
658+
659+
bool isOSObject = false;
660+
if (const auto nd =
661+
dyn_cast_or_null<clang::NamedDecl>(typeDecl->getClangDecl()))
662+
isOSObject = !DeclAndTypePrinter::maybeGetOSObjectBaseName(nd).empty();
663+
bool addPointer = (typeDecl->isObjC() && !isOSObject) ||
664+
(classDecl && classDecl->isForeignReferenceType());
659665

660666
if (objCxxOnly)
661667
os << "#if defined(__OBJC__)\n";

test/Interop/SwiftToCxx/stdlib/core-foundation-types-in-cxx.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
// RUN: %FileCheck %s < %t/UseCoreFoundation.h
55

66
// RUN: echo "#include <netinet/in.h>" > %t/full-header.h
7+
// RUN: echo "#include <dispatch/dispatch.h>" >> %t/full-header.h
78
// RUN: cat %t/UseCoreFoundation.h >> %t/full-header.h
89
// RUN: %target-interop-build-clangxx -std=gnu++20 -fobjc-arc -c -x objective-c++-header %t/full-header.h -o %t/o.o
910

1011
// REQUIRES: objc_interop
1112

1213
import CoreFoundation
1314
import Foundation
15+
import Dispatch
16+
17+
public func testDispatch(x: DispatchSemaphore) {}
1418

1519
public func foobar(_ a: CFData) -> Bool {
1620
true
@@ -33,7 +37,9 @@ public enum MyEnum {
3337
// CHECK: SWIFT_EXTERN bool $s17UseCoreFoundation6foobarySbSo9CFDataRefaF(CFDataRef _Nonnull a) SWIFT_NOEXCEPT SWIFT_CALL; // foobar(_:)
3438
// CHECK: SWIFT_EXTERN CFDateRef _Nullable $s17UseCoreFoundation13returnsCFDateSo0E3RefaSgyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // returnsCFDate()
3539
// CHECK: SWIFT_EXTERN void $s17UseCoreFoundation11takesCFDate1xySo0E3RefaSg_tF(CFDateRef _Nullable x) SWIFT_NOEXCEPT SWIFT_CALL; // takesCFDate(x:)
40+
// CHECK: SWIFT_EXTERN void $s17UseCoreFoundation12testDispatch1xySo21OS_dispatch_semaphoreC_tF(dispatch_semaphore_t _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // testDispatch(x:)
3641

3742
// CHECK: SWIFT_INLINE_THUNK swift::Optional<in_addr> networkThing() noexcept SWIFT_SYMBOL("s:17UseCoreFoundation12networkThingSo7in_addrVSgyF") SWIFT_WARN_UNUSED_RESULT {
3843
// CHECK: SWIFT_INLINE_THUNK CFDateRef _Nullable returnsCFDate() noexcept SWIFT_SYMBOL("s:17UseCoreFoundation13returnsCFDateSo0E3RefaSgyF") SWIFT_WARN_UNUSED_RESULT {
3944
// CHECK: SWIFT_INLINE_THUNK void takesCFDate(CFDateRef _Nullable x) noexcept SWIFT_SYMBOL("s:17UseCoreFoundation11takesCFDate1xySo0E3RefaSg_tF") {
45+
// CHECK: SWIFT_INLINE_THUNK void testDispatch(dispatch_semaphore_t _Nonnull x) noexcept SWIFT_SYMBOL("s:17UseCoreFoundation12testDispatch1xySo21OS_dispatch_semaphoreC_tF") {

0 commit comments

Comments
 (0)