Skip to content

Commit c45216d

Browse files
authored
[lldb] Update Measurement data formatter (#9127)
rdar://121940851
1 parent ab5b104 commit c45216d

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

lldb/source/Plugins/Language/Swift/FoundationValueTypes.cpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -144,45 +144,49 @@ bool lldb_private::formatters::swift::IndexPath_SummaryProvider(
144144

145145
bool lldb_private::formatters::swift::Measurement_SummaryProvider(
146146
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
147-
static ConstString g_value("value");
148-
static ConstString g_unit("unit");
149-
static ConstString g__symbol("_symbol");
147+
ValueObjectSP value_sp, symbol_sp;
150148

151-
ValueObjectSP value_sp(valobj.GetChildAtNamePath({g_value}));
152-
if (!value_sp)
153-
return false;
149+
value_sp = valobj.GetChildMemberWithName("value");
150+
if (value_sp) {
151+
// Measurement structure prior to macOS 14.
152+
ValueObjectSP unit_sp(valobj.GetChildMemberWithName("unit"));
153+
if (!unit_sp)
154+
return false;
154155

155-
ValueObjectSP unit_sp(valobj.GetChildAtNamePath({g_unit}));
156-
if (!unit_sp)
157-
return false;
156+
ProcessSP process_sp(valobj.GetProcessSP());
157+
if (!process_sp)
158+
return false;
158159

159-
ProcessSP process_sp(valobj.GetProcessSP());
160-
if (!process_sp)
161-
return false;
160+
ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process_sp);
161+
if (!objc_runtime)
162+
return false;
162163

163-
ObjCLanguageRuntime *objc_runtime =
164-
ObjCLanguageRuntime::Get(*process_sp);
165-
if (!objc_runtime)
166-
return false;
164+
auto descriptor_sp(objc_runtime->GetClassDescriptor(*unit_sp));
165+
if (!descriptor_sp)
166+
return false;
167167

168-
auto descriptor_sp(objc_runtime->GetClassDescriptor(*unit_sp));
169-
if (!descriptor_sp)
170-
return false;
168+
if (descriptor_sp->GetNumIVars() == 0)
169+
return false;
171170

172-
if (descriptor_sp->GetNumIVars() == 0)
173-
return false;
171+
auto ivar = descriptor_sp->GetIVarAtIndex(0);
172+
if (!ivar.m_type.IsValid())
173+
return false;
174174

175-
auto ivar = descriptor_sp->GetIVarAtIndex(0);
176-
if (!ivar.m_type.IsValid())
177-
return false;
175+
symbol_sp =
176+
unit_sp->GetSyntheticChildAtOffset(ivar.m_offset, ivar.m_type, true);
177+
if (!symbol_sp)
178+
return false;
178179

179-
ValueObjectSP symbol_sp(
180-
unit_sp->GetSyntheticChildAtOffset(ivar.m_offset, ivar.m_type, true));
181-
if (!symbol_sp)
182-
return false;
180+
symbol_sp = symbol_sp->GetQualifiedRepresentationIfAvailable(
181+
lldb::eDynamicDontRunTarget, true);
182+
} else {
183+
// Measurement structure as of macOS 14+.
184+
value_sp = valobj.GetChildMemberWithName("_doubleValue");
185+
symbol_sp = valobj.GetChildAtNamePath({"unit", "_symbol"});
186+
}
183187

184-
symbol_sp = symbol_sp->GetQualifiedRepresentationIfAvailable(
185-
lldb::eDynamicDontRunTarget, true);
188+
if (!value_sp || !symbol_sp)
189+
return false;
186190

187191
DataExtractor data_extractor;
188192
Status error;

lldb/test/API/lang/swift/foundation_value_types/measurement/TestSwiftFoundationTypeMeasurement.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,38 @@
99
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
#
1111
# ------------------------------------------------------------------------------
12-
import lldbsuite.test.lldbinline as lldbinline
12+
import lldb
1313
from lldbsuite.test.decorators import *
14+
from lldbsuite.test.lldbtest import *
15+
from lldbsuite.test import lldbutil
1416

15-
lldbinline.MakeInlineTest(__file__, globals(),
16-
decorators=[swiftTest,skipUnlessDarwin,
17-
expectedFailureAll(bugnumber="rdar://60396797",
18-
setting=('symbols.use-swift-clangimporter', 'false'))
19-
])
17+
18+
class TestCase(TestBase):
19+
@swiftTest
20+
@skipUnlessDarwin
21+
@expectedFailureAll(
22+
bugnumber="rdar://60396797",
23+
setting=("symbols.use-swift-clangimporter", "false"),
24+
)
25+
def test_measurement(self):
26+
self.build()
27+
lldbutil.run_to_source_breakpoint(
28+
self, "break here", lldb.SBFileSpec("main.swift")
29+
)
30+
self.expect("expr -- measurement", substrs=["1.25 m"])
31+
32+
@swiftTest
33+
@skipUnlessDarwin
34+
def test_measurement_without_swift_ast_context(self):
35+
self.build()
36+
lldbutil.run_to_source_breakpoint(
37+
self, "break here", lldb.SBFileSpec("main.swift")
38+
)
39+
40+
# This setting is to test that measurements can be printed using
41+
# TypeSystemSwiftTypeRef only. If the SwiftASTContext fallback were
42+
# enabled, it can have the unwatned effect of suppressing failures
43+
# within the TypeRef type system.
44+
self.runCmd("settings set symbols.swift-enable-ast-context false")
45+
46+
self.expect("frame variable measurement", substrs=["1.25 m"])

lldb/test/API/lang/swift/foundation_value_types/measurement/main.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import Foundation
1313

1414
func main() {
1515
var measurement = Measurement(value: 1.25, unit: Unit(symbol: "m"))
16-
print("done!") //% self.expect("frame variable measurement", substrs=['1.25 m'])
17-
//% self.expect("expression -d run -- measurement", substrs=['1.25 m'])
16+
print("break here")
1817
}
1918

2019
main()

0 commit comments

Comments
 (0)