Skip to content

Commit 920b46e

Browse files
committed
[lldb] Add expression command options in dwim-print
Adopt `expression`'s options in `dwim-print`. This is primarily added to support the `--language`/`-l` flag. Differential Revision: https://reviews.llvm.org/D144114
1 parent e49b93e commit 920b46e

File tree

7 files changed

+97
-53
lines changed

7 files changed

+97
-53
lines changed

lldb/include/lldb/Interpreter/Options.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "lldb/lldb-private.h"
2121

2222
#include "llvm/ADT/ArrayRef.h"
23+
#include "llvm/ADT/StringRef.h"
2324

2425
namespace lldb_private {
2526

@@ -290,6 +291,21 @@ class OptionGroupOptions : public Options {
290291
/// copying the option definition.
291292
void Append(OptionGroup *group, uint32_t src_mask, uint32_t dst_mask);
292293

294+
/// Append selected options from a OptionGroup class.
295+
///
296+
/// Append the subset of options from \a group, where the "long_option" value
297+
/// is _not_ in \a exclude_long_options.
298+
///
299+
/// \param[in] group
300+
/// A group of options to take option values from and copy their
301+
/// definitions into this class.
302+
///
303+
/// \param[in] exclude_long_options
304+
/// A set of long option strings which indicate which option values values
305+
/// to limit from \a group.
306+
void Append(OptionGroup *group,
307+
llvm::ArrayRef<llvm::StringRef> exclude_long_options);
308+
293309
void Finalize();
294310

295311
bool DidFinalize() { return m_did_finalize; }

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
3636
OptionGroupFormat::OPTION_GROUP_FORMAT |
3737
OptionGroupFormat::OPTION_GROUP_GDB_FMT,
3838
LLDB_OPT_SET_1);
39+
StringRef exclude_expr_options[] = {"debug", "top-level"};
40+
m_option_group.Append(&m_expr_options, exclude_expr_options);
3941
m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4042
m_option_group.Finalize();
4143
}
@@ -57,11 +59,11 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef command,
5759
m_cmd_name);
5860
return false;
5961
}
62+
6063
auto verbosity = GetDebugger().GetDWIMPrintVerbosity();
6164

6265
DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions(
63-
eLanguageRuntimeDescriptionDisplayVerbosityFull,
64-
m_format_options.GetFormat());
66+
m_expr_options.m_verbosity, m_format_options.GetFormat());
6567

