Skip to content

Commit ae1f804

Browse files
author
Gabor Horvath
committed
[cxx-interop] Fix extra indirection when exporting CFData arguments/return values
The clang nodes associated with Swift's Core Foundation types can already be represented by a pointer. The interop code does not need to add an extra layer of indirection in those cases. rdar://119840281
1 parent 382a8e7 commit ae1f804

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ getCxxReferencePointeeTypeOrNone(const clang::Type *type);
678678
/// Returns true if the given type is a C++ `const` reference type.
679679
bool isCxxConstReferenceType(const clang::Type *type);
680680

681+
/// Determine whether this typedef is a CF type.
682+
bool isCFTypeDecl(const clang::TypedefNameDecl *Decl);
683+
684+
/// Determine the imported CF type for the given typedef-name, or the empty
685+
/// string if this is not an imported CF type name.
686+
llvm::StringRef getCFTypeName(const clang::TypedefNameDecl *decl);
681687
} // namespace importer
682688

683689
struct ClangInvocationFileMapping {

lib/ClangImporter/CFTypeInfo.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ class CFPointeeInfo {
107107
return Decl.get<const clang::TypedefNameDecl *>();
108108
}
109109
};
110-
111-
/// Determine whether this typedef is a CF type.
112-
bool isCFTypeDecl(const clang::TypedefNameDecl *Decl);
113-
114-
/// Determine the imported CF type for the given typedef-name, or the empty
115-
/// string if this is not an imported CF type name.
116-
llvm::StringRef getCFTypeName(const clang::TypedefNameDecl *decl);
117110
}
118111
}
119112

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/IRGen/IRABIDetailsProvider.h"
3232
#include "clang/AST/ASTContext.h"
3333
#include "clang/AST/Attr.h"
34+
#include "clang/AST/Decl.h"
3435
#include "clang/AST/DeclObjC.h"
3536
#include "llvm/ADT/STLExtras.h"
3637

@@ -273,8 +274,13 @@ class CFunctionSignatureTypePrinter
273274
bool isInOutParam) {
274275
auto *cd = CT->getDecl();
275276
if (cd->hasClangNode()) {
276-
ClangSyntaxPrinter(os).printClangTypeReference(cd->getClangDecl());
277-
os << " *"
277+
const auto *clangDecl = cd->getClangDecl();
278+
ClangSyntaxPrinter(os).printClangTypeReference(clangDecl);
279+
bool alreadyPointer = false;
280+
if (const auto *typedefDecl = dyn_cast<clang::TypedefNameDecl>(clangDecl))
281+
if (importer::isCFTypeDecl(typedefDecl))
282+
alreadyPointer = true;
283+
os << (alreadyPointer ? " " : " *")
278284
<< (!optionalKind || *optionalKind == OTK_None ? "_Nonnull"
279285
: "_Nullable");
280286
if (isInOutParam) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseCoreFoundation -enable-experimental-cxx-interop -clang-header-expose-decls=all-public -emit-clang-header-path %t/UseCoreFoundation.h
4+
// RUN: %FileCheck %s < %t/UseCoreFoundation.h
5+
6+
// REQUIRES: objc_interop
7+
8+
import CoreFoundation
9+
10+
public func foobar(_ a: CFData) -> Bool {
11+
true
12+
}
13+
14+
// CHECK: SWIFT_EXTERN bool $s17UseCoreFoundation6foobarySbSo9CFDataRefaF(CFDataRef _Nonnull a) SWIFT_NOEXCEPT SWIFT_CALL; // foobar(_:)

0 commit comments

Comments
 (0)