Skip to content

Commit 0018113

Browse files
committed
[interop] support 'default' mode for '-cxx-interoperability-mode' option
Default corresponds to the Swift language version used to compile the input. Swift-5.9 is still supported for now, but will be removed shortly
1 parent 8fd028c commit 0018113

File tree

10 files changed

+38
-23
lines changed

10 files changed

+38
-23
lines changed

docs/CppInteroperability/GettingStartedWithC++Interop.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module CxxTest {
3232
Add the C++ module to the include path and enable C++ interop:
3333
- Navigate to your project directory
3434
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
35-
- Under `Custom Flags` -> `Other Swift Flags` add `-cxx-interoperability-mode=swift-5.9`
35+
- Under `Custom Flags` -> `Other Swift Flags` add `-cxx-interoperability-mode=default`
3636
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/CxxTest`).
3737

3838
- This should now allow your to import your C++ Module into any `.swift` file.
@@ -81,7 +81,7 @@ After creating your Swift package project, follow the steps [Creating a Module t
8181
- Swift code will be in `Sources/CxxInterop` called `main.swift`
8282
- C++ source code follows the example shown in [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code)
8383
- Under targets, add the name of your C++ module and the directory containing the Swift code as a target.
84-
- In the target defining your Swift target, add a`dependencies` to the C++ Module, the `path`, `source`, and `swiftSettings` with `unsafeFlags` with the source to the C++ Module, and enable `-cxx-interoperability-mode=swift-5.9`
84+
- In the target defining your Swift target, add a`dependencies` to the C++ Module, the `path`, `source`, and `swiftSettings` with `unsafeFlags` with the source to the C++ Module, and enable `-cxx-interoperability-mode=default`
8585

8686
```
8787
//In Package Manifest
@@ -111,7 +111,7 @@ let package = Package(
111111
sources: [ "main.swift" ],
112112
swiftSettings: [.unsafeFlags([
113113
"-I", "Sources/CxxTest",
114-
"-cxx-interoperability-mode=swift-5.9",
114+
"-cxx-interoperability-mode=default",
115115
])]
116116
),
117117
]
@@ -144,7 +144,7 @@ After creating your project follow the steps [Creating a Module to contain your
144144
- Create a `CMakeLists.txt` file and configure for your project
145145
- In`add_library` invoke `cxx-support` with the path to the C++ implementation file
146146
- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/CxxTest`
147-
- Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-cxx-interoperability-mode=swift-5.9`.
147+
- Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-cxx-interoperability-mode=default`.
148148
- In the example below we will be following the file structure used in [Creating a Swift Package](#Creating-a-Swift-Package)
149149

150150
```
@@ -167,7 +167,7 @@ target_include_directories(cxx-support PUBLIC
167167
168168
add_executable(CxxInterop ./Sources/CxxInterop/main.swift)
169169
target_compile_options(CxxInterop PRIVATE
170-
"SHELL:-cxx-interoperability-mode=swift-5.9"
170+
"SHELL:-cxx-interoperability-mode=default"
171171
target_link_libraries(CxxInterop PRIVATE cxx-support)
172172
173173
```

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,9 @@ WARNING(compiler_plugin_not_loaded,none,
514514
ERROR(dont_enable_interop_and_compat,none,
515515
"do not pass both -enable-experimental-cxx-interop and "
516516
"-cxx-interoperability-mode. Remove -enable-experimental-cxx-interop.", ())
517-
518-
ERROR(invalid_interop_compat_mode,none,
519-
"invalid option passed to -cxx-interoperability-mode. Please select either "
520-
"'off' or 'swift-5.9'.", ())
517+
518+
NOTE(valid_cxx_interop_modes,none,
519+
"valid arguments to '-cxx-interoperability-mode=' are %0", (StringRef))
521520
NOTE(swift_will_maintain_compat,none,
522521
"Swift will maintain source compatibility for imported APIs based on the "
523522
"selected compatibility mode, so updating the Swift compiler will not "

lib/Frontend/CompilerInvocation.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,26 @@ enum class CxxCompatMode {
438438
static CxxCompatMode validateCxxInteropCompatibilityMode(StringRef mode) {
439439
if (mode == "off")
440440
return CxxCompatMode::off;
441+
if (mode == "default")
442+
return CxxCompatMode::enabled;
443+
// FIXME: Drop swift-5.9.
441444
if (mode == "swift-5.9")
442445
return CxxCompatMode::enabled;
443446
return CxxCompatMode::invalid;
444447
}
445448

449+
static void diagnoseCxxInteropCompatMode(Arg *verArg, ArgList &Args,
450+
DiagnosticEngine &diags) {
451+
// General invalid argument error
452+
diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
453+
verArg->getAsString(Args), verArg->getValue());
454+
455+
// Note valid C++ interoperability modes.
456+
auto validVers = {llvm::StringRef("off"), llvm::StringRef("default")};
457+
auto versStr = "'" + llvm::join(validVers, "', '") + "'";
458+
diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr);
459+
}
460+
446461
static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
447462
DiagnosticEngine &Diags,
448463
const FrontendOptions &FrontendOpts) {
@@ -940,10 +955,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
940955
auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
941956
Opts.EnableCXXInterop |= (interopCompatMode == CxxCompatMode::enabled);
942957

943-
if (interopCompatMode == CxxCompatMode::invalid) {
944-
Diags.diagnose(SourceLoc(), diag::invalid_interop_compat_mode);
945-
Diags.diagnose(SourceLoc(), diag::swift_will_maintain_compat);
946-
}
958+
if (interopCompatMode == CxxCompatMode::invalid)
959+
diagnoseCxxInteropCompatMode(A, Args, Diags);
947960
}
948961

949962
if (Args.hasArg(OPT_enable_experimental_cxx_interop)) {

test/Interop/Cxx/driver/enable-interop-flag-depr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: rm -rf %t
22
// RUN: split-file %s %t
3-
// RUN: not %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop -cxx-interoperability-mode=swift-5.9 2>&1 | %FileCheck %s
3+
// RUN: not %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop -cxx-interoperability-mode=default 2>&1 | %FileCheck %s
44

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

test/Interop/Cxx/driver/invalid-interop-compat-mode.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// RUN: split-file %s %t
33
// RUN: not %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -cxx-interoperability-mode=swift-5.8 2>&1 | %FileCheck %s
44

5+
// Note: swift-5.9 is still supported, but will be removed.
6+
// RUN: %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -cxx-interoperability-mode=swift-5.9
7+
58
//--- Inputs/module.modulemap
69
module Test {
710
header "test.h"
@@ -14,5 +17,5 @@ module Test {
1417

1518
import Test
1619

17-
// CHECK: error: invalid option passed to -cxx-interoperability-mode. Please select either 'off' or 'swift-5.9'.
18-
// CHECK: note: Swift will maintain source compatibility for imported APIs based on the selected compatibility mode, so updating the Swift compiler will not change how APIs are imported.
20+
// CHECK: error: invalid value 'swift-5.8' in '-cxx-interoperability-mode=swift-5.8'
21+
// CHECK: note: valid arguments to '-cxx-interoperability-mode=' are 'off', 'default'

test/Interop/SwiftToCxx/cross-module-refs/imported-struct-refs-in-cxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// RUN: %FileCheck %s < %t/uses-structs.h
77
// RUN: %check-interop-cxx-header-in-clang(-I %t %t/uses-structs.h)
88

9-
// RUN: %target-swift-frontend %s -typecheck -module-name UsesStructs -I %t -cxx-interoperability-mode=swift-5.9 -emit-clang-header-path %t/uses-structs-default.h -clang-header-expose-module Structs=structs.h
9+
// RUN: %target-swift-frontend %s -typecheck -module-name UsesStructs -I %t -cxx-interoperability-mode=default -emit-clang-header-path %t/uses-structs-default.h -clang-header-expose-module Structs=structs.h
1010
// RUN: %check-interop-cxx-header-in-clang(-I %t %t/uses-structs-default.h)
1111

1212
import Structs

test/SourceKit/InterfaceGen/gen_clang_cxx_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: split-file %s %t
33

44
// RUN: %sourcekitd-test -req=interface-gen -module CxxModule -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -I %t/Inputs -target %target-triple %clang-importer-sdk-nosource | %FileCheck %s
5-
// RUN: %sourcekitd-test -req=interface-gen -module CxxModule -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -cxx-interoperability-mode=swift-5.9 -I %t/Inputs -target %target-triple %clang-importer-sdk-nosource | %FileCheck %s
5+
// RUN: %sourcekitd-test -req=interface-gen -module CxxModule -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -cxx-interoperability-mode=default -I %t/Inputs -target %target-triple %clang-importer-sdk-nosource | %FileCheck %s
66

77

88
// RUN: not %sourcekitd-test -req=interface-gen -module CxxModule -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -Xcc -DERROR -I %t/Inputs -target %target-triple %clang-importer-sdk-nosource 2>&1 | %FileCheck --check-prefix=NOLOAD %s

test/SourceKit/InterfaceGen/gen_clang_libcxx_sdk_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %sourcekitd-test -req=interface-gen -module CxxStdlib -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -cxx-interoperability-mode=swift-5.9 -target %target-triple -sdk %sdk | %FileCheck %s
1+
// RUN: %sourcekitd-test -req=interface-gen -module CxxStdlib -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -cxx-interoperability-mode=default -target %target-triple -sdk %sdk | %FileCheck %s
22

33
// REQUIRES: OS=macosx
44

test/lit.cfg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,8 @@ elif swift_test_mode == 'with_cxx_interop':
744744
config.available_features.add("nonexecutable_test")
745745
config.available_features.add("executable_test")
746746
config.available_features.add("with_cxx_interop")
747-
config.swift_frontend_test_options += ' -cxx-interoperability-mode=swift-5.9'
748-
config.swift_driver_test_options += ' -cxx-interoperability-mode=swift-5.9'
747+
config.swift_frontend_test_options += ' -cxx-interoperability-mode=default'
748+
config.swift_driver_test_options += ' -cxx-interoperability-mode=default'
749749
else:
750750
lit_config.fatal("Unknown test mode %r" % swift_test_mode)
751751

@@ -2400,7 +2400,7 @@ config.substitutions.insert(0, ('%check-cxx-header-in-clang',
24002400
config.substitutions.append(('%env-', config.target_env_prefix))
24012401

24022402
config.substitutions.append(('%target-clangxx', '%s -std=c++11' % config.target_clang))
2403-
config.substitutions.append(('%target-swiftxx-frontend', '%s -cxx-interoperability-mode=swift-5.9' % config.target_swift_frontend))
2403+
config.substitutions.append(('%target-swiftxx-frontend', '%s -cxx-interoperability-mode=default' % config.target_swift_frontend))
24042404

24052405
config.substitutions.append(('%target-runtime', config.target_runtime))
24062406

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
724724
}
725725
if (retryWithCxxEnabled) {
726726
std::vector<const char *> AdjustedArgs(Args.begin(), Args.end());
727-
AdjustedArgs.push_back("-cxx-interoperability-mode=swift-5.9");
727+
AdjustedArgs.push_back("-cxx-interoperability-mode=default");
728728
return editorOpenInterface(Consumer, Name, ModuleName, Group, AdjustedArgs,
729729
SynthesizedExtensions, InterestedUSR);
730730
}

0 commit comments

Comments
 (0)