[Lldb-commits] [lldb] 7f05ff8 - [Bug 49018][lldb] Fix incorrect help text for 'memory write' command

Venkata Ramanaiah Nalamothu via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 26 05:44:33 PST 2021


Author: Venkata Ramanaiah Nalamothu
Date: 2021-11-26T19:14:26+05:30
New Revision: 7f05ff8be481f6db23615c028280fd92c2080f5f

URL: https://github.com/llvm/llvm-project/commit/7f05ff8be481f6db23615c028280fd92c2080f5f
DIFF: https://github.com/llvm/llvm-project/commit/7f05ff8be481f6db23615c028280fd92c2080f5f.diff

LOG: [Bug 49018][lldb] Fix incorrect help text for 'memory write' command

Certain commands like 'memory write', 'register read' etc all use
the OptionGroupFormat options but the help usage text for those
options is not customized to those commands.

One such example is:

  (lldb) help memory read
           -s <byte-size> ( --size <byte-size> )
               The size in bytes to use when displaying with the selected format.
  (lldb) help memory write
	   -s <byte-size> ( --size <byte-size> )
               The size in bytes to use when displaying with the selected format.

This patch allows such commands to overwrite the help text for the options
in the OptionGroupFormat group as needed and fixes help text of memory write.

llvm.org/pr49018.

Reviewed By: DavidSpickett

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

Added: 
    

Modified: 
    lldb/include/lldb/Interpreter/OptionGroupFormat.h
    lldb/source/Commands/CommandObjectMemory.cpp
    lldb/source/Interpreter/OptionGroupFormat.cpp
    lldb/test/API/commands/help/TestHelp.py

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Interpreter/OptionGroupFormat.h b/lldb/include/lldb/Interpreter/OptionGroupFormat.h
index 2d445b8a6c20a..551688b0d25f1 100644
--- a/lldb/include/lldb/Interpreter/OptionGroupFormat.h
+++ b/lldb/include/lldb/Interpreter/OptionGroupFormat.h
@@ -16,6 +16,9 @@
 
 namespace lldb_private {
 
+typedef std::vector<std::tuple<lldb::CommandArgumentType, const char *>>
+    OptionGroupFormatUsageTextVector;
+
 // OptionGroupFormat
 
 class OptionGroupFormat : public OptionGroup {
@@ -30,7 +33,10 @@ class OptionGroupFormat : public OptionGroup {
       uint64_t default_byte_size =
           UINT64_MAX, // Pass UINT64_MAX to disable the "--size" option
       uint64_t default_count =
-          UINT64_MAX); // Pass UINT64_MAX to disable the "--count" option
+          UINT64_MAX, // Pass UINT64_MAX to disable the "--count" option
+      OptionGroupFormatUsageTextVector usage_text_vector = {}
+      // Use to override default option usage text with the command specific one
+  );
 
   ~OptionGroupFormat() override = default;
 
@@ -73,6 +79,7 @@ class OptionGroupFormat : public OptionGroup {
   char m_prev_gdb_format;
   char m_prev_gdb_size;
   bool m_has_gdb_format;
+  OptionDefinition m_option_definitions[4];
 };
 
 } // namespace lldb_private

diff  --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp
index ff508a939f0be..094ce6f8558fe 100644
--- a/lldb/source/Commands/CommandObjectMemory.cpp
+++ b/lldb/source/Commands/CommandObjectMemory.cpp
@@ -1222,7 +1222,15 @@ class CommandObjectMemoryWrite : public CommandObjectParsed {
             interpreter, "memory write",
             "Write to the memory of the current target process.", nullptr,
             eCommandRequiresProcess | eCommandProcessMustBeLaunched),
