Skip to content

Commit 71ee93d

Browse files
committed
Frontend: Refactor import collection for module interface printing.
Consolidates duplicated code. NFC.
1 parent 3847c34 commit 71ee93d

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

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

0 commit comments

Comments
 (0)