Skip to content

ModuleInterface: add a frontend flag to skip printing import statement corresponding to a module name. #41552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Frontend/ModuleInterfaceSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct ModuleInterfaceOptions {

/// Intentionally print invalid syntax into the file.
bool DebugPrintInvalidSyntax = false;

/// A list of modules we shouldn't import in the public interfaces.
std::vector<std::string> ModulesToSkipInPublicInterface;
};

extern version::Version InterfaceFormatVersion;
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1012,4 +1012,9 @@ def new_driver_path
: Separate<["-"], "new-driver-path">, MetaVarName<"<path>">,
HelpText<"Path of the new driver to be used">;

def skip_import_in_public_interface:
Separate<["-"], "skip-import-in-public-interface">,
HelpText<"Skip the import statement corresponding to a module name "
"when printing the public interface.">;

} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
Opts.PrintSPIs = true;
}
}
for (auto val: Args.getAllArgValues(OPT_skip_import_in_public_interface)) {
Opts.ModulesToSkipInPublicInterface.push_back(val);
}
}

/// Save a copy of any flags marked as ModuleInterfaceOption, if running
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/ModuleInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ static void printImports(raw_ostream &out,
continue;
}

if (llvm::count(Opts.ModulesToSkipInPublicInterface,
importedModule->getName().str())) {
continue;
}

llvm::SmallSetVector<Identifier, 4> spis;
M->lookupImportedSPIGroups(importedModule, spis);

Expand Down
1 change: 1 addition & 0 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
// Copy the settings from the module interface to add SPI printing.
ModuleInterfaceOptions privOpts = Invocation.getModuleInterfaceOptions();
privOpts.PrintSPIs = true;
privOpts.ModulesToSkipInPublicInterface.clear();

hadAnyError |= printModuleInterfaceIfNeeded(
Invocation.getPrivateModuleInterfaceOutputPathForWholeModule(),
Expand Down
23 changes: 23 additions & 0 deletions test/ModuleInterface/skip-import-in-public-interface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/I)

// RUN: %target-swift-frontend -emit-module -module-name Foo %s -DFoo -emit-module-path %t/I/Foo.swiftmodule
// RUN: %target-swift-frontend -typecheck -module-name Bar %s -I %t/I -emit-module-interface-path %t/Bar.swiftinterface -emit-private-module-interface-path %t/Bar.private.swiftinterface -skip-import-in-public-interface Foo

// RUN: %FileCheck %s --check-prefix=PUBLIC-INTERFACE < %t/Bar.swiftinterface
// RUN: %FileCheck %s --check-prefix=PRIVATE-INTERFACE < %t/Bar.private.swiftinterface

#if Foo

public func fooFunc() {}

#else

import Foo

public func barFunc() {}

#endif

// PUBLIC-INTERFACE-NOT: import Foo
// PRIVATE-INTERFACE: import Foo