Skip to content

Commit e6d9bd6

Browse files
authored
[lldb] Fix Language Plugin decl printing (#6795)
`SwiftLanguage::GetDeclPrintingHelper` is a helper function used for Swift types. This code contains some very similar logic to the default logic in `ValueObjectPrinter`. Specifically: `SwiftLanguage`: ```c++ else if (!options.m_hide_name) stream.Printf(" ="); ``` `ValueObjectPrinter`: ```c++ else if (ShouldShowName()) m_stream->Printf(" ="); ``` The function `ValueObjectPrinter::ShouldShowName` is not accessible by `SwiftLanguage`. To allow `SwiftLanguage` to make the right decision, and fit within the existing API boundary, this change uses a custom print options, which sets calls `SetHideName` according to the value of `ShouldShowName()`. This allows the Swift language decl printing helper to correctly know whether to print the name, or not.
1 parent 73f3d7e commit e6d9bd6

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lldb/source/DataFormatters/ValueObjectPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,14 @@ void ValueObjectPrinter::PrintDecl() {
300300
ConstString type_name_cstr(typeName.GetString());
301301
ConstString var_name_cstr(varName.GetString());
302302

303+
DumpValueObjectOptions decl_print_options;
304+
// Pass printing helpers an option object that indicates whether the name
305+
// should be shown or hidden.
306+
decl_print_options.SetHideName(!ShouldShowName());
307+
303308
StreamString dest_stream;
304309
if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
305-
m_options, dest_stream)) {
310+
decl_print_options, dest_stream)) {
306311
decl_printed = true;
307312
m_stream->PutCString(dest_stream.GetString());
308313
}

lldb/test/API/commands/dwim-print/swift/TestDWIMPrintSwift.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ def test_swift_po_non_address_hex(self):
2828
self, "// break here", lldb.SBFileSpec("main.swift")
2929
)
3030
self.expect(f"dwim-print -O -- 0x1000", substrs=["4096"])
31+
32+
@swiftTest
33+
def test_print_swift_object_does_not_show_name(self):
34+
"""Ensure that objects are printed without a name, and without the '='
35+
that would follow the name."""
36+
self.build()
37+
lldbutil.run_to_source_breakpoint(
38+
self, "// break here", lldb.SBFileSpec("main.swift")
39+
)
40+
41+
self.expect(f"dwim-print user", patterns=[r"^\(a\.User\) 0x[0-9a-f]{7,} \{"])

lldb/test/API/commands/dwim-print/swift/main.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ class Object: CustomStringConvertible {
66
}
77
}
88

9+
class User {
10+
var id: Int = 314159265358979322
11+
var name: String = "Gwendolyn"
12+
var groups: (admin: Bool, staff: Bool) = (false, true)
13+
}
14+
915
func main() {
1016
let object = Object()
11-
_ = object // break here
17+
let user = User()
18+
// break here
19+
print(object, user)
1220
}
1321

1422
main()

0 commit comments

Comments
 (0)