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