Skip to content

Commit a97f092

Browse files
committed
[AST] Add callback to the module loading methods
This patch adds an optional callback function argument to each AST module loading method. 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 4ae6b92 commit a97f092

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

include/swift/AST/ASTContext.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,27 +1108,37 @@ class ASTContext final {
11081108
/// be returned.
11091109
///
11101110
/// \returns The requested module, or NULL if the module cannot be found.
1111-
ModuleDecl *getModule(ImportPath::Module ModulePath);
1111+
ModuleDecl *getModule(ImportPath::Module ModulePath,
1112+
std::function<bool(llvm::StringRef)> callback = {});
11121113

11131114
/// Attempts to load the matching overlay module for the given clang
11141115
/// module into this ASTContext.
11151116
///
11161117
/// \returns The Swift overlay module corresponding to the given Clang module,
11171118
/// or NULL if the overlay module cannot be found.
1118-
ModuleDecl *getOverlayModule(const FileUnit *ClangModule);
1119+
ModuleDecl *
1120+
getOverlayModule(const FileUnit *ClangModule,
1121+
std::function<bool(llvm::StringRef)> callback = {});
11191122

1120-
ModuleDecl *getModuleByName(StringRef ModuleName);
1123+
ModuleDecl *
1124+
getModuleByName(StringRef ModuleName,
1125+
std::function<bool(llvm::StringRef)> callback = {});
11211126

1122-
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
1127+
ModuleDecl *
1128+
getModuleByIdentifier(Identifier ModuleID,
1129+
std::function<bool(llvm::StringRef)> callback = {});
11231130

11241131
/// Returns the standard library module, or null if the library isn't present.
11251132
///
11261133
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module
11271134
/// if it hasn't been set yet.
1128-
ModuleDecl *getStdlibModule(bool loadIfAbsent = false);
1135+
ModuleDecl *
1136+
getStdlibModule(bool loadIfAbsent = false,
1137+
std::function<bool(llvm::StringRef)> callback = {});
11291138

1130-
ModuleDecl *getStdlibModule() const {
1131-
return const_cast<ASTContext *>(this)->getStdlibModule(false);
1139+
ModuleDecl *
1140+
getStdlibModule(std::function<bool(llvm::StringRef)> callback = {}) const {
1141+
return const_cast<ASTContext *>(this)->getStdlibModule(false, callback);
11321142
}
11331143

11341144
/// Insert an externally-sourced module into the set of known loaded modules

lib/AST/ASTContext.cpp

Lines changed: 24 additions & 9 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

@@ -2203,7 +2204,8 @@ bool ASTContext::canImportModule(ImportPath::Module ModuleName,
22032204
}
22042205

22052206
ModuleDecl *
2206-
ASTContext::getModule(ImportPath::Module ModulePath) {
2207+
ASTContext::getModule(ImportPath::Module ModulePath,
2208+
std::function<bool(llvm::StringRef)> callback) {
22072209
assert(!ModulePath.empty());
22082210

22092211
if (auto *M = getLoadedModule(ModulePath))
@@ -2212,6 +2214,8 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
22122214
auto moduleID = ModulePath[0];
22132215
for (auto &importer : getImpl().ModuleLoaders) {
22142216
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath)) {
2217+
if (callback)
2218+
callback(llvm::formatv("Loading module {0}", M->getNameStr()).str());
22152219
if (LangOpts.EnableModuleLoadingRemarks) {
22162220
Diags.diagnose(ModulePath.getSourceRange().Start,
22172221
diag::module_loaded,
@@ -2224,7 +2228,9 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
22242228
return nullptr;
22252229
}
22262230

2227-
ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
2231+
ModuleDecl *
2232+
ASTContext::getOverlayModule(const FileUnit *FU,
2233+
std::function<bool(llvm::StringRef)> callback) {
22282234
assert(FU && FU->getKind() == FileUnitKind::ClangModule &&
22292235
"Overlays can only be retrieved for clang modules!");
22302236
ImportPath::Module::Builder builder(FU->getParentModule()->getName());
@@ -2238,30 +2244,39 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
22382244
if (importer.get() == getClangModuleLoader())
22392245
continue;
22402246
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath)) {
2247+
if (callback)
2248+
callback(
2249+
llvm::formatv("Loading overlay module {0}", M->getNameStr()).str());
22412250
return M;
22422251
}
22432252
}
22442253

22452254
return nullptr;
22462255
}
22472256

2248-
ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
2257+
ModuleDecl *
2258+
ASTContext::getModuleByName(StringRef ModuleName,
2259+
std::function<bool(llvm::StringRef)> callback) {
22492260
ImportPath::Module::Builder builder(*this, ModuleName, /*separator=*/'.');
2250-
return getModule(builder.get());
2261+
return getModule(builder.get(), callback);
22512262
}
22522263

2253-
ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
2264+
ModuleDecl *ASTContext::getModuleByIdentifier(
2265+
Identifier ModuleID, std::function<bool(llvm::StringRef)> callback) {
22542266
ImportPath::Module::Builder builder(ModuleID);
2255-
return getModule(builder.get());
2267+
return getModule(builder.get(), callback);
22562268
}
22572269

2258-
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
2270+
ModuleDecl *
2271+
ASTContext::getStdlibModule(bool loadIfAbsent,
2272+
std::function<bool(llvm::StringRef)> callback) {
22592273
if (TheStdlibModule)
22602274
return TheStdlibModule;
22612275

22622276
if (loadIfAbsent) {
22632277
auto mutableThis = const_cast<ASTContext*>(this);
2264-
TheStdlibModule = mutableThis->getModuleByIdentifier(StdlibModuleName);
2278+
TheStdlibModule =
2279+
mutableThis->getModuleByIdentifier(StdlibModuleName, callback);
22652280
} else {
22662281
TheStdlibModule = getLoadedModule(StdlibModuleName);
22672282
}

0 commit comments

Comments
 (0)