@@ -723,11 +723,11 @@ getEmbedBitcodeInvocationArguments(std::vector<std::string> &invocationArgStrs,
723
723
void
724
724
importer::addCommonInvocationArguments (
725
725
std::vector<std::string> &invocationArgStrs,
726
- ASTContext &ctx) {
726
+ ASTContext &ctx, bool ignoreClangTarget ) {
727
727
using ImporterImpl = ClangImporter::Implementation;
728
728
llvm::Triple triple = ctx.LangOpts .Target ;
729
729
// Use clang specific target triple if given.
730
- if (ctx.LangOpts .ClangTarget .has_value ()) {
730
+ if (ctx.LangOpts .ClangTarget .has_value () && !ignoreClangTarget ) {
731
731
triple = ctx.LangOpts .ClangTarget .value ();
732
732
}
733
733
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts ;
@@ -975,7 +975,7 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
975
975
}
976
976
977
977
std::vector<std::string>
978
- ClangImporter::getClangArguments (ASTContext &ctx) {
978
+ ClangImporter::getClangArguments (ASTContext &ctx, bool ignoreClangTarget ) {
979
979
std::vector<std::string> invocationArgStrs;
980
980
// Clang expects this to be like an actual command line. So we need to pass in
981
981
// "clang" for argv[0]
@@ -996,7 +996,7 @@ ClangImporter::getClangArguments(ASTContext &ctx) {
996
996
getEmbedBitcodeInvocationArguments (invocationArgStrs, ctx);
997
997
break ;
998
998
}
999
- addCommonInvocationArguments (invocationArgStrs, ctx);
999
+ addCommonInvocationArguments (invocationArgStrs, ctx, ignoreClangTarget );
1000
1000
return invocationArgStrs;
1001
1001
}
1002
1002
@@ -1099,15 +1099,6 @@ ClangImporter::create(ASTContext &ctx,
1099
1099
std::unique_ptr<ClangImporter> importer{
1100
1100
new ClangImporter (ctx, tracker, dwarfImporterDelegate)};
1101
1101
auto &importerOpts = ctx.ClangImporterOpts ;
1102
- importer->Impl .ClangArgs = getClangArguments (ctx);
1103
- ArrayRef<std::string> invocationArgStrs = importer->Impl .ClangArgs ;
1104
- if (importerOpts.DumpClangDiagnostics ) {
1105
- llvm::errs () << " '" ;
1106
- llvm::interleave (
1107
- invocationArgStrs, [](StringRef arg) { llvm::errs () << arg; },
1108
- [] { llvm::errs () << " ' '" ; });
1109
- llvm::errs () << " '\n " ;
1110
- }
1111
1102
1112
1103
if (isPCHFilenameExtension (importerOpts.BridgingHeader )) {
1113
1104
importer->Impl .setSinglePCHImport (importerOpts.BridgingHeader );
@@ -1147,6 +1138,15 @@ ClangImporter::create(ASTContext &ctx,
1147
1138
1148
1139
// Create a new Clang compiler invocation.
1149
1140
{
1141
+ importer->Impl .ClangArgs = getClangArguments (ctx);
1142
+ ArrayRef<std::string> invocationArgStrs = importer->Impl .ClangArgs ;
1143
+ if (importerOpts.DumpClangDiagnostics ) {
1144
+ llvm::errs () << " '" ;
1145
+ llvm::interleave (
1146
+ invocationArgStrs, [](StringRef arg) { llvm::errs () << arg; },
1147
+ [] { llvm::errs () << " ' '" ; });
1148
+ llvm::errs () << " '\n " ;
1149
+ }
1150
1150
importer->Impl .Invocation = createClangInvocation (
1151
1151
importer.get (), importerOpts, VFS, invocationArgStrs);
1152
1152
if (!importer->Impl .Invocation )
@@ -1222,6 +1222,27 @@ ClangImporter::create(ASTContext &ctx,
1222
1222
clang::SourceLocation ());
1223
1223
clangDiags.setFatalsAsError (ctx.Diags .getShowDiagnosticsAfterFatalError ());
1224
1224
1225
+ // Use Clang to configure/save options for Swift IRGen/CodeGen
1226
+ if (ctx.LangOpts .ClangTarget .has_value ()) {
1227
+ // If '-clang-target' is set, create a mock invocation with the Swift triple
1228
+ // to configure CodeGen and Target options for Swift compilation.
1229
+ auto swiftTargetClangArgs = getClangArguments (ctx, true );
1230
+ ArrayRef<std::string> invocationArgStrs = swiftTargetClangArgs;
1231
+ auto swiftTargetClangInvocation = createClangInvocation (
1232
+ importer.get (), importerOpts, VFS, invocationArgStrs);
1233
+ if (!swiftTargetClangInvocation)
1234
+ return nullptr ;
1235
+ importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1236
+ clangDiags, swiftTargetClangInvocation->TargetOpts ));
1237
+ importer->Impl .setSwiftCodeGenOptions (new clang::CodeGenOptions (
1238
+ swiftTargetClangInvocation->getCodeGenOpts ()));
1239
+ } else {
1240
+ // Just use the existing Invocation's directly
1241
+ importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1242
+ clangDiags, importer->Impl .Invocation ->TargetOpts ));
1243
+ importer->Impl .setSwiftCodeGenOptions (
1244
+ new clang::CodeGenOptions (importer->Impl .Invocation ->getCodeGenOpts ()));
1245
+ }
1225
1246
1226
1247
// Create the associated action.
1227
1248
importer->Impl .Action .reset (new ParsingAction (ctx, *importer,
@@ -1880,7 +1901,7 @@ bool ClangImporter::canImportModule(ImportPath::Module modulePath,
1880
1901
clang::Module *m;
1881
1902
auto &ctx = Impl.getClangASTContext ();
1882
1903
auto &lo = ctx.getLangOpts ();
1883
- auto &ti = getTargetInfo ();
1904
+ auto &ti = getModuleAvailabilityTarget ();
1884
1905
1885
1906
auto available = clangModule->isAvailable (lo, ti, r, mh, m);
1886
1907
if (!available)
@@ -3688,10 +3709,14 @@ StringRef ClangModuleUnit::getLoadedFilename() const {
3688
3709
return StringRef ();
3689
3710
}
3690
3711
3691
- clang::TargetInfo &ClangImporter::getTargetInfo () const {
3712
+ clang::TargetInfo &ClangImporter::getModuleAvailabilityTarget () const {
3692
3713
return Impl.Instance ->getTarget ();
3693
3714
}
3694
3715
3716
+ clang::TargetInfo &ClangImporter::getTargetInfo () const {
3717
+ return *Impl.getSwiftTargetInfo ();
3718
+ }
3719
+
3695
3720
clang::ASTContext &ClangImporter::getClangASTContext () const {
3696
3721
return Impl.getClangASTContext ();
3697
3722
}
@@ -3721,8 +3746,8 @@ clang::Sema &ClangImporter::getClangSema() const {
3721
3746
return Impl.getClangSema ();
3722
3747
}
3723
3748
3724
- clang::CodeGenOptions &ClangImporter::getClangCodeGenOpts () const {
3725
- return Impl.getClangCodeGenOpts ();
3749
+ clang::CodeGenOptions &ClangImporter::getCodeGenOpts () const {
3750
+ return * Impl.getSwiftCodeGenOptions ();
3726
3751
}
3727
3752
3728
3753
std::string ClangImporter::getClangModuleHash () const {
0 commit comments