Skip to content

Commit 201c4b6

Browse files
authored
Merge pull request #64486 from xymus/import-filter-refactor
[Sema] Intro common sets of import filters to simplify calls to `getImportedModules`
2 parents 8090366 + 205a2ed commit 201c4b6

File tree

11 files changed

+41
-60
lines changed

11 files changed

+41
-60
lines changed

include/swift/AST/Module.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,28 @@ class ModuleDecl
871871
/// \sa getImportedModules
872872
using ImportFilter = OptionSet<ImportFilterKind>;
873873

874+
/// Returns an \c ImportFilter with all elements of \c ImportFilterKind.
875+
constexpr static ImportFilter getImportFilterAll() {
876+
return {ImportFilterKind::Exported,
877+
ImportFilterKind::Default,
878+
ImportFilterKind::ImplementationOnly,
879+
ImportFilterKind::PackageOnly,
880+
ImportFilterKind::SPIOnly,
881+
ImportFilterKind::ShadowedByCrossImportOverlay};
882+
}
883+
884+
/// Import kinds visible to the module declaring them.
885+
///
886+
/// This leaves out \c ShadowedByCrossImportOverlay as even if present in
887+
/// the sources it's superseded by the cross-overlay as the local import.
888+
constexpr static ImportFilter getImportFilterLocal() {
889+
return {ImportFilterKind::Exported,
890+
ImportFilterKind::Default,
891+
ImportFilterKind::ImplementationOnly,
892+
ImportFilterKind::PackageOnly,
893+
ImportFilterKind::SPIOnly};
894+
}
895+
874896
/// Looks up which modules are imported by this module.
875897
///
876898
/// \p filter controls whether public, private, or any imports are included

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4511,14 +4511,8 @@ namespace {
45114511
llvm::SmallVector<ValueDecl *> results;
45124512
llvm::SmallVector<ImportedModule> importedModules;
45134513

4514-
ModuleDecl::ImportFilter moduleImportFilter = ModuleDecl::ImportFilterKind::Default;
4515-
moduleImportFilter |= ModuleDecl::ImportFilterKind::Exported;
4516-
moduleImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
4517-
moduleImportFilter |= ModuleDecl::ImportFilterKind::PackageOnly;
4518-
moduleImportFilter |= ModuleDecl::ImportFilterKind::SPIOnly;
4519-
moduleImportFilter |= ModuleDecl::ImportFilterKind::ShadowedByCrossImportOverlay;
4520-
4521-
mainModule->getImportedModules(importedModules, moduleImportFilter);
4514+
mainModule->getImportedModules(importedModules,
4515+
ModuleDecl::getImportFilterAll());
45224516

45234517
for (auto &import : importedModules) {
45244518
if (import.importedModule->isNonSwiftModule())

lib/FrontendTool/ImportedModules.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ bool swift::emitImportedModules(ModuleDecl *mainModule,
8383
if (!clangImporter->importBridgingHeader(implicitHeaderPath, mainModule)) {
8484
SmallVector<ImportedModule, 16> imported;
8585
clangImporter->getImportedHeaderModule()->getImportedModules(
86-
imported, {ModuleDecl::ImportFilterKind::Exported,
87-
ModuleDecl::ImportFilterKind::Default,
88-
ModuleDecl::ImportFilterKind::ImplementationOnly,
89-
ModuleDecl::ImportFilterKind::PackageOnly,
90-
ModuleDecl::ImportFilterKind::SPIOnly});
86+
imported, ModuleDecl::getImportFilterLocal());
9187

9288
for (auto IM : imported) {
9389
if (auto clangModule = IM.importedModule->findUnderlyingClangModule())

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,7 @@ static bool contains(const SetLike &setLike, Item item) {
110110
/// By default, all imports are included.
111111
static void getImmediateImports(
112112
ModuleDecl *module, SmallPtrSetImpl<ModuleDecl *> &imports,
113-
ModuleDecl::ImportFilter importFilter = {
114-
ModuleDecl::ImportFilterKind::Exported,
115-
ModuleDecl::ImportFilterKind::Default,
116-
ModuleDecl::ImportFilterKind::ImplementationOnly,
117-
ModuleDecl::ImportFilterKind::PackageOnly,
118-
ModuleDecl::ImportFilterKind::SPIOnly,
119-
ModuleDecl::ImportFilterKind::ShadowedByCrossImportOverlay}) {
113+
ModuleDecl::ImportFilter importFilter = ModuleDecl::getImportFilterAll()) {
120114
SmallVector<ImportedModule, 8> importList;
121115
module->getImportedModules(importList, importFilter);
122116

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,7 @@ void swift::ide::deliverCompletionResults(
12841284
{
12851285
// Collect modules directly imported in this SourceFile.
12861286
SmallVector<ImportedModule, 4> directImport;
1287-
SF.getImportedModules(directImport,
1288-
{ModuleDecl::ImportFilterKind::Default,
1289-
ModuleDecl::ImportFilterKind::ImplementationOnly});
1287+
SF.getImportedModules(directImport, ModuleDecl::getImportFilterLocal());
12901288
for (auto import : directImport)
12911289
explictlyImportedModules.insert(import.importedModule);
12921290

@@ -1383,14 +1381,7 @@ void swift::ide::deliverCompletionResults(
13831381

13841382
// Add results for all imported modules.
13851383
SmallVector<ImportedModule, 4> Imports;
1386-
SF.getImportedModules(
1387-
Imports, {
1388-
ModuleDecl::ImportFilterKind::Exported,
1389-
ModuleDecl::ImportFilterKind::Default,
1390-
ModuleDecl::ImportFilterKind::ImplementationOnly,
1391-
ModuleDecl::ImportFilterKind::PackageOnly,
1392-
ModuleDecl::ImportFilterKind::SPIOnly,
1393-
});
1384+
SF.getImportedModules(Imports, ModuleDecl::getImportFilterLocal());
13941385

13951386
for (auto Imported : Imports) {
13961387
for (auto Import : namelookup::getAllImports(Imported.importedModule))

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ void CompletionLookup::collectImportedModules(
306306
SmallVector<ImportedModule, 16> Imported;
307307
SmallVector<ImportedModule, 16> FurtherImported;
308308
CurrDeclContext->getParentSourceFile()->getImportedModules(
309-
Imported, {ModuleDecl::ImportFilterKind::Exported,
310-
ModuleDecl::ImportFilterKind::Default,
311-
ModuleDecl::ImportFilterKind::ImplementationOnly});
309+
Imported, ModuleDecl::getImportFilterLocal());
312310

313311
for (ImportedModule &imp : Imported)
314312
directImportedModules.insert(imp.importedModule->getNameStr());

lib/IDE/ImportDepth.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ ImportDepth::ImportDepth(ASTContext &context,
3434
auxImports.insert(pair.first);
3535

3636
// Private imports from this module.
37-
// FIXME: only the private imports from the current source file.
38-
// FIXME: ImportFilterKind::ShadowedByCrossImportOverlay?
3937
SmallVector<ImportedModule, 16> mainImports;
40-
main->getImportedModules(mainImports,
41-
{ModuleDecl::ImportFilterKind::Default,
42-
ModuleDecl::ImportFilterKind::ImplementationOnly});
38+
main->getImportedModules(mainImports, ModuleDecl::getImportFilterLocal());
4339
for (auto &import : mainImports) {
4440
uint8_t depth = 1;
4541
if (auxImports.count(import.importedModule->getName().str()))

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,10 +2046,8 @@ void IRGenDebugInfoImpl::finalize() {
20462046
// Get the list of imported modules (which may actually be different
20472047
// from all ImportDecls).
20482048
SmallVector<ImportedModule, 8> ModuleWideImports;
2049-
IGM.getSwiftModule()->getImportedModules(
2050-
ModuleWideImports, {ModuleDecl::ImportFilterKind::Exported,
2051-
ModuleDecl::ImportFilterKind::Default,
2052-
ModuleDecl::ImportFilterKind::ImplementationOnly});
2049+
IGM.getSwiftModule()->getImportedModules(ModuleWideImports,
2050+
ModuleDecl::getImportFilterLocal());
20532051
for (auto M : ModuleWideImports)
20542052
if (!ImportedModules.count(M.importedModule))
20552053
createImportedModule(MainFile, M, MainFile, 0);

lib/Index/Index.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ class SourceFileOrModule {
129129

130130
void
131131
getImportedModules(SmallVectorImpl<ImportedModule> &Modules) const {
132-
constexpr ModuleDecl::ImportFilter ImportFilter = {
133-
ModuleDecl::ImportFilterKind::Exported,
134-
ModuleDecl::ImportFilterKind::Default,
135-
ModuleDecl::ImportFilterKind::ImplementationOnly};
132+
constexpr ModuleDecl::ImportFilter ImportFilter =
133+
ModuleDecl::getImportFilterLocal();
136134

137135
if (auto *SF = SFOrMod.dyn_cast<SourceFile *>()) {
138136
SF->getImportedModules(Modules, ImportFilter);

lib/Index/IndexRecord.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,10 +898,8 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
898898

899899
// Module dependencies.
900900
SmallVector<ImportedModule, 8> imports;
901-
primarySourceFile->getImportedModules(
902-
imports, {ModuleDecl::ImportFilterKind::Exported,
903-
ModuleDecl::ImportFilterKind::Default,
904-
ModuleDecl::ImportFilterKind::ImplementationOnly});
901+
primarySourceFile->getImportedModules(imports,
902+
ModuleDecl::getImportFilterLocal());
905903
StringScratchSpace moduleNameScratch;
906904
addModuleDependencies(imports, indexStorePath, indexClangModules,
907905
indexSystemModules, skipStdlib, includeLocals,

0 commit comments

Comments
 (0)