Skip to content

Commit d5fa9c1

Browse files
authored
Merge pull request #61317 from hyp/eng/another-flag
[interop][SwiftToCxx] add -enable-experimental-cxx-interop-in-clang-h…
2 parents b74d766 + c193a63 commit d5fa9c1

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ class FrontendOptions {
392392
/// header.
393393
bool ExposePublicDeclsInClangHeader = false;
394394

395+
/// Emit C++ bindings for the exposed Swift declarations in the generated
396+
/// clang header.
397+
bool EnableExperimentalCxxInteropInClangHeader = false;
398+
395399
/// \return true if the given action only parses without doing other compilation steps.
396400
static bool shouldActionOnlyParse(ActionType);
397401

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ def enable_experimental_cxx_interop :
630630
Flags<[FrontendOption, HelpHidden, ModuleInterfaceOption]>,
631631
HelpText<"Enable experimental C++ interop code generation and config directives">;
632632

633+
def enable_experimental_cxx_interop_in_clang_header :
634+
Flag<["-"], "enable-experimental-cxx-interop-in-clang-header">,
635+
Flags<[FrontendOption, NoDriverOption, HelpHidden]>,
636+
HelpText<"Enable experimental Swift to C++ interop code generation in generated Clang header">;
637+
633638
def experimental_cxx_stdlib :
634639
Separate<["-"], "experimental-cxx-stdlib">,
635640
Flags<[HelpHidden]>,

include/swift/PrintAsClang/PrintAsClang.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/Identifier.h"
1919

2020
namespace swift {
21+
class FrontendOptions;
2122
class IRGenOptions;
2223
class ModuleDecl;
2324
class ValueDecl;
@@ -34,7 +35,7 @@ class ValueDecl;
3435
/// Returns true on error.
3536
bool printAsClangHeader(raw_ostream &out, ModuleDecl *M,
3637
StringRef bridgingHeader,
37-
bool ExposePublicDeclsInClangHeader,
38+
const FrontendOptions &frontendOpts,
3839
const IRGenOptions &irGenOpts);
3940
}
4041

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ bool ArgsToFrontendOptionsConverter::convert(
286286
Opts.DisableBuildingInterface = Args.hasArg(OPT_disable_building_interface);
287287
Opts.ExposePublicDeclsInClangHeader =
288288
Args.hasArg(OPT_clang_header_expose_public_decls);
289+
Opts.EnableExperimentalCxxInteropInClangHeader =
290+
Args.hasArg(OPT_enable_experimental_cxx_interop_in_clang_header);
289291

290292
computeImportObjCHeaderOptions();
291293
computeImplicitImportModuleNames(OPT_import_module, /*isTestable=*/false);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
181181
/// \see swift::printAsClangHeader
182182
static bool printAsClangHeaderIfNeeded(StringRef outputPath, ModuleDecl *M,
183183
StringRef bridgingHeader,
184-
bool ExposePublicDeclsInClangHeader,
184+
const FrontendOptions &frontendOpts,
185185
const IRGenOptions &irGenOpts) {
186186
if (outputPath.empty())
187187
return false;
188-
return withOutputFile(
189-
M->getDiags(), outputPath, [&](raw_ostream &out) -> bool {
190-
return printAsClangHeader(out, M, bridgingHeader,
191-
ExposePublicDeclsInClangHeader, irGenOpts);
192-
});
188+
return withOutputFile(M->getDiags(), outputPath,
189+
[&](raw_ostream &out) -> bool {
190+
return printAsClangHeader(out, M, bridgingHeader,
191+
frontendOpts, irGenOpts);
192+
});
193193
}
194194

195195
/// Prints the stable module interface for \p M to \p outputPath.
@@ -936,8 +936,8 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
936936
}
937937
hadAnyError |= printAsClangHeaderIfNeeded(
938938
Invocation.getClangHeaderOutputPathForAtMostOnePrimary(),
939-
Instance.getMainModule(), BridgingHeaderPathForPrint,
940-
opts.ExposePublicDeclsInClangHeader, Invocation.getIRGenOptions());
939+
Instance.getMainModule(), BridgingHeaderPathForPrint, opts,
940+
Invocation.getIRGenOptions());
941941
}
942942

943943
// Only want the header if there's been any errors, ie. there's not much

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/PrettyStackTrace.h"
2121
#include "swift/Basic/Version.h"
2222
#include "swift/ClangImporter/ClangImporter.h"
23+
#include "swift/Frontend/FrontendOptions.h"
2324

2425
#include "clang/Basic/Module.h"
2526

@@ -506,7 +507,7 @@ static std::string getModuleContentsCxxString(
506507

507508
bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
508509
StringRef bridgingHeader,
509-
bool ExposePublicDeclsInClangHeader,
510+
const FrontendOptions &frontendOpts,
510511
const IRGenOptions &irGenOpts) {
511512
llvm::PrettyStackTraceString trace("While generating Clang header");
512513

@@ -523,12 +524,15 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
523524
emitObjCConditional(os, [&] { os << objcModuleContents.str(); });
524525
emitCxxConditional(os, [&] {
525526
// FIXME: Expose Swift with @expose by default.
526-
if (ExposePublicDeclsInClangHeader ||
527-
M->DeclContext::getASTContext().LangOpts.EnableCXXInterop) {
527+
bool enableCxx = frontendOpts.ExposePublicDeclsInClangHeader ||
528+
frontendOpts.EnableExperimentalCxxInteropInClangHeader ||
529+
M->DeclContext::getASTContext().LangOpts.EnableCXXInterop;
530+
if (enableCxx) {
528531
SmallPtrSet<ImportModuleTy, 8> imports;
529532
auto contents = getModuleContentsCxxString(
530533
*M, imports, interopContext,
531-
/*requiresExposedAttribute=*/!ExposePublicDeclsInClangHeader);
534+
/*requiresExposedAttribute=*/
535+
!frontendOpts.ExposePublicDeclsInClangHeader);
532536
// FIXME: In ObjC++ mode, we do not need to reimport duplicate modules.
533537
writeImports(os, imports, *M, bridgingHeader, /*useCxxImport=*/true);
534538
os << contents;

test/Interop/SwiftToCxx/expose-attr/expose-swift-decls-to-cxx.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
// RUN: %check-interop-cxx-header-in-clang(%t/expose.h -Wno-error=unused-function)
66

7+
// RUN: %target-swift-frontend %s -typecheck -module-name Expose -enable-experimental-cxx-interop-in-clang-header -emit-clang-header-path %t/expose.h
8+
// RUN: %FileCheck %s < %t/expose.h
9+
710
// RUN: %empty-directory(%t)
811
// RUN: %target-swift-frontend %s -emit-module -module-name Expose -o %t
912
// RUN: %target-swift-frontend -parse-as-library %t/Expose.swiftmodule -typecheck -module-name Expose -enable-experimental-cxx-interop -emit-clang-header-path %t/expose.h

0 commit comments

Comments
 (0)