Skip to content

[interop][SwiftToCxxToSwift] hide reverse interop module namespaces f… #61962

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 1 commit into from
Nov 8, 2022
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
3 changes: 3 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,9 @@ namespace {

Decl *VisitNamespaceDecl(const clang::NamespaceDecl *decl) {
DeclContext *dc = nullptr;
// Do not import namespace declarations marked as 'swift_private'.
if (decl->hasAttr<clang::SwiftPrivateAttr>())
return nullptr;
// If this is a top-level namespace, don't put it in the module we're
// importing, put it in the "__ObjC" module that is implicitly imported.
if (!decl->getParent()->isNamespace())
Expand Down
23 changes: 17 additions & 6 deletions lib/PrintAsClang/ClangSyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ bool ClangSyntaxPrinter::isClangKeyword(Identifier name) {
return ClangSyntaxPrinter::isClangKeyword(name.str());
}

void ClangSyntaxPrinter::printIdentifier(StringRef name) {
void ClangSyntaxPrinter::printIdentifier(StringRef name) const {
os << name;
if (ClangSyntaxPrinter::isClangKeyword(name))
os << '_';
}

void ClangSyntaxPrinter::printBaseName(const ValueDecl *decl) {
void ClangSyntaxPrinter::printBaseName(const ValueDecl *decl) const {
assert(decl->getName().isSimpleName());
printIdentifier(cxx_translation::getNameForCxx(decl));
}
Expand Down Expand Up @@ -136,12 +136,23 @@ void ClangSyntaxPrinter::printNominalTypeQualifier(
os << "::";
}

void ClangSyntaxPrinter::printModuleNamespaceStart(
const ModuleDecl &moduleContext) const {
os << "namespace ";
printBaseName(&moduleContext);
os << " __attribute__((swift_private))";
os << " {\n";
}

/// Print a C++ namespace declaration with the give name and body.
void ClangSyntaxPrinter::printNamespace(
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia) const {
os << "namespace ";
namePrinter(os);
if (trivia == NamespaceTrivia::AttributeSwiftPrivate)
os << " __attribute__((swift_private))";
os << " {\n\n";
bodyPrinter(os);
os << "\n} // namespace ";
Expand All @@ -150,9 +161,9 @@ void ClangSyntaxPrinter::printNamespace(
}

void ClangSyntaxPrinter::printNamespace(
StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
printNamespace([&](raw_ostream &os) { os << name; }, bodyPrinter);
StringRef name, llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia) const {
printNamespace([&](raw_ostream &os) { os << name; }, bodyPrinter, trivia);
}

void ClangSyntaxPrinter::printExternC(
Expand Down
20 changes: 12 additions & 8 deletions lib/PrintAsClang/ClangSyntaxPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class ClangSyntaxPrinter {

/// Print a given identifier. If the identifer conflicts with a keyword, add a
/// trailing underscore.
void printIdentifier(StringRef name);
void printIdentifier(StringRef name) const;

/// Print the base name of the given declaration.
void printBaseName(const ValueDecl *decl);
void printBaseName(const ValueDecl *decl) const;

/// Print the C-style prefix for the given module name, that's used for
/// C type names inside the module.
Expand Down Expand Up @@ -118,14 +118,18 @@ class ClangSyntaxPrinter {
void printNominalTypeQualifier(const NominalTypeDecl *typeDecl,
const ModuleDecl *moduleContext);

enum class NamespaceTrivia { None, AttributeSwiftPrivate };

void printModuleNamespaceStart(const ModuleDecl &moduleContext) const;

/// Print a C++ namespace declaration with the give name and body.
void
printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;
void printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia = NamespaceTrivia::None) const;

void
printNamespace(StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;
void printNamespace(StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia = NamespaceTrivia::None) const;

/// Print an extern C block with given body.
void
Expand Down
4 changes: 3 additions & 1 deletion lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
os << "#ifdef __cplusplus\n";
os << "namespace ";
M.ValueDecl::getName().print(os);
os << " __attribute__((swift_private))";
os << " {\n";
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
os << "extern \"C\" {\n";
Expand All @@ -800,6 +801,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
// Construct a C++ namespace for the module.
ClangSyntaxPrinter(os).printNamespace(
[&](raw_ostream &os) { M.ValueDecl::getName().print(os); },
[&](raw_ostream &os) { os << moduleOS.str(); });
[&](raw_ostream &os) { os << moduleOS.str(); },
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate);
return info;
}
10 changes: 4 additions & 6 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ void ClangValueTypePrinter::printTypePrecedingGenericTraits(
}
os << "#pragma clang diagnostic pop\n";
os << "} // namespace swift\n";
os << "\nnamespace ";
printer.printBaseName(moduleContext);
os << " {\n";
os << "\n";
printer.printModuleNamespaceStart(*moduleContext);
}

void ClangValueTypePrinter::printTypeGenericTraits(
Expand Down Expand Up @@ -592,7 +591,6 @@ void ClangValueTypePrinter::printTypeGenericTraits(
os << "} // namespace\n";
os << "#pragma clang diagnostic pop\n";
os << "} // namespace swift\n";
os << "\nnamespace ";
printer.printBaseName(moduleContext);
os << " {\n";
os << "\n";
printer.printModuleNamespaceStart(*moduleContext);
}
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/class/swift-actor-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final actor ActorWithField {
}
}

// CHECK: namespace Actor {
// CHECK: namespace Actor __attribute__((swift_private)) {
// CHECK: SWIFT_EXTERN void * _Nonnull $s5Actor0A9WithFieldCACycfC(SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // init()
// CHECK: SWIFT_EXTERN void $s5Actor0A9WithFieldC6methodyyF(SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // method()

Expand Down
8 changes: 4 additions & 4 deletions test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public final class ClassWithIntField {
}
}

// CHECK: namespace Class {
// CHECK: namespace Class __attribute__((swift_private)) {

// CHECK: SWIFT_EXTERN void * _Nonnull $s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(void * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // passThroughClassWithIntField(_:)
// CHECK-NEXT: SWIFT_EXTERN void * _Nonnull $s5Class06returnA12WithIntFieldAA0acdE0CyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // returnClassWithIntField()
// CHECK-NEXT: SWIFT_EXTERN void $s5Class04takeA12WithIntFieldyyAA0acdE0CF(void * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // takeClassWithIntField(_:)
// CHECK-NEXT: SWIFT_EXTERN void $s5Class04takeA17WithIntFieldInoutyyAA0acdE0CzF(void * _Nonnull * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // takeClassWithIntFieldInout(_:)

// CHECK: namespace Class {
// CHECK: namespace Class __attribute__((swift_private)) {

// CHECK: class ClassWithIntField;
// CHECK-NEXT: } // end namespace
Expand All @@ -43,7 +43,7 @@ public final class ClassWithIntField {
// CHECK-NEXT: } // namespace swift

// CHECK: namespace
// CHECK-SAME: Class {
// CHECK-SAME: Class __attribute__((swift_private)) {

// CHECK: namespace
// CHECK-SAME: _impl {
Expand Down Expand Up @@ -92,7 +92,7 @@ public final class ClassWithIntField {
// CHECK-NEXT: #pragma clang diagnostic pop
// CHECK-NEXT: } // namespace swift
// CHECK-EMPTY:
// CHECK-NEXT: namespace Class {
// CHECK-NEXT: namespace Class __attribute__((swift_private)) {

// CHECK: inline ClassWithIntField passThroughClassWithIntField(const ClassWithIntField& x) noexcept SWIFT_WARN_UNUSED_RESULT {
// CHECK-NEXT: return _impl::_impl_ClassWithIntField::makeRetained(_impl::$s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(x)));
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/core/gen-header-for-module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public func reprintedInImportedModule() -> Int {
return 42
}

// CHECK: namespace Core {
// CHECK: namespace Core __attribute__((swift_private)) {
// CHECK: swift::Int reprintedInImportedModule() noexcept SWIFT_WARN_UNUSED_RESULT {
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public struct S {
// CHECK-NEXT: #endif
// CHECK-NEXT: vwTable->initializeWithTake(destStorage, srcStorage, metadata._0);
// CHECK-NEXT: }
// CHECK: namespace Enums {
// CHECK: namespace Enums __attribute__((swift_private)) {
// CHECK: inline E E::_impl_x::operator()(double val) const {
// CHECK-NEXT: auto result = E::_make();
// CHECK-NEXT: memcpy(result._getOpaquePointer(), &val, sizeof(val));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public enum EmptyEnum {}
public enum SingleCaseEnum { case first }

// CHECK: namespace Enums {
// CHECK: namespace Enums __attribute__((swift_private)) {
// CHECK-NOT: class EmptyEnum final {
// CHECK-NOT: class SingleCaseEnum final {
// CHECK: } // namespace Enums
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/functions/cdecl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// RUN: %check-interop-cxx-header-in-clang(%t/cdecl.h)

// CHECK-LABEL: namespace CdeclFunctions {
// CHECK-LABEL: namespace CdeclFunctions __attribute__((swift_private)) {

// CHECK: namespace _impl {
// CHECK: SWIFT_EXTERN int cfuncPassTwo(int x, int y) SWIFT_NOEXCEPT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)

// CHECK-LABEL: namespace Functions {
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) {

// CHECK-LABEL: namespace _impl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)

// CHECK-LABEL: namespace Functions {
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) {

// CHECK-LABEL: namespace _impl {

Expand Down
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/functions/swift-functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)

// CHECK-LABEL: namespace Functions {
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) {

// CHECK-LABEL: namespace _impl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// CHECK-NOT: SWIFT_EXTERN bool $s9Functions24alwaysEmitIntoClientFuncyS2bF(bool x) SWIFT_NOEXCEPT SWIFT_CALL; // alwaysEmitIntoClientFunc(_:)

// CHECK: namespace Functions {
// CHECK: namespace Functions __attribute__((swift_private)) {
// CHECK-EMPTY:
// CHECK-EMPTY:
// CHECK-EMPTY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// CHECK-NOT: SWIFT_EXTERN double $s9Functions9asyncFuncyS2dYaF(double x) SWIFT_NOEXCEPT SWIFT_CALL; // asyncFunc(_:)

// CHECK: namespace Functions {
// CHECK: namespace Functions __attribute__((swift_private)) {
// CHECK-EMPTY:
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct LaterGeneric<T> {
// CHECK: class ComesFirstEnum final {
// CHECK: LaterGeneric<ComesFirstEnum> returnsLaterOpt() const;

// CHECK: namespace Generics {
// CHECK: namespace Generics __attribute__((swift_private)) {
// CHECK-EMPTY:
// CHECK-NEXT: namespace _impl {
// CHECK-EMPTY:
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/module/module-to-namespace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

// RUN: %check-interop-cxx-header-in-clang(%t/empty.h)

// CHECK-LABEL: namespace Test {
// CHECK-LABEL: namespace Test __attribute__((swift_private)) {
// CHECK: } // namespace Test
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public func test() -> String {
return ""
}

// CHECK: namespace Swift {
// CHECK: namespace Swift __attribute__((swift_private)) {
// CHECK: class String final {
2 changes: 1 addition & 1 deletion test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// FIXME: remove need for -Wno-shadow

// CHECK: namespace Swift {
// CHECK: namespace Swift __attribute__((swift_private)) {

// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public struct FirstSmallStruct {
// CHECK-NEXT: #pragma clang diagnostic pop
// CHECK-NEXT: } // namespace swift
// CHECK-EMPTY:
// CHECK-NEXT: namespace Structs {
// CHECK-NEXT: namespace Structs __attribute__((swift_private)) {

@frozen public struct FrozenStruct {
private let storedInt: Int32
Expand Down
8 changes: 4 additions & 4 deletions test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

// RUN: %check-interop-cxx-header-in-clang(%t/structs.h -Wno-unused-private-field -Wno-unused-function)

// CHECK: namespace Structs {
// CHECK: namespace Structs __attribute__((swift_private)) {
// CHECK: namespace _impl {

// CHECK: namespace Structs {
// CHECK: namespace Structs __attribute__((swift_private)) {

// CHECK: class StructWithIntField;
// CHECK-NEXT: } // end namespace
Expand All @@ -20,7 +20,7 @@
// CHECK-NEXT: #pragma clang diagnostic pop
// CHECK-NEXT: } // namespace swift

// CHECK: namespace Structs {
// CHECK: namespace Structs __attribute__((swift_private)) {

// CHECK: namespace _impl {
// CHECK-EMPTY:
Expand Down Expand Up @@ -95,7 +95,7 @@
// CHECK-NEXT: #pragma clang diagnostic pop
// CHECK-NEXT: } // namespace swift
// CHECK-EMPTY:
// CHECK-NEXT: namespace Structs {
// CHECK-NEXT: namespace Structs __attribute__((swift_private)) {

public struct StructWithIntField {
let field: Int64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-swift-frontend %s -typecheck -module-name Structs -clang-header-expose-decls=all-public -emit-clang-header-path %t/structs.h
// RUN: %FileCheck %s < %t/structs.h

// CHECK: namespace Structs {
// CHECK: namespace Structs __attribute__((swift_private)) {

// CHECK-NOT: class ZeroSizedStruct final {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: touch %t/swiftMod.h
// RUN: %target-swift-frontend -typecheck %t/swiftMod.swift -typecheck -module-name SwiftMod -emit-clang-header-path %t/swiftMod.h -I %t -enable-experimental-cxx-interop

// RUN: %FileCheck %s < %t/swiftMod.h

// RUN: %target-swift-frontend -typecheck %t/swiftMod.swift -typecheck -module-name SwiftMod -emit-clang-header-path %t/swiftMod2.h -I %t -enable-experimental-cxx-interop

// RUN: %check-interop-cxx-header-in-clang(%t/swiftMod2.h -Wno-error)

//--- header.h
#include "swiftMod.h"

//--- module.modulemap
module SwiftToCxxTest {
header "header.h"
requires cplusplus
}

//--- swiftMod.swift
import SwiftToCxxTest

@_expose(Cxx)
public func testFunction() -> String {
let arr = Swift.Array<Int>()
let rng = Swift.SystemRandomNumberGenerator()
return ""
}

// CHECK: namespace Swift __attribute__((swift_private)) {
// CHECK: namespace SwiftMod __attribute__((swift_private)) {
// CHECK-NOT: namespace Swift {
2 changes: 1 addition & 1 deletion test/PrintAsCxx/empty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
// CHECK: } // namespace swift
// CHECK-EMPTY:
// CHECK-NEXT: #endif
// CHECK: namespace empty {
// CHECK: namespace empty __attribute__((swift_private)) {
// CHECK: } // namespace empty
// CHECK: #endif

Expand Down