Skip to content

Commit 6fec6a9

Browse files
authored
[lldb][Formatters] Make libc++ and libstdc++ std::shared_ptr formatters consistent with each other (#147165)
This patch adjusts the libcxx and libstdcxx std::shared_ptr formatters to look the same. Changes to libcxx: * Now creates a synthetic child called `pointer` (like we already do for `std::unique_ptr`) Changes to libstdcxx: * When asked to dereference the pointer, cast the type of the result ValueObject to the element type (which we get from the template argument to std::shared_ptr). Before: ``` (std::__shared_ptr<int, __gnu_cxx::_S_atomic>::element_type) *foo = 123 ``` After: ``` (int) *foo = 123 ``` Tested in #147141
1 parent d06956e commit 6fec6a9

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ lldb_private::formatters::LibCxxVectorIteratorSyntheticFrontEndCreator(
238238

239239
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
240240
LibcxxSharedPtrSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
241-
: SyntheticChildrenFrontEnd(*valobj_sp), m_cntrl(nullptr) {
241+
: SyntheticChildrenFrontEnd(*valobj_sp), m_cntrl(nullptr),
242+
m_ptr_obj(nullptr) {
242243
if (valobj_sp)
243244
Update();
244245
}
@@ -251,28 +252,25 @@ llvm::Expected<uint32_t> lldb_private::formatters::
251252
lldb::ValueObjectSP
252253
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
253254
uint32_t idx) {
254-
if (!m_cntrl)
255+
if (!m_cntrl || !m_ptr_obj)
255256
return lldb::ValueObjectSP();
256257

257258
ValueObjectSP valobj_sp = m_backend.GetSP();
258259
if (!valobj_sp)
259260
return lldb::ValueObjectSP();
260261

261262
if (idx == 0)
262-
return valobj_sp->GetChildMemberWithName("__ptr_");
263+
return m_ptr_obj->GetSP();
263264

264265
if (idx == 1) {
265-
if (auto ptr_sp = valobj_sp->GetChildMemberWithName("__ptr_")) {
266-
Status status;
267-
auto value_type_sp =
268-
valobj_sp->GetCompilerType()
269-
.GetTypeTemplateArgument(0).GetPointerType();
270-
ValueObjectSP cast_ptr_sp = ptr_sp->Cast(value_type_sp);
271-
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
272-
if (status.Success()) {
273-
return value_sp;
274-
}
275-
}
266+
Status status;
267+
auto value_type_sp = valobj_sp->GetCompilerType()
268+
.GetTypeTemplateArgument(0)
269+
.GetPointerType();
270+
ValueObjectSP cast_ptr_sp = m_ptr_obj->Cast(value_type_sp);
271+
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
272+
if (status.Success())
273+
return value_sp;
276274
}
277275

278276
return lldb::ValueObjectSP();
@@ -281,6 +279,7 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
281279
lldb::ChildCacheState
282280
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
283281
m_cntrl = nullptr;
282+
m_ptr_obj = nullptr;
284283

285284
ValueObjectSP valobj_sp = m_backend.GetSP();
286285
if (!valobj_sp)
@@ -290,6 +289,12 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
290289
if (!target_sp)
291290
return lldb::ChildCacheState::eRefetch;
292291

292+
auto ptr_obj_sp = valobj_sp->GetChildMemberWithName("__ptr_");
293+
if (!ptr_obj_sp)
294+
return lldb::ChildCacheState::eRefetch;
295+
296+
m_ptr_obj = ptr_obj_sp->Clone(ConstString("pointer")).get();
297+
293298
lldb::ValueObjectSP cntrl_sp(valobj_sp->GetChildMemberWithName("__cntrl_"));
294299

295300
m_cntrl = cntrl_sp.get(); // need to store the raw pointer to avoid a circular
@@ -300,10 +305,12 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
300305
llvm::Expected<size_t>
301306
lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
302307
GetIndexOfChildWithName(ConstString name) {
303-
if (name == "__ptr_")
308+
if (name == "__ptr_" || name == "pointer")
304309
return 0;
305-
if (name == "$$dereference$$")
310+
311+
if (name == "object" || name == "$$dereference$$")
306312
return 1;
313+
307314
return llvm::createStringError("Type has no child named '%s'",
308315
name.AsCString());
309316
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class LibcxxSharedPtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
105105

106106
private:
107107
ValueObject *m_cntrl;
108+
ValueObject *m_ptr_obj;
108109
};
109110

110111
class LibcxxUniquePtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class LibStdcppSharedPtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
7777
// objects are only destroyed when every shared pointer to any of them
7878
// is destroyed, so we must not store a shared pointer to any ValueObject
7979
// derived from our backend ValueObject (since we're in the same cluster).
80-
ValueObject* m_ptr_obj = nullptr; // Underlying pointer (held, not owned)
81-
ValueObject* m_obj_obj = nullptr; // Underlying object (held, not owned)
80+
ValueObject *m_ptr_obj = nullptr; // Underlying pointer (held, not owned)
8281
};
8382

8483
} // end of anonymous namespace
@@ -266,15 +265,20 @@ LibStdcppSharedPtrSyntheticFrontEnd::GetChildAtIndex(uint32_t idx) {
266265

267266
if (idx == 0)
268267
return m_ptr_obj->GetSP();
268+
269269
if (idx == 1) {
270-
if (m_ptr_obj && !m_obj_obj) {
271-
Status error;
272-
ValueObjectSP obj_obj = m_ptr_obj->Dereference(error);
273-
if (error.Success())
274-
m_obj_obj = obj_obj->Clone(ConstString("object")).get();
275-
}
276-
if (m_obj_obj)
277-
return m_obj_obj->GetSP();
270+
ValueObjectSP valobj_sp = m_backend.GetSP();
271+
if (!valobj_sp)
272+
return nullptr;
273+
274+
Status status;
275+
auto value_type_sp = valobj_sp->GetCompilerType()
276+
.GetTypeTemplateArgument(0)
277+
.GetPointerType();
278+
ValueObjectSP cast_ptr_sp = m_ptr_obj->Cast(value_type_sp);
279+
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
280+
if (status.Success())
281+
return value_sp;
278282
}
279283
return lldb::ValueObjectSP();
280284
}
@@ -293,7 +297,6 @@ lldb::ChildCacheState LibStdcppSharedPtrSyntheticFrontEnd::Update() {
293297
return lldb::ChildCacheState::eRefetch;
294298

295299
m_ptr_obj = ptr_obj_sp->Clone(ConstString("pointer")).get();
296-
m_obj_obj = nullptr;
297300

298301
return lldb::ChildCacheState::eRefetch;
299302
}
@@ -302,8 +305,10 @@ llvm::Expected<size_t>
302305
LibStdcppSharedPtrSyntheticFrontEnd::GetIndexOfChildWithName(ConstString name) {
303306
if (name == "pointer")
304307
return 0;
308+
305309
if (name == "object" || name == "$$dereference$$")
306310
return 1;
311+
307312
return llvm::createStringError("Type has no child named '%s'",
308313
name.AsCString());
309314
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_shared_ptr_variables(self):
2222
"sp_empty",
2323
type="std::shared_ptr<int>",
2424
summary="nullptr",
25-
children=[ValueCheck(name="__ptr_")],
25+
children=[ValueCheck(name="pointer")],
2626
)
2727
self.assertEqual(
2828
valobj.child[0].GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS), 0
@@ -35,23 +35,23 @@ def test_shared_ptr_variables(self):
3535
valobj = self.expect_var_path(
3636
"sp_int",
3737
type="std::shared_ptr<int>",
38-
children=[ValueCheck(name="__ptr_")],
38+
children=[ValueCheck(name="pointer")],
3939
)
4040
self.assertRegex(valobj.summary, r"^10( strong=1)? weak=0$")
4141
self.assertNotEqual(valobj.child[0].unsigned, 0)
4242

4343
valobj = self.expect_var_path(
4444
"sp_int_ref",
4545
type="std::shared_ptr<int> &",
46-
children=[ValueCheck(name="__ptr_")],
46+
children=[ValueCheck(name="pointer")],
4747
)
4848
self.assertRegex(valobj.summary, r"^10( strong=1)? weak=0$")
4949
self.assertNotEqual(valobj.child[0].unsigned, 0)
5050

5151
valobj = self.expect_var_path(
5252
"sp_int_ref_ref",
5353
type="std::shared_ptr<int> &&",
54-
children=[ValueCheck(name="__ptr_")],
54+
children=[ValueCheck(name="pointer")],
5555
)
5656
self.assertRegex(valobj.summary, r"^10( strong=1)? weak=0$")
5757
self.assertNotEqual(valobj.child[0].unsigned, 0)
@@ -66,7 +66,7 @@ def test_shared_ptr_variables(self):
6666
valobj = self.expect_var_path(
6767
"sp_str",
6868
type="std::shared_ptr<" + string_type + ">",
69-
children=[ValueCheck(name="__ptr_", summary='"hello"')],
69+
children=[ValueCheck(name="pointer", summary='"hello"')],
7070
)
7171
self.assertRegex(valobj.summary, r'^"hello"( strong=1)? weak=0$')
7272

@@ -85,7 +85,7 @@ def test_shared_ptr_variables(self):
8585
ValueCheck(name="name", summary='"steph"'),
8686
],
8787
)
88-
self.assertEqual(str(valobj), '(User) *__ptr_ = (id = 30, name = "steph")')
88+
self.assertEqual(str(valobj), '(User) *pointer = (id = 30, name = "steph")')
8989

9090
self.expect_var_path("sp_user->id", type="int", value="30")
9191
self.expect_var_path("sp_user->name", type="std::string", summary='"steph"')

0 commit comments

Comments
 (0)