Skip to content

Commit d1c8e64

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

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-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: 27 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,10 @@ 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(
2219+
llvm::formatv("Loading module overlay for {0}", M->getNameStr())
2220+
.str());
22152221
if (LangOpts.EnableModuleLoadingRemarks) {
22162222
Diags.diagnose(ModulePath.getSourceRange().Start,
22172223
diag::module_loaded,
@@ -2224,7 +2230,9 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
22242230
return nullptr;
22252231
}
22262232

2227-
ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
2233+
ModuleDecl *
2234+
ASTContext::getOverlayModule(const FileUnit *FU,
2235+
std::function<bool(llvm::StringRef)> callback) {
22282236
assert(FU && FU->getKind() == FileUnitKind::ClangModule &&
22292237
"Overlays can only be retrieved for clang modules!");
22302238
ImportPath::Module::Builder builder(FU->getParentModule()->getName());
@@ -2238,30 +2246,40 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
22382246
if (importer.get() == getClangModuleLoader())
22392247
continue;
22402248
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath)) {
2249+
if (callback)
2250+
callback(
2251+
llvm::formatv("Loading module overlay for {0}", M->getNameStr())
2252+
.str());
22412253
return M;
22422254
}
22432255
}
22442256

22452257
return nullptr;
22462258
}
22472259

2248-
ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
2260+
ModuleDecl *
2261+
ASTContext::getModuleByName(StringRef ModuleName,
2262+
std::function<bool(llvm::StringRef)> callback) {
22492263
ImportPath::Module::Builder builder(*this, ModuleName, /*separator=*/'.');
2250-
return getModule(builder.get());
2264+
return getModule(builder.get(), callback);
22512265
}
22522266

2253-
ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
2267+
ModuleDecl *ASTContext::getModuleByIdentifier(
2268+
Identifier ModuleID, std::function<bool(llvm::StringRef)> callback) {
22542269
ImportPath::Module::Builder builder(ModuleID);
2255-
return getModule(builder.get());
2270+
return getModule(builder.get(), callback);
22562271
}
22572272

2258-
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
2273+
ModuleDecl *
2274+
ASTContext::getStdlibModule(bool loadIfAbsent,
2275+
std::function<bool(llvm::StringRef)> callback) {
22592276
if (TheStdlibModule)
22602277
return TheStdlibModule;
22612278

22622279
if (loadIfAbsent) {
22632280
auto mutableThis = const_cast<ASTContext*>(this);
2264-
TheStdlibModule = mutableThis->getModuleByIdentifier(StdlibModuleName);
2281+
TheStdlibModule =
2282+
mutableThis->getModuleByIdentifier(StdlibModuleName, callback);
22652283
} else {
22662284
TheStdlibModule = getLoadedModule(StdlibModuleName);
22672285
}

0 commit comments

Comments
 (0)