Skip to content

Commit 84de12d

Browse files
committed
fixup! remove early-return for empty shared_ptrs; refactor weak/strong count printing
1 parent 63066cc commit 84de12d

File tree

5 files changed

+50
-21
lines changed

5 files changed

+50
-21
lines changed

lldb/include/lldb/DataFormatters/FormattersHelpers.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ 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,
58+
/// Prints the summary for the pointer value of a C++
59+
/// std::unique_ptr/std::shared_ptr/std::weak_ptr.
60+
void DumpCxxSmartPtrPointerSummary(Stream &stream, ValueObject &ptr,
6261
const TypeSummaryOptions &options);
6362

6463
Address GetArrayAddressOrPointerValue(ValueObject &valobj);

lldb/source/DataFormatters/FormattersHelpers.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,21 @@ lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
127127
return data_addr.address;
128128
}
129129

130-
bool lldb_private::formatters::DumpCxxSmartPtrPointerSummary(
130+
void lldb_private::formatters::DumpCxxSmartPtrPointerSummary(
131131
Stream &stream, ValueObject &ptr, const TypeSummaryOptions &options) {
132132
if (ptr.GetValueAsUnsigned(0) == 0) {
133133
stream.Printf("nullptr");
134-
return true;
134+
return;
135135
}
136136

137137
Status error;
138138
ValueObjectSP pointee_sp = ptr.Dereference(error);
139-
// FIXME: should we be handling this as an error?
140139
if (!pointee_sp || !error.Success())
141-
return false;
140+
return;
142141

143142
if (!pointee_sp->DumpPrintableRepresentation(
144143
stream, ValueObject::eValueObjectRepresentationStyleSummary,
145144
lldb::eFormatInvalid,
146145
ValueObject::PrintableRepresentationSpecialCases::eDisable, false))
147146
stream.Printf("ptr = 0x%" PRIx64, ptr.GetValueAsUnsigned(0));
148-
149-
return false;
150147
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,38 @@ bool lldb_private::formatters::LibcxxSmartPointerSummaryProvider(
157157
ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
158158
if (!valobj_sp)
159159
return false;
160-
ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
161-
ValueObjectSP count_sp(
162-
valobj_sp->GetChildAtNamePath({"__cntrl_", "__shared_owners_"}));
163-
ValueObjectSP weakcount_sp(
164-
valobj_sp->GetChildAtNamePath({"__cntrl_", "__shared_weak_owners_"}));
165160

166-
if (!ptr_sp)
161+
ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
162+
ValueObjectSP ctrl_sp(valobj_sp->GetChildMemberWithName("__cntrl_"));
163+
if (!ctrl_sp || !ptr_sp)
167164
return false;
168165

169-
if (!DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options))
166+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
167+
168+
bool success;
169+
uint64_t ctrl_addr = ctrl_sp->GetValueAsUnsigned(0, &success);
170+
// Empty control field. We're done.
171+
if (!success || ctrl_addr == 0)
170172
return true;
171173

172-
if (count_sp)
173-
stream.Printf(" strong=%" PRIu64, 1 + count_sp->GetValueAsUnsigned(0));
174+
if (auto count_sp = ctrl_sp->GetChildMemberWithName("__shared_owners_")) {
175+
bool success;
176+
uint64_t count = count_sp->GetValueAsUnsigned(0, &success);
177+
if (!success)
178+
return false;
179+
180+
stream.Printf(" strong=%" PRIu64, count + 1);
181+
}
174182

175-
if (weakcount_sp)
176-
stream.Printf(" weak=%" PRIu64, 1 + weakcount_sp->GetValueAsUnsigned(0));
183+
if (auto weak_count_sp =
184+
ctrl_sp->GetChildMemberWithName("__shared_weak_owners_")) {
185+
bool success;
186+
uint64_t count = weak_count_sp->GetValueAsUnsigned(0, &success);
187+
if (!success)
188+
return false;
189+
190+
stream.Printf(" weak=%" PRIu64, count + 1);
191+
}
177192

178193
return true;
179194
}

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/shared_ptr/TestDataFormatterLibcxxSharedPtr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ def test_shared_ptr_variables(self):
9191
self.expect_var_path("sp_user->id", type="int", value="30")
9292
self.expect_var_path("sp_user->name", type="std::string", summary='"steph"')
9393

94+
#valobj = self.expect_var_path(
95+
# "si",
96+
# type="std::shared_ptr<int> &",
97+
# children=[ValueCheck(name="__ptr_")],
98+
#)
99+
#self.assertRegex(valobj.summary, r"^10( strong=1)? weak=1$")
100+
101+
#valobj = self.expect_var_path(
102+
# "sie",
103+
# type="std::shared_ptr<int> &",
104+
# children=[ValueCheck(name="__ptr_")],
105+
#)
106+
#self.assertRegex(valobj.summary, r"^10( strong=1)? weak=1$")
107+
94108
self.runCmd("settings set target.experimental.use-DIL true")
95109
self.expect_var_path("ptr_node->value", value="1")
96110
self.expect_var_path("ptr_node->next->value", value="2")

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/shared_ptr/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ int main() {
2222
std::shared_ptr<NodeS>(new NodeS{nullptr, 2});
2323
ptr_node = std::shared_ptr<NodeS>(new NodeS{std::move(ptr_node), 1});
2424

25+
// Construct empty shared_ptr with non-null control field.
26+
std::shared_ptr<int> si(new int(47));
27+
std::shared_ptr<int> sie(si, nullptr);
28+
2529
return 0; // break here
2630
}

0 commit comments

Comments
 (0)