Skip to content

Commit 45f0be2

Browse files
author
Gabor Horvath
committed
[cxx-interop] Properly support OS objects in reverse interop
These objects are behind typedefs and user code supposed to use the typedef names. We already have some logic in place for Obj-C interop. Reuse the same logic for C++ to use the correct names. rdar://150453489
1 parent ab5fc57 commit 45f0be2

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"
@@ -100,6 +101,12 @@ bool ClangSyntaxPrinter::printNominalTypeOutsideMemberDeclInnerStaticAssert(
100101
}
101102

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

lib/PrintAsClang/DeclAndTypePrinter.cpp

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

31283128
StringRef
31293129
DeclAndTypePrinter::maybeGetOSObjectBaseName(const clang::NamedDecl *decl) {
3130+
if (!decl)
3131+
return StringRef();
31303132
StringRef name = decl->getName();
31313133
if (!name.consume_front("OS_"))
31323134
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"
@@ -628,8 +629,13 @@ void ClangValueTypePrinter::printTypeGenericTraits(
628629
os << "} // end namespace \n\n";
629630
os << "namespace swift SWIFT_PRIVATE_ATTR {\n";
630631
auto classDecl = dyn_cast<ClassDecl>(typeDecl);
631-
bool addPointer =
632-
typeDecl->isObjC() || (classDecl && classDecl->isForeignReferenceType());
632+
633+
bool isOSObject = false;
634+
if (const auto nd =
635+
dyn_cast_or_null<clang::NamedDecl>(typeDecl->getClangDecl()))
636+
isOSObject = !DeclAndTypePrinter::maybeGetOSObjectBaseName(nd).empty();
637+
bool addPointer = (typeDecl->isObjC() && !isOSObject) ||
638+
(classDecl && classDecl->isForeignReferenceType());
633639

634640
if (objCxxOnly)
635641
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)