Skip to content

Centralize the handling of completion for simple argument lists. #82085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2024

Conversation

jimingham
Copy link
Collaborator

Most commands were adding argument completion handling by themselves, resulting in a lot of unnecessary boilerplate. In many cases, this could be done generically given the argument definition and the entries in the g_argument_table.

I'm going to address this in a couple passes. In this first pass, I added handling of commands that have only one argument list, with one argument type, either single or repeated, and changed all the commands that are of this sort (and don't have other bits of business in their completers.)

I also added some missing connections between arg types and completions to the table, and added a RemoteFilename and RemotePath to use in places where we were using the Remote completers. Those arguments used to say they were "files" but they were in fact remote files.

I also added a module arg type to use where we were using the module completer. In that case, we should call the argument module.

Most commands were adding argument completion handling by themselves,
resulting in a lot of unnecessary boilerplate.  In many cases, this
could be done generically given the argument definition and the
entries in the g_argument_table.

I'm going to address this in a couple passes.  In this first pass,
I added handling of commands that have only one argument list, with
one argument type, either single or repeated, and changed all the
commands that are of this sort (and don't have other bits of business
in their completers.)

I also added some missing connections between arg types and completions
to the table, and added a RemoteFilename and RemotePath to use in
places where we were using the Remote completers.  Those arguments
used to say they were "files" but they were in fact remote files.

I also added a module arg type to use where we were using the module
completer.  In that case, we should call the argument module.
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2024

@llvm/pr-subscribers-lldb

Author: None (jimingham)

Changes

Most commands were adding argument completion handling by themselves, resulting in a lot of unnecessary boilerplate. In many cases, this could be done generically given the argument definition and the entries in the g_argument_table.

I'm going to address this in a couple passes. In this first pass, I added handling of commands that have only one argument list, with one argument type, either single or repeated, and changed all the commands that are of this sort (and don't have other bits of business in their completers.)

I also added some missing connections between arg types and completions to the table, and added a RemoteFilename and RemotePath to use in places where we were using the Remote completers. Those arguments used to say they were "files" but they were in fact remote files.

I also added a module arg type to use where we were using the module completer. In that case, we should call the argument module.


Patch is 39.50 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/82085.diff

19 Files Affected:

  • (modified) lldb/include/lldb/Interpreter/CommandObject.h (+8-1)
  • (modified) lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h (+12-9)
  • (modified) lldb/include/lldb/lldb-enumerations.h (+3)
  • (modified) lldb/source/Commands/CommandObjectCommands.cpp (-14)
  • (modified) lldb/source/Commands/CommandObjectDWIMPrint.cpp (-6)
  • (modified) lldb/source/Commands/CommandObjectDWIMPrint.h (-4)
  • (modified) lldb/source/Commands/CommandObjectFrame.cpp (-19)
  • (modified) lldb/source/Commands/CommandObjectPlatform.cpp (+17-57)
  • (modified) lldb/source/Commands/CommandObjectPlugin.cpp (-7)
  • (modified) lldb/source/Commands/CommandObjectProcess.cpp (+1-18)
  • (modified) lldb/source/Commands/CommandObjectRegister.cpp (+2-5)
  • (modified) lldb/source/Commands/CommandObjectSession.cpp (-7)
  • (modified) lldb/source/Commands/CommandObjectSettings.cpp (-8)
  • (modified) lldb/source/Commands/CommandObjectTarget.cpp (+3-26)
  • (modified) lldb/source/Commands/CommandObjectThread.cpp (+1-12)
  • (modified) lldb/source/Commands/CommandObjectType.cpp (-32)
  • (modified) lldb/source/Commands/CommandObjectWatchpoint.cpp (-10)
  • (modified) lldb/source/Interpreter/CommandObject.cpp (+36)
  • (modified) lldb/test/API/commands/help/TestHelp.py (+1-1)
