Skip to content

Commit 870e4cc

Browse files
committed
[lldb][Formatters] Consistently unwrap pointer element_type in std::shared_ptr formatters (llvm#147340)
Follow-up to llvm#147165 (review) Currently when we explicitly dereference a std::shared_ptr, both the libstdc++ and libc++ formatters will cast the type of the synthetic pointer child to whatever the `std::shared_ptr::element_type` is aliased to. E.g., ``` (lldb) v p (std::shared_ptr<int>) p = 10 strong=1 weak=0 { pointer = 0x000000010016c6a0 } (lldb) v *p (int) *p = 10 ``` However, when we print (or dereference) `p.pointer`, the type devolves to something less user-friendly: ``` (lldb) v p.pointer (std::shared_ptr<int>::element_type *) p.pointer = 0x000000010016c6a0 (lldb) v *p.pointer (std::shared_ptr<int>::element_type) *p.pointer = 10 ``` This patch changes both formatters to store the casted type. Then `GetChildAtIndex` will consistently use the unwrapped type. (cherry picked from commit f999918)
1 parent 1034c77 commit 870e4cc

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
1212
CPlusPlusLanguage.cpp
1313
CPlusPlusNameParser.cpp
1414
CxxStringTypes.cpp
15+
Generic.cpp
1516
GenericBitset.cpp
1617
GenericOptional.cpp
1718
LibCxx.cpp
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Generic.cpp ------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#include "Generic.h"
10+
11+
lldb::ValueObjectSP lldb_private::formatters::GetDesugaredSmartPointerValue(
12+
ValueObject &ptr, ValueObject &container) {
13+
auto container_type = container.GetCompilerType().GetNonReferenceType();
14+
if (!container_type)
15+
return nullptr;
16+
17+
auto arg = container_type.GetTypeTemplateArgument(0);
18+
if (!arg)
19+
return nullptr;
20+
21+
return ptr.Cast(arg.GetPointerType());
22+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace formatters {
1919
bool GenericOptionalSummaryProvider(ValueObject &valobj, Stream &stream,
2020
const TypeSummaryOptions &options);
2121

22+
/// Return the ValueObjectSP of the underlying pointer member whose type
23+
/// is a desugared 'std::shared_ptr::element_type *'.
24+
lldb::ValueObjectSP GetDesugaredSmartPointerValue(ValueObject &ptr,
25+
ValueObject &container);
26+
2227
} // namespace formatters
2328
} // namespace lldb_private
2429

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "lldb/ValueObject/ValueObject.h"
2525
#include "lldb/ValueObject/ValueObjectConstResult.h"
2626

27+
#include "Plugins/Language/CPlusPlus/CxxStringTypes.h"
28+
#include "Plugins/Language/CPlusPlus/Generic.h"
2729
#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
2830
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
2931
#include "lldb/lldb-enumerations.h"
@@ -263,11 +265,7 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
263265

264266
if (idx == 1) {
265267
Status status;
266-
auto value_type_sp = valobj_sp->GetCompilerType()
267-
.GetTypeTemplateArgument(0)
268-
.GetPointerType();
269-
ValueObjectSP cast_ptr_sp = m_ptr_obj->Cast(value_type_sp);
270-
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
268+
ValueObjectSP value_sp = m_ptr_obj->Dereference(status);
271269
if (status.Success())
272270
return value_sp;
273271
}
@@ -292,7 +290,11 @@ lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
292290
if (!ptr_obj_sp)
293291
return lldb::ChildCacheState::eRefetch;
294292

295-
m_ptr_obj = ptr_obj_sp->Clone(ConstString("pointer")).get();
293+
auto cast_ptr_sp = GetDesugaredSmartPointerValue(*ptr_obj_sp, *valobj_sp);
294+
if (!cast_ptr_sp)
295+
return lldb::ChildCacheState::eRefetch;
296+
297+
m_ptr_obj = cast_ptr_sp->Clone(ConstString("pointer")).get();
296298

297299
lldb::ValueObjectSP cntrl_sp(valobj_sp->GetChildMemberWithName("__cntrl_"));
298300

@@ -308,7 +310,7 @@ bool lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
308310

309311
size_t lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
310312
GetIndexOfChildWithName(ConstString name) {
311-
if (name == "__ptr_" || name == "pointer")
313+
if (name == "pointer")
312314
return 0;
313315

314316
if (name == "object" || name == "$$dereference$$")

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "LibStdcpp.h"
1010
#include "LibCxx.h"
1111

12+
#include "Plugins/Language/CPlusPlus/Generic.h"
1213
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1314
#include "lldb/DataFormatters/FormattersHelpers.h"
1415
#include "lldb/DataFormatters/StringPrinter.h"
@@ -392,11 +393,7 @@ LibStdcppSharedPtrSyntheticFrontEnd::GetChildAtIndex(uint32_t idx) {
392393
return nullptr;
393394

394395
Status status;
395-
auto value_type_sp = valobj_sp->GetCompilerType()
396-
.GetTypeTemplateArgument(0)
397-
.GetPointerType();
398-
ValueObjectSP cast_ptr_sp = m_ptr_obj->Cast(value_type_sp);
399-
ValueObjectSP value_sp = cast_ptr_sp->Dereference(status);
396+
ValueObjectSP value_sp = m_ptr_obj->Dereference(status);
400397
if (status.Success())
401398
return value_sp;
402399
}
@@ -416,7 +413,11 @@ lldb::ChildCacheState LibStdcppSharedPtrSyntheticFrontEnd::Update() {
416413
if (!ptr_obj_sp)
417414
return lldb::ChildCacheState::eRefetch;
418415

419-
m_ptr_obj = ptr_obj_sp->Clone(ConstString("pointer")).get();
416+
auto cast_ptr_sp = GetDesugaredSmartPointerValue(*ptr_obj_sp, *valobj_sp);
417+
if (!cast_ptr_sp)
418+
return lldb::ChildCacheState::eRefetch;
419+
420+
m_ptr_obj = cast_ptr_sp->Clone(ConstString("pointer")).get();
420421

421422
return lldb::ChildCacheState::eRefetch;
422423
}

lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/shared_ptr/TestDataFormatterStdSharedPtr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def do_test(self):
9999
"wie", type="std::weak_ptr<int>", summary="nullptr strong=2 weak=2"
100100
)
101101

102+
self.expect_var_path("si.pointer", type="int *")
103+
self.expect_var_path("*si.pointer", type="int", value="47")
104+
self.expect_var_path("si.object", type="int", value="47")
105+
102106
@add_test_categories(["libc++"])
103107
def test_libcxx(self):
104108
self.build(dictionary={"USE_LIBCPP": 1})

0 commit comments

Comments
 (0)