-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
f8fbc8a
435d4c6
778037c
b2084b2
2049600
1babc48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
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" | ||
} | ||
} | ||
} |
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); | ||
Xazax-hun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); |
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; |
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 | ||
hnrklssn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
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) | ||
} |
There was a problem hiding this comment.
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?