Skip to content

Commit 450d470

Browse files
authored
Merge pull request #8941 from apple/dl/lldb-Prefer-triple-from-module-in-precise-invocations
[lldb] Prefer triple from module in precise invocations
2 parents 4523e78 + a8eb9f9 commit 450d470

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,15 @@ exe_scope = exe_ctx.GetBestExecutionContextScope();
731731
return error("could not create a Swift scratch context: ",
732732
m_err.AsCString());
733733

734-
const SymbolContext *sc =
735-
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
734+
// For playgrounds, the target triple should be used for expression
735+
// evaluation, not the current module. This requires disabling precise
736+
// compiler invocations.
737+
//
738+
// To disable precise compiler invocations, pass a null SymbolContext.
739+
const SymbolContext *sc = nullptr;
740+
if (!m_runs_in_playground_or_repl)
741+
sc = &frame->GetSymbolContext(lldb::eSymbolContextFunction);
742+
736743
auto *swift_ast_ctx = m_swift_scratch_ctx->get()->GetSwiftASTContext(sc);
737744
m_swift_ast_ctx =
738745
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(swift_ast_ctx);

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,20 +2909,42 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
29092909
return exe_module_sp->GetArchitecture().GetTriple();
29102910
};
29112911

2912-
llvm::Triple computed_triple;
2913-
llvm::Triple target_triple = target.GetArchitecture().GetTriple();
2912+
ArchSpec module_arch;
2913+
llvm::Triple module_triple;
2914+
if (sc.module_sp) {
2915+
module_arch = sc.module_sp->GetArchitecture();
2916+
module_triple = module_arch.GetTriple();
2917+
}
29142918

2915-
if (target.GetArchitecture().IsFullySpecifiedTriple()) {
2919+
ArchSpec target_arch = target.GetArchitecture();
2920+
llvm::Triple target_triple = target_arch.GetTriple();
2921+
2922+
ArchSpec preferred_arch;
2923+
llvm::Triple preferred_triple;
2924+
if (module_arch && module_arch.IsFullySpecifiedTriple()) {
2925+
LOG_PRINTF(GetLog(LLDBLog::Types),
2926+
"Preferring module triple %s over target triple %s.",
2927+
module_triple.str().c_str(), target_triple.str().c_str());
2928+
preferred_arch = module_arch;
2929+
preferred_triple = module_triple;
2930+
} else {
2931+
// When no viable module triple, fallback to the target triple.
2932+
preferred_arch = target_arch;
2933+
preferred_triple = target_triple;
2934+
}
2935+
2936+
llvm::Triple computed_triple;
2937+
if (preferred_arch.IsFullySpecifiedTriple()) {
29162938
// If a fully specified triple was passed in, for example
29172939
// through CreateTargetWithFileAndTargetTriple(), prefer that.
29182940
LOG_PRINTF(GetLog(LLDBLog::Types), "Fully specified target triple %s.",
2919-
target_triple.str().c_str());
2920-
computed_triple = target_triple;
2941+
preferred_triple.str().c_str());
2942+
computed_triple = preferred_triple;
29212943
} else {
29222944
// Underspecified means that one or more of vendor, os, or os
29232945
// version (Darwin only) is missing.
29242946
LOG_PRINTF(GetLog(LLDBLog::Types), "Underspecified target triple %s.",
2925-
target_triple.str().c_str());
2947+
preferred_triple.str().c_str());
29262948
llvm::VersionTuple platform_version;
29272949
PlatformSP platform_sp(target.GetPlatform());
29282950
if (platform_sp)
@@ -2939,16 +2961,16 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
29392961
// present, since there might be some ambiguity about the
29402962
// plaform (e.g., ios-macabi runs on the macOS, but uses iOS
29412963
// version numbers).
2942-
if (!platform_version.empty() &&
2943-
target_triple.getEnvironment() == llvm::Triple::UnknownEnvironment) {
2964+
if (!platform_version.empty() && preferred_triple.getEnvironment() ==
2965+
llvm::Triple::UnknownEnvironment) {
29442966
LOG_PRINTF(GetLog(LLDBLog::Types), "Completing triple based on platform.");
29452967

29462968
llvm::SmallString<32> buffer;
29472969
{
29482970
llvm::raw_svector_ostream os(buffer);
2949-
os << target_triple.getArchName() << '-';
2950-
os << target_triple.getVendorName() << '-';
2951-
os << llvm::Triple::getOSTypeName(target_triple.getOS());
2971+
os << preferred_triple.getArchName() << '-';
2972+
os << preferred_triple.getVendorName() << '-';
2973+
os << llvm::Triple::getOSTypeName(preferred_triple.getOS());
29522974
os << platform_version.getAsString();
29532975
}
29542976
computed_triple = llvm::Triple(buffer);

lldb/test/API/lang/swift/deployment_target/TestSwiftDeploymentTarget.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,31 @@ def test_swift_deployment_target_from_macho(self):
6666
self.filecheck('platform shell cat ""%s"' % log, __file__)
6767
# CHECK: SwiftASTContextForExpressions::SetTriple({{.*}}apple-macosx11.0.0
6868
# CHECK-NOT: SwiftASTContextForExpressions::RegisterSectionModules("a.out"){{.*}} AST Data blobs
69+
70+
@skipUnlessDarwin # This test uses macOS triples explicitly.
71+
@skipIfDarwinEmbedded
72+
@skipIf(macos_version=["<", "11.1"])
73+
@skipIf(setting=("symbols.swift-precise-compiler-invocation", "false"))
74+
@swiftTest
75+
def test_swift_precise_compiler_invocation_triple(self):
76+
"""
77+
Ensure expressions prefer the target triple of their module, as it may
78+
differ from the target triple of the target. This is necessary for
79+
explicitly built modules.
80+
"""
81+
self.build()
82+
log = self.getBuildArtifact("types.log")
83+
self.runCmd(f'log enable lldb types -f "{log}"')
84+
lldbutil.run_to_source_breakpoint(
85+
self, "break here", lldb.SBFileSpec("NewerTarget.swift")
86+
)
87+
self.expect(
88+
"image list -t libNewerTarget.dylib",
89+
substrs=["-apple-macosx11.1.0"],
90+
)
91+
self.expect("expression self", substrs=["i = 23"])
92+
self.filecheck(
93+
f'platform shell cat "{log}"', __file__, "-check-prefix=CHECK-PRECISE"
94+
)
95+
# CHECK-PRECISE: SwiftASTContextForExpressions(module: "NewerTarget", cu: "NewerTarget.swift")::CreateInstance() -- Fully specified target triple {{.*}}-apple-macosx11.1.0
96+
# CHECK-PRECISE: SwiftASTContextForExpressions(module: "NewerTarget", cu: "NewerTarget.swift")::SetTriple("{{.*}}-apple-macosx11.1.0")

0 commit comments

Comments
 (0)