Skip to content

Commit 62993ff

Browse files
authored
Merge pull request #76783 from tshortli/import-filter-refactor
Frontend: Refactor import collection for module interface printing
2 parents 35b9c1b + 71ee93d commit 62993ff

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -934,16 +934,13 @@ class ModuleDecl
934934
enum class ImportFilterKind {
935935
/// Include imports declared with `@_exported`.
936936
Exported = 1 << 0,
937-
/// Include "regular" imports with an access-level of `public`.
937+
/// Include "regular" imports with an effective access level of `public`.
938938
Default = 1 << 1,
939939
/// Include imports declared with `@_implementationOnly`.
940940
ImplementationOnly = 1 << 2,
941-
/// Include imports declared with `package import`.
941+
/// Include imports declared with an access level of `package`.
942942
PackageOnly = 1 << 3,
943-
/// Include imports marked `internal` or lower. These differs form
944-
/// implementation-only imports by stricter type-checking and loading
945-
/// policies. At this moment, we can group them under the same category
946-
/// as they have the same loading behavior.
943+
/// Include imports with an effective access level of `internal` or lower.
947944
InternalOrBelow = 1 << 4,
948945
/// Include imports declared with `@_spiOnly`.
949946
SPIOnly = 1 << 5,
@@ -983,7 +980,7 @@ class ModuleDecl
983980
/// \p filter controls whether public, private, or any imports are included
984981
/// in this list.
985982
void getImportedModules(SmallVectorImpl<ImportedModule> &imports,
986-
ImportFilter filter = ImportFilterKind::Exported) const;
983+
ImportFilter filter) const;
987984

988985
/// Looks up which external macros are defined by this file.
989986
void getExternalMacros(SmallVectorImpl<ExternalMacroPlugin> &macros) const;

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ static void printImports(raw_ostream &out,
270270
ModuleDecl::ImportFilterKind::Default,
271271
ModuleDecl::ImportFilterKind::ShadowedByCrossImportOverlay};
272272

273+
using ImportSet = llvm::SmallSet<ImportedModule, 8, ImportedModule::Order>;
274+
auto getImports = [M](ModuleDecl::ImportFilter filter) -> ImportSet {
275+
SmallVector<ImportedModule, 8> matchingImports;
276+
M->getImportedModules(matchingImports, filter);
277+
ImportSet importSet;
278+
importSet.insert(matchingImports.begin(), matchingImports.end());
279+
return importSet;
280+
};
281+
273282
// With -experimental-spi-imports:
274283
// When printing the private or package swiftinterface file, print implementation-only
275284
// imports only if they are also SPI. First, list all implementation-only imports and
@@ -282,10 +291,7 @@ static void printImports(raw_ostream &out,
282291
ModuleDecl::ImportFilterKind::ImplementationOnly);
283292

284293
// Only consider modules imported consistently as implementation-only.
285-
M->getImportedModules(allImports,
286-
allImportFilter);
287-
llvm::SmallSet<ImportedModule, 8, ImportedModule::Order> allImportSet;
288-
allImportSet.insert(allImports.begin(), allImports.end());
294+
ImportSet allImportSet = getImports(allImportFilter);
289295

290296
for (auto import: ioiImports)
291297
if (allImportSet.count(import) == 0)
@@ -295,16 +301,13 @@ static void printImports(raw_ostream &out,
295301
}
296302

