@@ -716,11 +716,11 @@ getEmbedBitcodeInvocationArguments(std::vector<std::string> &invocationArgStrs,
716
716
void
717
717
importer::addCommonInvocationArguments (
718
718
std::vector<std::string> &invocationArgStrs,
719
- ASTContext &ctx) {
719
+ ASTContext &ctx, bool ignoreClangTarget ) {
720
720
using ImporterImpl = ClangImporter::Implementation;
721
721
llvm::Triple triple = ctx.LangOpts .Target ;
722
722
// Use clang specific target triple if given.
723
- if (ctx.LangOpts .ClangTarget .has_value ()) {
723
+ if (ctx.LangOpts .ClangTarget .has_value () && !ignoreClangTarget ) {
724
724
triple = ctx.LangOpts .ClangTarget .value ();
725
725
}
726
726
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts ;
@@ -970,7 +970,7 @@ ClangImporter::getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
970
970
}
971
971
972
972
std::vector<std::string>
973
- ClangImporter::getClangArguments (ASTContext &ctx) {
973
+ ClangImporter::getClangArguments (ASTContext &ctx, bool ignoreClangTarget ) {
974
974
std::vector<std::string> invocationArgStrs;
975
975
// Clang expects this to be like an actual command line. So we need to pass in
976
976
// "clang" for argv[0]
@@ -991,7 +991,7 @@ ClangImporter::getClangArguments(ASTContext &ctx) {
991
991
getEmbedBitcodeInvocationArguments (invocationArgStrs, ctx);
992
992
break ;
993
993
}
994
- addCommonInvocationArguments (invocationArgStrs, ctx);
994
+ addCommonInvocationArguments (invocationArgStrs, ctx, ignoreClangTarget );
995
995
return invocationArgStrs;
996
996
}
997
997
@@ -1094,15 +1094,6 @@ ClangImporter::create(ASTContext &ctx,
1094
1094
std::unique_ptr<ClangImporter> importer{
1095
1095
new ClangImporter (ctx, tracker, dwarfImporterDelegate)};
1096
1096
auto &importerOpts = ctx.ClangImporterOpts ;
1097
- importer->Impl .ClangArgs = getClangArguments (ctx);
1098
- ArrayRef<std::string> invocationArgStrs = importer->Impl .ClangArgs ;
1099
- if (importerOpts.DumpClangDiagnostics ) {
1100
- llvm::errs () << " '" ;
1101
- llvm::interleave (
1102
- invocationArgStrs, [](StringRef arg) { llvm::errs () << arg; },
1103
- [] { llvm::errs () << " ' '" ; });
1104
- llvm::errs () << " '\n " ;
1105
- }
1106
1097
1107
1098
if (isPCHFilenameExtension (importerOpts.BridgingHeader )) {
1108
1099
importer->Impl .setSinglePCHImport (importerOpts.BridgingHeader );
@@ -1142,6 +1133,15 @@ ClangImporter::create(ASTContext &ctx,
1142
1133
1143
1134
// Create a new Clang compiler invocation.
1144
1135
{
1136
+ importer->Impl .ClangArgs = getClangArguments (ctx);
1137
+ ArrayRef<std::string> invocationArgStrs = importer->Impl .ClangArgs ;
1138
+ if (importerOpts.DumpClangDiagnostics ) {
1139
+ llvm::errs () << " '" ;
1140
+ llvm::interleave (
1141
+ invocationArgStrs, [](StringRef arg) { llvm::errs () << arg; },
1142
+ [] { llvm::errs () << " ' '" ; });
1143
+ llvm::errs () << " '\n " ;
1144
+ }
1145
1145
importer->Impl .Invocation = createClangInvocation (
1146
1146
importer.get (), importerOpts, VFS, invocationArgStrs);
1147
1147
if (!importer->Impl .Invocation )
@@ -1217,6 +1217,27 @@ ClangImporter::create(ASTContext &ctx,
1217
1217
clang::SourceLocation ());
1218
1218
clangDiags.setFatalsAsError (ctx.Diags .getShowDiagnosticsAfterFatalError ());
1219
1219
1220
+ // Use Clang to configure/save options for Swift IRGen/CodeGen
1221
+ if (ctx.LangOpts .ClangTarget .has_value ()) {
1222
+ // If '-clang-target' is set, create a mock invocation with the Swift triple
1223
+ // to configure CodeGen and Target options for Swift compilation.
1224
+ auto swiftTargetClangArgs = getClangArguments (ctx, true );
1225
+ ArrayRef<std::string> invocationArgStrs = swiftTargetClangArgs;
1226
+ auto swiftTargetClangInvocation = createClangInvocation (
1227
+ importer.get (), importerOpts, VFS, invocationArgStrs);
1228
+ if (!swiftTargetClangInvocation)
1229
+ return nullptr ;
1230
+ importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1231
+ clangDiags, swiftTargetClangInvocation->TargetOpts ));
1232
+ importer->Impl .setSwiftCodeGenOptions (new clang::CodeGenOptions (
1233
+ swiftTargetClangInvocation->getCodeGenOpts ()));
1234
+ } else {
1235
+ // Just use the existing Invocation's directly
1236
+ importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1237
+ clangDiags, importer->Impl .Invocation ->TargetOpts ));
1238
+ importer->Impl .setSwiftCodeGenOptions (
1239
+ new clang::CodeGenOptions (importer->Impl .Invocation ->getCodeGenOpts ()));
1240
+ }
1220
1241
1221
1242
// Create the associated action.
1222
1243
importer->Impl .Action .reset (new ParsingAction (ctx, *importer,
@@ -1875,7 +1896,7 @@ bool ClangImporter::canImportModule(ImportPath::Module modulePath,
1875
1896
clang::Module *m;
1876
1897
auto &ctx = Impl.getClangASTContext ();
1877
1898
auto &lo = ctx.getLangOpts ();
1878
- auto &ti = getTargetInfo ();
1899
+ auto &ti = getModuleAvailabilityTarget ();
1879
1900
1880
1901
auto available = clangModule->isAvailable (lo, ti, r, mh, m);
1881
1902
if (!available)
@@ -3683,10 +3704,14 @@ StringRef ClangModuleUnit::getLoadedFilename() const {
3683
3704
return StringRef ();
3684
3705
}
3685
3706
3686
- clang::TargetInfo &ClangImporter::getTargetInfo () const {
3707
+ clang::TargetInfo &ClangImporter::getModuleAvailabilityTarget () const {
3687
3708
return Impl.Instance ->getTarget ();
3688
3709
}
3689
3710
3711
+ clang::TargetInfo &ClangImporter::getTargetInfo () const {
3712
+ return *Impl.getSwiftTargetInfo ();
3713
+ }
3714
+
3690
3715
clang::ASTContext &ClangImporter::getClangASTContext () const {
3691
3716
return Impl.getClangASTContext ();
3692
3717
}
@@ -3716,8 +3741,8 @@ clang::Sema &ClangImporter::getClangSema() const {
3716
3741
return Impl.getClangSema ();
3717
3742
}
3718
3743
3719
- clang::CodeGenOptions &ClangImporter::getClangCodeGenOpts () const {
3720
- return Impl.getClangCodeGenOpts ();
3744
+ clang::CodeGenOptions &ClangImporter::getCodeGenOpts () const {
3745
+ return * Impl.getSwiftCodeGenOptions ();
3721
3746
}
3722
3747
3723
3748
std::string ClangImporter::getClangModuleHash () const {
0 commit comments