Skip to content

Commit c7eb1b0

Browse files
committed
[lldb] Consult summary provider before printing children of root references
When printing the root of a value, if it's a reference its children are unconditionally printed - in contrast to pointers whose children are only printed if a sufficient pointer depth is given. However, the children are printed even when there's a summary provider that says not to. If a summary provider exists, this change consults it to determine if children should be printed. For example, given a variable of type `std::string &`, this change has the following effect: Before: ``` (lldb) p string_ref (std::string &) string_ref = "one two three four five six seven eight nine ten": { __r_ = { std::__1::__compressed_pair_elem<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, 0, false> = { __value_ = { = { __l = (__data_ = "one two three four five six seven eight nine ten", __size_ = 48, __cap_ = 64, __is_long_ = 1) __s = (__data_ = "@\0p\U00000001\0`\0\00\0\0\0\0\0\0\0@", __padding_ = "\x80t<", __size_ = '\0', __is_long_ = '\x01') __r = { __words ={...} } } } } } } ``` After: ``` (lldb) p string_ref (std::string &) string_ref = "one two three four five six seven eight nine ten" ``` rdar://73248786 Differential Revision: https://reviews.llvm.org/D151748
1 parent 397f2e9 commit c7eb1b0

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

lldb/source/DataFormatters/ValueObjectPrinter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,13 @@ bool ValueObjectPrinter::ShouldPrintChildren(
516516
if (m_options.m_pointer_as_array)
517517
return true;
518518

519-
TypeSummaryImpl *entry = GetSummaryFormatter();
520-
521519
if (m_options.m_use_objc)
522520
return false;
523521

522+
bool print_children = true;
523+
if (TypeSummaryImpl *type_summary = GetSummaryFormatter())
524+
print_children = type_summary->DoesPrintChildren(m_valobj);
525+
524526
if (is_failed_description || !HasReachedMaximumDepth()) {
525527
// We will show children for all concrete types. We won't show pointer
526528
// contents unless a pointer depth has been specified. We won't reference
@@ -538,7 +540,7 @@ bool ValueObjectPrinter::ShouldPrintChildren(
538540

539541
const bool is_root_level = m_curr_depth == 0;
540542

541-
if (is_ref && is_root_level) {
543+
if (is_ref && is_root_level && print_children) {
542544
// If this is the root object (depth is zero) that we are showing and
543545
// it is a reference, and no pointer depth has been supplied print out
544546
// what it references. Don't do this at deeper depths otherwise we can
@@ -549,7 +551,7 @@ bool ValueObjectPrinter::ShouldPrintChildren(
549551
return curr_ptr_depth.CanAllowExpansion();
550552
}
551553

552-
return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
554+
return print_children || m_summary.empty();
553555
}
554556
return false;
555557
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
def test(self):
9+
self.build()
10+
lldbutil.run_to_source_breakpoint(
11+
self, "break here", lldb.SBFileSpec("main.cpp")
12+
)
13+
14+
self.dbg.HandleCommand(
15+
f"type summary add --expand -s 'some summary' SummaryAndChildren"
16+
)
17+
self.dbg.HandleCommand(f"type summary add -s 'some summary' SummaryOnly")
18+
19+
self.expect(
20+
"v summary_and_children_ref", substrs=["some summary", "child = 30"]
21+
)
22+
self.expect(
23+
"v summary_only_ref", patterns=["some summary", "(?s)^(?!.*child = )"]
24+
)
25+
self.expect(
26+
"v children_only_ref", patterns=["(?s)^(?!.*some summary)", "child = 30"]
27+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
3+
struct SummaryAndChildren {
4+
int child = 30;
5+
};
6+
7+
struct SummaryOnly {
8+
int child = 30;
9+
};
10+
11+
struct ChildrenOnly {
12+
int child = 30;
13+
};
14+
15+
int main() {
16+
SummaryAndChildren summary_and_children;
17+
SummaryOnly summary_only;
18+
ChildrenOnly children_only;
19+
auto &summary_and_children_ref = summary_and_children;
20+
auto &summary_only_ref = summary_only;
21+
auto &children_only_ref = children_only;
22+
printf("break here\n");
23+
return 0;
24+
}

0 commit comments

Comments
 (0)