|
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/MacroDefinition.h" |
| 21 | +#include "swift/AST/PluginLoader.h" |
20 | 22 | #include "swift/AST/SourceFile.h"
|
21 |
| -#include "swift/Basic/Assertions.h" |
22 | 23 | #include "swift/Frontend/Frontend.h"
|
23 | 24 | #include "llvm/CAS/CASProvidingFileSystem.h"
|
24 | 25 | #include "llvm/CAS/CachingOnDiskFileSystem.h"
|
@@ -104,6 +105,20 @@ void ModuleDependencyInfo::addTestableImport(ImportPath::Module module) {
|
104 | 105 | dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get())->addTestableImport(module);
|
105 | 106 | }
|
106 | 107 |
|
| 108 | +void ModuleDependencyInfo::addMacroDependency(StringRef module, |
| 109 | + StringRef libraryPath, |
| 110 | + StringRef executablePath) { |
| 111 | + if (auto swiftSourceStorage = |
| 112 | + dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get())) |
| 113 | + swiftSourceStorage->addMacroDependency(module, libraryPath, executablePath); |
| 114 | + else if (auto swiftInterfaceStorage = |
| 115 | + dyn_cast<SwiftInterfaceModuleDependenciesStorage>(storage.get())) |
| 116 | + swiftInterfaceStorage->addMacroDependency(module, libraryPath, |
| 117 | + executablePath); |
| 118 | + else |
| 119 | + llvm_unreachable("Unexpected dependency kind"); |
| 120 | +} |
| 121 | + |
107 | 122 | bool ModuleDependencyInfo::isTestableImport(StringRef moduleName) const {
|
108 | 123 | if (auto swiftSourceDepStorage = getAsSwiftSourceModule())
|
109 | 124 | return swiftSourceDepStorage->testableImports.contains(moduleName);
|
@@ -184,35 +199,45 @@ void ModuleDependencyInfo::addModuleImports(
|
184 | 199 | SmallVector<Decl *, 32> decls;
|
185 | 200 | sourceFile.getTopLevelDecls(decls);
|
186 | 201 | for (auto decl : decls) {
|
187 |
| - auto importDecl = dyn_cast<ImportDecl>(decl); |
188 |
| - if (!importDecl) |
189 |
| - continue; |
190 |
| - |
191 |
| - ImportPath::Builder scratch; |
192 |
| - auto realPath = importDecl->getRealModulePath(scratch); |
193 |
| - |
194 |
| - // Explicit 'Builtin' import is not a part of the module's |
195 |
| - // dependency set, does not exist on the filesystem, |
196 |
| - // and is resolved within the compiler during compilation. |
197 |
| - SmallString<64> importedModuleName; |
198 |
| - realPath.getString(importedModuleName); |
199 |
| - if (importedModuleName == BUILTIN_NAME) |
200 |
| - continue; |
201 |
| - |
202 |
| - // Ignore/diagnose tautological imports akin to import resolution |
203 |
| - if (!swift::dependencies::checkImportNotTautological( |
204 |
| - realPath, importDecl->getLoc(), sourceFile, |
205 |
| - importDecl->isExported())) |
206 |
| - continue; |
207 |
| - |
208 |
| - addModuleImport(realPath, &alreadyAddedModules, |
209 |
| - sourceManager, importDecl->getLoc()); |
210 |
| - |
211 |
| - // Additionally, keep track of which dependencies of a Source |
212 |
| - // module are `@Testable`. |
213 |
| - if (getKind() == swift::ModuleDependencyKind::SwiftSource && |
214 |
| - importDecl->isTestable()) |
215 |
| - addTestableImport(realPath); |
| 202 | + if (auto importDecl = dyn_cast<ImportDecl>(decl)) { |
| 203 | + ImportPath::Builder scratch; |
| 204 | + auto realPath = importDecl->getRealModulePath(scratch); |
| 205 | + |
| 206 | + // Explicit 'Builtin' import is not a part of the module's |
| 207 | + // dependency set, does not exist on the filesystem, |
| 208 | + // and is resolved within the compiler during compilation. |
| 209 | + SmallString<64> importedModuleName; |
| 210 | + realPath.getString(importedModuleName); |
| 211 | + if (importedModuleName == BUILTIN_NAME) |
| 212 | + continue; |
| 213 | + |
| 214 | + // Ignore/diagnose tautological imports akin to import resolution |
| 215 | + if (!swift::dependencies::checkImportNotTautological( |
| 216 | + realPath, importDecl->getLoc(), sourceFile, |
| 217 | + importDecl->isExported())) |
| 218 | + continue; |
| 219 | + |
| 220 | + addModuleImport(realPath, &alreadyAddedModules, sourceManager, |
| 221 | + importDecl->getLoc()); |
| 222 | + |
| 223 | + // Additionally, keep track of which dependencies of a Source |
| 224 | + // module are `@Testable`. |
| 225 | + if (getKind() == swift::ModuleDependencyKind::SwiftSource && |
| 226 | + importDecl->isTestable()) |
| 227 | + addTestableImport(realPath); |
| 228 | + } else if (auto macroDecl = dyn_cast<MacroDecl>(decl)) { |
| 229 | + auto macroDef = macroDecl->getDefinition(); |
| 230 | + auto &ctx = macroDecl->getASTContext(); |
| 231 | + if (macroDef.kind != MacroDefinition::Kind::External) |
| 232 | + continue; |
| 233 | + auto external = macroDef.getExternalMacro(); |
| 234 | + PluginLoader &loader = ctx.getPluginLoader(); |
| 235 | + auto &entry = loader.lookupPluginByModuleName(external.moduleName); |
| 236 | + if (entry.libraryPath.empty() && entry.executablePath.empty()) |
| 237 | + continue; |
| 238 | + addMacroDependency(external.moduleName.str(), entry.libraryPath, |
| 239 | + entry.executablePath); |
| 240 | + } |
216 | 241 | }
|
217 | 242 |
|
218 | 243 | auto fileName = sourceFile.getFilename();
|
@@ -609,58 +634,6 @@ void SwiftDependencyTracker::addCommonSearchPathDeps(
|
609 | 634 | // Add VFSOverlay file.
|
610 | 635 | for (auto &Overlay: Opts.VFSOverlayFiles)
|
611 | 636 | FS->status(Overlay);
|
612 |
| - |
613 |
| - // Add plugin dylibs from the toolchain only by look through the plugin search |
614 |
| - // directory. |
615 |
| - auto recordFiles = [&](StringRef Path) { |
616 |
| - std::error_code EC; |
617 |
| - for (auto I = FS->dir_begin(Path, EC); |
618 |
| - !EC && I != llvm::vfs::directory_iterator(); I = I.increment(EC)) { |
619 |
| - if (I->type() != llvm::sys::fs::file_type::regular_file) |
620 |
| - continue; |
621 |
| -#if defined(_WIN32) |
622 |
| - constexpr StringRef libPrefix{}; |
623 |
| - constexpr StringRef libSuffix = ".dll"; |
624 |
| -#else |
625 |
| - constexpr StringRef libPrefix = "lib"; |
626 |
| - constexpr StringRef libSuffix = LTDL_SHLIB_EXT; |
627 |
| -#endif |
628 |
| - StringRef filename = llvm::sys::path::filename(I->path()); |
629 |
| - if (filename.starts_with(libPrefix) && filename.ends_with(libSuffix)) |
630 |
| - FS->status(I->path()); |
631 |
| - } |
632 |
| - }; |
633 |
| - for (auto &entry : Opts.PluginSearchOpts) { |
634 |
| - switch (entry.getKind()) { |
635 |
| - |
636 |
| - // '-load-plugin-library <library path>'. |
637 |
| - case PluginSearchOption::Kind::LoadPluginLibrary: { |
638 |
| - auto &val = entry.get<PluginSearchOption::LoadPluginLibrary>(); |
639 |
| - FS->status(val.LibraryPath); |
640 |
| - break; |
641 |
| - } |
642 |
| - |
643 |
| - // '-load-plugin-executable <executable path>#<module name>, ...'. |
644 |
| - case PluginSearchOption::Kind::LoadPluginExecutable: { |
645 |
| - // We don't have executable plugin in toolchain. |
646 |
| - break; |
647 |
| - } |
648 |
| - |
649 |
| - // '-plugin-path <library search path>'. |
650 |
| - case PluginSearchOption::Kind::PluginPath: { |
651 |
| - auto &val = entry.get<PluginSearchOption::PluginPath>(); |
652 |
| - recordFiles(val.SearchPath); |
653 |
| - break; |
654 |
| - } |
655 |
| - |
656 |
| - // '-external-plugin-path <library search path>#<server path>'. |
657 |
| - case PluginSearchOption::Kind::ExternalPluginPath: { |
658 |
| - auto &val = entry.get<PluginSearchOption::ExternalPluginPath>(); |
659 |
| - recordFiles(val.SearchPath); |
660 |
| - break; |
661 |
| - } |
662 |
| - } |
663 |
| - } |
664 | 637 | }
|
665 | 638 |
|
666 | 639 | void SwiftDependencyTracker::startTracking() {
|
|
0 commit comments