diff --git a/lldb/include/lldb/Interpreter/CommandObject.h b/lldb/include/lldb/Interpreter/CommandObject.h
index 7b427de0264f75..537acb450296b5 100644
--- a/lldb/include/lldb/Interpreter/CommandObject.h
+++ b/lldb/include/lldb/Interpreter/CommandObject.h
@@ -239,6 +239,13 @@ class CommandObject : public std::enable_shared_from_this<CommandObject> {
   ///    The completion request that needs to be answered.
   virtual void HandleCompletion(CompletionRequest &request);
 
+  /// The default version handles argument definitions that have only one
+  /// argument type, and use one of the argument types that have an entry in
+  /// the CommonCompletions.  Override this if you have a more complex 
+  /// argument setup.  
+  /// FIXME: we should be able to extend this to more complex argument 
+  /// definitions provided we have completers for all the argument types.
+  ///  
   /// The input array contains a parsed version of the line.
   ///
   /// We've constructed the map of options and their arguments as well if that
@@ -248,7 +255,7 @@ class CommandObject : public std::enable_shared_from_this<CommandObject> {
   ///    The completion request that needs to be answered.
   virtual void
   HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) {}
+                           OptionElementVector &opt_element_vector);
 
   bool HelpTextContainsWord(llvm::StringRef search_word,
                             bool search_short_help = true,
diff --git a/lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h b/lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h
index d0cf54c31ca73f..ab5eff699b4cf0 100644
--- a/lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h
+++ b/lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h
@@ -243,7 +243,7 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
     { lldb::eArgTypeLogCategory, "log-category", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a category within a log channel, e.g. all (try \"log list\" to see a list of all channels and their categories." },
     { lldb::eArgTypeLogChannel, "log-channel", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a log channel, e.g. process.gdb-remote (try \"log list\" to see a list of all channels and their categories)." },
     { lldb::eArgTypeMethod, "method", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A C++ method name." },
-    { lldb::eArgTypeName, "name", lldb::eTypeCategoryNameCompletion, {}, { nullptr, false }, "Help text goes here." },
+    { lldb::eArgTypeName, "name", lldb::eTypeCategoryNameCompletion, {}, { nullptr, false }, "The name of a type category." },
     { lldb::eArgTypeNewPathPrefix, "new-path-prefix", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Help text goes here." },
     { lldb::eArgTypeNumLines, "num-lines", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The number of lines to use." },
     { lldb::eArgTypeNumberPerLine, "number-per-line", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The number of items per line to display." },
@@ -262,7 +262,7 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
     { lldb::eArgTypeQueueName, "queue-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of the thread queue." },
     { lldb::eArgTypeRegisterName, "register-name", lldb::CompletionType::eNoCompletion, {}, { RegisterNameHelpTextCallback, true }, nullptr },
     { lldb::eArgTypeRegularExpression, "regular-expression", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A POSIX-compliant extended regular expression." },
-    { lldb::eArgTypeRunArgs, "run-args", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
+    { lldb::eArgTypeRunArgs, "run-args", lldb::CompletionType::eDiskFileCompletion, {}, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
     { lldb::eArgTypeRunMode, "run-mode", lldb::CompletionType::eNoCompletion, g_running_mode, { nullptr, false }, "Help text goes here." },
     { lldb::eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", lldb::CompletionType::eNoCompletion, g_script_synchro_type, { nullptr, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
     { lldb::eArgTypeScriptLang, "script-language", lldb::CompletionType::eNoCompletion, g_script_option_enumeration, { nullptr, false }, "The scripting language to be used for script-based commands.  Supported languages are python and lua." },
@@ -270,21 +270,21 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
     { lldb::eArgTypeSelector, "selector", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "An Objective-C selector name." },
     { lldb::eArgTypeSettingIndex, "setting-index", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "An index into a settings variable that is an array (try 'settings list' to see all the possible settings variables and their types)." },
     { lldb::eArgTypeSettingKey, "setting-key", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A key into a settings variables that is a dictionary (try 'settings list' to see all the possible settings variables and their types)." },
-    { lldb::eArgTypeSettingPrefix, "setting-prefix", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
-    { lldb::eArgTypeSettingVariableName, "setting-variable-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a settable internal debugger variable.  Type 'settings list' to see a complete list of such variables." },
-    { lldb::eArgTypeShlibName, "shlib-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a shared library." },
+    { lldb::eArgTypeSettingPrefix, "setting-prefix", lldb::CompletionType::eSettingsNameCompletion, {}, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
+    { lldb::eArgTypeSettingVariableName, "setting-variable-name", lldb::CompletionType::eSettingsNameCompletion, {}, { nullptr, false }, "The name of a settable internal debugger variable.  Type 'settings list' to see a complete list of such variables." },
+    { lldb::eArgTypeShlibName, "shlib-name", lldb::CompletionType::eDiskFileCompletion, {}, { nullptr, false }, "The name of a shared library." },
     { lldb::eArgTypeSourceFile, "source-file", lldb::eSourceFileCompletion, {}, { nullptr, false }, "The name of a source file.." },
     { lldb::eArgTypeSortOrder, "sort-order", lldb::CompletionType::eNoCompletion, g_sort_option_enumeration, { nullptr, false }, "Specify a sort order when dumping lists." },
     { lldb::eArgTypeStartAddress, "start-address", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Help text goes here." },
     { lldb::eArgTypeSummaryString, "summary-string", lldb::CompletionType::eNoCompletion, {}, { SummaryStringHelpTextCallback, true }, nullptr },
     { lldb::eArgTypeSymbol, "symbol", lldb::eSymbolCompletion, {}, { nullptr, false }, "Any symbol name (function name, variable, argument, etc.)" },
-    { lldb::eArgTypeThreadID, "thread-id", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Thread ID number." },
-    { lldb::eArgTypeThreadIndex, "thread-index", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Index into the process' list of threads." },
+    { lldb::eArgTypeThreadID, "thread-id", lldb::CompletionType::eThreadIndexCompletion, {}, { nullptr, false }, "Thread ID number." },
+    { lldb::eArgTypeThreadIndex, "thread-index", lldb::CompletionType::eThreadIndexCompletion, {}, { nullptr, false }, "Index into the process' list of threads." },
     { lldb::eArgTypeThreadName, "thread-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The thread's name." },
     { lldb::eArgTypeTypeName, "type-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A type name." },
     { lldb::eArgTypeUnsignedInteger, "unsigned-integer", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "An unsigned integer." },
     { lldb::eArgTypeUnixSignal, "unix-signal", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A valid Unix signal name or number (e.g. SIGKILL, KILL or 9)." },
-    { lldb::eArgTypeVarName, "variable-name", lldb::CompletionType::eNoCompletion, {} ,{ nullptr, false }, "The name of a variable in your program." },
+    { lldb::eArgTypeVarName, "variable-name", lldb::CompletionType::eVariablePathCompletion, {} ,{ nullptr, false }, "The name of a variable in your program." },
     { lldb::eArgTypeValue, "value", lldb::CompletionType::eNoCompletion, g_dependents_enumeration, { nullptr, false }, "A value could be anything, depending on where and how it is used." },
     { lldb::eArgTypeWidth, "width", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Help text goes here." },
     { lldb::eArgTypeNone, "none", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "No help available for this." },
@@ -302,8 +302,11 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
     { lldb::eArgTypeRecognizerID, "frame-recognizer-id", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The ID for a stack frame recognizer." },
     { lldb::eArgTypeConnectURL, "process-connect-url", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A URL-style specification for a remote connection." },
     { lldb::eArgTypeTargetID, "target-id", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The index ID for an lldb Target." },
-    { lldb::eArgTypeStopHookID, "stop-hook-id", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The ID you receive when you create a stop-hook." },
+    { lldb::eArgTypeStopHookID, "stop-hook-id", lldb::CompletionType::eStopHookIDCompletion, {}, { nullptr, false }, "The ID you receive when you create a stop-hook." },
     { lldb::eArgTypeCompletionType, "completion-type", lldb::CompletionType::eNoCompletion, g_completion_type, { nullptr, false }, "The completion type to use when adding custom commands. If none is specified, the command won't use auto-completion." },
+    { lldb::eArgTypeRemotePath, "remote-path", lldb::CompletionType::eRemoteDiskFileCompletion, {}, { nullptr, false }, "A path on the system managed by the current platform." },
+    { lldb::eArgTypeRemoteFilename, "remote-filename", lldb::CompletionType::eRemoteDiskFileCompletion, {}, { nullptr, false }, "A file on the system managed by the current platform." },
+    { lldb::eArgTypeModule, "module", lldb::CompletionType::eModuleCompletion, {}, { nullptr, false }, "The name of a module loaded into the current target." },
     // clang-format on
 };
 
diff --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h
index 50cbccba4d7c56..adcf7e874c855e 100644
--- a/lldb/include/lldb/lldb-enumerations.h
+++ b/lldb/include/lldb/lldb-enumerations.h
@@ -677,6 +677,9 @@ enum CommandArgumentType {
   eArgTypeTargetID,
   eArgTypeStopHookID,
   eArgTypeCompletionType,
+  eArgTypeRemotePath,
+  eArgTypeRemoteFilename,
+  eArgTypeModule,
   eArgTypeLastArg // Always keep this entry as the last entry in this
                   // enumeration!!
 };
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index a51e5ab1af30c6..73671b0fa70719 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -63,13 +63,6 @@ class CommandObjectCommandsSource : public CommandObjectParsed {
     return std::string("");
   }
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr);
-  }
-
   Options *GetOptions() override { return &m_options; }
 
 protected:
@@ -1272,13 +1265,6 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
 
   ~CommandObjectCommandsScriptImport() override = default;
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr);
-  }
-
   Options *GetOptions() override { return &m_options; }
 
 protected:
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 695f3d7931cd0a..fb2cc106ffd2dd 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -52,12 +52,6 @@ CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
 
 Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; }
 
-void CommandObjectDWIMPrint::HandleArgumentCompletion(
-    CompletionRequest &request, OptionElementVector &opt_element_vector) {
-  lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-      GetCommandInterpreter(), lldb::eVariablePathCompletion, request, nullptr);
-}
-
 void 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 d868f8964c2ac5..01ba9c225e3301 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.h
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.h
@@ -39,10 +39,6 @@ class CommandObjectDWIMPrint : public CommandObjectRaw {
 
   bool WantsCompletion() override { return true; }
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override;
-
 private:
   void DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 
diff --git a/lldb/source/Commands/CommandObjectFrame.cpp b/lldb/source/Commands/CommandObjectFrame.cpp
index 17a7e858b24e58..b39b1a006c1f7b 100644
--- a/lldb/source/Commands/CommandObjectFrame.cpp
+++ b/lldb/source/Commands/CommandObjectFrame.cpp
@@ -283,16 +283,6 @@ class CommandObjectFrameSelect : public CommandObjectParsed {
 
   ~CommandObjectFrameSelect() override = default;
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    if (request.GetCursorIndex() != 0)
-      return;
-
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eFrameIndexCompletion, request, nullptr);
-  }
-
   Options *GetOptions() override { return &m_options; }
 
 protected:
@@ -443,15 +433,6 @@ may even involve JITing and running code in the target program.)");
 
   Options *GetOptions() override { return &m_option_group; }
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    // Arguments are the standard source file completer.
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eVariablePathCompletion, request,
-        nullptr);
-  }
-
 protected:
   llvm::StringRef GetScopeString(VariableSP var_sp) {
     if (!var_sp)
diff --git a/lldb/source/Commands/CommandObjectPlatform.cpp b/lldb/source/Commands/CommandObjectPlatform.cpp
index 790f1dbb475358..7b960029d3cf28 100644
--- a/lldb/source/Commands/CommandObjectPlatform.cpp
+++ b/lldb/source/Commands/CommandObjectPlatform.cpp
@@ -418,7 +418,7 @@ class CommandObjectPlatformMkDir : public CommandObjectParsed {
       : CommandObjectParsed(interpreter, "platform mkdir",
                             "Make a new directory on the remote end.", nullptr,
                             0) {
-    CommandArgumentData thread_arg{eArgTypePath, eArgRepeatPlain};
+    CommandArgumentData thread_arg{eArgTypeRemotePath, eArgRepeatPlain};
     m_arguments.push_back({thread_arg});
   }
 
@@ -467,21 +467,12 @@ class CommandObjectPlatformFOpen : public CommandObjectParsed {
   CommandObjectPlatformFOpen(CommandInterpreter &interpreter)
       : CommandObjectParsed(interpreter, "platform file open",
                             "Open a file on the remote end.", nullptr, 0) {
-    CommandArgumentData path_arg{eArgTypePath, eArgRepeatPlain};
+    CommandArgumentData path_arg{eArgTypeRemotePath, eArgRepeatPlain};
     m_arguments.push_back({path_arg});
   }
 
   ~CommandObjectPlatformFOpen() override = default;
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    if (request.GetCursorIndex() == 0)
-      lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-          GetCommandInterpreter(), lldb::eRemoteDiskFileCompletion, request,
-          nullptr);
-  }
-
   void DoExecute(Args &args, CommandReturnObject &result) override {
     PlatformSP platform_sp(
         GetDebugger().GetPlatformList().GetSelectedPlatform());
@@ -795,7 +786,7 @@ class CommandObjectPlatformGetFile : public CommandObjectParsed {
     CommandArgumentData file_arg_remote, file_arg_host;
 
     // Define the first (and only) variant of this arg.
-    file_arg_remote.arg_type = eArgTypeFilename;
+    file_arg_remote.arg_type = eArgTypeRemoteFilename;
     file_arg_remote.arg_repetition = eArgRepeatPlain;
     // There is only one variant this argument could be; put it into the
     // argument entry.
@@ -876,7 +867,7 @@ class CommandObjectPlatformGetSize : public CommandObjectParsed {
     CommandArgumentData file_arg_remote;
 
     // Define the first (and only) variant of this arg.
-    file_arg_remote.arg_type = eArgTypeFilename;
+    file_arg_remote.arg_type = eArgTypeRemoteFilename;
     file_arg_remote.arg_repetition = eArgRepeatPlain;
     // There is only one variant this argument could be; put it into the
     // argument entry.
@@ -888,17 +879,6 @@ class CommandObjectPlatformGetSize : public CommandObjectParsed {
 
   ~CommandObjectPlatformGetSize() override = default;
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    if (request.GetCursorIndex() != 0)
-      return;
-
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eRemoteDiskFileCompletion, request,
-        nullptr);
-  }
-
   void DoExecute(Args &args, CommandReturnObject &result) override {
     // If the number of arguments is incorrect, issue an error message.
     if (args.GetArgumentCount() != 1) {
@@ -946,7 +926,7 @@ class CommandObjectPlatformGetPermissions : public CommandObjectParsed {
     CommandArgumentData file_arg_remote;
 
     // Define the first (and only) variant of this arg.
-    file_arg_remote.arg_type = eArgTypeFilename;
+    file_arg_remote.arg_type = eArgTypeRemoteFilename;
     file_arg_remote.arg_repetition = eArgRepeatPlain;
     // There is only one variant this argument could be; put it into the
     // argument entry.
@@ -958,17 +938,6 @@ class CommandObjectPlatformGetPermissions : public CommandObjectParsed {
 
   ~CommandObjectPlatformGetPermissions() override = default;
 
-  void
-  HandleArgumentCompletion(CompletionRequest &request,
-                           OptionElementVector &opt_element_vector) override {
-    if (request.GetCursorIndex() != 0)
-      return;
-
-    lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
-        GetCommandInterpreter(), lldb::eRemoteDiskFileCompletion, request,
-        nullptr);
-  }
-
   void DoExecute(Args &args, CommandReturnObject &result) override {
     // If the number of arguments is incorrect, issue an error message.
     if (args.GetArgumentCount() != 1) {
@@ -1015,7 +984,7 @@ cla...
[truncated]

Copy link

github-actions bot commented Feb 17, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 6ce03ff3fef8fb6fa9afe8eb22c6d98bced26d48 8966713f9c72acba87ea4163c8f202c879ca8ad4 -- lldb/include/lldb/Interpreter/CommandObject.h lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h lldb/include/lldb/lldb-enumerations.h lldb/source/Commands/CommandObjectCommands.cpp lldb/source/Commands/CommandObjectDWIMPrint.cpp lldb/source/Commands/CommandObjectDWIMPrint.h lldb/source/Commands/CommandObjectFrame.cpp lldb/source/Commands/CommandObjectPlatform.cpp lldb/source/Commands/CommandObjectPlugin.cpp lldb/source/Commands/CommandObjectProcess.cpp lldb/source/Commands/CommandObjectRegister.cpp lldb/source/Commands/CommandObjectSession.cpp lldb/source/Commands/CommandObjectSettings.cpp lldb/source/Commands/CommandObjectTarget.cpp lldb/source/Commands/CommandObjectThread.cpp lldb/source/Commands/CommandObjectType.cpp lldb/source/Commands/CommandObjectWatchpoint.cpp lldb/source/Interpreter/CommandObject.cpp
View the diff from clang-format here.
diff --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp
index 08129bfd64..f174b18349 100644
--- a/lldb/source/Interpreter/CommandObject.cpp
+++ b/lldb/source/Interpreter/CommandObject.cpp
@@ -316,7 +316,7 @@ void CommandObject::HandleArgumentCompletion(
     assert(entry_ptr && "We said there was one entry, but there wasn't.");
     return; // Not worth crashing if asserts are off...
   }
-  
+
   CommandArgumentEntry &entry = *entry_ptr;
   // For now, we only handle the simple case of one homogenous argument type.
   if (entry.size() != 1)
@@ -338,10 +338,8 @@ void CommandObject::HandleArgumentCompletion(
 
   lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
       GetCommandInterpreter(), arg_entry->completion_type, request, nullptr);
-
 }
 
-
 bool CommandObject::HelpTextContainsWord(llvm::StringRef search_word,
                                          bool search_short_help,
                                          bool search_long_help,

@jimingham
Copy link
Collaborator Author

jimingham commented Feb 17, 2024

BTW, in case this is confusing, the completers that were returning if the cursor was not in the first argument slot was preventing:

(lldb) command arg1 <TAB>
from starting another completion when the argument was eArgTypePlain (only one arg). I moved that check to the CommandObject::HandleArgumentCompletion so that's no longer required.

Also, I on purpose didn't change any argument completions that I had to think hard about. I'll do those on subsequent passes.

@jimingham
Copy link
Collaborator Author

I didn't add any new tests, the TestCompletion.py is pretty thorough already.

@@ -243,7 +243,7 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
{ lldb::eArgTypeLogCategory, "log-category", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a category within a log channel, e.g. all (try \"log list\" to see a list of all channels and their categories." },
{ lldb::eArgTypeLogChannel, "log-channel", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a log channel, e.g. process.gdb-remote (try \"log list\" to see a list of all channels and their categories)." },
{ lldb::eArgTypeMethod, "method", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "A C++ method name." },
{ lldb::eArgTypeName, "name", lldb::eTypeCategoryNameCompletion, {}, { nullptr, false }, "Help text goes here." },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help text DOES go here! :)

@@ -1139,6 +1097,15 @@ class CommandObjectPlatformProcessLaunch : public CommandObjectParsed {
m_arguments.push_back({run_arg_arg});
}

void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way you could move this to the generic CommandObject implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one caused a test failure when I tried the simple implementation, so it goes into the second round.


CommandArgumentEntry *entry_ptr = GetArgumentEntryAtIndex(0);
if (!entry_ptr)
return; // Maybe this should be an assert, this shouldn't be possible.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it shouldn't be possible, I think an assert is warranted here. I would say add the assertion above the early return (and keep the early return since the assertion will be compiled out in release builds).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert is fine, but please leave the if (!entry_ptr) return; in just in case. We don't want to crash if we can help it, but the assert will let us know something is wrong during testing.

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix. Just a few comments


CommandArgumentEntry *entry_ptr = GetArgumentEntryAtIndex(0);
if (!entry_ptr)
return; // Maybe this should be an assert, this shouldn't be possible.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert is fine, but please leave the if (!entry_ptr) return; in just in case. We don't want to crash if we can help it, but the assert will let us know something is wrong during testing.

Comment on lines +321 to +322
if (entry.size() != 1)
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really that hard to add multiple? Can't we handle N completion types as long as they are not custom?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for the second round of fixes.

Comment on lines 324 to 340
// Look up the completion type, and if it has one, invoke it:
const CommandObject::ArgumentTableEntry *arg_entry
= FindArgumentDataByType(entry[0].arg_type);
const ArgumentRepetitionType repeat = entry[0].arg_repetition;

if (arg_entry == nullptr || arg_entry->completion_type == lldb::eNoCompletion)
return;

// FIXME: This should be handled higher in the Command Parser.
// Check the case where this command only takes one argument, and don't do
// the completion if we aren't on the first entry:
if (repeat == eArgRepeatPlain && request.GetCursorIndex() != 0)
return;

lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), arg_entry->completion_type, request, nullptr);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can handle N completion types, this code could be put into a function and run multiple times with a different index, or we can just make a loop

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything not mechanical is for the second pass.

@jimingham
Copy link
Collaborator Author

jimingham commented Feb 19, 2024

This is responding to both Greg and Alex's "can't we also do X completion" comments:

It was clear that this was going to be a whole lot of change, some of it purely mechanical, and some requiring a little thought. To make it easier to review, I'm doing the work in stages.

This first stage was:

Make the base class handle homogenous arguments of one type, either with single repeat or multiple repeats of that argument type. I made that change, and changed all the arguments that were obviously of that sort. If any of the extant completions were outside this scope, a little tricky, or caused a test failure I didn't understand, I left them for the second pass. That way we can review the ones that aren't mechanical without all the noise of the obvious ones.

Since I'm leaving all the completions I'm not handling in the base class intact, this won't leave anything broken, so I think this is a reasonable way to proceed.

@jimingham
Copy link
Collaborator Author

The next two pieces are "one heterogenous argument list" and then "multiple argument lists".

The first one should be easy. The second one is harder because you have to figure out what group matches the current state of the command line.

Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jimingham jimingham merged commit 2163149 into llvm:main Feb 20, 2024
@rastogishubham
Copy link
Contributor

@jimingham It seems like this change broke greendragon TestCompletion.py

https://green.lab.llvm.org/green/view/LLDB/job/lldb-cmake/66519/

rastogishubham added a commit that referenced this pull request Feb 20, 2024
…ts. (#82085)"

This reverts commit 2163149.

Reverted because of greendragon failure:

******************** TEST 'lldb-api :: functionalities/completion/TestCompletion.py' FAILED ********************
Script:
@rastogishubham
Copy link
Contributor

Reverted with 9ed8b27

jimingham added a commit to jimingham/from-apple-llvm-project that referenced this pull request Feb 20, 2024
jimingham added a commit that referenced this pull request Feb 20, 2024
…#82428)

This is a follow-on to:

#82085

The completer for register names was missing from the argument table. I
somehow missed that the only register completer test was x86_64, so that
test broke.

I added the completer in to the right slot in the argument table, and
added a small completions test that just uses the alias register names.
If we end up having a platform that doesn't define register names, we'll
have to skip this test there, but it should add a sniff test for
register completion that will run most everywhere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants