@@ -723,11 +723,11 @@ getEmbedBitcodeInvocationArguments(std::vector<std::string> &invocationArgStrs,
723723void
724724importer::addCommonInvocationArguments (
725725 std::vector<std::string> &invocationArgStrs,
726- ASTContext &ctx) {
726+ ASTContext &ctx, bool ignoreClangTarget ) {
727727 using ImporterImpl = ClangImporter::Implementation;
728728 llvm::Triple triple = ctx.LangOpts .Target ;
729729 // Use clang specific target triple if given.
730- if (ctx.LangOpts .ClangTarget .has_value ()) {
730+ if (ctx.LangOpts .ClangTarget .has_value () && !ignoreClangTarget ) {
731731 triple = ctx.LangOpts .ClangTarget .value ();
732732 }
733733 SearchPathOptions &searchPathOpts = ctx.SearchPathOpts ;
@@ -975,7 +975,7 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
975975}
976976
977977std::vector<std::string>
978- ClangImporter::getClangArguments (ASTContext &ctx) {
978+ ClangImporter::getClangArguments (ASTContext &ctx, bool ignoreClangTarget ) {
979979 std::vector<std::string> invocationArgStrs;
980980 // Clang expects this to be like an actual command line. So we need to pass in
981981 // "clang" for argv[0]
@@ -996,7 +996,7 @@ ClangImporter::getClangArguments(ASTContext &ctx) {
996996 getEmbedBitcodeInvocationArguments (invocationArgStrs, ctx);
997997 break ;
998998 }
999- addCommonInvocationArguments (invocationArgStrs, ctx);
999+ addCommonInvocationArguments (invocationArgStrs, ctx, ignoreClangTarget );
10001000 return invocationArgStrs;
10011001}
10021002
@@ -1099,15 +1099,6 @@ ClangImporter::create(ASTContext &ctx,
10991099 std::unique_ptr<ClangImporter> importer{
11001100 new ClangImporter (ctx, tracker, dwarfImporterDelegate)};
11011101 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- }
11111102
11121103 if (isPCHFilenameExtension (importerOpts.BridgingHeader )) {
11131104 importer->Impl .setSinglePCHImport (importerOpts.BridgingHeader );
@@ -1147,6 +1138,15 @@ ClangImporter::create(ASTContext &ctx,
11471138
11481139 // Create a new Clang compiler invocation.
11491140 {
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+ }
11501150 importer->Impl .Invocation = createClangInvocation (
11511151 importer.get (), importerOpts, VFS, invocationArgStrs);
11521152 if (!importer->Impl .Invocation )
@@ -1222,6 +1222,27 @@ ClangImporter::create(ASTContext &ctx,
12221222 clang::SourceLocation ());
12231223 clangDiags.setFatalsAsError (ctx.Diags .getShowDiagnosticsAfterFatalError ());
12241224
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+ }
12251246
12261247 // Create the associated action.
12271248 importer->Impl .Action .reset (new ParsingAction (ctx, *importer,
@@ -1880,7 +1901,7 @@ bool ClangImporter::canImportModule(ImportPath::Module modulePath,
18801901 clang::Module *m;
18811902 auto &ctx = Impl.getClangASTContext ();
18821903 auto &lo = ctx.getLangOpts ();
1883- auto &ti = getTargetInfo ();
1904+ auto &ti = getModuleAvailabilityTarget ();
18841905
18851906 auto available = clangModule->isAvailable (lo, ti, r, mh, m);
18861907 if (!available)
@@ -3688,10 +3709,14 @@ StringRef ClangModuleUnit::getLoadedFilename() const {
36883709 return StringRef ();
36893710}
36903711
3691- clang::TargetInfo &ClangImporter::getTargetInfo () const {
3712+ clang::TargetInfo &ClangImporter::getModuleAvailabilityTarget () const {
36923713 return Impl.Instance ->getTarget ();
36933714}
36943715
3716+ clang::TargetInfo &ClangImporter::getTargetInfo () const {
3717+ return *Impl.getSwiftTargetInfo ();
3718+ }
3719+
36953720clang::ASTContext &ClangImporter::getClangASTContext () const {
36963721 return Impl.getClangASTContext ();
36973722}
@@ -3721,8 +3746,8 @@ clang::Sema &ClangImporter::getClangSema() const {
37213746 return Impl.getClangSema ();
37223747}
37233748
3724- clang::CodeGenOptions &ClangImporter::getClangCodeGenOpts () const {
3725- return Impl.getClangCodeGenOpts ();
3749+ clang::CodeGenOptions &ClangImporter::getCodeGenOpts () const {
3750+ return * Impl.getSwiftCodeGenOptions ();
37263751}
37273752
37283753std::string ClangImporter::getClangModuleHash () const {
0 commit comments