Skip to content

Commit beb3aaf

Browse files
authored
[lldb] Prefer triple from module in precise invocations (#8966)
(cherry-picked from PR #8941) (cherry-picked from commit eed7d856bfb88baaa07addfe1664e4c7934ed4d6)
1 parent a5930b6 commit beb3aaf

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
@@ -2906,20 +2906,42 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
29062906
return exe_module_sp->GetArchitecture().GetTriple();
29072907
};
29082908

2909-
llvm::Triple computed_triple;
2910-
llvm::Triple target_triple = target.GetArchitecture().GetTriple();
2909+
ArchSpec module_arch;
2910+
llvm::Triple module_triple;
2911+
if (sc.module_sp) {
2912+
module_arch = sc.module_sp->GetArchitecture();
2913+
module_triple = module_arch.GetTriple();
2914+
}
29112915

2912-
if (target.GetArchitecture().IsFullySpecifiedTriple()) {
2916+
ArchSpec target_arch = target.GetArchitecture();
2917+
llvm::Triple target_triple = target_arch.GetTriple();
2918+
2919+
ArchSpec preferred_arch;
2920+
llvm::Triple preferred_triple;
2921+
if (module_arch && module_arch.IsFullySpecifiedTriple()) {
2922+
LOG_PRINTF(GetLog(LLDBLog::Types),
2923+
"Preferring module triple %s over target triple %s.",
2924+
module_triple.str().c_str(), target_triple.str().c_str());
2925+
preferred_arch = module_arch;
2926+
preferred_triple = module_triple;
2927+
} else {
2928+
// When no viable module triple, fallback to the target triple.
2929+
preferred_arch = target_arch;
2930+
preferred_triple = target_triple;
2931+
}
2932+
2933+
llvm::Triple computed_triple;
2934+
if (preferred_arch.IsFullySpecifiedTriple()) {
29132935
// If a fully specified triple was passed in, for example
29142936
// through CreateTargetWithFileAndTargetTriple(), prefer that.
29152937
LOG_PRINTF(GetLog(LLDBLog::Types), "Fully specified target triple %s.",
2916-
target_triple.str().c_str());
2917-
computed_triple = target_triple;
2938+
preferred_triple.str().c_str());
2939+
computed_triple = preferred_triple;
29182940
} else {
29192941
// Underspecified means that one or more of vendor, os, or os
29202942
// version (Darwin only) is missing.
29212943
LOG_PRINTF(GetLog(LLDBLog::Types), "Underspecified target triple %s.",
2922-
target_triple.str().c_str());
2944+
preferred_triple.str().c_str());
29232945
llvm::VersionTuple platform_version;
29242946
PlatformSP platform_sp(target.GetPlatform());
29252947
if (platform_sp)
@@ -2936,16 +2958,16 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
29362958
// present, since there might be some ambiguity about the
29372959
// plaform (e.g., ios-macabi runs on the macOS, but uses iOS
29382960
// version numbers).
2939-
if (!platform_version.empty() &&
2940-
target_triple.getEnvironment() == llvm::Triple::UnknownEnvironment) {
2961+
if (!platform_version.empty() && preferred_triple.getEnvironment() ==
2962+
llvm::Triple::UnknownEnvironment) {
29412963
LOG_PRINTF(GetLog(LLDBLog::Types), "Completing triple based on platform.");
29422964

29432965
llvm::SmallString<32> buffer;
29442966
{
29452967
llvm::raw_svector_ostream os(buffer);
2946-
os << target_triple.getArchName() << '-';
2947-
os << target_triple.getVendorName() << '-';
2948-
os << llvm::Triple::getOSTypeName(target_triple.getOS());
2968+
os << preferred_triple.getArchName() << '-';
2969+
os << preferred_triple.getVendorName() << '-';
2970+
os << llvm::Triple::getOSTypeName(preferred_triple.getOS());
29492971
os << platform_version.getAsString();
29502972
}
29512973
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)