[Lldb-commits] [lldb] 8794712 - [lldb] Add variable completion to dwim-print

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 6 10:42:43 PST 2023


Author: Dave Lee
Date: 2023-03-06T10:42:32-08:00
New Revision: 8794712e8877ed366542c32e28abe40d0a9ced6d

URL: https://github.com/llvm/llvm-project/commit/8794712e8877ed366542c32e28abe40d0a9ced6d
DIFF: https://github.com/llvm/llvm-project/commit/8794712e8877ed366542c32e28abe40d0a9ced6d.diff

LOG: [lldb] Add variable completion to dwim-print

Enable completion of variables for `dwim-print` command.

Differential Revision: https://reviews.llvm.org/D145124

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectDWIMPrint.cpp
    lldb/source/Commands/CommandObjectDWIMPrint.h
    lldb/test/API/functionalities/completion/TestCompletion.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index a348f416af22..e2e6f6706dee 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -32,6 +32,10 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
                        "Print a variable or expression.",
                        "dwim-print [<variable-name> | <expression>]",
                        eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
+
+  CommandArgumentData var_name_arg(eArgTypeVarName, eArgRepeatPlain);
+  m_arguments.push_back({var_name_arg});
+
   m_option_group.Append(&m_format_options,
                         OptionGroupFormat::OPTION_GROUP_FORMAT |
                             OptionGroupFormat::OPTION_GROUP_GDB_FMT,
@@ -44,6 +48,13 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
 
 Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; }
 
+void CommandObjectDWIMPrint::HandleArgumentCompletion(
+    CompletionRequest &request, OptionElementVector &opt_element_vector) {
+  CommandCompletions::InvokeCommonCompletionCallbacks(
+      GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion,
+      request, nullptr);
+}
+
 bool CommandObjectDWIMPrint::DoExecute(StringRef command,
                                        CommandReturnObject &result) {
   m_option_group.NotifyOptionParsingStarting(&m_exe_ctx);

diff  --git a/lldb/source/Commands/CommandObjectDWIMPrint.h b/lldb/source/Commands/CommandObjectDWIMPrint.h
index 8a14a6c7bb53..3fc6c01d4729 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.h
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.h
@@ -37,6 +37,12 @@ class CommandObjectDWIMPrint : public CommandObjectRaw {
 
   Options *GetOptions() override;
 
+  bool WantsCompletion() override { return true; }
+
+  void
+  HandleArgumentCompletion(CompletionRequest &request,
+                           OptionElementVector &opt_element_vector) override;
+
 private:
   bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 

diff  --git a/lldb/test/API/functionalities/completion/TestCompletion.py b/lldb/test/API/functionalities/completion/TestCompletion.py
index f2d3fb704900..baaf9f57162c 100644
--- a/lldb/test/API/functionalities/completion/TestCompletion.py
+++ b/lldb/test/API/functionalities/completion/TestCompletion.py
@@ -36,42 +36,55 @@ def test_de(self):
 
     def test_frame_variable(self):
         self.build()
-        self.main_source = "main.cpp"
-        self.main_source_spec = lldb.SBFileSpec(self.main_source)
 
-        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
-                                          '// Break here', self.main_source_spec)
+        _, process, _, _ = lldbutil.run_to_source_breakpoint(
+            self, '// Break here', lldb.SBFileSpec("main.cpp"))
         self.assertState(process.GetState(), lldb.eStateStopped)
 
         # Since CommandInterpreter has been corrected to update the current execution
         # context at the beginning of HandleCompletion, we're here explicitly testing
         # the scenario where "frame var" is completed without any preceding commands.
+        self.do_test_variable_completion('frame variable')
 
-        self.complete_from_to('frame variable fo',
-                              'frame variable fooo')
-        self.complete_from_to('frame variable fooo.',
-                              'frame variable fooo.')
-        self.complete_from_to('frame variable fooo.dd',
-                              'frame variable fooo.dd')
-
-        self.complete_from_to('frame variable ptr_fooo->',
-                              'frame variable ptr_fooo->')
-        self.complete_from_to('frame variable ptr_fooo->dd',
-                              'frame variable ptr_fooo->dd')
-
-        self.complete_from_to('frame variable cont',
-                              'frame variable container')
-        self.complete_from_to('frame variable container.',
-                              'frame variable container.MemberVar')
-        self.complete_from_to('frame variable container.Mem',
-                              'frame variable container.MemberVar')
-
-        self.complete_from_to('frame variable ptr_cont',
-                              'frame variable ptr_container')
-        self.complete_from_to('frame variable ptr_container->',
-                              'frame variable ptr_container->MemberVar')
-        self.complete_from_to('frame variable ptr_container->Mem',
-                              'frame variable ptr_container->MemberVar')
+    def test_dwim_print(self):
+        self.build()
+
+        _, process, _, _ = lldbutil.run_to_source_breakpoint(
+            self, '// Break here', lldb.SBFileSpec("main.cpp"))
+        self.assertState(process.GetState(), lldb.eStateStopped)
+
+        # Since CommandInterpreter has been corrected to update the current execution
+        # context at the beginning of HandleCompletion, we're here explicitly testing
+        # the scenario where "frame var" is completed without any preceding commands.
+        self.do_test_variable_completion('dwim-print')
+
+
+    def do_test_variable_completion(self, command):
+        self.complete_from_to(f'{command} fo',
+                              f'{command} fooo')
+        self.complete_from_to(f'{command} fooo.',
+                              f'{command} fooo.')
+        self.complete_from_to(f'{command} fooo.dd',
+                              f'{command} fooo.dd')
+
+        self.complete_from_to(f'{command} ptr_fooo->',
+                              f'{command} ptr_fooo->')
+        self.complete_from_to(f'{command} ptr_fooo->dd',
+                              f'{command} ptr_fooo->dd')
+
+        self.complete_from_to(f'{command} cont',
+                              f'{command} container')
+        self.complete_from_to(f'{command} container.',
+                              f'{command} container.MemberVar')
+        self.complete_from_to(f'{command} container.Mem',
+                              f'{command} container.MemberVar')
+
+        self.complete_from_to(f'{command} ptr_cont',
+                              f'{command} ptr_container')
+        self.complete_from_to(f'{command} ptr_container->',
+                              f'{command} ptr_container->MemberVar')
+        self.complete_from_to(f'{command} ptr_container->Mem',
+                              f'{command} ptr_container->MemberVar')
 
     def test_process_attach_dash_dash_con(self):
         """Test that 'process attach --con' completes to 'process attach --continue '."""


        


More information about the lldb-commits mailing list