[Lldb-commits] [lldb] 39ea676 - [lldb] Compute fully qualified command names in FindCommandsForApropos

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Sun Jan 9 12:11:47 PST 2022


Author: Dave Lee
Date: 2022-01-09T12:11:32-08:00
New Revision: 39ea676d9d0ea467b7e5fe2d5c25d22a2d906041

URL: https://github.com/llvm/llvm-project/commit/39ea676d9d0ea467b7e5fe2d5c25d22a2d906041
DIFF: https://github.com/llvm/llvm-project/commit/39ea676d9d0ea467b7e5fe2d5c25d22a2d906041.diff

LOG: [lldb] Compute fully qualified command names in FindCommandsForApropos

Fixes incomplete command names in `apropos` results.

The full command names given by `apropos` have come from command name string
literals given to `CommandObject` constructors. For most commands, this has
been accurate, but some commands have incorrect strings. This results in
`apropos` output that doesn't tell the user the full command name they might
want learn more about. These strings can be fixed.

There's a seperate issue that can't be fixed as easily: plugin commands. With
the way they're implemented, plugin commands have to exclude the root command
from their command name string. To illustrate, the `language objc` subcommand
has to set its command name string to "objc", which results in apropos printing
results as `objc ...` instead of `language objc ...`.

To fix both of these issues, this commit changes `FindCommandsForApropos` to
derive the fully qualified command name using the keys of subcommand maps.

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

(cherry picked from commit b3bfd595a548cd85b12e4e83729436cb73b26f29)

Added: 
    

Modified: 
    lldb/include/lldb/Interpreter/CommandInterpreter.h
    lldb/source/Interpreter/CommandInterpreter.cpp
    lldb/test/API/commands/command/container/TestContainerCommands.py
    lldb/test/API/commands/command/container/welcome.py

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index e6f0d5f9c4d43..3efb59fc05647 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -649,7 +649,7 @@ class CommandInterpreter : public Broadcaster,
 
   void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found,
                               StringList &commands_help,
-                              CommandObject::CommandMap &command_map);
+                              const CommandObject::CommandMap &command_map);
 
   // An interruptible wrapper around the stream output
   void PrintCommandOutput(Stream &stream, llvm::StringRef str);

diff  --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index 085b06bce0eae..59c23716bf89c 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -2841,12 +2841,10 @@ void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text,
 
 void CommandInterpreter::FindCommandsForApropos(
     llvm::StringRef search_word, StringList &commands_found,
-    StringList &commands_help, CommandObject::CommandMap &command_map) {
-  CommandObject::CommandMap::const_iterator pos;
-
-  for (pos = command_map.begin(); pos != command_map.end(); ++pos) {
-    llvm::StringRef command_name = pos->first;
-    CommandObject *cmd_obj = pos->second.get();
+    StringList &commands_help, const CommandObject::CommandMap &command_map) {
+  for (const auto &pair : command_map) {
+    llvm::StringRef command_name = pair.first;
+    CommandObject *cmd_obj = pair.second.get();
 
     const bool search_short_help = true;
     const bool search_long_help = false;
@@ -2856,14 +2854,19 @@ void CommandInterpreter::FindCommandsForApropos(
         cmd_obj->HelpTextContainsWord(search_word, search_short_help,
                                       search_long_help, search_syntax,
                                       search_options)) {
-      commands_found.AppendString(cmd_obj->GetCommandName());
+      commands_found.AppendString(command_name);
       commands_help.AppendString(cmd_obj->GetHelp());
     }
 
-    if (cmd_obj->IsMultiwordObject()) {
-      CommandObjectMultiword *cmd_multiword = cmd_obj->GetAsMultiwordCommand();
-      FindCommandsForApropos(search_word, commands_found, commands_help,
-                             cmd_multiword->GetSubcommandDictionary());
+    if (auto *multiword_cmd = cmd_obj->GetAsMultiwordCommand()) {
+      StringList subcommands_found;
+      FindCommandsForApropos(search_word, subcommands_found, commands_help,
+                             multiword_cmd->GetSubcommandDictionary());
+      for (const auto &subcommand_name : subcommands_found) {
+        std::string qualified_name =
+            (command_name + " " + subcommand_name).str();
+        commands_found.AppendString(qualified_name);
+      }
     }
   }
 }

diff  --git a/lldb/test/API/commands/command/container/TestContainerCommands.py b/lldb/test/API/commands/command/container/TestContainerCommands.py
index 408303dd43a54..0ef31a0da25dc 100644
--- a/lldb/test/API/commands/command/container/TestContainerCommands.py
+++ b/lldb/test/API/commands/command/container/TestContainerCommands.py
@@ -68,7 +68,7 @@ def container_add(self):
         self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
                     substrs=["Hello friend, welcome again to LLDB"])
 
-        self.expect("apropos welcome", "welcome should show up in apropos", substrs=["Just a docstring for the second Welcome"])
+        self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
         
         # Make sure we give good errors when the input is wrong:
         self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component",

diff  --git a/lldb/test/API/commands/command/container/welcome.py b/lldb/test/API/commands/command/container/welcome.py
index 6283e69564006..9d435e1794488 100644
--- a/lldb/test/API/commands/command/container/welcome.py
+++ b/lldb/test/API/commands/command/container/welcome.py
@@ -21,7 +21,7 @@ def __init__(self, debugger, session_dict):
         pass
 
     def get_short_help(self):
-        return "Just a docstring for the second Welcome\nA command that says hello to LLDB users"
+        return "A docstring for the second Welcome\nA command that says hello to LLDB users"
 
     def __call__(self, debugger, args, exe_ctx, result):
         print('Hello ' + args + ', welcome again to LLDB', file=result)


        


More information about the lldb-commits mailing list