Skip to content

Commit 8d6cd2a

Browse files
committed
Add cxx interop support to symbol graph extract
1 parent 07edd9c commit 8d6cd2a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

include/swift/Option/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def enable_experimental_cxx_interop :
758758

759759
def cxx_interoperability_mode :
760760
Joined<["-"], "cxx-interoperability-mode=">,
761-
Flags<[FrontendOption, ModuleInterfaceOption]>,
761+
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption]>,
762762
HelpText<"Enables C++ interoperability; pass 'default' to enable or 'off' to disable">;
763763

764764
def experimental_c_foreign_reference_types :

lib/DriverTool/swift_symbolgraph_extract_main.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,50 @@
3030
using namespace swift;
3131
using namespace options;
3232

33+
enum class CxxCompatMode {
34+
invalid,
35+
enabled,
36+
off
37+
};
38+
39+
static std::pair<CxxCompatMode, version::Version>
40+
validateCxxInteropCompatibilityMode(StringRef mode) {
41+
if (mode == "off")
42+
return {CxxCompatMode::off, {}};
43+
if (mode == "default")
44+
return {CxxCompatMode::enabled, {}};
45+
if (mode == "upcoming-swift")
46+
return {CxxCompatMode::enabled,
47+
version::Version({version::getUpcomingCxxInteropCompatVersion()})};
48+
if (mode == "swift-6")
49+
return {CxxCompatMode::enabled, version::Version({6})};
50+
// Swift-5.9 corresponds to the Swift 5 language mode when
51+
// Swift 5 is the default language version.
52+
if (mode == "swift-5.9")
53+
return {CxxCompatMode::enabled, version::Version({5})};
54+
// Note: If this is updated, corresponding code in
55+
// InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl needs
56+
// to be updated also.
57+
return {CxxCompatMode::invalid, {}};
58+
}
59+
60+
static void diagnoseCxxInteropCompatMode(llvm::opt::Arg *verArg,
61+
llvm::opt::InputArgList &Args,
62+
DiagnosticEngine &diags) {
63+
// General invalid argument error
64+
diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
65+
verArg->getAsString(Args), verArg->getValue());
66+
67+
// Note valid C++ interoperability modes.
68+
auto validVers = {llvm::StringRef("off"), llvm::StringRef("default"),
69+
llvm::StringRef("swift-6"), llvm::StringRef("swift-5.9")};
70+
auto versStr = "'" + llvm::join(validVers, "', '") + "'";
71+
diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr);
72+
}
73+
3374
int swift_symbolgraph_extract_main(ArrayRef<const char *> Args,
3475
const char *Argv0, void *MainAddr) {
76+
printf("hello rauhul\n");
3577
INITIALIZE_LLVM();
3678

3779
CompilerInvocation Invocation;
@@ -196,6 +238,23 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args,
196238
.Default(AccessLevel::Public);
197239
}
198240

241+
LangOptions& LangOpts = Invocation.getLangOptions();
242+
if (auto *A = ParsedArgs.getLastArg(OPT_cxx_interoperability_mode)) {
243+
auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
244+
LangOpts.EnableCXXInterop |=
245+
(interopCompatMode.first == CxxCompatMode::enabled);
246+
if (LangOpts.EnableCXXInterop) {
247+
LangOpts.cxxInteropCompatVersion = interopCompatMode.second;
248+
// The default is tied to the current language version.
249+
if (LangOpts.cxxInteropCompatVersion.empty())
250+
LangOpts.cxxInteropCompatVersion =
251+
LangOpts.EffectiveLanguageVersion.asMajorVersion();
252+
}
253+
254+
if (interopCompatMode.first == CxxCompatMode::invalid)
255+
diagnoseCxxInteropCompatMode(A, ParsedArgs, Diags);
256+
}
257+
199258
std::string InstanceSetupError;
200259
if (CI.setup(Invocation, InstanceSetupError)) {
201260
llvm::outs() << InstanceSetupError << '\n';

0 commit comments

Comments
 (0)