297303
/// Collect @_spiOnly imports that are not imported elsewhere publicly.
298-
llvm::SmallSet<ImportedModule, 4, ImportedModule::Order> spiOnlyImportSet;
304+
ImportSet spiOnlyImportSet;
299305
if (!Opts.printPublicInterface()) {
300306
SmallVector<ImportedModule, 4> spiOnlyImports, otherImports;
301307
M->getImportedModules(spiOnlyImports,
302308
ModuleDecl::ImportFilterKind::SPIOnly);
303309

304-
M->getImportedModules(otherImports,
305-
allImportFilter);
306-
llvm::SmallSet<ImportedModule, 8, ImportedModule::Order> otherImportsSet;
307-
otherImportsSet.insert(otherImports.begin(), otherImports.end());
310+
ImportSet otherImportsSet = getImports(allImportFilter);
308311

309312
// Rule out inconsistent imports.
310313
for (auto import: spiOnlyImports)
@@ -316,25 +319,19 @@ static void printImports(raw_ostream &out,
316319

317320
// Collect the public imports as a subset so that we can mark them with
318321
// '@_exported'.
319-
SmallVector<ImportedModule, 8> exportedImports;
320-
M->getImportedModules(exportedImports, ModuleDecl::ImportFilterKind::Exported);
321-
llvm::SmallSet<ImportedModule, 8, ImportedModule::Order> exportedImportSet;
322-
exportedImportSet.insert(exportedImports.begin(), exportedImports.end());
322+
ImportSet exportedImportSet =
323+
getImports(ModuleDecl::ImportFilterKind::Exported);
323324

324325
// All of the above are considered `public` including `@_spiOnly public import`
325326
// and `@_spi(name) public import`, and should override `package import`.
326327
// Track the `public` imports here to determine whether to override.
327-
llvm::SmallSet<ImportedModule, 8, ImportedModule::Order> publicImportSet;
328-
SmallVector<ImportedModule, 8> publicImports;
329-
M->getImportedModules(publicImports, allImportFilter);
330-
publicImportSet.insert(publicImports.begin(), publicImports.end());
328+
ImportSet publicImportSet = getImports(allImportFilter);
331329

332330
// Used to determine whether `package import` should be overriden below.
333-
llvm::SmallSet<ImportedModule, 8, ImportedModule::Order> packageOnlyImportSet;
331+
ImportSet packageOnlyImportSet;
334332
if (Opts.printPackageInterface()) {
335-
SmallVector<ImportedModule, 8> packageOnlyImports;
336-
M->getImportedModules(packageOnlyImports, ModuleDecl::ImportFilterKind::PackageOnly);
337-
packageOnlyImportSet.insert(packageOnlyImports.begin(), packageOnlyImports.end());
333+
packageOnlyImportSet =
334+
getImports(ModuleDecl::ImportFilterKind::PackageOnly);
338335
allImportFilter |= ModuleDecl::ImportFilterKind::PackageOnly;
339336
}
340337

@@ -382,12 +379,6 @@ static void printImports(raw_ostream &out,
382379

383380
if (!Opts.printPublicInterface()) {
384381
// An import visible in the private or package swiftinterface only.
385-
//
386-
// In the long term, we want to print this attribute for consistency and
387-
// to enforce exportability analysis of generated code.
388-
// For now, not printing the attribute allows us to have backwards
389-
// compatible swiftinterfaces and we can live without
390-
// checking the generate code for a while.
391382
if (spiOnlyImportSet.count(import))
392383
out << "@_spiOnly ";
393384

lib/IDE/ImportDepth.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ImportDepth::ImportDepth(ASTContext &context,
6464

6565
// Add imports to the worklist.
6666
SmallVector<ImportedModule, 16> imports;
67-
module->getImportedModules(imports);
67+
module->getImportedModules(imports, ModuleDecl::ImportFilterKind::Exported);
6868
for (auto &import : imports) {
6969
uint8_t next = std::max(depth, uint8_t(depth + 1)); // unsigned wrap
7070

lib/Index/Index.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,12 +2091,8 @@ void IndexSwiftASTWalker::collectRecursiveModuleImports(
20912091
return;
20922092
}
20932093

2094-
ModuleDecl::ImportFilter ImportFilter;
2095-
ImportFilter |= ModuleDecl::ImportFilterKind::Exported;
2096-
ImportFilter |= ModuleDecl::ImportFilterKind::Default;
2097-
// FIXME: ImportFilterKind::ShadowedByCrossImportOverlay?
20982094
SmallVector<ImportedModule, 8> Imports;
2099-
TopMod.getImportedModules(Imports);
2095+
TopMod.getImportedModules(Imports, ModuleDecl::ImportFilterKind::Exported);
21002096

21012097
for (auto Import : Imports) {
21022098
collectRecursiveModuleImports(*Import.importedModule, Visited);

0 commit comments

Comments
 (0)