5959#include " swift/SymbolGraphGen/SymbolGraphOptions.h"
6060
6161#include " clang/AST/ASTContext.h"
62+ #include " clang/Basic/DarwinSDKInfo.h"
6263#include " clang/Basic/TargetInfo.h"
6364#include " clang/Basic/TargetOptions.h"
6465#include " clang/Driver/Driver.h"
6566#include " clang/Frontend/CompilerInstance.h"
6667#include " clang/Frontend/TextDiagnosticPrinter.h"
6768#include " clang/Lex/Preprocessor.h"
68-
6969#include " clang/Lex/PreprocessorOptions.h"
7070#include " llvm/ADT/ArrayRef.h"
7171#include " llvm/ADT/STLExtras.h"
@@ -2634,6 +2634,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
26342634
26352635 swift::ModuleDecl *stdlib =
26362636 ast_context->getStdlibModule (can_create);
2637+ swift_ast_sp->m_post_first_import = true ;
26372638 if (!stdlib || IsDWARFImported (*stdlib)) {
26382639 logError (" couldn't load the Swift stdlib" );
26392640 return {};
@@ -3031,6 +3032,24 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
30313032 ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
30323033 triple);
30333034 ConfigureModuleCachePath (*swift_ast_sp);
3035+ {
3036+ // ModuleInterfaceBuilder creates a separate CompilerInvocation to
3037+ // perform implicit Clang module imports. They will always use the SDK
3038+ // version as deployment target, even if that is in the future. To
3039+ // avoid building modules twice, match this behavior.
3040+ auto darwin_sdk_info = clang::parseDarwinSDKInfo (
3041+ *llvm::vfs::getRealFileSystem (), swift_ast_sp->GetPlatformSDKPath ());
3042+ if (!darwin_sdk_info)
3043+ llvm::consumeError (darwin_sdk_info.takeError ());
3044+ else if (*darwin_sdk_info) {
3045+ auto sdk_triple = triple;
3046+ sdk_triple.setOSName (std::string (triple.getOSTypeName (triple.getOS ())) +
3047+ (*darwin_sdk_info)->getVersion ().getAsString ());
3048+ auto &ci_args = swift_ast_sp->GetClangImporterOptions ().ExtraArgs ;
3049+ ci_args.push_back (" -target" );
3050+ ci_args.push_back (sdk_triple.str ());
3051+ }
3052+ }
30343053
30353054 std::vector<swift::PluginSearchOption> plugin_search_options;
30363055 std::vector<std::pair<std::string, bool >> module_search_paths;
@@ -3109,11 +3128,6 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
31093128 framework_search_paths);
31103129 swift_ast_sp->SetCompilerInvocationLLDBOverrides ();
31113130
3112- if (!swift_ast_sp->GetClangImporter ()) {
3113- logError (" couldn't create a ClangImporter" );
3114- return {};
3115- }
3116-
31173131 // Initialize the compiler plugin search paths.
31183132 auto &opts = swift_ast_sp->GetSearchPathOptions ();
31193133 opts.PluginSearchOpts .insert (opts.PluginSearchOpts .end (),
@@ -3173,22 +3187,13 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
31733187 swift_ast_sp->RegisterSectionModules (*image_sp, module_names);
31743188 }
31753189
3176- // FIXME: It should be sufficient to just import the sc.comp_unit's module .
3177- if (!for_expressions && module_sp)
3178- swift_ast_sp->ImportSectionModules (*module_sp, module_names);
3179-
3180- if (target_sp)
3181- LOG_PRINTF (GetLog (LLDBLog::Types), " ((Target*)%p) = %p" ,
3182- static_cast <void *>(target_sp.get ()),
3183- static_cast <void *>(swift_ast_sp.get ()));
3184-
3185- if (swift_ast_sp->HasFatalErrors ()) {
3186- logError (swift_ast_sp->GetFatalErrors ().AsCString ());
3187- return {};
3188- }
3189-
31903190 {
31913191 auto ast_context = swift_ast_sp->GetASTContext ();
3192+ if (!ast_context) {
3193+ logError (" couldn't initialize Swift cxompiler" );
3194+ return {};
3195+ }
3196+
31923197 // Report progress on module importing by using a callback function in
31933198 // swift::ASTContext
31943199 Progress progress (" Importing Swift standard library" );
@@ -3209,12 +3214,27 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
32093214 const bool can_create = true ;
32103215 swift::ModuleDecl *stdlib =
32113216 ast_context->getStdlibModule (can_create);
3217+ swift_ast_sp->m_post_first_import = true ;
32123218 if (!stdlib || IsDWARFImported (*stdlib)) {
32133219 logError (" couldn't load the Swift stdlib" );
32143220 return {};
32153221 }
32163222 }
32173223
3224+ // FIXME: It should be sufficient to just import the sc.comp_unit's module .
3225+ if (!for_expressions && module_sp)
3226+ swift_ast_sp->ImportSectionModules (*module_sp, module_names);
3227+
3228+ if (target_sp)
3229+ LOG_PRINTF (GetLog (LLDBLog::Types), " ((Target*)%p) = %p" ,
3230+ static_cast <void *>(target_sp.get ()),
3231+ static_cast <void *>(swift_ast_sp.get ()));
3232+
3233+ if (swift_ast_sp->HasFatalErrors ()) {
3234+ logError (swift_ast_sp->GetFatalErrors ().AsCString ());
3235+ return {};
3236+ }
3237+
32183238 return swift_ast_sp;
32193239}
32203240
@@ -3334,6 +3354,7 @@ void SwiftASTContext::SetTriple(const SymbolContext &sc,
33343354
33353355bool SwiftASTContext::SetTriple (const llvm::Triple triple, Module *module ) {
33363356 VALID_OR_RETURN (false );
3357+ assert (!m_post_first_import);
33373358 if (triple.str ().empty ())
33383359 return false ;
33393360
@@ -3496,12 +3517,10 @@ swift::ClangImporterOptions &SwiftASTContext::GetClangImporterOptions() {
34963517 const auto &props = ModuleList::GetGlobalModuleListProperties ();
34973518 props.GetClangModulesCachePath ().GetPath (path);
34983519 clang_importer_options.ModuleCachePath = std::string (path);
3499-
3500- FileSpec clang_dir_spec;
3501- clang_dir_spec = GetClangResourceDir ();
3502- if (FileSystem::Instance ().Exists (clang_dir_spec))
3503- clang_importer_options.OverrideResourceDir = clang_dir_spec.GetPath ();
3520+
35043521 clang_importer_options.DebuggerSupport = true ;
3522+ // Matches swift/Frontend.cpp
3523+ clang_importer_options.Optimization = " -Os" ;
35053524 clang_importer_options.PreferSerializedBridgingHeader =
35063525 props.GetSwiftPreferSerializedBridgingHeader ();
35073526
@@ -3661,35 +3680,33 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
36613680 auto &clang_importer_options = GetClangImporterOptions ();
36623681 if (!m_ast_context_ap->SearchPathOpts .getSDKPath ().empty () ||
36633682 TargetHasNoSDK ()) {
3664- if (!clang_importer_options.OverrideResourceDir .empty ()) {
3665- // Create the DWARFImporterDelegate.
3666- const auto &props = ModuleList::GetGlobalModuleListProperties ();
3667- if (props.GetUseSwiftDWARFImporter ())
3668- m_dwarfimporter_delegate_up =
3669- std::make_unique<SwiftDWARFImporterDelegate>(*this );
3670- auto importer_diags = getScopedDiagnosticConsumer ();
3671- clang_importer_ap = swift::ClangImporter::create (
3672- *m_ast_context_ap, " " , m_dependency_tracker.get (),
3673- m_dwarfimporter_delegate_up.get ());
3674-
3675- // Handle any errors.
3676- if (!clang_importer_ap || importer_diags->HasErrors ()) {
3677- AddDiagnostic (eSeverityError, " failed to create ClangImporter" );
3678- if (GetLog (LLDBLog::Types)) {
3679- DiagnosticManager diagnostic_manager;
3680- importer_diags->PrintDiagnostics (diagnostic_manager);
3681- std::string underlying_error = diagnostic_manager.GetString ();
3682- HEALTH_LOG_PRINTF (" failed to initialize ClangImporter: %s" ,
3683- underlying_error.c_str ());
3684- }
3685- }
3686- if (clang_importer_ap) {
3687- auto clangModuleCache = swift::getModuleCachePathFromClang (
3688- clang_importer_ap->getClangInstance ());
3689- if (!clangModuleCache.empty ())
3690- moduleCachePath = clangModuleCache;
3683+ // Create the DWARFImporterDelegate.
3684+ const auto &props = ModuleList::GetGlobalModuleListProperties ();
3685+ if (props.GetUseSwiftDWARFImporter ())
3686+ m_dwarfimporter_delegate_up =
3687+ std::make_unique<SwiftDWARFImporterDelegate>(*this );
3688+ auto importer_diags = getScopedDiagnosticConsumer ();
3689+ clang_importer_ap = swift::ClangImporter::create (
3690+ *m_ast_context_ap, " " , m_dependency_tracker.get (),
3691+ m_dwarfimporter_delegate_up.get ());
3692+
3693+ // Handle any errors.
3694+ if (!clang_importer_ap || importer_diags->HasErrors ()) {
3695+ AddDiagnostic (eSeverityError, " failed to create ClangImporter" );
3696+ if (GetLog (LLDBLog::Types)) {
3697+ DiagnosticManager diagnostic_manager;
3698+ importer_diags->PrintDiagnostics (diagnostic_manager);
3699+ std::string underlying_error = diagnostic_manager.GetString ();
3700+ HEALTH_LOG_PRINTF (" failed to initialize ClangImporter: %s" ,
3701+ underlying_error.c_str ());
36913702 }
36923703 }
3704+ if (clang_importer_ap) {
3705+ auto clangModuleCache = swift::getModuleCachePathFromClang (
3706+ clang_importer_ap->getClangInstance ());
3707+ if (!clangModuleCache.empty ())
3708+ moduleCachePath = clangModuleCache;
3709+ }
36933710 }
36943711 LOG_PRINTF (GetLog (LLDBLog::Types), " Using Clang module cache path: %s" ,
36953712 moduleCachePath.c_str ());
0 commit comments