Skip to content

Commit 6b582f7

Browse files
Merge pull request #4948 from swiftwasm/main
[pull] swiftwasm from main
2 parents cf93ebf + 9159f87 commit 6b582f7

File tree

147 files changed

+888
-164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+888
-164
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ WARNING(warning_cannot_multithread_batch_mode,none,
106106
"ignoring -num-threads argument; cannot multithread batch mode", ())
107107
ERROR(error_cannot_explicit_interface_build_in_mode,none,
108108
"'-explicit-interface-module-build' only supported when building a module from interface ('-compile-module-from-interface')'", ())
109+
ERROR(error_cannot_direct_cc1_pcm_build_in_mode,none,
110+
"'-direct-clang-cc1-module-build' only supported when building a PCM ('-emit-pcm')'", ())
109111
ERROR(error_unsupported_option_argument,none,
110112
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
111113
ERROR(error_immediate_mode_missing_stdlib,none,

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,11 @@ namespace swift {
808808
/// contains the full option set.
809809
bool ExtraArgsOnly = false;
810810

811+
/// When building a PCM, rely on the Swift frontend's command-line -Xcc flags
812+
/// to build the Clang module via Clang frontend directly,
813+
/// and completly bypass the Clang driver.
814+
bool DirectClangCC1ModuleBuild = false;
815+
811816
/// Return a hash code of any components from these options that should
812817
/// contribute to a Swift Bridging PCH hash.
813818
llvm::hash_code getPCHHashComponents() const {

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/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,10 @@ def explicit_interface_module_build :
889889
Flag<["-"], "explicit-interface-module-build">,
890890
HelpText<"Use the specified command-line to build the module from interface, instead of flags specified in the interface">;
891891

892+
def direct_clang_cc1_module_build :
893+
Flag<["-"], "direct-clang-cc1-module-build">,
894+
HelpText<"Use the specified -Xcc options to build a PCM by using Clang frontend directly, bypassing the Clang driver">;
895+
892896
def build_module_from_parseable_interface :
893897
Flag<["-"], "build-module-from-parseable-interface">,
894898
Alias<compile_module_from_interface>,

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/ClangImporter/ClangImporter.cpp

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,27 +1040,51 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
10401040
invocationArgs.reserve(invocationArgStrs.size());
10411041
for (auto &argStr : invocationArgStrs)
10421042
invocationArgs.push_back(argStr.c_str());
1043-
// Set up a temporary diagnostic client to report errors from parsing the
1044-
// command line, which may be important for Swift clients if, for example,
1045-
// they're using -Xcc options. Unfortunately this diagnostic engine has to
1046-
// use the default options because the /actual/ options haven't been parsed
1047-
// yet.
1048-
//
1049-
// The long-term client for Clang diagnostics is set up below, after the
1050-
// clang::CompilerInstance is created.
1051-
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> tempDiagOpts{
1052-
new clang::DiagnosticOptions
1053-
};
10541043

1055-
ClangDiagnosticConsumer tempDiagClient{importer->Impl, *tempDiagOpts,
1056-
importerOpts.DumpClangDiagnostics};
1057-
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> tempClangDiags =
1058-
clang::CompilerInstance::createDiagnostics(tempDiagOpts.get(),
1059-
&tempDiagClient,
1060-
/*owned*/false);
1044+
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> clangDiags;
1045+
std::unique_ptr<clang::CompilerInvocation> CI;
1046+
if (importerOpts.DirectClangCC1ModuleBuild) {
1047+
// In this mode, we bypass createInvocationFromCommandLine, which goes
1048+
// through the Clang driver, and use strictly cc1 arguments to instantiate a
1049+
// clang Instance directly, assuming that the set of '-Xcc <X>' frontend flags is
1050+
// fully sufficient to do so.
1051+
1052+
// Because we are bypassing the Clang driver, we must populate
1053+
// the diagnostic options here explicitly.
1054+
std::unique_ptr<clang::DiagnosticOptions> clangDiagOpts =
1055+
clang::CreateAndPopulateDiagOpts(invocationArgs);
1056+
ClangDiagnosticConsumer diagClient{importer->Impl, *clangDiagOpts,
1057+
importerOpts.DumpClangDiagnostics};
1058+
clangDiags = clang::CompilerInstance::createDiagnostics(
1059+
clangDiagOpts.release(), &diagClient,
1060+
/*owned*/ false);
1061+
1062+
// Finally, use the CC1 command-line and the diagnostic engine
1063+
// to instantiate our Invocation.
1064+
CI = std::make_unique<clang::CompilerInvocation>();
1065+
if (!clang::CompilerInvocation::CreateFromArgs(
1066+
*CI, invocationArgs, *clangDiags, invocationArgs[0]))
1067+
return nullptr;
1068+
} else {
1069+
// Set up a temporary diagnostic client to report errors from parsing the
1070+
// command line, which may be important for Swift clients if, for example,
1071+
// they're using -Xcc options. Unfortunately this diagnostic engine has to
1072+
// use the default options because the /actual/ options haven't been parsed
1073+
// yet.
1074+
//
1075+
// The long-term client for Clang diagnostics is set up below, after the
1076+
// clang::CompilerInstance is created.
1077+
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> tempDiagOpts{
1078+
new clang::DiagnosticOptions};
10611079

1062-
auto CI = clang::createInvocationFromCommandLine(
1063-
invocationArgs, tempClangDiags, VFS, false, CC1Args);
1080+
ClangDiagnosticConsumer tempDiagClient{importer->Impl, *tempDiagOpts,
1081+
importerOpts.DumpClangDiagnostics};
1082+
clangDiags = clang::CompilerInstance::createDiagnostics(tempDiagOpts.get(),
1083+
&tempDiagClient,
1084+
/*owned*/ false);
1085+
CI = clang::createInvocationFromCommandLine(invocationArgs, clangDiags, VFS,
1086+
false, CC1Args);
1087+
}
10641088

10651089
if (!CI) {
10661090
return CI;
@@ -1077,8 +1101,9 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
10771101
// rdar://77516546 is tracking that the clang importer should be more
10781102
// resilient and provide a module even if there were building it.
10791103
auto TempVFS = clang::createVFSFromCompilerInvocation(
1080-
*CI, *tempClangDiags,
1104+
*CI, *clangDiags,
10811105
VFS ? VFS : importer->Impl.SwiftContext.SourceMgr.getFileSystem());
1106+
10821107
std::vector<std::string> FilteredModuleMapFiles;
10831108
for (auto ModuleMapFile : CI->getFrontendOpts().ModuleMapFiles) {
10841109
if (TempVFS->exists(ModuleMapFile)) {
@@ -1105,13 +1130,11 @@ ClangImporter::create(ASTContext &ctx,
11051130
if (importerOpts.DumpClangDiagnostics) {
11061131
llvm::errs() << "'";
11071132
llvm::interleave(
1108-
invocationArgStrs, [](StringRef arg) { llvm::errs() << arg; },
1109-
[] { llvm::errs() << "' '"; });
1133+
invocationArgStrs, [](StringRef arg) { llvm::errs() << arg; },
1134+
[] { llvm::errs() << "' '"; });
11101135
llvm::errs() << "'\n";
11111136
}
11121137

1113-
1114-
11151138
if (isPCHFilenameExtension(importerOpts.BridgingHeader)) {
11161139
importer->Impl.setSinglePCHImport(importerOpts.BridgingHeader);
11171140
importer->Impl.IsReadingBridgingPCH = true;

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ std::pair<FuncDecl *, FuncDecl *> SwiftDeclSynthesizer::makeBitFieldAccessors(
981981
Ctx, structDecl->getDeclContext(), clang::SourceLocation(),
982982
clang::SourceLocation(), cGetterName, cGetterType, cGetterTypeInfo,
983983
clang::SC_Static);
984+
cGetterDecl->setImplicit();
984985
cGetterDecl->setImplicitlyInline();
985986
assert(!cGetterDecl->isExternallyVisible());
986987

@@ -1002,6 +1003,7 @@ std::pair<FuncDecl *, FuncDecl *> SwiftDeclSynthesizer::makeBitFieldAccessors(
10021003
Ctx, structDecl->getDeclContext(), clang::SourceLocation(),
10031004
clang::SourceLocation(), cSetterName, cSetterType, cSetterTypeInfo,
10041005
clang::SC_Static);
1006+
cSetterDecl->setImplicit();
10051007
cSetterDecl->setImplicitlyInline();
10061008
assert(!cSetterDecl->isExternallyVisible());
10071009

@@ -1017,6 +1019,7 @@ std::pair<FuncDecl *, FuncDecl *> SwiftDeclSynthesizer::makeBitFieldAccessors(
10171019
auto cGetterSelf = clang::ParmVarDecl::Create(
10181020
Ctx, cGetterDecl, clang::SourceLocation(), clang::SourceLocation(),
10191021
cGetterSelfId, recordType, recordTypeInfo, clang::SC_None, nullptr);
1022+
cGetterSelf->setImplicit();
10201023
cGetterDecl->setParams(cGetterSelf);
10211024

10221025
auto cGetterSelfExpr = new (Ctx)
@@ -1040,13 +1043,15 @@ std::pair<FuncDecl *, FuncDecl *> SwiftDeclSynthesizer::makeBitFieldAccessors(
10401043
Ctx, cSetterDecl, clang::SourceLocation(), clang::SourceLocation(),
10411044
/* nameID? */ nullptr, fieldType, fieldTypeInfo, clang::SC_None,
10421045
nullptr);
1046+
cSetterValue->setImplicit();
10431047
cSetterParams.push_back(cSetterValue);
10441048
auto recordPointerTypeInfo =
10451049
Ctx.getTrivialTypeSourceInfo(recordPointerType);
10461050
auto cSetterSelf = clang::ParmVarDecl::Create(
10471051
Ctx, cSetterDecl, clang::SourceLocation(), clang::SourceLocation(),
10481052
/* nameID? */ nullptr, recordPointerType, recordPointerTypeInfo,
10491053
clang::SC_None, nullptr);
1054+
cSetterSelf->setImplicit();
10501055
cSetterParams.push_back(cSetterSelf);
10511056
cSetterDecl->setParams(cSetterParams);
10521057

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/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,7 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
12731273
}
12741274

12751275
Opts.ExtraArgsOnly |= Args.hasArg(OPT_extra_clang_options_only);
1276+
Opts.DirectClangCC1ModuleBuild |= Args.hasArg(OPT_direct_clang_cc1_module_build);
12761277

12771278
if (const Arg *A = Args.getLastArg(OPT_pch_output_dir)) {
12781279
Opts.PrecompiledHeaderOutputDir = A->getValue();

0 commit comments

Comments
 (0)