Skip to content

Commit 1b38440

Browse files
author
git apple-llvm automerger
committed
Merge commit '1274e908987d' from swift/release/6.1 into stable/20240723
2 parents a40deca + 1274e90 commit 1b38440

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
@@ -2814,7 +2814,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28142814
return {};
28152815
}
28162816

2817-
bool handled_sdk_path = false;
2817+
bool sdk_path_override = false;
28182818
ModuleList module_module;
28192819
if (!target_sp)
28202820
module_module.Append(module_sp);
@@ -2828,43 +2828,25 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28282828
swift_ast_sp->SetPlatformSDKPath(target_sdk_spec.GetPath());
28292829
LOG_PRINTF(GetLog(LLDBLog::Types), "Using target SDK override: %s",
28302830
target_sdk_spec.GetPath().c_str());
2831-
handled_sdk_path = true;
2831+
sdk_path_override = true;
28322832
}
28332833

28342834
// Get the precise SDK from the symbol context.
2835+
std::optional<XcodeSDK> sdk;
28352836
if (cu)
28362837
if (auto platform_sp = Platform::GetHostPlatform()) {
28372838
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*cu);
28382839
if (!sdk_or_err)
28392840
Debugger::ReportError("Error while parsing SDK path from debug info: " +
28402841
toString(sdk_or_err.takeError()));
28412842
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());
28492846
}
28502847
}
28512848

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.
28682850

28692851
// First, prime the compiler with the options from the main executable:
28702852
bool got_serialized_options = false;
@@ -2909,13 +2891,30 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29092891
ArchSpec preferred_arch;
29102892
llvm::Triple preferred_triple;
29112893
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());
29122907
preferred_arch = target_arch;
29132908
preferred_triple = target_triple;
29142909
} else if (module_arch &&
29152910
(!target_arch || module_arch.IsFullySpecifiedTriple())) {
2911+
LOG_PRINTF(GetLog(LLDBLog::Types),
2912+
"Prefer module triple.");
29162913
preferred_arch = module_arch;
29172914
preferred_triple = module_triple;
29182915
} else {
2916+
LOG_PRINTF(GetLog(LLDBLog::Types),
2917+
"No viable alternatives: Prefer target triple.");
29192918
// When no viable module triple, fallback to the target triple.
29202919
preferred_arch = target_arch;
29212920
preferred_triple = target_triple;
@@ -2995,6 +2994,27 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
29952994
}
29962995

29972996
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+
29983018
std::string resource_dir = HostInfo::GetSwiftResourceDir(
29993019
triple, swift_ast_sp->GetPlatformSDKPath());
30003020
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
@@ -1792,8 +1792,11 @@ const char *TypeSystemSwiftTypeRef::DeriveKeyFor(const SymbolContext &sc) {
17921792
if (ConstString name = GetSwiftModuleFor(sc))
17931793
return name.GetCString();
17941794

1795-
if (sc.module_sp)
1796-
return sc.module_sp->GetFileSpec().GetFilename().GetCString();
1795+
if (sc.module_sp) {
1796+
if (sc.module_sp->GetFileSpec())
1797+
return sc.module_sp->GetFileSpec().GetFilename().GetCString();
1798+
return sc.module_sp->GetObjectName().GetCString();
1799+
}
17971800
return nullptr;
17981801
}
17991802

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)