Skip to content

Commit 808ccd4

Browse files
committed
[SymbolGraphGen] Handle cxx module imports in swift-symbolgraph-extract
Updates swift-symbolgraph-extract to parse "-cxx-interoperability-mode" flags and update the underlying compiler invocation. This fixes a bug where were are unable to extract the symbol graph from swiftmodules with transitive cxx modules because we parsed cxx headers as c headers. rdar://128888548 (Add support for parsing cxx headers)
1 parent 07edd9c commit 808ccd4

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_BASIC_LANGOPTIONS_H
1919
#define SWIFT_BASIC_LANGOPTIONS_H
2020

21+
#include "swift/AST/DiagnosticsFrontend.h"
2122
#include "swift/Basic/Feature.h"
2223
#include "swift/Basic/FixedBitSet.h"
2324
#include "swift/Basic/FunctionBodySkipping.h"
@@ -31,6 +32,7 @@
3132
#include "llvm/ADT/SmallString.h"
3233
#include "llvm/ADT/SmallVector.h"
3334
#include "llvm/ADT/StringRef.h"
35+
#include "llvm/Option/ArgList.h"
3436
#include "llvm/Support/Regex.h"
3537
#include "llvm/Support/VersionTuple.h"
3638
#include "llvm/Support/raw_ostream.h"
@@ -319,6 +321,9 @@ namespace swift {
319321
/// to the Swift language version.
320322
version::Version cxxInteropCompatVersion;
321323

324+
void setCxxInteropFromArgs(llvm::opt::ArgList &Args,
325+
swift::DiagnosticEngine &Diags);
326+
322327
bool CForeignReferenceTypes = false;
323328

324329
/// Imports getters and setters as computed properties.

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args,
196196
.Default(AccessLevel::Public);
197197
}
198198

199+
Invocation.getLangOptions().setCxxInteropFromArgs(ParsedArgs, Diags);
200+
199201
std::string InstanceSetupError;
200202
if (CI.setup(Invocation, InstanceSetupError)) {
201203
llvm::outs() << InstanceSetupError << '\n';

lib/Frontend/CompilerInvocation.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,40 @@ static void diagnoseCxxInteropCompatMode(Arg *verArg, ArgList &Args,
525525
diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr);
526526
}
527527

528+
void LangOptions::setCxxInteropFromArgs(ArgList &Args,
529+
swift::DiagnosticEngine &Diags) {
530+
if (Arg *A = Args.getLastArg(options::OPT_cxx_interoperability_mode)) {
531+
if (Args.hasArg(options::OPT_enable_experimental_cxx_interop)) {
532+
Diags.diagnose(SourceLoc(), diag::dont_enable_interop_and_compat);
533+
}
534+
535+
auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
536+
EnableCXXInterop |=
537+
(interopCompatMode.first == CxxCompatMode::enabled);
538+
if (EnableCXXInterop) {
539+
cxxInteropCompatVersion = interopCompatMode.second;
540+
// The default is tied to the current language version.
541+
if (cxxInteropCompatVersion.empty())
542+
cxxInteropCompatVersion =
543+
EffectiveLanguageVersion.asMajorVersion();
544+
}
545+
546+
if (interopCompatMode.first == CxxCompatMode::invalid)
547+
diagnoseCxxInteropCompatMode(A, Args, Diags);
548+
}
549+
550+
if (Args.hasArg(options::OPT_enable_experimental_cxx_interop)) {
551+
Diags.diagnose(SourceLoc(), diag::enable_interop_flag_deprecated);
552+
Diags.diagnose(SourceLoc(), diag::swift_will_maintain_compat);
553+
EnableCXXInterop |= true;
554+
// Using the deprecated option only forces the 'swift-5.9' compat
555+
// mode.
556+
if (cxxInteropCompatVersion.empty())
557+
cxxInteropCompatVersion =
558+
validateCxxInteropCompatibilityMode("swift-5.9").second;
559+
}
560+
}
561+
528562
static std::optional<swift::StrictConcurrency>
529563
parseStrictConcurrency(StringRef value) {
530564
return llvm::StringSwitch<std::optional<swift::StrictConcurrency>>(value)
@@ -1255,37 +1289,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
12551289
if (const Arg *A = Args.getLastArg(OPT_clang_target)) {
12561290
Opts.ClangTarget = llvm::Triple(A->getValue());
12571291
}
1258-
1259-
if (Arg *A = Args.getLastArg(OPT_cxx_interoperability_mode)) {
1260-
if (Args.hasArg(OPT_enable_experimental_cxx_interop)) {
1261-
Diags.diagnose(SourceLoc(), diag::dont_enable_interop_and_compat);
1262-
}
1263-
1264-
auto interopCompatMode = validateCxxInteropCompatibilityMode(A->getValue());
1265-
Opts.EnableCXXInterop |=
1266-
(interopCompatMode.first == CxxCompatMode::enabled);
1267-
if (Opts.EnableCXXInterop) {
1268-
Opts.cxxInteropCompatVersion = interopCompatMode.second;
1269-
// The default is tied to the current language version.
1270-
if (Opts.cxxInteropCompatVersion.empty())
1271-
Opts.cxxInteropCompatVersion =
1272-
Opts.EffectiveLanguageVersion.asMajorVersion();
1273-
}
1274-
1275-
if (interopCompatMode.first == CxxCompatMode::invalid)
1276-
diagnoseCxxInteropCompatMode(A, Args, Diags);
1277-
}
1278-
1279-
if (Args.hasArg(OPT_enable_experimental_cxx_interop)) {
1280-
Diags.diagnose(SourceLoc(), diag::enable_interop_flag_deprecated);
1281-
Diags.diagnose(SourceLoc(), diag::swift_will_maintain_compat);
1282-
Opts.EnableCXXInterop |= true;
1283-
// Using the deprecated option only forces the 'swift-5.9' compat
1284-
// mode.
1285-
if (Opts.cxxInteropCompatVersion.empty())
1286-
Opts.cxxInteropCompatVersion =
1287-
validateCxxInteropCompatibilityMode("swift-5.9").second;
1288-
}
1292+
1293+
Opts.setCxxInteropFromArgs(Args, Diags);
12891294

12901295
Opts.EnableObjCInterop =
12911296
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,

0 commit comments

Comments
 (0)