Skip to content

Commit 3199d3e

Browse files
committed
[lldb] Update dwim-print to show expanded objc instances
When printing an ObjC object, which is a pointer, lldb has handled it like it treats any other pointer, printing only pointer address and class name. The object is not expanded, and its children are not shown. This change updates the dwim-print to print expanded objc pointers - it is assumed that's what the user meant. Note that this is currently possible using the `--ptr-depth`/`-P` flag, but the default is 0. With this change, when dwim-print prints objc objects, the default is effectively changed to 1.
1 parent 0a6d797 commit 3199d3e

File tree

7 files changed

+53
-7
lines changed

7 files changed

+53
-7
lines changed

lldb/include/lldb/DataFormatters/DumpValueObjectOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class DumpValueObjectOptions {
125125

126126
DumpValueObjectOptions &SetRevealEmptyAggregates(bool reveal = true);
127127

128+
DumpValueObjectOptions &SetExpandPointerTypeFlags(unsigned flags);
129+
128130
DumpValueObjectOptions &SetElementCount(uint32_t element_count = 0);
129131

130132
DumpValueObjectOptions &
@@ -142,6 +144,7 @@ class DumpValueObjectOptions {
142144
DeclPrintingHelper m_decl_printing_helper;
143145
ChildPrintingDecider m_child_printing_decider;
144146
PointerAsArraySettings m_pointer_as_array;
147+
unsigned m_expand_ptr_type_flags = 0;
145148
bool m_use_synthetic : 1;
146149
bool m_scope_already_checked : 1;
147150
bool m_flat_output : 1;

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
8787

8888
DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions(
8989
m_expr_options.m_verbosity, m_format_options.GetFormat());
90-
dump_options.SetHideRootName(suppress_result);
90+
dump_options.SetHideRootName(suppress_result)
91+
.SetExpandPointerTypeFlags(lldb::eTypeIsObjC);
9192

9293
bool is_po = m_varobj_options.use_objc;
9394

lldb/source/DataFormatters/DumpValueObjectOptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ DumpValueObjectOptions::SetRevealEmptyAggregates(bool reveal) {
201201
return *this;
202202
}
203203

204+
DumpValueObjectOptions &
205+
DumpValueObjectOptions::SetExpandPointerTypeFlags(unsigned flags) {
206+
m_expand_ptr_type_flags = flags;
207+
return *this;
208+
}
209+
204210
DumpValueObjectOptions &
205211
DumpValueObjectOptions::SetElementCount(uint32_t element_count) {
206212
m_pointer_as_array = PointerAsArraySettings(element_count);

lldb/source/DataFormatters/ValueObjectPrinter.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,14 @@ bool ValueObjectPrinter::ShouldPrintChildren(
554554
return false;
555555

556556
const bool is_root_level = m_curr_depth == 0;
557-
558-
if (is_ref && is_root_level && print_children) {
559-
// If this is the root object (depth is zero) that we are showing and
560-
// it is a reference, and no pointer depth has been supplied print out
561-
// what it references. Don't do this at deeper depths otherwise we can
562-
// end up with infinite recursion...
557+
const bool is_expanded_ptr =
558+
is_ptr && m_type_flags.Test(m_options.m_expand_ptr_type_flags);
559+
560+
if ((is_ref || is_expanded_ptr) && is_root_level && print_children) {
561+
// If this is the root object (depth is zero) that we are showing and it
562+
// is either a reference or a preferred type of pointer, then print it.
563+
// Don't do this at deeper depths otherwise we can end up with infinite
564+
// recursion...
563565
return true;
564566
}
565567

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OBJC_SOURCES := main.m
2+
3+
include Makefile.rules
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Test dwim-print with objc instances.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test.decorators import *
8+
import lldbsuite.test.lldbutil as lldbutil
9+
10+
11+
class TestCase(TestBase):
12+
13+
def test(self):
14+
self.build()
15+
lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.m"))
16+
self.expect("dwim-print obj", substrs=["_number = 15"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@interface Object : NSObject
4+
@property(nonatomic) int number;
5+
@end
6+
7+
@implementation Object
8+
@end
9+
10+
int main(int argc, char **argv) {
11+
Object *obj = [Object new];
12+
obj.number = 15;
13+
puts("break here");
14+
return 0;
15+
}

0 commit comments

Comments
 (0)