[Macro][Dependencies] Properly model macro dependencies in the scanner

Add function to handle all macro dependencies kinds in the scanner,
including taking care of the macro definitions in the module interface
for its client to use. The change involves:
  * Encode the macro definition inside the binary module
  * Resolve macro modules in the dependencies scanners, including those
    declared inside the dependency modules.
  * Propagate the macro defined from the direct dependencies to track
    all the potentially available modules inside a module compilation.
This commit is contained in:
Steven Wu
2024-09-19 10:57:08 -07:00
parent b9dc764541
commit e0541b0357
20 changed files with 326 additions and 56 deletions

View File

@@ -16,7 +16,9 @@
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/ImportCache.h"
#include "swift/AST/Module.h"
#include "swift/AST/ModuleDependencies.h"
#include "swift/AST/PluginLoader.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FileTypes.h"
@@ -477,6 +479,26 @@ SerializedModuleLoaderBase::getImportsOfModule(
importedHeader};
}
std::optional<MacroPluginDependency>
SerializedModuleLoaderBase::resolveMacroPlugin(const ExternalMacroPlugin &macro,
StringRef packageName) {
if (macro.MacroAccess == ExternalMacroPlugin::Access::Internal)
return std::nullopt;
if (macro.MacroAccess == ExternalMacroPlugin::Access::Package &&
packageName != Ctx.LangOpts.PackageName)
return std::nullopt;
auto &loader = Ctx.getPluginLoader();
auto &entry =
loader.lookupPluginByModuleName(Ctx.getIdentifier(macro.ModuleName));
if (entry.libraryPath.empty() && entry.executablePath.empty())
return std::nullopt;
return MacroPluginDependency{entry.libraryPath.str(),
entry.executablePath.str()};
}
llvm::ErrorOr<ModuleDependencyInfo>
SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
bool isTestableImport) {
@@ -562,6 +584,15 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
definingModulePath, isFramework, loadedModuleFile->isStaticLibrary(),
/*module-cache-key*/ "", userModuleVer);
for (auto &macro : loadedModuleFile->getExternalMacros()) {
auto deps =
resolveMacroPlugin(macro, loadedModuleFile->getModulePackageName());
if (!deps)
continue;
dependencies.addMacroDependency(macro.ModuleName, deps->LibraryPath,
deps->ExecutablePath);
}
return std::move(dependencies);
}
@@ -1729,6 +1760,11 @@ void SerializedASTFile::getImportedModules(
File.getImportedModules(imports, filter);
}
void SerializedASTFile::getExternalMacros(
SmallVectorImpl<ExternalMacroPlugin> &macros) const {
File.getExternalMacros(macros);
}
void SerializedASTFile::collectLinkLibrariesFromImports(
ModuleDecl::LinkLibraryCallback callback) const {
llvm::SmallVector<ImportedModule, 8> Imports;