Skip to content

Extend PreModuleImportCallback to support bridging header compilation #71318

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
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
18 changes: 13 additions & 5 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,19 @@ class ASTContext final {
/// OutputBackend for writing outputs.
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutputBackend;

enum ModuleImportKind {
Module = 0,
Overlay,
BridgingHeader
};
using PreModuleImportCallbackPtr =
std::function<void(StringRef ModuleName, ModuleImportKind Kind)>;
/// Set the callback function that is invoked before Swift module importing is
/// performed
void SetPreModuleImportCallback(
std::function<void(llvm::StringRef ModuleName, bool IsOverlay)> callback);
/// performed.
void SetPreModuleImportCallback(PreModuleImportCallbackPtr callback);

/// Call the PreModuleImportCallback. Used by ClangImporter.
void PreModuleImportHook(StringRef ModuleName, ModuleImportKind Kind) const;

/// If the shared pointer is not a \c nullptr and the pointee is \c true,
/// all operations working on this ASTContext should be aborted at the next
Expand Down Expand Up @@ -414,8 +423,7 @@ class ASTContext final {
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;

/// An optional generic callback function invoked prior to importing a module.
mutable std::function<void(llvm::StringRef ModuleName, bool IsOverlay)>
PreModuleImportCallback;
mutable PreModuleImportCallbackPtr PreModuleImportCallback;

public:
/// Allocate - Allocate memory from the ASTContext bump pointer.
Expand Down
13 changes: 9 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,16 @@ ASTContext::~ASTContext() {
}

void ASTContext::SetPreModuleImportCallback(
std::function<void(llvm::StringRef ModuleName, bool IsOverlay)> callback) {
PreModuleImportCallbackPtr callback) {
PreModuleImportCallback = callback;
}

void ASTContext::PreModuleImportHook(StringRef ModuleName,
ModuleImportKind Kind) const {
if (PreModuleImportCallback)
PreModuleImportCallback(ModuleName, Kind);
}

llvm::BumpPtrAllocator &ASTContext::getAllocator(AllocationArena arena) const {
switch (arena) {
case AllocationArena::Permanent:
Expand Down Expand Up @@ -2475,8 +2481,7 @@ ASTContext::getModule(ImportPath::Module ModulePath, bool AllowMemoryCached) {
return M;

auto moduleID = ModulePath[0];
if (PreModuleImportCallback)
PreModuleImportCallback(moduleID.Item.str(), false /*=IsOverlay*/);
PreModuleImportHook(moduleID.Item.str(), ModuleImportKind::Module);
for (auto &importer : getImpl().ModuleLoaders) {
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath,
AllowMemoryCached)) {
Expand Down Expand Up @@ -2509,7 +2514,7 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
SmallString<16> path;
ModPath.getString(path);
if (!path.empty())
PreModuleImportCallback(path.str(), /*IsOverlay=*/true);
PreModuleImportCallback(path.str(), ModuleImportKind::Overlay);
}
for (auto &importer : getImpl().ModuleLoaders) {
if (importer.get() == getClangModuleLoader())
Expand Down
4 changes: 4 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,10 @@ bool ClangImporter::Implementation::importHeader(
std::unique_ptr<llvm::MemoryBuffer> sourceBuffer,
bool implicitImport) {

// Progress update for the debugger.
SwiftContext.PreModuleImportHook(
headerName, ASTContext::ModuleImportKind::BridgingHeader);

// Don't even try to load the bridging header if the Clang AST is in a bad
// state. It could cause a crash.
auto &clangDiags = getClangASTContext().getDiagnostics();
Expand Down