6668
// First, try `expr` as the name of a frame variable.
6769
if (StackFrame *frame = m_exe_ctx.GetFramePtr()) {
@@ -87,9 +89,12 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef command,
8789
Target &target = target_ptr ? *target_ptr : GetDummyTarget();
8890

8991
auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
92+
const EvaluateExpressionOptions eval_options =
93+
m_expr_options.GetEvaluateExpressionOptions(target, m_varobj_options);
9094
ValueObjectSP valobj_sp;
91-
if (target.EvaluateExpression(expr, exe_scope, valobj_sp) ==
92-
eExpressionCompleted) {
95+
ExpressionResults expr_result =
96+
target.EvaluateExpression(expr, exe_scope, valobj_sp, eval_options);
97+
if (expr_result == eExpressionCompleted) {
9398
if (verbosity != eDWIMPrintVerbosityNone) {
9499
StringRef flags;
95100
if (args.HasArgs())

lldb/source/Commands/CommandObjectDWIMPrint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTDWIMPRINT_H
1010
#define LLDB_SOURCE_COMMANDS_COMMANDOBJECTDWIMPRINT_H
1111

12+
#include "CommandObjectExpression.h"
1213
#include "lldb/Interpreter/CommandObject.h"
1314
#include "lldb/Interpreter/OptionGroupFormat.h"
1415
#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
@@ -42,6 +43,7 @@ class CommandObjectDWIMPrint : public CommandObjectRaw {
4243
OptionGroupOptions m_option_group;
4344
OptionGroupFormat m_format_options = lldb::eFormatDefault;
4445
OptionGroupValueObjectDisplay m_varobj_options;
46+
CommandObjectExpression::CommandOptions m_expr_options;
4547
};
4648

4749
} // namespace lldb_private

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,48 @@ CommandObjectExpression::CommandOptions::GetDefinitions() {
181181
return llvm::ArrayRef(g_expression_options);
182182
}
183183

184+
EvaluateExpressionOptions
185+
CommandObjectExpression::CommandOptions::GetEvaluateExpressionOptions(
186+
const Target &target, const OptionGroupValueObjectDisplay &display_opts) {
187+
EvaluateExpressionOptions options;
188+
options.SetCoerceToId(display_opts.use_objc);
189+
if (m_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact)
190+
options.SetSuppressPersistentResult(display_opts.use_objc);
191+
options.SetUnwindOnError(unwind_on_error);
192+
options.SetIgnoreBreakpoints(ignore_breakpoints);
193+
options.SetKeepInMemory(true);
194+
options.SetUseDynamic(display_opts.use_dynamic);
195+
options.SetTryAllThreads(try_all_threads);
196+
options.SetDebug(debug);
197+
options.SetLanguage(language);
198+
options.SetExecutionPolicy(
199+
allow_jit ? EvaluateExpressionOptions::default_execution_policy
200+
: lldb_private::eExecutionPolicyNever);
201+
202+
bool auto_apply_fixits;
203+
if (this->auto_apply_fixits == eLazyBoolCalculate)
204+
auto_apply_fixits = target.GetEnableAutoApplyFixIts();
205+
else
206+
auto_apply_fixits = this->auto_apply_fixits == eLazyBoolYes;
207+
208+
options.SetAutoApplyFixIts(auto_apply_fixits);
209+
options.SetRetriesWithFixIts(target.GetNumberOfRetriesWithFixits());
210+
211+
if (top_level)
212+
options.SetExecutionPolicy(eExecutionPolicyTopLevel);
213+
214+
// If there is any chance we are going to stop and want to see what went
215+
// wrong with our expression, we should generate debug info
216+
if (!ignore_breakpoints || !unwind_on_error)
217+
options.SetGenerateDebugInfo(true);
218+
219+
if (timeout > 0)
220+
options.SetTimeout(std::chrono::microseconds(timeout));
221+
else
222+
options.SetTimeout(std::nullopt);
223+
return options;
224+
}
225+
184226
CommandObjectExpression::CommandObjectExpression(
185227
CommandInterpreter &interpreter)
186228
: CommandObjectRaw(interpreter, "expression",
@@ -343,50 +385,6 @@ CanBeUsedForElementCountPrinting(ValueObject &valobj) {
343385
return Status();
344386
}
345387

346-
EvaluateExpressionOptions
347-
CommandObjectExpression::GetEvalOptions(const Target &target) {
348-
EvaluateExpressionOptions options;
349-
options.SetCoerceToId(m_varobj_options.use_objc);
350-
if (m_command_options.m_verbosity ==
351-
eLanguageRuntimeDescriptionDisplayVerbosityCompact)
352-
options.SetSuppressPersistentResult(m_varobj_options.use_objc);
353-
options.SetUnwindOnError(m_command_options.unwind_on_error);
354-
options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints);
355-
options.SetKeepInMemory(true);
356-
options.SetUseDynamic(m_varobj_options.use_dynamic);
357-
options.SetTryAllThreads(m_command_options.try_all_threads);
358-
options.SetDebug(m_command_options.debug);
359-
options.SetLanguage(m_command_options.language);
360-
options.SetExecutionPolicy(
361-
m_command_options.allow_jit
362-
? EvaluateExpressionOptions::default_execution_policy
363-
: lldb_private::eExecutionPolicyNever);
364-
365-
bool auto_apply_fixits;
366-
if (m_command_options.auto_apply_fixits == eLazyBoolCalculate)
367-
auto_apply_fixits = target.GetEnableAutoApplyFixIts();
368-
else
369-
auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes;
370-
371-
options.SetAutoApplyFixIts(auto_apply_fixits);
372-
options.SetRetriesWithFixIts(target.GetNumberOfRetriesWithFixits());
373-
374-
if (m_command_options.top_level)
375-
options.SetExecutionPolicy(eExecutionPolicyTopLevel);
376-
377-
// If there is any chance we are going to stop and want to see what went
378-
// wrong with our expression, we should generate debug info
379-
if (!m_command_options.ignore_breakpoints ||
380-
!m_command_options.unwind_on_error)
381-
options.SetGenerateDebugInfo(true);
382-
383-
if (m_command_options.timeout > 0)
384-
options.SetTimeout(std::chrono::microseconds(m_command_options.timeout));
385-
else
386-
options.SetTimeout(std::nullopt);
387-
return options;
388-
}
389-
390388
bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
391389
Stream &output_stream,
392390
Stream &error_stream,
@@ -407,7 +405,8 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
407405
return false;
408406
}
409407

410-
const EvaluateExpressionOptions options = GetEvalOptions(target);
408+
const EvaluateExpressionOptions options =
409+
m_command_options.GetEvaluateExpressionOptions(target, m_varobj_options);
411410
ExpressionResults success = target.EvaluateExpression(
412411
expr, frame, result_valobj_sp, options, &m_fixed_expression);
413412

lldb/source/Commands/CommandObjectExpression.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class CommandObjectExpression : public CommandObjectRaw,
3535

3636
void OptionParsingStarting(ExecutionContext *execution_context) override;
3737

38+
/// Return the appropriate expression options used for evaluating the
39+
/// expression in the given target.
40+
EvaluateExpressionOptions GetEvaluateExpressionOptions(
41+
const Target &target,
42+
const OptionGroupValueObjectDisplay &display_opts);
43+
3844
bool top_level;
3945
bool unwind_on_error;
4046
bool ignore_breakpoints;
@@ -67,10 +73,6 @@ class CommandObjectExpression : public CommandObjectRaw,
6773

6874
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
6975

70-
/// Return the appropriate expression options used for evaluating the
71-
/// expression in the given target.
72-
EvaluateExpressionOptions GetEvalOptions(const Target &target);
73-
7476
/// Evaluates the given expression.
7577
/// \param output_stream The stream to which the evaluation result will be
7678
/// printed.

lldb/source/Interpreter/Options.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,19 @@ void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask,
781781
}
782782
}
783783

