Skip to content

Commit 1274e90

Browse files
Merge pull request #9728 from adrian-prantl/141173613
[lldb] Make sure triple and SDK always match.
2 parents 4c67ec0 + 9bc04d3 commit 1274e90

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
@@ -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,

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

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

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

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)