-        m_option_group(), m_format_options(eFormatBytes, 1, UINT64_MAX),
+        m_option_group(),
+        m_format_options(
+            eFormatBytes, 1, UINT64_MAX,
+            {std::make_tuple(
+                 eArgTypeFormat,
+                 "The format to use for each of the value to be written."),
+             std::make_tuple(
+                 eArgTypeByteSize,
+                 "The size in bytes to write from input file or each value.")}),
         m_memory_options() {
     CommandArgumentEntry arg1;
     CommandArgumentEntry arg2;

diff  --git a/lldb/source/Interpreter/OptionGroupFormat.cpp b/lldb/source/Interpreter/OptionGroupFormat.cpp
index 1cc5e70282c1a..a2ca9ff398189 100644
--- a/lldb/source/Interpreter/OptionGroupFormat.cpp
+++ b/lldb/source/Interpreter/OptionGroupFormat.cpp
@@ -16,15 +16,7 @@
 using namespace lldb;
 using namespace lldb_private;
 
-OptionGroupFormat::OptionGroupFormat(lldb::Format default_format,
-                                     uint64_t default_byte_size,
-                                     uint64_t default_count)
-    : m_format(default_format, default_format),
-      m_byte_size(default_byte_size, default_byte_size),
-      m_count(default_count, default_count), m_prev_gdb_format('x'),
-      m_prev_gdb_size('w') {}
-
-static constexpr OptionDefinition g_option_table[] = {
+static constexpr OptionDefinition g_default_option_definitions[] = {
     {LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument,
      nullptr, {}, 0, eArgTypeFormat,
      "Specify a format to be used for display."},
@@ -39,8 +31,34 @@ static constexpr OptionDefinition g_option_table[] = {
      "The number of total items to display."},
 };
 
+OptionGroupFormat::OptionGroupFormat(
+    lldb::Format default_format, uint64_t default_byte_size,
+    uint64_t default_count, OptionGroupFormatUsageTextVector usage_text_vector)
+    : m_format(default_format, default_format),
+      m_byte_size(default_byte_size, default_byte_size),
+      m_count(default_count, default_count), m_prev_gdb_format('x'),
+      m_prev_gdb_size('w') {
+  // Copy the default option definitions.
+  std::copy(std::begin(g_default_option_definitions),
+            std::end(g_default_option_definitions),
+            std::begin(m_option_definitions));
+
+  for (auto usage_text_tuple : usage_text_vector) {
+    switch (std::get<0>(usage_text_tuple)) {
+    case eArgTypeFormat:
+      m_option_definitions[0].usage_text = std::get<1>(usage_text_tuple);
+      break;
+    case eArgTypeByteSize:
+      m_option_definitions[2].usage_text = std::get<1>(usage_text_tuple);
+      break;
+    default:
+      llvm_unreachable("Unimplemented option");
+    }
+  }
+}
+
 llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() {
-  auto result = llvm::makeArrayRef(g_option_table);
+  auto result = llvm::makeArrayRef(m_option_definitions);
   if (m_byte_size.GetDefaultValue() < UINT64_MAX) {
     if (m_count.GetDefaultValue() < UINT64_MAX)
       return result;
@@ -54,7 +72,7 @@ Status OptionGroupFormat::SetOptionValue(uint32_t option_idx,
                                          llvm::StringRef option_arg,
                                          ExecutionContext *execution_context) {
   Status error;
-  const int short_option = g_option_table[option_idx].short_option;
+  const int short_option = m_option_definitions[option_idx].short_option;
 
   switch (short_option) {
   case 'f':

diff  --git a/lldb/test/API/commands/help/TestHelp.py b/lldb/test/API/commands/help/TestHelp.py
index c6af3b1877e1f..5bd85ddcf7062 100644
--- a/lldb/test/API/commands/help/TestHelp.py
+++ b/lldb/test/API/commands/help/TestHelp.py
@@ -225,3 +225,21 @@ def test_help_format_output(self):
             "help format",
             matching=True,
             substrs=['<format> -- One of the format names'])
+
+    @no_debug_info_test
+    def test_help_option_group_format_options_usage(self):
+        """Test that help on commands that use OptionGroupFormat options provide relevant help specific to that command."""
+        self.expect(
+            "help memory read",
+            matching=True,
+            substrs=[
+                "-f <format> ( --format <format> )", "Specify a format to be used for display.",
+                "-s <byte-size> ( --size <byte-size> )", "The size in bytes to use when displaying with the selected format."])
+
+        self.expect(
+            "help memory write",
+            matching=True,
+            substrs=[
+                "-f <format> ( --format <format> )", "The format to use for each of the value to be written.",
+                "-s <byte-size> ( --size <byte-size> )", "The size in bytes to write from input file or each value."])
+


        


More information about the lldb-commits mailing list