784+
void OptionGroupOptions::Append(
785+
OptionGroup *group, llvm::ArrayRef<llvm::StringRef> exclude_long_options) {
786+
auto group_option_defs = group->GetDefinitions();
787+
for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
788+
const auto &definition = group_option_defs[i];
789+
if (llvm::is_contained(exclude_long_options, definition.long_option))
790+
continue;
791+
792+
m_option_infos.push_back(OptionInfo(group, i));
793+
m_option_defs.push_back(definition);
794+
}
795+
}
796+
784797
void OptionGroupOptions::Finalize() {
785798
m_did_finalize = true;
786799
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,10 @@ def test_display_flags(self):
103103
lldbutil.run_to_name_breakpoint(self, "main")
104104
self._expect_cmd(f"dwim-print -T -- argc", "frame variable")
105105
self._expect_cmd(f"dwim-print -T -- argc + 1", "expression")
106+
107+
def test_expression_language(self):
108+
"""Test that the language flag doesn't affect the choice of command."""
109+
self.build()
110+
lldbutil.run_to_name_breakpoint(self, "main")
111+
self._expect_cmd(f"dwim-print -l c++ -- argc", "frame variable")
112+
self._expect_cmd(f"dwim-print -l c++ -- argc + 1", "expression")

0 commit comments

Comments
 (0)