Skip to content

Commit 0727d04

Browse files
committed
[interop][SwiftToCxx] correctly emit names for anonymous structs that use a typedef when emitting a Clang type reference in the generated header
1 parent aa397c7 commit 0727d04

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ bool ClangSyntaxPrinter::printNominalTypeOutsideMemberDeclInnerStaticAssert(
9696
}
9797

9898
void ClangSyntaxPrinter::printClangTypeReference(const clang::Decl *typeDecl) {
99+
if (cast<clang::NamedDecl>(typeDecl)->getDeclName().isEmpty() &&
100+
isa<clang::TagDecl>(typeDecl)) {
101+
if (auto *tnd =
102+
cast<clang::TagDecl>(typeDecl)->getTypedefNameForAnonDecl()) {
103+
printClangTypeReference(tnd);
104+
return;
105+
}
106+
}
99107
auto &clangCtx = typeDecl->getASTContext();
100108
clang::PrintingPolicy pp(clangCtx.getLangOpts());
101109
const auto *NS = clang::NestedNameSpecifier::getRequiredQualification(

test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ namespace ns {
8080

8181
using SimpleTypedef = int;
8282

83+
typedef struct { float column; } anonymousStruct;
84+
85+
namespace ns {
86+
87+
using anonStructInNS = struct { float row; };
88+
89+
}
90+
8391
//--- module.modulemap
8492
module CxxTest {
8593
header "header.h"
@@ -149,6 +157,12 @@ public func takeTrivial(_ x: Trivial) {
149157
public func takeTrivialInout(_ x: inout Trivial) {
150158
}
151159

160+
@_expose(Cxx)
161+
public struct Strct {
162+
public let transform: anonymousStruct
163+
public let transform2: ns.anonStructInNS
164+
}
165+
152166
// CHECK: #if __has_feature(objc_modules)
153167
// CHECK: #if __has_feature(objc_modules)
154168
// CHECK-NEXT: #if __has_warning("-Watimport-in-framework-header")
@@ -284,3 +298,9 @@ public func takeTrivialInout(_ x: inout Trivial) {
284298
// CHECK: SWIFT_INLINE_THUNK void takeTrivialInout(Trivial& x) noexcept SWIFT_SYMBOL({{.*}}) {
285299
// CHECK-NEXT: return _impl::$s8UseCxxTy16takeTrivialInoutyySo0E0VzF(swift::_impl::getOpaquePointer(x));
286300
// CHECK-NEXT: }
301+
302+
// CHECK: SWIFT_INLINE_THUNK anonymousStruct Strct::getTransform() const {
303+
// CHECK-NEXT: alignas(alignof(anonymousStruct)) char storage[sizeof(anonymousStruct)];
304+
305+
// CHECK: SWIFT_INLINE_THUNK ns::anonStructInNS Strct::getTransform2() const {
306+
// CHECK-NEXT: alignas(alignof(ns::anonStructInNS)) char storage[sizeof(ns::anonStructInNS)];

0 commit comments

Comments
 (0)