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