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