@@ -2814,7 +2814,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28142814 return {};
28152815 }
28162816
2817- bool handled_sdk_path = false ;
2817+ bool sdk_path_override = false ;
28182818 ModuleList module_module;
28192819 if (!target_sp)
28202820 module_module.Append (module_sp);
@@ -2828,43 +2828,25 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28282828 swift_ast_sp->SetPlatformSDKPath (target_sdk_spec.GetPath ());
28292829 LOG_PRINTF (GetLog (LLDBLog::Types), " Using target SDK override: %s" ,
28302830 target_sdk_spec.GetPath ().c_str ());
2831- handled_sdk_path = true ;
2831+ sdk_path_override = true ;
28322832 }
28332833
28342834 // Get the precise SDK from the symbol context.
2835+ std::optional<XcodeSDK> sdk;
28352836 if (cu)
28362837 if (auto platform_sp = Platform::GetHostPlatform ()) {
28372838 auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo (*cu);
28382839 if (!sdk_or_err)
28392840 Debugger::ReportError (" Error while parsing SDK path from debug info: " +
28402841 toString (sdk_or_err.takeError ()));
28412842 else {
2842- std::string sdk_path = GetSDKPath (m_description, *sdk_or_err);
2843- if (!sdk_path.empty ()) {
2844- swift_ast_sp->SetPlatformSDKPath (sdk_path);
2845- handled_sdk_path = true ;
2846- LOG_PRINTF (GetLog (LLDBLog::Types), " Using precise SDK: %s" ,
2847- sdk_path.c_str ());
2848- }
2843+ sdk = *sdk_or_err;
2844+ LOG_PRINTF (GetLog (LLDBLog::Types), " Using precise SDK: %s" ,
2845+ sdk->GetString ().str ().c_str ());
28492846 }
28502847 }
28512848
2852- if (!handled_sdk_path) {
2853- for (size_t mi = 0 ; mi != num_images; ++mi) {
2854- ModuleSP module_sp = modules.GetModuleAtIndex (mi);
2855- if (!HasSwiftModules (*module_sp))
2856- continue ;
2857-
2858- std::string sdk_path = GetSDKPathFromDebugInfo (m_description, *module_sp);
2859-
2860- if (sdk_path.empty ())
2861- continue ;
2862-
2863- swift_ast_sp->SetPlatformSDKPath (sdk_path);
2864- handled_sdk_path = true ;
2865- break ;
2866- }
2867- }
2849+ // Derive the triple next.
28682850
28692851 // First, prime the compiler with the options from the main executable:
28702852 bool got_serialized_options = false ;
@@ -2909,13 +2891,30 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29092891 ArchSpec preferred_arch;
29102892 llvm::Triple preferred_triple;
29112893 if (is_repl) {
2894+ LOG_PRINTF (GetLog (LLDBLog::Types), " REPL: prefer target triple." );
2895+ preferred_arch = target_arch;
2896+ preferred_triple = target_triple;
2897+ } else if (!sdk_path_override && !sdk && target_arch) {
2898+ LOG_PRINTF (GetLog (LLDBLog::Types),
2899+ " No Swift debug info: prefer target triple." );
2900+ if (!target_arch.IsCompatibleMatch (module_arch))
2901+ HEALTH_LOG_PRINTF (
2902+ " SwiftASTContext requested for a non-Swift translation unit. Using "
2903+ " target triple \" %s\" , which is not compatible with this "
2904+ " translation unit's triple \" %s\" . Expressions may behave "
2905+ " unexpectedly because of this." ,
2906+ target_triple.str ().c_str (), module_triple.str ().c_str ());
29122907 preferred_arch = target_arch;
29132908 preferred_triple = target_triple;
29142909 } else if (module_arch &&
29152910 (!target_arch || module_arch.IsFullySpecifiedTriple ())) {
2911+ LOG_PRINTF (GetLog (LLDBLog::Types),
2912+ " Prefer module triple." );
29162913 preferred_arch = module_arch;
29172914 preferred_triple = module_triple;
29182915 } else {
2916+ LOG_PRINTF (GetLog (LLDBLog::Types),
2917+ " No viable alternatives: Prefer target triple." );
29192918 // When no viable module triple, fallback to the target triple.
29202919 preferred_arch = target_arch;
29212920 preferred_triple = target_triple;
@@ -2995,6 +2994,27 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29952994 }
29962995
29972996 llvm::Triple triple = swift_ast_sp->GetTriple ();
2997+
2998+ // Triple has been derived, find a matching SDK.
2999+ if (!sdk_path_override) {
3000+ XcodeSDK::Type sdk_type_for_triple = XcodeSDK::GetSDKTypeForTriple (triple);
3001+ if (sdk && sdk->GetType () != sdk_type_for_triple) {
3002+ HEALTH_LOG_PRINTF (" Precise SDK is not compatible with triple. Ignoring." );
3003+ XcodeSDK::Info info{sdk_type_for_triple, {}, sdk->IsAppleInternalSDK ()};
3004+ sdk = XcodeSDK (info);
3005+ }
3006+ if (!sdk) {
3007+ XcodeSDK::Info info{sdk_type_for_triple, {}, false };
3008+ sdk = XcodeSDK (info);
3009+ }
3010+
3011+ std::string sdk_path = GetSDKPath (m_description, *sdk);
3012+ if (!sdk_path.empty ()) {
3013+ swift_ast_sp->SetPlatformSDKPath (sdk_path);
3014+ LOG_PRINTF (GetLog (LLDBLog::Types), " Using SDK: %s" , sdk_path.c_str ());
3015+ }
3016+ }
3017+
29983018 std::string resource_dir = HostInfo::GetSwiftResourceDir (
29993019 triple, swift_ast_sp->GetPlatformSDKPath ());
30003020 ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
0 commit comments