Skip to content

[MacrosOnImports][Swiftify] Copy module imports from clang node's module to its Swift macro SourceFile #81859

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ namespace swift {

/// Resolve imports for a source file generated to adapt a given
/// Clang module.
void performImportResolutionForClangMacroBuffer(
SourceFile &SF, ModuleDecl *clangModule
);
void performImportResolutionForClangMacroBuffer(SourceFile &SF);

/// Once type-checking is complete, this instruments code with calls to an
/// intrinsic that record the expected values of local variables so they can
Expand Down
24 changes: 24 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,30 @@ ClangModuleUnit *ClangImporter::Implementation::getWrapperForModule(
if (auto mainModule = SwiftContext.MainModule) {
implicitImportInfo = mainModule->getImplicitImportInfo();
}

// Decls in this Swift module may originate in the top-level clang module with the
// same name, since decls in clang submodules are dumped into the top-level module.
// Add all transitive submodules as implicit imports in case any of them are marked
// `explicit`. The content in explicit modules isn't normally made visible when
// importing the parent module, but in this case the parent module is all we have.
llvm::SmallVector<const clang::Module *, 32> SubmoduleWorklist;
SubmoduleWorklist.push_back(underlying);
while (!SubmoduleWorklist.empty()) {
const clang::Module *CurrModule = SubmoduleWorklist.pop_back_val();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put in a set of already-visited modules so we don't re-explore the same subgraph many times?

for (auto *I : CurrModule->Imports) {
// Make sure that synthesized Swift code in the clang module wrapper
// (e.g. _SwiftifyImport macro expansions) can access the same symbols as
// if it were actually in the clang module, by copying the imports.
std::string swiftModuleName = isCxxStdModule(I) ?
static_cast<std::string>(SwiftContext.Id_CxxStdlib) :
I->getFullModuleName();
ImportPath::Builder importPath(SwiftContext, swiftModuleName, '.');
UnloadedImportedModule importedModule(importPath.copyTo(SwiftContext), ImportKind::Module);
implicitImportInfo.AdditionalUnloadedImports.push_back(importedModule);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we restrict ourselves to only recording the explicit submodules we find rather than recording every submodule we find?

}
for (auto *Submodule : CurrModule->submodules())
SubmoduleWorklist.push_back(Submodule);
}
ClangModuleUnit *file = nullptr;
auto wrapper = ModuleDecl::create(name, SwiftContext, implicitImportInfo,
[&](ModuleDecl *wrapper, auto addFile) {
Expand Down
7 changes: 3 additions & 4 deletions lib/Sema/ImportResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,14 @@ void swift::performImportResolution(SourceFile &SF) {
verify(SF);
}

void swift::performImportResolutionForClangMacroBuffer(
SourceFile &SF, ModuleDecl *clangModule
) {
void swift::performImportResolutionForClangMacroBuffer(SourceFile &SF) {
// If we've already performed import resolution, bail.
if (SF.ASTStage == SourceFile::ImportsResolved)
return;

// `getWrapperForModule` has already declared all the implicit clang module
// imports we need
ImportResolver resolver(SF);
resolver.addImplicitImport(clangModule);

// FIXME: This is a hack that we shouldn't need, but be sure that we can
// see the Swift standard library.
Expand Down
6 changes: 2 additions & 4 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,10 +1060,8 @@ createMacroSourceFile(std::unique_ptr<llvm::MemoryBuffer> buffer,
/*parsingOpts=*/{}, /*isPrimary=*/false);
if (auto parentSourceFile = dc->getParentSourceFile())
macroSourceFile->setImports(parentSourceFile->getImports());
else if (auto clangModuleUnit =
dyn_cast<ClangModuleUnit>(dc->getModuleScopeContext())) {
auto clangModule = clangModuleUnit->getParentModule();
performImportResolutionForClangMacroBuffer(*macroSourceFile, clangModule);
else if (isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
performImportResolutionForClangMacroBuffer(*macroSourceFile);
}
return macroSourceFile;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

typedef int a_t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

typedef int b_t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
typedef int c_t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
typedef int d_t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
typedef int e_t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ModuleA {
header "module-a.h"
export *
}
module ModuleB {
header "module-b.h"
}
module ModuleOuter {
module ModuleC {
header "module-c.h"
export *
}
explicit module ModuleD {
header "module-d.h"
export *
}
}
module ModuleDeep {
module ModuleDeepNested {
module ModuleDeepNestedNested {
header "module-e.h"
}
}
}
20 changes: 20 additions & 0 deletions test/Interop/C/swiftify-import/Inputs/clang-includes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "TransitiveModules/module-a.h"
#include "TransitiveModules/module-b.h"
#include "TransitiveModules/module-c.h"
#include "TransitiveModules/module-d.h"
#include "TransitiveModules/module-e.h"

#define __counted_by(x) __attribute__((__counted_by__(x)))
#define __noescape __attribute__((noescape))

void basic_include(const a_t *__counted_by(len) p __noescape, a_t len);

void non_exported_include(const b_t *__counted_by(len) p __noescape, b_t len);

void submodule_include(const c_t *__counted_by(len) p __noescape, c_t len);

void explicit_submodule_include(const d_t *__counted_by(len) p __noescape, d_t len);

void deep_submodule_noexport(const e_t *__counted_by(len) p __noescape, e_t len);
7 changes: 7 additions & 0 deletions test/Interop/C/swiftify-import/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ module CommentsClang {
header "comments.h"
export *
}
module ClangIncludesModule {
header "clang-includes.h"
export *
}
module ClangIncludesNoExportModule {
header "clang-includes.h"
}
58 changes: 58 additions & 0 deletions test/Interop/C/swiftify-import/clang-includes-explicit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// REQUIRES: swift_feature_SafeInteropWrappers

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift -cxx-interoperability-mode=default

//--- test.swift
import A1.B1.C1.D1

public func callUnsafe(_ p: UnsafeMutableRawPointer) {
let _ = foo(p, 13)
}

public func callSafe(_ p: UnsafeMutableRawBufferPointer) {
let _ = foo(p)
}

//--- Inputs/module.modulemap
module A1 {
explicit module B1 {
explicit module C1 {
explicit module D1 {
header "D1.h"
}
}
}
}

module A2 {
explicit module B2 {
header "B2.h"
export C2

explicit module C2 {
header "C2.h"
}
}
}

//--- Inputs/D1.h
#pragma once

#include "B2.h"
#define __sized_by(s) __attribute__((__sized_by__(s)))

c2_t foo(void * _Nonnull __sized_by(size), int size);

//--- Inputs/B2.h
#pragma once

#include "C2.h"

//--- Inputs/C2.h
#pragma once

typedef int c2_t;
60 changes: 60 additions & 0 deletions test/Interop/C/swiftify-import/clang-includes-noexport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// REQUIRES: swift_feature_SafeInteropWrappers

// RUN: %target-swift-ide-test -print-module -module-to-print=ClangIncludesNoExportModule -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s

// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesNoExport.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesNoExport.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s -cxx-interoperability-mode=default

import ClangIncludesNoExportModule

// CHECK: import ModuleA
// CHECK-NEXT: import ModuleB
// CHECK-NOT: import
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DougGregor I don't fully get why only the module's imports that themselves are top-level modules show up in the dump. It seems to work regardless, but it seems a bit off. Do you know if this is intended?

// CHECK-EMPTY:

// CHECK-NEXT: func basic_include(_ p: UnsafePointer<a_t>!, _ len: a_t)
// CHECK-NEXT: func non_exported_include(_ p: UnsafePointer<b_t>!, _ len: b_t)
// CHECK-NEXT: func submodule_include(_ p: UnsafePointer<c_t>!, _ len: c_t)
// CHECK-NEXT: func explicit_submodule_include(_ p: UnsafePointer<d_t>!, _ len: d_t)
// CHECK-NEXT: func deep_submodule_noexport(_ p: UnsafePointer<e_t>!, _ len: e_t)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func basic_include(_ p: Span<a_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func deep_submodule_noexport(_ p: Span<e_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func explicit_submodule_include(_ p: Span<d_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func non_exported_include(_ p: Span<b_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func submodule_include(_ p: Span<c_t>)

public func callBasicInclude(_ p: Span<CInt>) {
basic_include(p)
}

public func callNonExported(_ p: Span<CInt>) {
non_exported_include(p)
}

public func callSubmoduleInclude(_ p: Span<CInt>) {
submodule_include(p)
}

public func callExplicitSubmoduleInclude(_ p: Span<CInt>) {
explicit_submodule_include(p)
}

public func callDeepSubmoduleNoexport(_ p: Span<CInt>) {
deep_submodule_noexport(p)
}
60 changes: 60 additions & 0 deletions test/Interop/C/swiftify-import/clang-includes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// REQUIRES: swift_feature_SafeInteropWrappers

// RUN: %target-swift-ide-test -print-module -module-to-print=ClangIncludesModule -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s

// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludes.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludes.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s -cxx-interoperability-mode=default

import ClangIncludesModule

// CHECK: @_exported import ModuleA
// CHECK-NEXT: @_exported import ModuleB
// CHECK-NOT: import
// CHECK-EMPTY:

// CHECK-NEXT: func basic_include(_ p: UnsafePointer<a_t>!, _ len: a_t)
// CHECK-NEXT: func non_exported_include(_ p: UnsafePointer<b_t>!, _ len: b_t)
// CHECK-NEXT: func submodule_include(_ p: UnsafePointer<c_t>!, _ len: c_t)
// CHECK-NEXT: func explicit_submodule_include(_ p: UnsafePointer<d_t>!, _ len: d_t)
// CHECK-NEXT: func deep_submodule_noexport(_ p: UnsafePointer<e_t>!, _ len: e_t)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func basic_include(_ p: Span<a_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func deep_submodule_noexport(_ p: Span<e_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func explicit_submodule_include(_ p: Span<d_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func non_exported_include(_ p: Span<b_t>)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func submodule_include(_ p: Span<c_t>)

public func callBasicInclude(_ p: Span<CInt>) {
basic_include(p)
}

public func callNonExported(_ p: Span<CInt>) {
non_exported_include(p)
}

public func callSubmoduleInclude(_ p: Span<CInt>) {
submodule_include(p)
}

public func callExplicitSubmoduleInclude(_ p: Span<CInt>) {
explicit_submodule_include(p)
}

public func callDeepSubmoduleNoexport(_ p: Span<CInt>) {
deep_submodule_noexport(p)
}