Skip to content

Commit ec4e0e2

Browse files
committed
Allow users to opt-out of implicit Cxx import with -disable-implicit-cxx-module-import
1 parent 6ea604b commit ec4e0e2

File tree

10 files changed

+41
-7
lines changed

10 files changed

+41
-7
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ namespace swift {
402402
bool DisableImplicitBacktracingModuleImport =
403403
!SWIFT_IMPLICIT_BACKTRACING_IMPORT;
404404

405+
/// Disable the implicit import of the Cxx module.
406+
bool DisableImplicitCxxModuleImport = false;
407+
405408
// Whether to use checked continuations when making an async call from
406409
// Swift into ObjC. If false, will use unchecked continuations instead.
407410
bool UseCheckedAsyncObjCBridging = false;

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ using llvm::BCVBR;
4040
/// Every .moddepcache file begins with these 4 bytes, for easy identification.
4141
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D','C'};
4242
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR =
43-
6; // mappedPCMPath
43+
7; // isSystem
4444
/// Increment this on every change.
4545
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 1;
4646

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ class CompilerInvocation {
405405
/// imported.
406406
bool shouldImportSwiftBacktracing() const;
407407

408+
/// Whether the CXX module should be implicitly imported.
409+
bool shouldImportCxx() const;
410+
408411
/// Performs input setup common to these tools:
409412
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
410413
/// Return value includes the buffer so caller can keep it alive.

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ def disable_implicit_backtracing_module_import : Flag<["-"],
502502
"disable-implicit-backtracing-module-import">,
503503
HelpText<"Disable the implicit import of the _Backtracing module.">;
504504

505+
def disable_implicit_cxx_module_import : Flag<["-"],
506+
"disable-implicit-cxx-module-import">,
507+
HelpText<"Disable the implicit import of the C++ Standard Library module.">;
508+
505509
def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
506510
HelpText<"Don't run SIL ARC optimization passes.">;
507511
def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
637637
Opts.DisableImplicitStringProcessingModuleImport |=
638638
Args.hasArg(OPT_disable_implicit_string_processing_module_import);
639639

640+
Opts.DisableImplicitCxxModuleImport |=
641+
Args.hasArg(OPT_disable_implicit_cxx_module_import);
642+
640643
Opts.DisableImplicitBacktracingModuleImport =
641644
Args.hasFlag(OPT_disable_implicit_backtracing_module_import,
642645
OPT_enable_implicit_backtracing_module_import,

lib/Frontend/Frontend.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,26 @@ bool CompilerInvocation::shouldImportSwiftBacktracing() const {
10941094
FrontendOptions::ParseInputMode::SwiftModuleInterface;
10951095
}
10961096

1097+
bool CompilerInvocation::shouldImportCxx() const {
1098+
// C++ Interop is disabled
1099+
if (!getLangOptions().EnableCXXInterop)
1100+
return false;
1101+
// Avoid C++ stdlib when building Swift stdlib
1102+
if (getImplicitStdlibKind() == ImplicitStdlibKind::Builtin)
1103+
return false;
1104+
// Avoid importing Cxx when building Cxx itself
1105+
if (getFrontendOptions().ModuleName == CXX_MODULE_NAME)
1106+
return false;
1107+
// Cxx cannot be imported when Library evolution is enabled
1108+
if (getFrontendOptions().EnableLibraryEvolution)
1109+
return false;
1110+
// Implicit import of Cxx is disabled
1111+
if (getLangOptions().DisableImplicitCxxModuleImport)
1112+
return false;
1113+
1114+
return true;
1115+
}
1116+
10971117
/// Implicitly import the SwiftOnoneSupport module in non-optimized
10981118
/// builds. This allows for use of popular specialized functions
10991119
/// from the standard library, which makes the non-optimized builds
@@ -1286,9 +1306,7 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
12861306
}
12871307

12881308
if (Invocation.getLangOptions().EnableCXXInterop) {
1289-
if (imports.StdlibKind != ImplicitStdlibKind::Builtin &&
1290-
Invocation.getFrontendOptions().ModuleName != CXX_MODULE_NAME &&
1291-
canImportCxx())
1309+
if (Invocation.shouldImportCxx() && canImportCxx())
12921310
pushImport(CXX_MODULE_NAME);
12931311
if (canImportCxxShim())
12941312
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,8 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
19871987
compatVersion = "default";
19881988
else if (langOpts.cxxInteropCompatVersion[0] == 5)
19891989
compatVersion = "swift-5.9";
1990+
else if (langOpts.cxxInteropCompatVersion[0] == 6)
1991+
compatVersion = "swift-6";
19901992
else if (langOpts.cxxInteropCompatVersion[0] ==
19911993
version::getUpcomingCxxInteropCompatVersion())
19921994
compatVersion = "upcoming-swift";

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
10311031
if (M.hasCxxInteroperability() &&
10321032
M.getResilienceStrategy() != ResilienceStrategy::Resilient &&
10331033
!Ctx.LangOpts.EnableCXXInterop &&
1034-
Ctx.LangOpts.RequireCxxInteropToImportCxxInteropModule) {
1034+
Ctx.LangOpts.RequireCxxInteropToImportCxxInteropModule &&
1035+
M.getName().str() != CXX_MODULE_NAME) {
10351036
auto loc = diagLoc.value_or(SourceLoc());
10361037
Ctx.Diags.diagnose(loc, diag::need_cxx_interop_to_import_module,
10371038
M.getName());

test/Interop/Cxx/library-evolution/prohibit-cxx-api-in-evolving-libraries.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
3-
// RUN: %target-swift-frontend %t/test.swift -I %t/Inputs -typecheck -enable-library-evolution -enable-experimental-cxx-interop -disable-availability-checking -verify
3+
// RUN: %target-swift-frontend %t/test.swift -I %t/Inputs -typecheck -enable-library-evolution -enable-experimental-cxx-interop -disable-availability-checking -disable-implicit-cxx-module-import -verify
44

55
//--- Inputs/module.modulemap
66
module CxxModule {

test/Interop/Cxx/library-evolution/prohibit-cxx-calls-in-evolving-inlinable-bodies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
3-
// RUN: %target-swift-frontend %t/test.swift -I %t/Inputs -typecheck -enable-library-evolution -enable-experimental-cxx-interop -verify
3+
// RUN: %target-swift-frontend %t/test.swift -I %t/Inputs -typecheck -enable-library-evolution -enable-experimental-cxx-interop -disable-implicit-cxx-module-import -verify
44

55
//--- Inputs/module.modulemap
66
module CxxModule {

0 commit comments

Comments
 (0)