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 4 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
28 changes: 27 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2290,9 +2290,13 @@ ClangImporter::Implementation::lookupModule(StringRef moduleName) {
return loadFromMM();
}

#define DEBUG_TYPE "foo"
ModuleDecl *ClangImporter::Implementation::loadModuleClang(
SourceLoc importLoc, ImportPath::Module path) {
auto realModuleName = SwiftContext.getRealModuleName(path.front().Item).str();
llvm::dbgs() << "loadModuleClang '";
path.print(llvm::dbgs());
llvm::dbgs() << "'\n";

// Convert the Swift import path over to a Clang import path.
SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4>
Expand Down Expand Up @@ -2331,9 +2335,17 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
}

clang::SourceLocation clangImportLoc = getNextIncludeLoc();
llvm::dbgs() << "loading clang module '";
llvm::interleave(path, [](auto IdInfoPair) { llvm::dbgs() << IdInfoPair.first->getName(); },
[]() { llvm::dbgs() << "."; });
llvm::dbgs() << "'\n";
clang::ModuleLoadResult result =
Instance->loadModule(clangImportLoc, path, visibility,
/*IsInclusionDirective=*/false);
llvm::dbgs() << "loaded clang module '";
llvm::interleave(path, [](auto IdInfoPair) { llvm::dbgs() << IdInfoPair.first->getName(); },
[]() { llvm::dbgs() << "."; });
llvm::dbgs() << "'\n";

if (!preservedIndexStorePathOption.empty()) {
// Restore the -index-store-path option.
Expand Down Expand Up @@ -2395,9 +2407,10 @@ ClangImporter::loadModule(SourceLoc importLoc,
}

ModuleDecl *ClangImporter::Implementation::loadModule(
SourceLoc importLoc, ImportPath::Module path) {
SourceLoc importLoc, ImportPath::Module path, const std::string caller) {
ModuleDecl *MD = nullptr;
ASTContext &ctx = getNameImporter().getContext();
llvm::dbgs() << "loadModule called from " << caller << "\n";

// `CxxStdlib` is the only accepted spelling of the C++ stdlib module name.
if (path.front().Item.is("std") ||
Expand All @@ -2419,6 +2432,7 @@ ModuleDecl *ClangImporter::Implementation::loadModule(
ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
const clang::Module *clangModule, SourceLoc importLoc) {
assert(clangModule);
llvm::dbgs() << "finishLoadingClangModule\n";

// Bump the generation count.
bumpGeneration();
Expand Down Expand Up @@ -2464,6 +2478,7 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
SwiftContext.addLoadedModule(result);
}

llvm::dbgs() << "finished loading clang module\n";
return result;
}

Expand Down Expand Up @@ -2798,6 +2813,17 @@ ClangModuleUnit *ClangImporter::Implementation::getWrapperForModule(
if (auto mainModule = SwiftContext.MainModule) {
implicitImportInfo = mainModule->getImplicitImportInfo();
}
for (auto *I : underlying->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
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);
}
ClangModuleUnit *file = nullptr;
auto wrapper = ModuleDecl::create(name, SwiftContext, implicitImportInfo,
[&](ModuleDecl *wrapper, auto addFile) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
public:
/// Load a module using either method.
ModuleDecl *loadModule(SourceLoc importLoc,
ImportPath::Module path);
ImportPath::Module path, const std::string caller = __func__);

void recordImplicitUnwrapForDecl(ValueDecl *decl, bool isIUO) {
if (!isIUO)
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
3 changes: 2 additions & 1 deletion lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3054,9 +3054,10 @@ ModuleDecl *ModuleFile::getModule(ModuleID MID) {
}

ModuleDecl *ModuleFile::getModule(ImportPath::Module name,
bool allowLoading) {
bool allowLoading, const std::string caller) {
if (name.empty() || name.front().Item.empty())
return getContext().TheBuiltinModule;
llvm::dbgs() << "getModule called from " << caller << "\n";

// FIXME: duplicated from ImportResolver::getModule
Identifier parentName = FileContext->getParentModule()->getName();
Expand Down
20 changes: 15 additions & 5 deletions lib/Serialization/DeserializationErrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,26 @@ class InavalidAvailabilityDomainError
class PrettyStackTraceModuleFile : public llvm::PrettyStackTraceEntry {
const char *Action;
const ModuleFile &MF;
const std::string caller;
public:
explicit PrettyStackTraceModuleFile(const char *action, ModuleFile &module)
: Action(action), MF(module) {}
explicit PrettyStackTraceModuleFile(ModuleFile &module)
: PrettyStackTraceModuleFile("While reading from", module) {}
explicit PrettyStackTraceModuleFile(const char *action, ModuleFile &module, std::string c = __func__)
: Action(action), MF(module), caller(c) {
llvm::dbgs() << "start module read of ";
MF.outputDiagnosticInfo(llvm::dbgs());
llvm::dbgs() << " (called from " << c << ")\n";
}
explicit PrettyStackTraceModuleFile(ModuleFile &module, std::string caller = __func__)
: PrettyStackTraceModuleFile("While reading from", module, caller) {}

~PrettyStackTraceModuleFile() {
llvm::dbgs() << "finish module read of ";
MF.outputDiagnosticInfo(llvm::dbgs());
llvm::dbgs() << " (called from " << caller << ")\n";
}
void print(raw_ostream &os) const override {
os << Action << " ";
MF.outputDiagnosticInfo(os);
os << "\n";
os << " (called from " << caller << ")\n";
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ class ModuleFile
///
/// If the name matches the name of the current module, a shadowed module
/// is loaded instead.
ModuleDecl *getModule(ImportPath::Module name, bool allowLoading = false);
ModuleDecl *getModule(ImportPath::Module name, bool allowLoading = false, std::string caller = __func__);

/// Returns the generic signature for the given ID.
GenericSignature getGenericSignature(serialization::GenericSignatureID ID);
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"
}
59 changes: 59 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,59 @@
// 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
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be nice to have a run line with C++ interop enabled just in case. Sometimes there are subtle differences between the two modes.


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.1, 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.1, 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.1, 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.1, 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.1, 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)
}
59 changes: 59 additions & 0 deletions test/Interop/C/swiftify-import/clang-includes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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

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.1, 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.1, 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.1, 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.1, 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.1, 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)
}
2 changes: 1 addition & 1 deletion test/Interop/Cxx/stdlib/std-span-interface.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -plugin-path %swift-plugin-dir -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -print-module -module-to-print=StdSpan -source-filename=x -enable-experimental-cxx-interop -Xcc -std=c++20 -module-cache-path %t > %t/interface.swift
// RUN: %target-swift-ide-test -plugin-path %swift-plugin-dir -Xcc -mllvm -Xcc -debug-only=foo -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -print-module -module-to-print=StdSpan -source-filename=x -enable-experimental-cxx-interop -Xcc -std=c++20 -module-cache-path %t > %t/interface.swift
// RUN: %FileCheck %s < %t/interface.swift

// Make sure we trigger typechecking and SIL diagnostics
Expand Down