@@ -1427,22 +1427,12 @@ ClangImporter::create(ASTContext &ctx,
14271427 importer.get (), importerOpts, VFS, *swiftTargetClangArgs);
14281428 if (!swiftTargetClangInvocation)
14291429 return nullptr ;
1430- auto targetInfo = clang::TargetInfo::CreateTargetInfo (
1431- clangDiags, swiftTargetClangInvocation->getTargetOpts ());
1432- // Ensure the target info has configured target-specific defines
1433- std::string defineBuffer;
1434- llvm::raw_string_ostream predefines (defineBuffer);
1435- clang::MacroBuilder builder (predefines);
1436- targetInfo->getTargetDefines (instance.getLangOpts (), builder);
1437- importer->Impl .setSwiftTargetInfo (targetInfo);
1438- importer->Impl .setSwiftCodeGenOptions (new clang::CodeGenOptions (
1439- swiftTargetClangInvocation->getCodeGenOpts ()));
1430+
1431+ importer->Impl .configureOptionsForCodeGen (clangDiags,
1432+ swiftTargetClangInvocation.get ());
14401433 } else {
1441- // Just use the existing Invocation's directly
1442- importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1443- clangDiags, importer->Impl .Invocation ->getTargetOpts ()));
1444- importer->Impl .setSwiftCodeGenOptions (
1445- new clang::CodeGenOptions (importer->Impl .Invocation ->getCodeGenOpts ()));
1434+ // Set using the existing invocation.
1435+ importer->Impl .configureOptionsForCodeGen (clangDiags);
14461436 }
14471437
14481438 // Create the associated action.
@@ -4132,7 +4122,7 @@ clang::TargetInfo &ClangImporter::getModuleAvailabilityTarget() const {
41324122}
41334123
41344124clang::TargetInfo &ClangImporter::getTargetInfo () const {
4135- return * Impl.getSwiftTargetInfo ();
4125+ return Impl.getCodeGenTargetInfo ();
41364126}
41374127
41384128clang::ASTContext &ClangImporter::getClangASTContext () const {
@@ -4165,7 +4155,7 @@ clang::Sema &ClangImporter::getClangSema() const {
41654155}
41664156
41674157clang::CodeGenOptions &ClangImporter::getCodeGenOpts () const {
4168- return * Impl.getSwiftCodeGenOptions ();
4158+ return Impl.getCodeGenOptions ();
41694159}
41704160
41714161std::string ClangImporter::getClangModuleHash () const {
@@ -4612,6 +4602,37 @@ void ClangImporter::Implementation::getMangledName(
46124602 }
46134603}
46144604
4605+ void ClangImporter::Implementation::configureOptionsForCodeGen (
4606+ clang::DiagnosticsEngine &Diags, clang::CompilerInvocation *CI) {
4607+ clang::TargetInfo *targetInfo = nullptr ;
4608+ if (CI) {
4609+ TargetOpts.reset (new clang::TargetOptions (std::move (CI->getTargetOpts ())));
4610+ CodeGenOpts.reset (
4611+ new clang::CodeGenOptions (std::move (CI->getCodeGenOpts ())));
4612+ targetInfo = clang::TargetInfo::CreateTargetInfo (Diags, *TargetOpts);
4613+
4614+ // Ensure the target info has configured target-specific defines
4615+ std::string defineBuffer;
4616+ llvm::raw_string_ostream predefines (defineBuffer);
4617+ clang::MacroBuilder builder (predefines);
4618+ targetInfo->getTargetDefines (Instance->getLangOpts (), builder);
4619+ } else {
4620+ targetInfo =
4621+ clang::TargetInfo::CreateTargetInfo (Diags, Invocation->getTargetOpts ());
4622+ }
4623+
4624+ CodeGenTargetInfo.reset (targetInfo);
4625+ }
4626+
4627+ clang::CodeGenOptions &
4628+ ClangImporter::Implementation::getCodeGenOptions () const {
4629+ if (CodeGenOpts) {
4630+ return *CodeGenOpts.get ();
4631+ }
4632+
4633+ return Invocation->getCodeGenOpts ();
4634+ }
4635+
46154636// ---------------------------------------------------------------------------
46164637// Swift lookup tables
46174638// ---------------------------------------------------------------------------
0 commit comments