Skip to content

Commit df15e94

Browse files
Merge pull request #9751 from adrian-prantl/cherry-pick-next-lldb-Make-sure-triple-and-SDK-always-match
[Cherry-pick into next] [lldb] Make sure triple and SDK always match.
2 parents 24cca7a + 3c18c27 commit df15e94

File tree

7 files changed

+95
-27
lines changed

7 files changed

+95
-27
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,8 +1775,11 @@ const char *TypeSystemSwiftTypeRef::DeriveKeyFor(const SymbolContext &sc) {
17751775
if (ConstString name = GetSwiftModuleFor(sc))
17761776
return name.GetCString();
17771777

1778-
if (sc.module_sp)
1779-
return sc.module_sp->GetFileSpec().GetFilename().GetCString();
1778+
if (sc.module_sp) {
1779+
if (sc.module_sp->GetFileSpec())
1780+
return sc.module_sp->GetFileSpec().GetFilename().GetCString();
1781+
return sc.module_sp->GetObjectName().GetCString();
1782+
}
17801783
return nullptr;
17811784
}
17821785

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
SWIFT_SOURCES = main.swift
2+
SWIFTFLAGS_EXTRAS = -I.
3+
SWIFT_BRIDGING_HEADER = bridging-header.h
4+
LD_EXTRAS = -L. -lDylib
5+
6+
all: dylib $(EXE)
7+
8+
include Makefile.rules
9+
10+
.PHONY: dylib
11+
dylib:
12+
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
13+
ARCH=$(ARCH) DSYMUTIL=$(DSYMUTIL) \
14+
VPATH=$(SRCDIR) -I $(SRCDIR) \
15+
-f $(THIS_FILE_DIR)/Makefile.rules \
16+
DYLIB_C_SOURCES=dylib.c \
17+
DYLIB_NAME=Dylib \
18+
DYLIB_ONLY=YES \
19+
DEBUG_INFO_FLAG= \
20+
all
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
class TestSwiftExpressionNoDebugInfo(TestBase):
7+
NO_DEBUG_INFO_TESTCASE = True
8+
9+
@swiftTest
10+
@skipUnlessDarwin
11+
def test_missing_var(self):
12+
"""Test running a Swift expression in a non-Swift context"""
13+
self.build()
14+
target, process, thread, bkpt = lldbutil.run_to_name_breakpoint(
15+
self, 'foo')
16+
17+
types_log = self.getBuildArtifact("types.log")
18+
self.expect("log enable lldb types -f " + types_log)
19+
self.expect('expr -l Swift -- 1')
20+
self.filecheck('platform shell cat "%s"' % types_log, __file__)
21+
# CHECK: No Swift debug info: prefer target triple.
22+
# CHECK: Using SDK: {{.*}}MacOSX
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void foo();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void foo() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo()

0 commit comments

Comments
 (0)