Skip to content

Commit c266e9d

Browse files
authored
Merge pull request #62185 from xymus/improve-rmodule-loading
Improve `-Rmodule-loading` to show both the path to the source and to the cached file actually loaded
2 parents f3feea5 + 319d498 commit c266e9d

18 files changed

+144
-35
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,10 @@ REMARK(cross_import_added,none,
989989
(Identifier, Identifier, Identifier))
990990

991991
REMARK(module_loaded,none,
992-
"loaded module at %0",
993-
(StringRef))
992+
"loaded module %0"
993+
"%select{| (overlay for a clang dependency)}1"
994+
"; source: '%2', loaded: '%3'",
995+
(Identifier, unsigned, StringRef, StringRef))
994996

995997
REMARK(explicit_interface_build_skipped,none,
996998
"Skipped rebuilding module at %0 - up-to-date",

include/swift/AST/FileUnit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ class LoadedFile : public FileUnit {
404404
/// This is usually a filesystem path.
405405
virtual StringRef getFilename() const;
406406

407+
/// Get the path to the file loaded by the compiler. Usually the binary
408+
/// swiftmodule file or a pcm in the cache. Returns an empty string if not
409+
/// applicable.
410+
virtual StringRef getLoadedFilename() const { return StringRef(); }
411+
407412
virtual StringRef getFilenameForPrivateDecl(const ValueDecl *decl) const {
408413
return StringRef();
409414
}

include/swift/AST/Module.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,17 @@ class ModuleDecl
836836
/// string if this is not applicable.
837837
StringRef getModuleFilename() const;
838838

839+
/// Get the path to the file defining this module, what we consider the
840+
/// source of truth about the module. Usually a swiftinterface file for a
841+
/// resilient module, a swiftmodule for a non-resilient module, or the
842+
/// modulemap for a clang module. Returns an empty string if not applicable.
843+
StringRef getModuleSourceFilename() const;
844+
845+
/// Get the path to the file loaded by the compiler. Usually the binary
846+
/// swiftmodule file or a pcm in the cache. Returns an empty string if not
847+
/// applicable.
848+
StringRef getModuleLoadedFilename() const;
849+
839850
/// \returns true if this module is the "swift" standard library module.
840851
bool isStdlibModule() const;
841852

include/swift/ClangImporter/ClangModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class ClangModuleUnit final : public LoadedFile {
110110

111111
virtual StringRef getFilename() const override;
112112

113+
virtual StringRef getLoadedFilename() const override;
114+
113115
virtual const clang::Module *getUnderlyingClangModule() const override {
114116
return getClangModule();
115117
}

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ class SerializedASTFile final : public LoadedFile {
451451

452452
virtual StringRef getFilename() const override;
453453

454+
virtual StringRef getLoadedFilename() const override;
455+
454456
virtual StringRef getModuleDefiningPath() const override;
455457

456458
ValueDecl *getMainDecl() const override;

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,10 @@ ASTContext::getModule(ImportPath::Module ModulePath, bool AllowMemoryCached) {
24792479
if (LangOpts.EnableModuleLoadingRemarks) {
24802480
Diags.diagnose(ModulePath.getSourceRange().Start,
24812481
diag::module_loaded,
2482-
M->getModuleFilename());
2482+
M->getName(),
2483+
/*overlay=*/false,
2484+
M->getModuleSourceFilename(),
2485+
M->getModuleLoadedFilename());
24832486
}
24842487
return M;
24852488
}
@@ -2507,8 +2510,17 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
25072510
for (auto &importer : getImpl().ModuleLoaders) {
25082511
if (importer.get() == getClangModuleLoader())
25092512
continue;
2510-
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath))
2513+
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath)) {
2514+
if (LangOpts.EnableModuleLoadingRemarks) {
2515+
Diags.diagnose(SourceLoc(),
2516+
diag::module_loaded,
2517+
M->getName(),
2518+
/*overlay=*/true,
2519+
M->getModuleSourceFilename(),
2520+
M->getModuleLoadedFilename());
2521+
}
25112522
return M;
2523+
}
25122524
}
25132525

25142526
return nullptr;

lib/AST/Module.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,25 @@ StringRef ModuleDecl::getModuleFilename() const {
19651965
return Result;
19661966
}
19671967

1968+
StringRef ModuleDecl::getModuleSourceFilename() const {
1969+
for (auto F : getFiles()) {
1970+
if (auto *SFU = dyn_cast<SynthesizedFileUnit>(F))
1971+
continue;
1972+
return F->getModuleDefiningPath();
1973+
}
1974+
1975+
return StringRef();
1976+
}
1977+
1978+
StringRef ModuleDecl::getModuleLoadedFilename() const {
1979+
for (auto F : getFiles()) {
1980+
if (auto LF = dyn_cast<LoadedFile>(F)) {
1981+
return LF->getLoadedFilename();
1982+
}
1983+
}
1984+
return StringRef();
1985+
}
1986+
19681987
bool ModuleDecl::isStdlibModule() const {
19691988
return !getParent() && getName() == getASTContext().StdlibModuleName;
19701989
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3631,6 +3631,12 @@ StringRef ClangModuleUnit::getFilename() const {
36313631
return StringRef();
36323632
}
36333633

3634+
StringRef ClangModuleUnit::getLoadedFilename() const {
3635+
if (const clang::FileEntry *F = clangModule->getASTFile())
3636+
return F->getName();
3637+
return StringRef();
3638+
}
3639+
36343640
clang::TargetInfo &ClangImporter::getTargetInfo() const {
36353641
return Impl.Instance->getTarget();
36363642
}

lib/Serialization/ModuleFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@ class ModuleFile
712712
return Core->ModuleInputBuffer->getBufferIdentifier();
713713
}
714714

715+
StringRef getModuleLoadedFilename() const {
716+
return Core->ModuleInputBuffer->getBufferIdentifier();
717+
}
718+
715719
StringRef getTargetTriple() const {
716720
return Core->TargetTriple;
717721
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,10 @@ StringRef SerializedASTFile::getFilename() const {
15821582
return File.getModuleFilename();
15831583
}
15841584

1585+
StringRef SerializedASTFile::getLoadedFilename() const {
1586+
return File.getModuleLoadedFilename();
1587+
}
1588+
15851589
StringRef SerializedASTFile::getTargetTriple() const {
15861590
return File.getTargetTriple();
15871591
}

0 commit comments

Comments
 (0)