Skip to content

Commit 9ba3d7a

Browse files
committed
[lldb] Add ability to hide the root name of a value
When printing a value, allow the root value's name to be elided, without omiting the names of child values. At the API level, this adds `SetHideRootName()`, which joins the existing `SetHideName()` function. This functionality is used by `dwim-print` and `expression`. Fixes an issue identified by @jgorbe in https://reviews.llvm.org/D145609. Differential Revision: https://reviews.llvm.org/D146783 (cherry picked from commit 23349d8)
1 parent 25f90c6 commit 9ba3d7a

File tree

8 files changed

+50
-12
lines changed

8 files changed

+50
-12
lines changed

lldb/include/lldb/DataFormatters/DumpValueObjectOptions.h

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

106106
DumpValueObjectOptions &SetHideRootType(bool hide_root_type = false);
107107

108+
DumpValueObjectOptions &SetHideRootName(bool hide_root_name);
109+
108110
DumpValueObjectOptions &SetHideName(bool hide_name = false);
109111

110112
DumpValueObjectOptions &SetHideValue(bool hide_value = false);
@@ -146,6 +148,7 @@ class DumpValueObjectOptions {
146148
bool m_show_location : 1;
147149
bool m_use_objc : 1;
148150
bool m_hide_root_type : 1;
151+
bool m_hide_root_name : 1;
149152
bool m_hide_name : 1;
150153
bool m_hide_value : 1;
151154
bool m_run_validator : 1;

lldb/include/lldb/DataFormatters/ValueObjectPrinter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class ValueObjectPrinter {
120120
bool HasReachedMaximumDepth();
121121

122122
private:
123+
bool ShouldShowName() const;
124+
123125
ValueObject *m_orig_valobj;
124126
ValueObject *m_valobj;
125127
Stream *m_stream;

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef command,
8484

8585
DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions(
8686
m_expr_options.m_verbosity, m_format_options.GetFormat());
87-
dump_options.SetHideName(eval_options.GetSuppressPersistentResult());
87+
dump_options.SetHideRootName(eval_options.GetSuppressPersistentResult());
8888

8989
StackFrame *frame = m_exe_ctx.GetFramePtr();
9090

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
482482

483483
DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
484484
m_command_options.m_verbosity, format));
485-
options.SetHideName(eval_options.GetSuppressPersistentResult());
485+
options.SetHideRootName(eval_options.GetSuppressPersistentResult());
486486
options.SetVariableFormatDisplayLanguage(
487487
result_valobj_sp->GetPreferredDisplayLanguage());
488488

lldb/source/DataFormatters/DumpValueObjectOptions.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ DumpValueObjectOptions::DumpValueObjectOptions()
1919
m_decl_printing_helper(), m_pointer_as_array(), m_use_synthetic(true),
2020
m_scope_already_checked(false), m_flat_output(false), m_ignore_cap(false),
2121
m_show_types(false), m_show_location(false), m_use_objc(false),
22-
m_hide_root_type(false), m_hide_name(false), m_hide_value(false),
23-
m_run_validator(false), m_use_type_display_name(true),
24-
m_allow_oneliner_mode(true), m_hide_pointer_value(false),
25-
m_reveal_empty_aggregates(true) {}
22+
m_hide_root_type(false), m_hide_root_name(false), m_hide_name(false),
23+
m_hide_value(false), m_run_validator(false),
24+
m_use_type_display_name(true), m_allow_oneliner_mode(true),
25+
m_hide_pointer_value(false), m_reveal_empty_aggregates(true) {}
2626

