Skip to content

Commit 65d444b

Browse files
[lldb][nfc] Factor out repeated code in DWIM Print (#85669)
The code that prints ValueObjects is duplicated across two different cases of the dwim-print command, and a subsequent commit will add a third case. As such, this commit factors out the common code into a lambda. A free function was considered, but there is too much function-local context required in that. We also reword some of the comments so that they stop counting cases, making it easier to add other cases later.
1 parent 8b8e1ad commit 65d444b

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
129129
}
130130
};
131131

132+
// Dump `valobj` according to whether `po` was requested or not.
133+
auto dump_val_object = [&](ValueObject &valobj) {
134+
if (is_po) {
135+
StreamString temp_result_stream;
136+
valobj.Dump(temp_result_stream, dump_options);
137+
llvm::StringRef output = temp_result_stream.GetString();
138+
maybe_add_hint(output);
139+
result.GetOutputStream() << output;
140+
} else {
141+
valobj.Dump(result.GetOutputStream(), dump_options);
142+
}
143+
};
144+
132145
// First, try `expr` as the name of a frame variable.
133146
if (frame) {
134147
auto valobj_sp = frame->FindVariable(ConstString(expr));
@@ -146,15 +159,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
146159
flags, expr);
147160
}
148161

149-
if (is_po) {
150-
StreamString temp_result_stream;
151-
valobj_sp->Dump(temp_result_stream, dump_options);
152-
llvm::StringRef output = temp_result_stream.GetString();
153-
maybe_add_hint(output);
154-
result.GetOutputStream() << output;
155-
} else {
156-
valobj_sp->Dump(result.GetOutputStream(), dump_options);
157-
}
162+
dump_val_object(*valobj_sp);
158163
result.SetStatus(eReturnStatusSuccessFinishResult);
159164
return;
160165
}
@@ -165,7 +170,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
165170
if (auto *state = target.GetPersistentExpressionStateForLanguage(language))
166171
if (auto var_sp = state->GetVariable(expr))
167172
if (auto valobj_sp = var_sp->GetValueObject()) {
168-
valobj_sp->Dump(result.GetOutputStream(), dump_options);
173+
dump_val_object(*valobj_sp);
169174
result.SetStatus(eReturnStatusSuccessFinishResult);
170175
return;
171176
}
@@ -196,17 +201,8 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
196201
expr);
197202
}
198203

199-
if (valobj_sp->GetError().GetError() != UserExpression::kNoResult) {
200-
if (is_po) {
201-
StreamString temp_result_stream;
202-
valobj_sp->Dump(temp_result_stream, dump_options);
203-
llvm::StringRef output = temp_result_stream.GetString();
204-
maybe_add_hint(output);
205-
result.GetOutputStream() << output;
206-
} else {
207-
valobj_sp->Dump(result.GetOutputStream(), dump_options);
208-
}
209-
}
204+
if (valobj_sp->GetError().GetError() != UserExpression::kNoResult)
205+
dump_val_object(*valobj_sp);
210206

211207
if (suppress_result)
212208
if (auto result_var_sp =

0 commit comments

Comments
 (0)