@@ -2813,7 +2813,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2813
2813
return {};
2814
2814
}
2815
2815
2816
- bool handled_sdk_path = false ;
2816
+ bool sdk_path_override = false ;
2817
2817
ModuleList module_module;
2818
2818
if (!target_sp)
2819
2819
module_module.Append (module_sp);
@@ -2827,43 +2827,25 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2827
2827
swift_ast_sp->SetPlatformSDKPath (target_sdk_spec.GetPath ());
2828
2828
LOG_PRINTF (GetLog (LLDBLog::Types), " Using target SDK override: %s" ,
2829
2829
target_sdk_spec.GetPath ().c_str ());
2830
- handled_sdk_path = true ;
2830
+ sdk_path_override = true ;
2831
2831
}
2832
2832
2833
2833
// Get the precise SDK from the symbol context.
2834
+ std::optional<XcodeSDK> sdk;
2834
2835
if (cu)
2835
2836
if (auto platform_sp = Platform::GetHostPlatform ()) {
2836
2837
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo (*cu);
2837
2838
if (!sdk_or_err)
2838
2839
Debugger ::ReportError (" Error while parsing SDK path from debug info: " +
2839
2840
toString (sdk_or_err.takeError ()));
2840
2841
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 ());
2848
2845
}
2849
2846
}
2850
2847
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.
2867
2849
2868
2850
// First, prime the compiler with the options from the main executable:
2869
2851
bool got_serialized_options = false ;
@@ -2908,13 +2890,30 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2908
2890
ArchSpec preferred_arch;
2909
2891
llvm::Triple preferred_triple;
2910
2892
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 ());
2911
2906
preferred_arch = target_arch;
2912
2907
preferred_triple = target_triple;
2913
2908
} else if (module_arch &&
2914
2909
(!target_arch || module_arch.IsFullySpecifiedTriple ())) {
2910
+ LOG_PRINTF (GetLog (LLDBLog::Types),
2911
+ " Prefer module triple." );
2915
2912
preferred_arch = module_arch;
2916
2913
preferred_triple = module_triple;
2917
2914
} else {
2915
+ LOG_PRINTF (GetLog (LLDBLog::Types),
2916
+ " No viable alternatives: Prefer target triple." );
2918
2917
// When no viable module triple, fallback to the target triple.
2919
2918
preferred_arch = target_arch;
2920
2919
preferred_triple = target_triple;
@@ -2994,6 +2993,27 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
2994
2993
}
2995
2994
2996
2995
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
+
2997
3017
std::string resource_dir = HostInfo::GetSwiftResourceDir (
2998
3018
triple, swift_ast_sp->GetPlatformSDKPath ());
2999
3019
ConfigureResourceDirs (swift_ast_sp->GetCompilerInvocation (), resource_dir,
0 commit comments