2727
DumpValueObjectOptions::DumpValueObjectOptions(ValueObject &valobj)
2828
: DumpValueObjectOptions() {
@@ -143,6 +143,12 @@ DumpValueObjectOptions::SetHideRootType(bool hide_root_type) {
143143
return *this;
144144
}
145145

146+
DumpValueObjectOptions &
147+
DumpValueObjectOptions::SetHideRootName(bool hide_root_name) {
148+
m_hide_root_name = hide_root_name;
149+
return *this;
150+
}
151+
146152
DumpValueObjectOptions &DumpValueObjectOptions::SetHideName(bool hide_name) {
147153
m_hide_name = hide_name;
148154
return *this;

lldb/source/DataFormatters/ValueObjectPrinter.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void ValueObjectPrinter::PrintDecl() {
275275

276276
StreamString varName;
277277

278-
if (!m_options.m_hide_name) {
278+
if (ShouldShowName()) {
279279
if (m_options.m_flat_output)
280280
m_valobj->GetExpressionPath(varName);
281281
else
@@ -314,7 +314,7 @@ void ValueObjectPrinter::PrintDecl() {
314314
m_stream->Printf("(%s) ", typeName.GetData());
315315
if (!varName.Empty())
316316
m_stream->Printf("%s =", varName.GetData());
317-
else if (!m_options.m_hide_name)
317+
else if (ShouldShowName())
318318
m_stream->Printf(" =");
319319
}
320320
}
@@ -437,7 +437,7 @@ bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
437437
if (m_options.m_hide_pointer_value &&
438438
IsPointerValue(m_valobj->GetCompilerType())) {
439439
} else {
440-
if (!m_options.m_hide_name)
440+
if (ShouldShowName())
441441
m_stream->PutChar(' ');
442442
m_stream->PutCString(m_value);
443443
value_printed = true;
@@ -459,7 +459,7 @@ bool ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
459459
// let's avoid the overly verbose no description error for a nil thing
460460
if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
461461
(!m_options.m_pointer_as_array)) {
462-
if (!m_options.m_hide_value || !m_options.m_hide_name)
462+
if (!m_options.m_hide_value || ShouldShowName())
463463
m_stream->Printf(" ");
464464
const char *object_desc = nullptr;
465465
if (value_printed || summary_printed)
@@ -587,8 +587,14 @@ void ValueObjectPrinter::PrintChildrenPreamble() {
587587
if (ShouldPrintValueObject())
588588
m_stream->EOL();
589589
} else {
590-
if (ShouldPrintValueObject())
591-
m_stream->PutCString(IsRef() ? ": {\n" : " {\n");
590+
if (ShouldPrintValueObject()) {
591+
if (IsRef()) {
592+
m_stream->PutCString(": ");
593+
} else if (ShouldShowName()) {
594+
m_stream->PutChar(' ');
595+
}
596+
m_stream->PutCString("{\n");
597+
}
592598
m_stream->IndentMore();
593599
}
594600
}
@@ -841,3 +847,9 @@ void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
841847
bool ValueObjectPrinter::HasReachedMaximumDepth() {
842848
return m_curr_depth >= m_options.m_max_depth;
843849
}
850+
851+
bool ValueObjectPrinter::ShouldShowName() const {
852+
if (m_curr_depth == 0)
853+
return !m_options.m_hide_root_name && !m_options.m_hide_name;
854+
return !m_options.m_hide_name;
855+
}

lldb/test/API/commands/dwim-print/TestDWIMPrint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ def test_expression_language(self):
112112
lldbutil.run_to_name_breakpoint(self, "main")
113113
self._expect_cmd(f"dwim-print -l c++ -- argc", "frame variable")
114114
self._expect_cmd(f"dwim-print -l c++ -- argc + 1", "expression")
115+
116+
def test_nested_values(self):
117+
"""Test dwim-print with nested values (structs, etc)."""
118+
self.build()
119+
lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.c"))
120+
self.runCmd("settings set auto-one-line-summaries false")
121+
self._expect_cmd(f"dwim-print s", "frame variable")
122+
self._expect_cmd(f"dwim-print (struct Structure)s", "expression")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
struct Structure {
2+
int number;
3+
};
4+
15
int main(int argc, char **argv) {
6+
struct Structure s;
7+
s.number = 30;
8+
// break here
29
return 0;
310
}

0 commit comments

Comments
 (0)