Skip to content

Commit 3197292

Browse files
authored
Merge pull request #41552 from nkcsgexi/63465931-2
ModuleInterface: add a frontend flag to skip printing import statement corresponding to a module name.
2 parents 5674086 + 1115332 commit 3197292

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ struct ModuleInterfaceOptions {
5555

5656
/// Intentionally print invalid syntax into the file.
5757
bool DebugPrintInvalidSyntax = false;
58+
59+
/// A list of modules we shouldn't import in the public interfaces.
60+
std::vector<std::string> ModulesToSkipInPublicInterface;
5861
};
5962

6063
extern version::Version InterfaceFormatVersion;

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,4 +1012,9 @@ def new_driver_path
10121012
: Separate<["-"], "new-driver-path">, MetaVarName<"<path>">,
10131013
HelpText<"Path of the new driver to be used">;
10141014

1015+
def skip_import_in_public_interface:
1016+
Separate<["-"], "skip-import-in-public-interface">,
1017+
HelpText<"Skip the import statement corresponding to a module name "
1018+
"when printing the public interface.">;
1019+
10151020
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
360360
Opts.PrintSPIs = true;
361361
}
362362
}
363+
for (auto val: Args.getAllArgValues(OPT_skip_import_in_public_interface)) {
364+
Opts.ModulesToSkipInPublicInterface.push_back(val);
365+
}
363366
}
364367

365368
/// Save a copy of any flags marked as ModuleInterfaceOption, if running

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ static void printImports(raw_ostream &out,
225225
continue;
226226
}
227227

228+
if (llvm::count(Opts.ModulesToSkipInPublicInterface,
229+
importedModule->getName().str())) {
230+
continue;
231+
}
232+
228233
llvm::SmallSetVector<Identifier, 4> spis;
229234
M->lookupImportedSPIGroups(importedModule, spis);
230235

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
866866
// Copy the settings from the module interface to add SPI printing.
867867
ModuleInterfaceOptions privOpts = Invocation.getModuleInterfaceOptions();
868868
privOpts.PrintSPIs = true;
869+
privOpts.ModulesToSkipInPublicInterface.clear();
869870

870871
hadAnyError |= printModuleInterfaceIfNeeded(
871872
Invocation.getPrivateModuleInterfaceOutputPathForWholeModule(),
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/I)
3+
4+
// RUN: %target-swift-frontend -emit-module -module-name Foo %s -DFoo -emit-module-path %t/I/Foo.swiftmodule
5+
// 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
6+
7+
// RUN: %FileCheck %s --check-prefix=PUBLIC-INTERFACE < %t/Bar.swiftinterface
8+
// RUN: %FileCheck %s --check-prefix=PRIVATE-INTERFACE < %t/Bar.private.swiftinterface
9+
10+
#if Foo
11+
12+
public func fooFunc() {}
13+
14+
#else
15+
16+
import Foo
17+
18+
public func barFunc() {}
19+
20+
#endif
21+
22+
// PUBLIC-INTERFACE-NOT: import Foo
23+
// PRIVATE-INTERFACE: import Foo

0 commit comments

Comments
 (0)