Skip to content

[SymbolGraphGen] Handle cxx module imports in swift-symbolgraph-extract #73963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef SWIFT_BASIC_LANGOPTIONS_H
#define SWIFT_BASIC_LANGOPTIONS_H

#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/Feature.h"
#include "swift/Basic/FixedBitSet.h"
#include "swift/Basic/FunctionBodySkipping.h"
Expand All @@ -31,6 +32,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -319,6 +321,9 @@ namespace swift {
/// to the Swift language version.
version::Version cxxInteropCompatVersion;

void setCxxInteropFromArgs(llvm::opt::ArgList &Args,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally this would go somewhere like ArgsToLangOptionsConverter (similarly to the existing ArgsToFrontendOptionsConverter), but I don't think that necessarily has to happen in this patch.

swift::DiagnosticEngine &Diags);

bool CForeignReferenceTypes = false;

/// Imports getters and setters as computed properties.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ def enable_experimental_cxx_interop :

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

def experimental_c_foreign_reference_types :
Expand Down
2 changes: 2 additions & 0 deletions lib/DriverTool/swift_symbolgraph_extract_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args,
.Default(AccessLevel::Public);
}

Invocation.getLangOptions().setCxxInteropFromArgs(ParsedArgs, Diags);

std::string InstanceSetupError;
if (CI.setup(Invocation, InstanceSetupError)) {
llvm::outs() << InstanceSetupError << '\n';
Expand Down
67 changes: 36 additions & 31 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,40 @@ static void diagnoseCxxInteropCompatMode(Arg *verArg, ArgList &Args,
diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr);
}

void LangOptions::setCxxInteropFromArgs(ArgList &Args,
swift::DiagnosticEngine &Diags) {
if (Arg *A = Args.getLastArg(options::OPT_cxx_interoperability_mode)) {
if (Args.hasArg(options::OPT_enable_experimental_cxx_interop)) {
Diags.diagnose(SourceLoc(), diag::dont_enable_interop_and_compat);
}

auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
EnableCXXInterop |=
(interopCompatMode.first == CxxCompatMode::enabled);
if (EnableCXXInterop) {
cxxInteropCompatVersion = interopCompatMode.second;
// The default is tied to the current language version.
if (cxxInteropCompatVersion.empty())
cxxInteropCompatVersion =
EffectiveLanguageVersion.asMajorVersion();
}

if (interopCompatMode.first == CxxCompatMode::invalid)
diagnoseCxxInteropCompatMode(A, Args, Diags);
}

if (Args.hasArg(options::OPT_enable_experimental_cxx_interop)) {
Diags.diagnose(SourceLoc(), diag::enable_interop_flag_deprecated);
Diags.diagnose(SourceLoc(), diag::swift_will_maintain_compat);
EnableCXXInterop |= true;
// Using the deprecated option only forces the 'swift-5.9' compat
// mode.
if (cxxInteropCompatVersion.empty())
cxxInteropCompatVersion =
validateCxxInteropCompatibilityMode("swift-5.9").second;
}
}

static std::optional<swift::StrictConcurrency>
parseStrictConcurrency(StringRef value) {
return llvm::StringSwitch<std::optional<swift::StrictConcurrency>>(value)
Expand Down Expand Up @@ -1255,37 +1289,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (const Arg *A = Args.getLastArg(OPT_clang_target)) {
Opts.ClangTarget = llvm::Triple(A->getValue());
}

if (Arg *A = Args.getLastArg(OPT_cxx_interoperability_mode)) {
if (Args.hasArg(OPT_enable_experimental_cxx_interop)) {
Diags.diagnose(SourceLoc(), diag::dont_enable_interop_and_compat);
}

auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
Opts.EnableCXXInterop |=
(interopCompatMode.first == CxxCompatMode::enabled);
if (Opts.EnableCXXInterop) {
Opts.cxxInteropCompatVersion = interopCompatMode.second;
// The default is tied to the current language version.
if (Opts.cxxInteropCompatVersion.empty())
Opts.cxxInteropCompatVersion =
Opts.EffectiveLanguageVersion.asMajorVersion();
}

if (interopCompatMode.first == CxxCompatMode::invalid)
diagnoseCxxInteropCompatMode(A, Args, Diags);
}

if (Args.hasArg(OPT_enable_experimental_cxx_interop)) {
Diags.diagnose(SourceLoc(), diag::enable_interop_flag_deprecated);
Diags.diagnose(SourceLoc(), diag::swift_will_maintain_compat);
Opts.EnableCXXInterop |= true;
// Using the deprecated option only forces the 'swift-5.9' compat
// mode.
if (Opts.cxxInteropCompatVersion.empty())
Opts.cxxInteropCompatVersion =
validateCxxInteropCompatibilityMode("swift-5.9").second;
}

Opts.setCxxInteropFromArgs(Args, Diags);

Opts.EnableObjCInterop =
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,
Expand Down