Skip to content

[lldb] Omit --show-globals in help target var #85855

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 1 commit into from
Mar 20, 2024

Conversation

felipepiovezan
Copy link
Contributor

This option doesn't exist. It is currently displayed by help target var due to a bug introduced by 41ae8e7 in 2018.

Some code for target var and frame var is shared, and some hard-code constants are used in order to filter out options that belong only to frame var. However, the aforementioned commit failed to update these constants properly. This patch addresses the issue by having a single place where the filtering of options needs to be done.

This option doesn't exist. It is currently displayed by `help target var` due to
a bug introduced by 41ae8e7 in 2018.

Some code for `target var` and `frame var` is shared, and some hard-code
constants are used in order to filter out options that belong only to `frame
var`. However, the aforementioned commit failed to update these constants
properly. This patch addresses the issue by having a _single_ place where the
filtering of options needs to be done.
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2024

@llvm/pr-subscribers-lldb

Author: Felipe de Azevedo Piovezan (felipepiovezan)

Changes

This option doesn't exist. It is currently displayed by help target var due to a bug introduced by 41ae8e7 in 2018.

Some code for target var and frame var is shared, and some hard-code constants are used in order to filter out options that belong only to frame var. However, the aforementioned commit failed to update these constants properly. This patch addresses the issue by having a single place where the filtering of options needs to be done.


Full diff: https://github.com/llvm/llvm-project/pull/85855.diff

2 Files Affected:

  • (modified) lldb/source/Interpreter/OptionGroupVariable.cpp (+12-14)
  • (modified) lldb/test/API/functionalities/target_var/TestTargetVar.py (+10)
diff --git a/lldb/source/Interpreter/OptionGroupVariable.cpp b/lldb/source/Interpreter/OptionGroupVariable.cpp
index 0e35a641361b82..99644b3f423c81 100644
--- a/lldb/source/Interpreter/OptionGroupVariable.cpp
+++ b/lldb/source/Interpreter/OptionGroupVariable.cpp
@@ -50,6 +50,11 @@ static constexpr OptionDefinition g_variable_options[] = {
      "Specify a summary string to use to format the variable output."},
 };
 
+static constexpr auto g_num_frame_options = 4;
+static const auto g_variable_options_noframe =
+    llvm::ArrayRef<OptionDefinition>(g_variable_options)
+        .drop_front(g_num_frame_options);
+
 static Status ValidateNamedSummary(const char *str, void *) {
   if (!str || !str[0])
     return Status("must specify a valid named summary");
@@ -77,9 +82,9 @@ OptionGroupVariable::SetOptionValue(uint32_t option_idx,
                                     llvm::StringRef option_arg,
                                     ExecutionContext *execution_context) {
   Status error;
-  if (!include_frame_options)
-    option_idx += 3;
-  const int short_option = g_variable_options[option_idx].short_option;
+  llvm::ArrayRef<OptionDefinition> variable_options =
+      include_frame_options ? g_variable_options : g_variable_options_noframe;
+  const int short_option = variable_options[option_idx].short_option;
   switch (short_option) {
   case 'r':
     use_regex = true;
@@ -128,16 +133,9 @@ void OptionGroupVariable::OptionParsingStarting(
   summary_string.Clear();
 }
 
-#define NUM_FRAME_OPTS 3
-
 llvm::ArrayRef<OptionDefinition> OptionGroupVariable::GetDefinitions() {
-  auto result = llvm::ArrayRef(g_variable_options);
-  // Show the "--no-args", "--no-locals" and "--show-globals" options if we are
-  // showing frame specific options
-  if (include_frame_options)
-    return result;
-
-  // Skip the "--no-args", "--no-locals" and "--show-globals" options if we are
-  // not showing frame specific options (globals only)
-  return result.drop_front(NUM_FRAME_OPTS);
+  // Show the "--no-args", "--no-recognized-args", "--no-locals" and
+  // "--show-globals" options if we are showing frame specific options
+  return include_frame_options ? g_variable_options
+                               : g_variable_options_noframe;
 }
diff --git a/lldb/test/API/functionalities/target_var/TestTargetVar.py b/lldb/test/API/functionalities/target_var/TestTargetVar.py
index a0f3663f036510..54b7b77b6773ce 100644
--- a/lldb/test/API/functionalities/target_var/TestTargetVar.py
+++ b/lldb/test/API/functionalities/target_var/TestTargetVar.py
@@ -15,6 +15,16 @@ class targetCommandTestCase(TestBase):
     def testTargetVarExpr(self):
         self.build()
         lldbutil.run_to_name_breakpoint(self, "main")
+        self.expect(
+            "help target variable",
+            substrs=[
+                "--no-args",
+                "--no-recognized-args",
+                "--no-locals",
+                "--show-globals",
+            ],
+            matching=False,
+        )
         self.expect("target variable i", substrs=["i", "42"])
         self.expect(
             "target variable var", patterns=["\(incomplete \*\) var = 0[xX](0)*dead"]

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.

Big fan of centralizing the option parsing here. I had one question about automating another portion of this, but otherwise LGTM.

@@ -50,6 +50,11 @@ static constexpr OptionDefinition g_variable_options[] = {
"Specify a summary string to use to format the variable output."},
};

static constexpr auto g_num_frame_options = 4;
Copy link
Member

Choose a reason for hiding this comment

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

Is this number computable? Or does it need to be hand-maintained?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly it seems we need to hand-maintain it. The basic thing we need to maintain is "here is an array of options for target var, and here is an array of options for frame var", and one array is a suffix of the other. And they have to be arrays, because of the indexing that is done on them.
Given all of that, I don't think there is a clean way to avoid having to maintain this constant, but the hope is that with the new approach it becomes harder to make the mistake that introduced this bug.

@felipepiovezan felipepiovezan merged commit 1232964 into llvm:main Mar 20, 2024
@felipepiovezan felipepiovezan deleted the felipe/fix_target_var_help branch March 20, 2024 14:03
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
This option doesn't exist. It is currently displayed by `help target
var` due to a bug introduced by 41ae8e7 in 2018.

Some code for `target var` and `frame var` is shared, and some hard-code
constants are used in order to filter out options that belong only to
`frame var`. However, the aforementioned commit failed to update these
constants properly. This patch addresses the issue by having a _single_
place where the filtering of options needs to be done.
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.

4 participants