Skip to content

Commit 7470260

Browse files
committed
[lldb] Add variable completion to dwim-print
Enable completion of variables for `dwim-print` command. Differential Revision: https://reviews.llvm.org/D145124 (cherry picked from commit 8794712)
1 parent cc22d39 commit 7470260

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
3232
"Print a variable or expression.",
3333
"dwim-print [<variable-name> | <expression>]",
3434
eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
35+
36+
CommandArgumentData var_name_arg(eArgTypeVarName, eArgRepeatPlain);
37+
m_arguments.push_back({var_name_arg});
38+
3539
m_option_group.Append(&m_format_options,
3640
OptionGroupFormat::OPTION_GROUP_FORMAT |
3741
OptionGroupFormat::OPTION_GROUP_GDB_FMT,
@@ -44,6 +48,13 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
4448

4549
Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; }
4650

51+
void CommandObjectDWIMPrint::HandleArgumentCompletion(
52+
CompletionRequest &request, OptionElementVector &opt_element_vector) {
53+
CommandCompletions::InvokeCommonCompletionCallbacks(
54+
GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion,
55+
request, nullptr);
56+
}
57+
4758
bool CommandObjectDWIMPrint::DoExecute(StringRef command,
4859
CommandReturnObject &result) {
4960
m_option_group.NotifyOptionParsingStarting(&m_exe_ctx);

lldb/source/Commands/CommandObjectDWIMPrint.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class CommandObjectDWIMPrint : public CommandObjectRaw {
3737

3838
Options *GetOptions() override;
3939

40+
bool WantsCompletion() override { return true; }
41+
42+
void
43+
HandleArgumentCompletion(CompletionRequest &request,
44+
OptionElementVector &opt_element_vector) override;
45+
4046
private:
4147
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
4248

lldb/test/API/functionalities/completion/TestCompletion.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,55 @@ def test_de(self):
3636

3737
def test_frame_variable(self):
3838
self.build()
39-
self.main_source = "main.cpp"
40-
self.main_source_spec = lldb.SBFileSpec(self.main_source)
4139

42-
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
43-
'// Break here', self.main_source_spec)
40+
_, process, _, _ = lldbutil.run_to_source_breakpoint(
41+
self, '// Break here', lldb.SBFileSpec("main.cpp"))
4442
self.assertState(process.GetState(), lldb.eStateStopped)
4543

4644
# Since CommandInterpreter has been corrected to update the current execution
4745
# context at the beginning of HandleCompletion, we're here explicitly testing
4846
# the scenario where "frame var" is completed without any preceding commands.
47+
self.do_test_variable_completion('frame variable')
4948

50-
self.complete_from_to('frame variable fo',
51-
'frame variable fooo')
52-
self.complete_from_to('frame variable fooo.',
53-
'frame variable fooo.')
54-
self.complete_from_to('frame variable fooo.dd',
55-
'frame variable fooo.dd')
56-
57-
self.complete_from_to('frame variable ptr_fooo->',
58-
'frame variable ptr_fooo->')
59-
self.complete_from_to('frame variable ptr_fooo->dd',
60-
'frame variable ptr_fooo->dd')
61-
62-
self.complete_from_to('frame variable cont',
63-
'frame variable container')
64-
self.complete_from_to('frame variable container.',
65-
'frame variable container.MemberVar')
66-
self.complete_from_to('frame variable container.Mem',
67-
'frame variable container.MemberVar')
68-
69-
self.complete_from_to('frame variable ptr_cont',
70-
'frame variable ptr_container')
71-
self.complete_from_to('frame variable ptr_container->',
72-
'frame variable ptr_container->MemberVar')
73-
self.complete_from_to('frame variable ptr_container->Mem',
74-
'frame variable ptr_container->MemberVar')
49+
def test_dwim_print(self):
50+
self.build()
51+
52+
_, process, _, _ = lldbutil.run_to_source_breakpoint(
53+
self, '// Break here', lldb.SBFileSpec("main.cpp"))
54+
self.assertState(process.GetState(), lldb.eStateStopped)
55+
56+
# Since CommandInterpreter has been corrected to update the current execution
57+
# context at the beginning of HandleCompletion, we're here explicitly testing
58+
# the scenario where "frame var" is completed without any preceding commands.
59+
self.do_test_variable_completion('dwim-print')
60+
61+
62+
def do_test_variable_completion(self, command):
63+
self.complete_from_to(f'{command} fo',
64+
f'{command} fooo')
65+
self.complete_from_to(f'{command} fooo.',
66+
f'{command} fooo.')
67+
self.complete_from_to(f'{command} fooo.dd',
68+
f'{command} fooo.dd')
69+
70+
self.complete_from_to(f'{command} ptr_fooo->',
71+
f'{command} ptr_fooo->')
72+
self.complete_from_to(f'{command} ptr_fooo->dd',
73+
f'{command} ptr_fooo->dd')
74+
75+
self.complete_from_to(f'{command} cont',
76+
f'{command} container')
77+
self.complete_from_to(f'{command} container.',
78+
f'{command} container.MemberVar')
79+
self.complete_from_to(f'{command} container.Mem',
80+
f'{command} container.MemberVar')
81+
82+
self.complete_from_to(f'{command} ptr_cont',
83+
f'{command} ptr_container')
84+
self.complete_from_to(f'{command} ptr_container->',
85+
f'{command} ptr_container->MemberVar')
86+
self.complete_from_to(f'{command} ptr_container->Mem',
87+
f'{command} ptr_container->MemberVar')
7588

7689
def test_process_attach_dash_dash_con(self):
7790
"""Test that 'process attach --con' completes to 'process attach --continue '."""

0 commit comments

Comments
 (0)