Skip to content

Commit 9963727

Browse files
committed
[lldb][DataFormatter] Format libstdc++ unique_ptr like we do libc++
The only difference is that with libc++ the summary string contains the derefernced pointer value. With libstdc++ we currently display the pointer itself, which seems redundant. E.g., ``` (std::unique_ptr<int>) iup = 0x55555556d2b0 { pointer = 0x000055555556d2b0 } (std::unique_ptr<std::basic_string<char> >) sup = 0x55555556d2d0 { pointer = "foobar" } ``` This patch moves the logic into a common helper that's shared between the libc++ and libstdc++ formatters.
1 parent dd495f1 commit 9963727

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

lldb/include/lldb/DataFormatters/FormattersHelpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ void AddFilter(TypeCategoryImpl::SharedPointer category_sp,
5555

5656
std::optional<size_t> ExtractIndexFromString(const char *item_name);
5757

58+
/// Returns \c false if the smart pointer formatters shouldn't continue
59+
/// formatting the rest of the object. Currently this is the case when the
60+
/// pointer is a nullptr.
61+
bool DumpCxxSmartPtrPointerSummary(Stream &stream, ValueObject &ptr,
62+
const TypeSummaryOptions &options);
63+
5864
Address GetArrayAddressOrPointerValue(ValueObject &valobj);
5965

6066
time_t GetOSXEpoch();

lldb/source/DataFormatters/FormattersHelpers.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,25 @@ lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
126126

127127
return data_addr.address;
128128
}
129+
130+
bool lldb_private::formatters::DumpCxxSmartPtrPointerSummary(
131+
Stream &stream, ValueObject &ptr, const TypeSummaryOptions &options) {
132+
if (ptr.GetValueAsUnsigned(0) == 0) {
133+
stream.Printf("nullptr");
134+
return true;
135+
}
136+
137+
Status error;
138+
ValueObjectSP pointee_sp = ptr.Dereference(error);
139+
// FIXME: should we be handling this as an error?
140+
if (!pointee_sp || !error.Success())
141+
return false;
142+
143+
if (!pointee_sp->DumpPrintableRepresentation(
144+
stream, ValueObject::eValueObjectRepresentationStyleSummary,
145+
lldb::eFormatInvalid,
146+
ValueObject::PrintableRepresentationSpecialCases::eDisable, false))
147+
stream.Printf("ptr = 0x%" PRIx64, ptr.GetValueAsUnsigned(0));
148+
149+
return false;
150+
}

lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,8 @@ bool lldb_private::formatters::LibcxxSmartPointerSummaryProvider(
166166
if (!ptr_sp)
167167
return false;
168168

169-
if (ptr_sp->GetValueAsUnsigned(0) == 0) {
170-
stream.Printf("nullptr");
169+
if (!DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options))
171170
return true;
172-
} else {
173-
bool print_pointee = false;
174-
Status error;
175-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
176-
if (pointee_sp && error.Success()) {
177-
if (pointee_sp->DumpPrintableRepresentation(
178-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
179-
lldb::eFormatInvalid,
180-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
181-
false))
182-
print_pointee = true;
183-
}
184-
if (!print_pointee)
185-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
186-
}
187171

188172
if (count_sp)
189173
stream.Printf(" strong=%" PRIu64, 1 + count_sp->GetValueAsUnsigned(0));
@@ -210,24 +194,7 @@ bool lldb_private::formatters::LibcxxUniquePointerSummaryProvider(
210194
if (!ptr_sp)
211195
return false;
212196

213-
if (ptr_sp->GetValueAsUnsigned(0) == 0) {
214-
stream.Printf("nullptr");
215-
return true;
216-
} else {
217-
bool print_pointee = false;
218-
Status error;
219-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
220-
if (pointee_sp && error.Success()) {
221-
if (pointee_sp->DumpPrintableRepresentation(
222-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
223-
lldb::eFormatInvalid,
224-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
225-
false))
226-
print_pointee = true;
227-
}
228-
if (!print_pointee)
229-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
230-
}
197+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
231198

232199
return true;
233200
}

lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,8 @@ bool LibStdcppUniquePtrSyntheticFrontEnd::GetSummary(
156156
if (!m_ptr_obj)
157157
return false;
158158

159-
bool success;
160-
uint64_t ptr_value = m_ptr_obj->GetValueAsUnsigned(0, &success);
161-
if (!success)
162-
return false;
163-
if (ptr_value == 0)
164-
stream.Printf("nullptr");
165-
else
166-
stream.Printf("0x%" PRIx64, ptr_value);
159+
DumpCxxSmartPtrPointerSummary(stream, *m_ptr_obj, options);
160+
167161
return true;
168162
}
169163

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/unique_ptr/TestDataFormatterStdUniquePtr.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ def test_with_run_command(self):
3131
self.assertTrue(frame.IsValid())
3232

3333
self.expect("frame variable nup", substrs=["nup = nullptr"])
34-
self.expect("frame variable iup", substrs=["iup = 0x"])
35-
self.expect("frame variable sup", substrs=["sup = 0x"])
34+
self.expect("frame variable iup", substrs=["iup = 123"])
35+
self.expect("frame variable sup", substrs=['sup = "foobar"'])
3636

3737
self.expect("frame variable ndp", substrs=["ndp = nullptr"])
3838
self.expect(
39-
"frame variable idp", substrs=["idp = 0x", "deleter = ", "a = 1", "b = 2"]
39+
"frame variable idp", substrs=["idp = 456", "deleter = ", "a = 1", "b = 2"]
4040
)
4141
self.expect(
42-
"frame variable sdp", substrs=["sdp = 0x", "deleter = ", "a = 3", "b = 4"]
42+
"frame variable sdp", substrs=['sdp = "baz"', "deleter = ", "a = 3", "b = 4"]
4343
)
4444

4545
self.assertEqual(
@@ -106,13 +106,13 @@ def test_recursive_unique_ptr(self):
106106
substrs=["stopped", "stop reason = breakpoint"],
107107
)
108108

109-
self.expect("frame variable f1->fp", substrs=["fp = 0x"])
109+
self.expect("frame variable f1->fp", substrs=["fp = Foo @ 0x"])
110110
self.expect(
111-
"frame variable --ptr-depth=1 f1->fp", substrs=["data = 2", "fp = 0x"]
111+
"frame variable --ptr-depth=1 f1->fp", substrs=["data = 2", "fp = Foo @ 0x"]
112112
)
113113
self.expect(
114114
"frame variable --ptr-depth=2 f1->fp",
115-
substrs=["data = 2", "fp = 0x", "data = 1"],
115+
substrs=["data = 2", "fp = Foo @ 0x", "data = 1"],
116116
)
117117

118118
frame = self.frame()

0 commit comments

Comments
 (0)