diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 8e43b21a10413..1a6a9779ad3a9 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -1427,22 +1427,12 @@ ClangImporter::create(ASTContext &ctx, importer.get(), importerOpts, VFS, *swiftTargetClangArgs); if (!swiftTargetClangInvocation) return nullptr; - auto targetInfo = clang::TargetInfo::CreateTargetInfo( - clangDiags, swiftTargetClangInvocation->getTargetOpts()); - // Ensure the target info has configured target-specific defines - std::string defineBuffer; - llvm::raw_string_ostream predefines(defineBuffer); - clang::MacroBuilder builder(predefines); - targetInfo->getTargetDefines(instance.getLangOpts(), builder); - importer->Impl.setSwiftTargetInfo(targetInfo); - importer->Impl.setSwiftCodeGenOptions(new clang::CodeGenOptions( - swiftTargetClangInvocation->getCodeGenOpts())); + + importer->Impl.configureOptionsForCodeGen(clangDiags, + swiftTargetClangInvocation.get()); } else { - // Just use the existing Invocation's directly - importer->Impl.setSwiftTargetInfo(clang::TargetInfo::CreateTargetInfo( - clangDiags, importer->Impl.Invocation->getTargetOpts())); - importer->Impl.setSwiftCodeGenOptions( - new clang::CodeGenOptions(importer->Impl.Invocation->getCodeGenOpts())); + // Set using the existing invocation. + importer->Impl.configureOptionsForCodeGen(clangDiags); } // Create the associated action. @@ -4132,7 +4122,7 @@ clang::TargetInfo &ClangImporter::getModuleAvailabilityTarget() const { } clang::TargetInfo &ClangImporter::getTargetInfo() const { - return *Impl.getSwiftTargetInfo(); + return Impl.getCodeGenTargetInfo(); } clang::ASTContext &ClangImporter::getClangASTContext() const { @@ -4165,7 +4155,7 @@ clang::Sema &ClangImporter::getClangSema() const { } clang::CodeGenOptions &ClangImporter::getCodeGenOpts() const { - return *Impl.getSwiftCodeGenOptions(); + return Impl.getCodeGenOptions(); } std::string ClangImporter::getClangModuleHash() const { @@ -4612,6 +4602,37 @@ void ClangImporter::Implementation::getMangledName( } } +void ClangImporter::Implementation::configureOptionsForCodeGen( + clang::DiagnosticsEngine &Diags, clang::CompilerInvocation *CI) { + clang::TargetInfo *targetInfo = nullptr; + if (CI) { + TargetOpts.reset(new clang::TargetOptions(std::move(CI->getTargetOpts()))); + CodeGenOpts.reset( + new clang::CodeGenOptions(std::move(CI->getCodeGenOpts()))); + targetInfo = clang::TargetInfo::CreateTargetInfo(Diags, *TargetOpts); + + // Ensure the target info has configured target-specific defines + std::string defineBuffer; + llvm::raw_string_ostream predefines(defineBuffer); + clang::MacroBuilder builder(predefines); + targetInfo->getTargetDefines(Instance->getLangOpts(), builder); + } else { + targetInfo = + clang::TargetInfo::CreateTargetInfo(Diags, Invocation->getTargetOpts()); + } + + CodeGenTargetInfo.reset(targetInfo); +} + +clang::CodeGenOptions & +ClangImporter::Implementation::getCodeGenOptions() const { + if (CodeGenOpts) { + return *CodeGenOpts.get(); + } + + return Invocation->getCodeGenOpts(); +} + // --------------------------------------------------------------------------- // Swift lookup tables // --------------------------------------------------------------------------- diff --git a/lib/ClangImporter/ImporterImpl.h b/lib/ClangImporter/ImporterImpl.h index a9645a9aff202..dded99b19ee8f 100644 --- a/lib/ClangImporter/ImporterImpl.h +++ b/lib/ClangImporter/ImporterImpl.h @@ -634,22 +634,22 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation /// corresponding to the instantiating Swift compilation's triple. These are /// to be used by all IRGen/CodeGen clients of `ClangImporter`. std::unique_ptr CodeGenTargetInfo; + /// - Important: Do not access directly. This field exists only to make sure + /// we own the target options stored in `CodeGenTargetInfo` because + /// `clang::TargetOptions` no longer co-owns them: + /// https://github.com/llvm/llvm-project/pull/106271. + std::unique_ptr TargetOpts; std::unique_ptr CodeGenOpts; -public: - void setSwiftTargetInfo(clang::TargetInfo *SwiftTargetInfo) { - CodeGenTargetInfo.reset(SwiftTargetInfo); - } - clang::TargetInfo *getSwiftTargetInfo() const { - return CodeGenTargetInfo.get(); - } + /// Sets the target & code generation options for use by IRGen/CodeGen + /// clients of `ClangImporter`. If `CI` is null, the data is drawn from the + /// importer's invocation. + void configureOptionsForCodeGen(clang::DiagnosticsEngine &Diags, + clang::CompilerInvocation *CI = nullptr); - void setSwiftCodeGenOptions(clang::CodeGenOptions *SwiftCodeGenOpts) { - CodeGenOpts.reset(SwiftCodeGenOpts); - } - clang::CodeGenOptions *getSwiftCodeGenOptions() const { - return CodeGenOpts.get(); - } + clang::TargetInfo &getCodeGenTargetInfo() const { return *CodeGenTargetInfo; } + + clang::CodeGenOptions &getCodeGenOptions() const; private: /// Generation number that is used for crude versioning.