Skip to content

[BridgingHeaderChaining] Bind bridging header module when load module #80512

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
Apr 9, 2025
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
10 changes: 10 additions & 0 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ class ClangImporter final : public ClangModuleLoader {
bool trackParsedSymbols = false,
bool implicitImport = false);

/// Bind the bridging header content to the module.
///
/// \param adapter The module that depends on the contents of this header.
/// \param diagLoc A location to attach any diagnostics to if import fails.
///
/// \returns true if there was an error importing the header.
///
/// \sa importBridgingHeader
bool bindBridgingHeader(ModuleDecl *adapter, SourceLoc diagLoc);

/// Returns the module that contains imports and declarations from all loaded
/// Objective-C header files.
///
Expand Down
16 changes: 10 additions & 6 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1869,12 +1869,7 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
bool trackParsedSymbols,
bool implicitImport) {
if (isPCHFilenameExtension(header)) {
Impl.ImportedHeaderOwners.push_back(adapter);
// We already imported this with -include-pch above, so we should have
// collected a bunch of PCH-encoded module imports that we just need to
// replay in handleDeferredImports.
Impl.handleDeferredImports(diagLoc);
return false;
return bindBridgingHeader(adapter, diagLoc);
}

clang::FileManager &fileManager = Impl.Instance->getFileManager();
Expand All @@ -1901,6 +1896,15 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
std::move(sourceBuffer), implicitImport);
}

bool ClangImporter::bindBridgingHeader(ModuleDecl *adapter, SourceLoc diagLoc) {
Impl.ImportedHeaderOwners.push_back(adapter);
// We already imported this with -include-pch above, so we should have
// collected a bunch of PCH-encoded module imports that we just need to
// replay in handleDeferredImports.
Impl.handleDeferredImports(diagLoc);
return false;
}

static llvm::Expected<llvm::cas::ObjectRef>
setupIncludeTreeInput(clang::CompilerInvocation &invocation,
StringRef headerPath, StringRef pchIncludeTree) {
Expand Down
19 changes: 11 additions & 8 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ ModuleFile::loadDependenciesForFileContext(const FileUnit *file,
if (dependency.isHeader()) {
// The path may be empty if the file being loaded is a partial AST,
// and the current compiler invocation is a merge-modules step.
if (!dependency.Core.RawPath.empty() &&
!M->getASTContext().SearchPathOpts.BridgingHeaderChaining) {
if (!dependency.Core.RawPath.empty()) {
// If using bridging header chaining, just bind the entire bridging
// header pch to the module. Otherwise, import the header.
bool hadError =
clangImporter->importHeader(dependency.Core.RawPath,
file->getParentModule(),
Core->importedHeaderInfo.fileSize,
Core->importedHeaderInfo.fileModTime,
Core->importedHeaderInfo.contents,
diagLoc);
M->getASTContext().SearchPathOpts.BridgingHeaderChaining
? clangImporter->bindBridgingHeader(file->getParentModule(),
diagLoc)
: clangImporter->importHeader(
dependency.Core.RawPath, file->getParentModule(),
Core->importedHeaderInfo.fileSize,
Core->importedHeaderInfo.fileModTime,
Core->importedHeaderInfo.contents, diagLoc);
if (hadError)
return error(Status::FailedToLoadBridgingHeader);
}
Expand Down
2 changes: 2 additions & 0 deletions test/ScanDependencies/bridging-header-autochaining.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ extension A {
public func testA() {}
}

public class AB : B {}

//--- user2.swift
import Test

Expand Down