@@ -2812,7 +2812,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28122812 return {};
28132813 }
28142814
2815- bool handled_sdk_path = false ;
2815+ bool sdk_path_override = false ;
28162816 ModuleList module_module;
28172817 if (!target_sp)
28182818 module_module.Append (module_sp);
@@ -2826,43 +2826,25 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28262826 swift_ast_sp->SetPlatformSDKPath (target_sdk_spec.GetPath ());
28272827 LOG_PRINTF (GetLog (LLDBLog::Types), " Using target SDK override: %s" ,
28282828 target_sdk_spec.GetPath ().c_str ());
2829- handled_sdk_path = true ;
2829+ sdk_path_override = true ;
28302830 }
28312831
28322832 // Get the precise SDK from the symbol context.
2833+ std::optional<XcodeSDK> sdk;
28332834 if (cu)
28342835 if (auto platform_sp = Platform::GetHostPlatform ()) {
28352836 auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo (*cu);
28362837 if (!sdk_or_err)
28372838 Debugger::ReportError (" Error while parsing SDK path from debug info: " +
28382839 toString (sdk_or_err.takeError ()));
28392840 else {
2840- std::string sdk_path = GetSDKPath (m_description, *sdk_or_err);
2841- if (!sdk_path.empty ()) {
2842- swift_ast_sp->SetPlatformSDKPath (sdk_path);
2843- handled_sdk_path = true ;
2844- LOG_PRINTF (GetLog (LLDBLog::Types), " Using precise SDK: %s" ,
2845- sdk_path.c_str ());
2846- }
2841+ sdk = *sdk_or_err;
2842+ LOG_PRINTF (GetLog (LLDBLog::Types), " Using precise SDK: %s" ,
2843+ sdk->GetString ().str ().c_str ());
28472844 }
28482845 }
28492846
2850- if (!handled_sdk_path) {
2851- for (size_t mi = 0 ; mi != num_images; ++mi) {
2852- ModuleSP module_sp = modules.GetModuleAtIndex (mi);
2853- if (!HasSwiftModules (*module_sp))
2854- continue ;
2855-
2856- std::string sdk_path = GetSDKPathFromDebugInfo (m_description, *module_sp);
2857-
2858- if (sdk_path.empty ())
2859- continue ;
2860-
2861- swift_ast_sp->SetPlatformSDKPath (sdk_path);
2862- handled_sdk_path = true ;
2863- break ;
2864- }
2865- }
2847+ // Derive the triple next.
28662848
28672849 // First, prime the compiler with the options from the main executable:
28682850 bool got_serialized_options = false ;
@@ -2907,13 +2889,30 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29072889 ArchSpec preferred_arch;
29082890 llvm::Triple preferred_triple;
29092891 if (is_repl) {
2892+ LOG_PRINTF (GetLog (LLDBLog::Types), " REPL: prefer target triple." );
2893+ preferred_arch = target_arch;
2894+ preferred_triple = target_triple;
2895+ } else if (!sdk_path_override && !sdk && target_arch) {
2896+ LOG_PRINTF (GetLog (LLDBLog::Types),
2897+ " No Swift debug info: prefer target triple." );
2898+ if (!target_arch.IsCompatibleMatch (module_arch))
2899+ HEALTH_LOG_PRINTF (
2900+ " SwiftASTContext requested for a non-Swift translation unit. Using "
2901+ " target triple \" %s\" , which is not compatible with this "
2902+ " translation unit's triple \" %s\" . Expressions may behave "
2903+ " unexpectedly because of this." ,
2904+ target_triple.str ().c_str (), module_triple.str ().c_str ());
29102905 preferred_arch = target_arch;
29112906 preferred_triple = target_triple;
29122907 } else if (module_arch &&
29132908 (!target_arch || module_arch.IsFullySpecifiedTriple ())) {
2909+ LOG_PRINTF (GetLog (LLDBLog::Types),
2910+ " Prefer module triple." );
29142911 preferred_arch = module_arch;
29152912 preferred_triple = module_triple;
29162913 } else {
2914+ LOG_PRINTF (GetLog (LLDBLog::Types),
2915+ " No viable alternatives: Prefer target triple." );
29172916 // When no viable module triple, fallback to the target triple.
29182917 preferred_arch = target_arch;
29192918 preferred_triple = target_triple;
@@ -2993,6 +2992,27 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29932992 }
29942993
29952994 llvm::Triple triple = swift_ast_sp->GetTriple ();
2995+
2996+ // Triple has been derived, find a matching SDK.
2997+ if (!sdk_path_override) {
2998+ XcodeSDK::Type sdk_type_for_triple = XcodeSDK::GetSDKTypeForTriple (triple);
2999+ if (sdk && sdk->GetType () != sdk_type_for_triple) {
3000+ HEALTH_LOG_PRINTF (" Precise SDK is not compatible with triple. Ignoring." );
3001+ XcodeSDK::Info info{sdk_type_for_triple, {}, sdk->IsAppleInternalSDK ()};
3002+ sdk = XcodeSDK (info);
3003+ }
3004+ if (!sdk) {
3005+ XcodeSDK::Info info{sdk_type_for_triple, {}, false };
3006+ sdk = XcodeSDK (info);
3007+ }
3008+
3009+ std::string sdk_path = GetSDKPath (m_description, *sdk);
3010+ if (!sdk_path.empty ()) {
3011+ swift_ast_sp->SetPlatformSDKPath (sdk_path);
3012+ LOG_PRINTF (GetLog (LLDBLog::Types), " Using SDK: %s" , sdk_path.c_str ());
3013+ }
3014+ }
3015+
29963016 std::string resource_dir = HostInfo::GetSwiftResourceDir (
29973017 triple, swift_ast_sp->GetPlatformSDKPath ());
29983018 ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
0 commit comments