Skip to content

Commit 0d89418

Browse files
committed
[AST] Add mechanism to notify module loading to other clients
This patch adds an optional callback function member to the AST Context. The callback gets invoked when a new module (or overlay module) gets loaded. This can be used for instance by other clients, such as lldb, to perform actions when a module gets loaded, like showing progress to the end-user. rdar://94165195 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 28a2c60 commit 0d89418

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

include/swift/AST/ASTContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ class ASTContext final {
374374
llvm::BumpPtrAllocator &
375375
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;
376376

377+
/// An optional generic callback function invoked prior to importing a module.
378+
mutable std::function<bool(llvm::StringRef ModuleName, bool IsOverlay)>
379+
PreModuleImportCallback;
380+
377381
public:
378382
/// Allocate - Allocate memory from the ASTContext bump pointer.
379383
void *Allocate(unsigned long bytes, unsigned alignment,
@@ -489,6 +493,10 @@ class ASTContext final {
489493
/// Set a new stats reporter.
490494
void setStatsReporter(UnifiedStatsReporter *stats);
491495

496+
/// Set the module import callback function.
497+
void setPreModuleImportCallback(
498+
std::function<bool(llvm::StringRef, bool)> Callback);
499+
492500
public:
493501
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
494502
/// specified string.
@@ -1107,6 +1115,9 @@ class ASTContext final {
11071115
/// If a module by this name has already been loaded, the existing module will
11081116
/// be returned.
11091117
///
1118+
/// \param ModulePath The module's \c ImportPath which describes
1119+
/// the name of the module being loaded, possibly including submodules.
1120+
11101121
/// \returns The requested module, or NULL if the module cannot be found.
11111122
ModuleDecl *getModule(ImportPath::Module ModulePath);
11121123

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
#include "swift/AST/PropertyWrappers.h"
4343
#include "swift/AST/ProtocolConformance.h"
4444
#include "swift/AST/RawComment.h"
45-
#include "swift/AST/SearchPathOptions.h"
4645
#include "swift/AST/SILLayout.h"
46+
#include "swift/AST/SearchPathOptions.h"
4747
#include "swift/AST/SemanticAttrs.h"
4848
#include "swift/AST/SourceFile.h"
4949
#include "swift/AST/SubstitutionMap.h"
@@ -65,6 +65,7 @@
6565
#include "llvm/IR/LLVMContext.h"
6666
#include "llvm/Support/Allocator.h"
6767
#include "llvm/Support/Compiler.h"
68+
#include "llvm/Support/FormatVariadic.h"
6869
#include <algorithm>
6970
#include <memory>
7071

@@ -692,6 +693,12 @@ void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
692693
Stats = stats;
693694
}
694695

696+
/// Set a new module loading callback function.
697+
void ASTContext::setPreModuleImportCallback(
698+
std::function<bool(llvm::StringRef, bool)> Callback) {
699+
PreModuleImportCallback = Callback;
700+
}
701+
695702
RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {
696703
return getImpl().TheSyntaxArena;
697704
}
@@ -2210,6 +2217,8 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
22102217
return M;
22112218

22122219
auto moduleID = ModulePath[0];
2220+
if (PreModuleImportCallback)
2221+
PreModuleImportCallback(moduleID.Item.str(), false /*=IsOverlay*/);
22132222
for (auto &importer : getImpl().ModuleLoaders) {
22142223
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath)) {
22152224
if (LangOpts.EnableModuleLoadingRemarks) {
@@ -2234,12 +2243,17 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
22342243
return Existing;
22352244
}
22362245

2246+
if (PreModuleImportCallback) {
2247+
SmallString<16> path;
2248+
ModPath.getString(path);
2249+
if (!path.empty())
2250+
PreModuleImportCallback(path.str(), true /*=IsOverlay*/);
2251+
}
22372252
for (auto &importer : getImpl().ModuleLoaders) {
22382253
if (importer.get() == getClangModuleLoader())
22392254
continue;
2240-
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath)) {
2255+
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath))
22412256
return M;
2242-
}
22432257
}
22442258

22452259
return nullptr;

0 commit comments

Comments
 (0)