Skip to content

Commit 352f3c2

Browse files
committed
Allow users to opt-out of implicit Cxx import with -disable-implicit-cxx-module-import
1 parent 2d86330 commit 352f3c2

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
@@ -498,6 +498,10 @@ def disable_implicit_backtracing_module_import : Flag<["-"],
498498
"disable-implicit-backtracing-module-import">,
499499
HelpText<"Disable the implicit import of the _Backtracing module.">;
500500

501+
def disable_implicit_cxx_module_import : Flag<["-"],
502+
"disable-implicit-cxx-module-import">,
503+
HelpText<"Disable the implicit import of the C++ Standard Library module.">;
504+
501505
def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
502506
HelpText<"Don't run SIL ARC optimization passes.">;
503507
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
@@ -642,6 +642,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
642642
Opts.DisableImplicitStringProcessingModuleImport |=
643643
Args.hasArg(OPT_disable_implicit_string_processing_module_import);
644644

645+
Opts.DisableImplicitCxxModuleImport |=
646+
Args.hasArg(OPT_disable_implicit_cxx_module_import);
647+
645648
Opts.DisableImplicitBacktracingModuleImport =
646649
Args.hasFlag(OPT_disable_implicit_backtracing_module_import,
647650
OPT_enable_implicit_backtracing_module_import,

lib/Frontend/Frontend.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,26 @@ bool CompilerInvocation::shouldImportSwiftBacktracing() const {
10841084
FrontendOptions::ParseInputMode::SwiftModuleInterface;
10851085
}
10861086

1087+
bool CompilerInvocation::shouldImportCxx() const {
1088+
// C++ Interop is disabled
1089+
if (!getLangOptions().EnableCXXInterop)
1090+
return false;
1091+
// Avoid C++ stdlib when building Swift stdlib
1092+
if (getImplicitStdlibKind() == ImplicitStdlibKind::Builtin)
1093+
return false;
1094+
// Avoid importing Cxx when building Cxx itself
1095+
if (getFrontendOptions().ModuleName == CXX_MODULE_NAME)
1096+
return false;
1097+
// Cxx cannot be imported when Library evolution is enabled
1098+
if (getFrontendOptions().EnableLibraryEvolution)
1099+
return false;
1100+
// Implicit import of Cxx is disabled
1101+
if (getLangOptions().DisableImplicitCxxModuleImport)
1102+
return false;
1103+
1104+
return true;
1105+
}
1106+
10871107
/// Implicitly import the SwiftOnoneSupport module in non-optimized
10881108
/// builds. This allows for use of popular specialized functions
10891109
/// from the standard library, which makes the non-optimized builds
@@ -1276,9 +1296,7 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
12761296
}
12771297

12781298
if (Invocation.getLangOptions().EnableCXXInterop) {
1279-
if (imports.StdlibKind != ImplicitStdlibKind::Builtin &&
1280-
Invocation.getFrontendOptions().ModuleName != CXX_MODULE_NAME &&
1281-
canImportCxx())
1299+
if (Invocation.shouldImportCxx() && canImportCxx())
12821300
pushImport(CXX_MODULE_NAME);
12831301
if (canImportCxxShim())
12841302
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,8 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
19851985
compatVersion = "default";
19861986
else if (langOpts.cxxInteropCompatVersion[0] == 5)
19871987
compatVersion = "swift-5.9";
1988+
else if (langOpts.cxxInteropCompatVersion[0] == 6)
1989+
compatVersion = "swift-6";
19881990
else if (langOpts.cxxInteropCompatVersion[0] ==
19891991
version::getUpcomingCxxInteropCompatVersion())
19901992
compatVersion = "upcoming-swift";

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
10231023
if (M.hasCxxInteroperability() &&
10241024
M.getResilienceStrategy() != ResilienceStrategy::Resilient &&
10251025
!Ctx.LangOpts.EnableCXXInterop &&
1026-
Ctx.LangOpts.RequireCxxInteropToImportCxxInteropModule) {
1026+
Ctx.LangOpts.RequireCxxInteropToImportCxxInteropModule &&
1027+
M.getName().str() != CXX_MODULE_NAME) {
10271028
auto loc = diagLoc.value_or(SourceLoc());
10281029
Ctx.Diags.diagnose(loc, diag::need_cxx_interop_to_import_module,
10291030
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)