|
| 1 | +//===--- ImportDepth.cpp --------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/IDE/ImportDepth.h" |
| 14 | +#include "swift/AST/Module.h" |
| 15 | +#include "clang/Basic/Module.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | +using namespace swift::ide; |
| 19 | + |
| 20 | +ImportDepth::ImportDepth(ASTContext &context, |
| 21 | + const FrontendOptions &frontendOptions) { |
| 22 | + llvm::DenseSet<ModuleDecl *> seen; |
| 23 | + std::deque<std::pair<ModuleDecl *, uint8_t>> worklist; |
| 24 | + |
| 25 | + StringRef mainModule = frontendOptions.ModuleName; |
| 26 | + auto *main = context.getLoadedModule(context.getIdentifier(mainModule)); |
| 27 | + assert(main && "missing main module"); |
| 28 | + worklist.emplace_back(main, uint8_t(0)); |
| 29 | + |
| 30 | + // Imports from -import-name such as Playground auxiliary sources are treated |
| 31 | + // specially by applying import depth 0. |
| 32 | + llvm::StringSet<> auxImports; |
| 33 | + for (const auto &pair : frontendOptions.getImplicitImportModuleNames()) |
| 34 | + auxImports.insert(pair.first); |
| 35 | + |
| 36 | + // Private imports from this module. |
| 37 | + // FIXME: only the private imports from the current source file. |
| 38 | + // FIXME: ImportFilterKind::ShadowedByCrossImportOverlay? |
| 39 | + SmallVector<ImportedModule, 16> mainImports; |
| 40 | + main->getImportedModules(mainImports, |
| 41 | + {ModuleDecl::ImportFilterKind::Default, |
| 42 | + ModuleDecl::ImportFilterKind::ImplementationOnly}); |
| 43 | + for (auto &import : mainImports) { |
| 44 | + uint8_t depth = 1; |
| 45 | + if (auxImports.count(import.importedModule->getName().str())) |
| 46 | + depth = 0; |
| 47 | + worklist.emplace_back(import.importedModule, depth); |
| 48 | + } |
| 49 | + |
| 50 | + // Fill depths with BFS over module imports. |
| 51 | + while (!worklist.empty()) { |
| 52 | + ModuleDecl *module; |
| 53 | + uint8_t depth; |
| 54 | + std::tie(module, depth) = worklist.front(); |
| 55 | + worklist.pop_front(); |
| 56 | + |
| 57 | + if (!seen.insert(module).second) |
| 58 | + continue; |
| 59 | + |
| 60 | + // Insert new module:depth mapping. |
| 61 | + const clang::Module *CM = module->findUnderlyingClangModule(); |
| 62 | + if (CM) { |
| 63 | + depths[CM->getFullModuleName()] = depth; |
| 64 | + } else { |
| 65 | + depths[module->getName().str()] = depth; |
| 66 | + } |
| 67 | + |
| 68 | + // Add imports to the worklist. |
| 69 | + SmallVector<ImportedModule, 16> imports; |
| 70 | + module->getImportedModules(imports); |
| 71 | + for (auto &import : imports) { |
| 72 | + uint8_t next = std::max(depth, uint8_t(depth + 1)); // unsigned wrap |
| 73 | + |
| 74 | + // Implicitly imported sub-modules get the same depth as their parent. |
| 75 | + if (const clang::Module *CMI = |
| 76 | + import.importedModule->findUnderlyingClangModule()) |
| 77 | + if (CM && CMI->isSubModuleOf(CM)) |
| 78 | + next = depth; |
| 79 | + worklist.emplace_back(import.importedModule, next); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments