|
17 | 17 | #include "swift/AST/Decl.h"
|
18 | 18 | #include "swift/AST/DiagnosticsFrontend.h"
|
19 | 19 | #include "swift/AST/DiagnosticsSema.h"
|
| 20 | +#include "swift/AST/Evaluator.h" |
| 21 | +#include "swift/AST/MacroDefinition.h" |
| 22 | +#include "swift/AST/PluginLoader.h" |
20 | 23 | #include "swift/AST/SourceFile.h"
|
| 24 | +#include "swift/AST/TypeCheckRequests.h" |
21 | 25 | #include "swift/Frontend/Frontend.h"
|
22 | 26 | #include "llvm/CAS/CASProvidingFileSystem.h"
|
23 | 27 | #include "llvm/CAS/CachingOnDiskFileSystem.h"
|
@@ -103,6 +107,21 @@ void ModuleDependencyInfo::addTestableImport(ImportPath::Module module) {
|
103 | 107 | dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get())->addTestableImport(module);
|
104 | 108 | }
|
105 | 109 |
|
| 110 | +void ModuleDependencyInfo::addMacroDependency(StringRef module, StringRef id, |
| 111 | + StringRef libraryPath, |
| 112 | + StringRef executablePath) { |
| 113 | + if (auto swiftSourceStorage = |
| 114 | + dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get())) |
| 115 | + swiftSourceStorage->addMacroDependency(module, id, libraryPath, |
| 116 | + executablePath); |
| 117 | + else if (auto swiftInterfaceStorage = |
| 118 | + dyn_cast<SwiftInterfaceModuleDependenciesStorage>(storage.get())) |
| 119 | + swiftInterfaceStorage->addMacroDependency(module, id, libraryPath, |
| 120 | + executablePath); |
| 121 | + else |
| 122 | + llvm_unreachable("Unexpected dependency kind"); |
| 123 | +} |
| 124 | + |
106 | 125 | bool ModuleDependencyInfo::isTestableImport(StringRef moduleName) const {
|
107 | 126 | if (auto swiftSourceDepStorage = getAsSwiftSourceModule())
|
108 | 127 | return swiftSourceDepStorage->testableImports.contains(moduleName);
|
@@ -183,35 +202,55 @@ void ModuleDependencyInfo::addModuleImports(
|
183 | 202 | SmallVector<Decl *, 32> decls;
|
184 | 203 | sourceFile.getTopLevelDecls(decls);
|
185 | 204 | for (auto decl : decls) {
|
186 |
| - auto importDecl = dyn_cast<ImportDecl>(decl); |
187 |
| - if (!importDecl) |
188 |
| - continue; |
189 |
| - |
190 |
| - ImportPath::Builder scratch; |
191 |
| - auto realPath = importDecl->getRealModulePath(scratch); |
192 |
| - |
193 |
| - // Explicit 'Builtin' import is not a part of the module's |
194 |
| - // dependency set, does not exist on the filesystem, |
195 |
| - // and is resolved within the compiler during compilation. |
196 |
| - SmallString<64> importedModuleName; |
197 |
| - realPath.getString(importedModuleName); |
198 |
| - if (importedModuleName == BUILTIN_NAME) |
199 |
| - continue; |
200 |
| - |
201 |
| - // Ignore/diagnose tautological imports akin to import resolution |
202 |
| - if (!swift::dependencies::checkImportNotTautological( |
203 |
| - realPath, importDecl->getLoc(), sourceFile, |
204 |
| - importDecl->isExported())) |
205 |
| - continue; |
| 205 | + if (auto importDecl = dyn_cast<ImportDecl>(decl)) { |
| 206 | + ImportPath::Builder scratch; |
| 207 | + auto realPath = importDecl->getRealModulePath(scratch); |
| 208 | + |
| 209 | + // Explicit 'Builtin' import is not a part of the module's |
| 210 | + // dependency set, does not exist on the filesystem, |
| 211 | + // and is resolved within the compiler during compilation. |
| 212 | + SmallString<64> importedModuleName; |
| 213 | + realPath.getString(importedModuleName); |
| 214 | + if (importedModuleName == BUILTIN_NAME) |
| 215 | + continue; |
206 | 216 |
|
207 |
| - addModuleImport(realPath, &alreadyAddedModules, |
208 |
| - sourceManager, importDecl->getLoc()); |
| 217 | + // Ignore/diagnose tautological imports akin to import resolution |
| 218 | + if (!swift::dependencies::checkImportNotTautological( |
| 219 | + realPath, importDecl->getLoc(), sourceFile, |
| 220 | + importDecl->isExported())) |
| 221 | + continue; |
209 | 222 |
|
210 |
| - // Additionally, keep track of which dependencies of a Source |
211 |
| - // module are `@Testable`. |
212 |
| - if (getKind() == swift::ModuleDependencyKind::SwiftSource && |
213 |
| - importDecl->isTestable()) |
214 |
| - addTestableImport(realPath); |
| 223 | + addModuleImport(realPath, &alreadyAddedModules, sourceManager, |
| 224 | + importDecl->getLoc()); |
| 225 | + |
| 226 | + // Additionally, keep track of which dependencies of a Source |
| 227 | + // module are `@Testable`. |
| 228 | + if (getKind() == swift::ModuleDependencyKind::SwiftSource && |
| 229 | + importDecl->isTestable()) |
| 230 | + addTestableImport(realPath); |
| 231 | + } else if (auto macroDecl = dyn_cast<MacroDecl>(decl)) { |
| 232 | + auto macroDef = macroDecl->getDefinition(); |
| 233 | + auto &ctx = macroDecl->getASTContext(); |
| 234 | + if (macroDef.kind != MacroDefinition::Kind::External) |
| 235 | + continue; |
| 236 | + auto external = macroDef.getExternalMacro(); |
| 237 | + CompilerPluginLoadRequest loadRequest{¯oDecl->getASTContext(), |
| 238 | + external.moduleName}; |
| 239 | + CompilerPluginLoadResult loaded = |
| 240 | + evaluateOrDefault(macroDecl->getASTContext().evaluator, loadRequest, |
| 241 | + CompilerPluginLoadResult::error("request error")); |
| 242 | + PluginLoader &loader = ctx.getPluginLoader(); |
| 243 | + if (auto exe = loaded.getAsExecutablePlugin()) { |
| 244 | + // Lookup again to find the library path. |
| 245 | + const auto &entry = |
| 246 | + loader.lookupPluginByModuleName(external.moduleName); |
| 247 | + addMacroDependency(external.moduleName.str(), exe->getIdentity(), |
| 248 | + entry.libraryPath, entry.executablePath); |
| 249 | + } else if (auto plugin = loaded.getAsLibraryPlugin()) { |
| 250 | + addMacroDependency(external.moduleName.str(), plugin->getIdentity(), |
| 251 | + plugin->getLibraryPath(), /*executablePath=*/""); |
| 252 | + } |
| 253 | + } |
215 | 254 | }
|
216 | 255 |
|
217 | 256 | auto fileName = sourceFile.getFilename();
|
@@ -534,58 +573,6 @@ void SwiftDependencyTracker::addCommonSearchPathDeps(
|
534 | 573 | // Add VFSOverlay file.
|
535 | 574 | for (auto &Overlay: Opts.VFSOverlayFiles)
|
536 | 575 | FS->status(Overlay);
|
537 |
| - |
538 |
| - // Add plugin dylibs from the toolchain only by look through the plugin search |
539 |
| - // directory. |
540 |
| - auto recordFiles = [&](StringRef Path) { |
541 |
| - std::error_code EC; |
542 |
| - for (auto I = FS->dir_begin(Path, EC); |
543 |
| - !EC && I != llvm::vfs::directory_iterator(); I = I.increment(EC)) { |
544 |
| - if (I->type() != llvm::sys::fs::file_type::regular_file) |
545 |
| - continue; |
546 |
| -#if defined(_WIN32) |
547 |
| - constexpr StringRef libPrefix{}; |
548 |
| - constexpr StringRef libSuffix = ".dll"; |
549 |
| -#else |
550 |
| - constexpr StringRef libPrefix = "lib"; |
551 |
| - constexpr StringRef libSuffix = LTDL_SHLIB_EXT; |
552 |
| -#endif |
553 |
| - StringRef filename = llvm::sys::path::filename(I->path()); |
554 |
| - if (filename.starts_with(libPrefix) && filename.ends_with(libSuffix)) |
555 |
| - FS->status(I->path()); |
556 |
| - } |
557 |
| - }; |
558 |
| - for (auto &entry : Opts.PluginSearchOpts) { |
559 |
| - switch (entry.getKind()) { |
560 |
| - |
561 |
| - // '-load-plugin-library <library path>'. |
562 |
| - case PluginSearchOption::Kind::LoadPluginLibrary: { |
563 |
| - auto &val = entry.get<PluginSearchOption::LoadPluginLibrary>(); |
564 |
| - FS->status(val.LibraryPath); |
565 |
| - break; |
566 |
| - } |
567 |
| - |
568 |
| - // '-load-plugin-executable <executable path>#<module name>, ...'. |
569 |
| - case PluginSearchOption::Kind::LoadPluginExecutable: { |
570 |
| - // We don't have executable plugin in toolchain. |
571 |
| - break; |
572 |
| - } |
573 |
| - |
574 |
| - // '-plugin-path <library search path>'. |
575 |
| - case PluginSearchOption::Kind::PluginPath: { |
576 |
| - auto &val = entry.get<PluginSearchOption::PluginPath>(); |
577 |
| - recordFiles(val.SearchPath); |
578 |
| - break; |
579 |
| - } |
580 |
| - |
581 |
| - // '-external-plugin-path <library search path>#<server path>'. |
582 |
| - case PluginSearchOption::Kind::ExternalPluginPath: { |
583 |
| - auto &val = entry.get<PluginSearchOption::ExternalPluginPath>(); |
584 |
| - recordFiles(val.SearchPath); |
585 |
| - break; |
586 |
| - } |
587 |
| - } |
588 |
| - } |
589 | 576 | }
|
590 | 577 |
|
591 | 578 | void SwiftDependencyTracker::startTracking() {
|
|
0 commit comments