[PATCH 1/1] gdb: Introduce new setting to filter out shadowed variables.

Stephan Rohr <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
From: "Sargsyan, Eduard" <[email protected]>

Add a new setting 'print shadowed on|off' to control the printing of
shadowed variables in 'info locals'.

When shadowed variables are present, each variable involved in a
shadowing relationship is annotated with its declaration location
'<file:line>'; the ones hidden by an inner declaration are additionally
marked ', shadowed'.

Given this code, stopped at line 10:

   1 int num = 1;
   2 int
   3 main ()
   4 {
   5   const char *str = "main";
   6   int num = 3;
   7   {
   8     const char *str = "nested";
   9     int num = 5;
  10     num = 0; //  break here
  11   }
  12   return num;
  13 }

By default, 'info locals' prints shadowed variables:

  (gdb) info locals
  str = 0x555555556009 "nested"  <main.c:8>
  num = 5  <main.c:9>
  str = 0x555555556004 "main"  <main.c:5, shadowed>
  num = 3  <main.c:6, shadowed>
  Use 'set print shadowed off' to hide shadowed variables.

The user may not want to print shadowed variables:

  (gdb) set print shadowed off
  (gdb) info locals
  str = 0x555555556009 "nested"
  num = 5
  Use 'set print shadowed on' to include shadowed variables.
---
 gdb/NEWS                                 |  8 ++++++++
 gdb/doc/gdb.texinfo                      | 15 +++++++++++++-
 gdb/printcmd.c                           |  8 +++++++-
 gdb/stack.c                              | 25 +++++++++++++++++++++++-
 gdb/testsuite/gdb.ada/var_shadowing.exp  |  1 +
 gdb/testsuite/gdb.base/options.exp       |  1 +
 gdb/testsuite/gdb.base/var-shadowing.exp | 15 ++++++++++++++
 gdb/valprint.c                           | 20 +++++++++++++++++++
 gdb/valprint.h                           |  3 +++
 9 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 10c182067f9..a3348436984 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,14 @@
 
 *** Changes since GDB 18
 
+* New commands
+
+set print shadowed on|off
+show print shadowed
+  This controls the output of the "info locals" command for C/C++/Fortran.  If
+  the option is 'off' shadowed variables will be omitted in output.  The
+  default is to print shadowed variables.
+
 *** Changes in GDB 18
 
 * Support for the Common Trace Format (CTF) has been removed.  GDB now
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 0030698dcee..232a9901a2c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -9212,7 +9212,8 @@ same name which is declared within an inner scope (decision block,
 method, or inner class).  When shadowing is detected, location
 information is added to all instances of the shadowed variable name.
 The outermost instances are additionally followed by @samp{shadowed}
-to indicate that they are not the active variable.
+to indicate that they are not the active variable.  Printing shadowed
+variables can be controlled by @ref{set print shadowed}.
 
 @item info locals [-q] [-t @var{type_regexp}] [@var{regexp}]
 Like @kbd{info locals}, but only print the local variables selected
@@ -12829,6 +12830,18 @@ Do not pretty print C@t{++} virtual function tables.
 
 @item show print vtbl
 Show whether C@t{++} virtual function tables are pretty printed, or not.
+
+@anchor{set print shadowed}
+@item set print shadowed
+@itemx set print shadowed on
+@cindex shadowed variables in C/C@t{++}/Fortran
+Print shadowed variables for C/C@t{++}/Fortran.  The default is on.
+
+@item set print shadowed off
+Do not print shadowed variables for C/C@t{++}/Fortran.
+
+@item show print shadowed
+Show whether C/C@t{++}/Fortran shadowed variables are printed or not.
 @end table
 
 @node Pretty Printing
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index c9e6e4886e3..303066065f8 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -2402,7 +2402,7 @@ print_variable_value (symbol *var, const frame_info_ptr &frame,
 
       /* Print <%line, shadowed> after the variable value only when it is
 	 variable shadowing case.  */
-      if (shadow_status != var_shadowing::NONE)
+      if (opts.print_shadowed && shadow_status != var_shadowing::NONE)
 	{
 	  /* Use lbasename instead of symtab_to_filename_for_display as the
 	     latter defaults to relative path while basename is preferred
@@ -2454,6 +2454,12 @@ print_variable_and_value (const char *name, symbol *var,
   if (name == nullptr)
     name = var->print_name ();
 
+  struct value_print_options opts;
+  get_user_print_options (&opts);
+
+  if (!opts.print_shadowed && shadow_status == var_shadowing::SHADOWED)
+    return;
+
   gdb_printf (stream, "%*s%ps = ", 2 * indent, "",
 	      styled_string (variable_name_style.style (), name));
 
diff --git a/gdb/stack.c b/gdb/stack.c
index 954ef6b11a4..cd21aa3a40c 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -2258,6 +2258,7 @@ struct print_variable_and_value_data
   int num_tabs;
   struct ui_file *stream;
   int values_printed;
+  bool has_shadowed_variables;
 
   void operator() (const char *print_name, struct symbol *sym,
 		   var_shadowing shadow_status);
@@ -2292,6 +2293,8 @@ print_variable_and_value_data::operator() (const char *print_name,
 			    shadow_status);
 
   values_printed = 1;
+
+  has_shadowed_variables |= (shadow_status == var_shadowing::SHADOWED);
 }
 
 /* Prepares the regular expression REG from REGEXP.
@@ -2351,6 +2354,7 @@ print_frame_local_vars (const frame_info_ptr &frame,
   cb_data.num_tabs = 4 * num_tabs;
   cb_data.stream = stream;
   cb_data.values_printed = 0;
+  cb_data.has_shadowed_variables = false;
 
   /* Temporarily change the selected frame to the given FRAME.
      This allows routines that rely on the selected frame instead
@@ -2360,7 +2364,26 @@ print_frame_local_vars (const frame_info_ptr &frame,
 
   iterate_over_block_local_vars_printing (block, cb_data);
 
-  if (!cb_data.values_printed && !quiet)
+  if (quiet)
+    return;
+
+  if (cb_data.values_printed)
+    {
+      if (!cb_data.has_shadowed_variables)
+	return;
+
+      value_print_options opts;
+      get_user_print_options (&opts);
+      if (opts.print_shadowed)
+	gdb_printf (stream,
+		    _("Use 'set print shadowed off' "
+		      "to hide shadowed variables.\n"));
+      else
+	gdb_printf (stream,
+		    _("Use 'set print shadowed on' "
+		      "to include shadowed variables.\n"));
+    }
+  else
     {
       if (regexp == NULL && t_regexp == NULL)
 	gdb_printf (stream, _("No locals.\n"));
diff --git a/gdb/testsuite/gdb.ada/var_shadowing.exp b/gdb/testsuite/gdb.ada/var_shadowing.exp
index ffa96b049cf..d927b0d9888 100644
--- a/gdb/testsuite/gdb.ada/var_shadowing.exp
+++ b/gdb/testsuite/gdb.ada/var_shadowing.exp
@@ -36,4 +36,5 @@ gdb_test "info locals" [multi_line \
     "i = 111\t<$testfile.adb:$i_level3>"  \
     "i = 11\t<$testfile.adb:$i_level2, shadowed>"  \
     "i = 1\t<$testfile.adb:$i_level1, shadowed>"  \
+    "Use 'set print shadowed off' to hide shadowed variables." \
 ] "info locals at innermost level"
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 35487ead6a7..34e4b642181 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -198,6 +198,7 @@ proc_with_prefix test-print {{prefix ""}} {
 	"-pretty"
 	"-raw-values"
 	"-repeats"
+	"-shadowed"
 	"-static-members"
 	"-symbol"
 	"-union"
diff --git a/gdb/testsuite/gdb.base/var-shadowing.exp b/gdb/testsuite/gdb.base/var-shadowing.exp
index 502cbad11f6..c3b9d77eb77 100644
--- a/gdb/testsuite/gdb.base/var-shadowing.exp
+++ b/gdb/testsuite/gdb.base/var-shadowing.exp
@@ -54,6 +54,7 @@ gdb_test "info locals"  [multi_line \
     "a = 101"   \
     "val1 = 1"  \
     "val2 = 2\t<$srcfile:$val2_d1, shadowed>"  \
+    "Use 'set print shadowed off' to hide shadowed variables." \
     ] "info locals first level"
 
 gdb_breakpoint $srcfile:$bp_line3
@@ -65,6 +66,7 @@ gdb_test "info locals" [multi_line \
     "a = 102"   \
     "val1 = 1\t<$srcfile:$val1_d1, shadowed>"  \
     "val2 = 2\t<$srcfile:$val2_d1, shadowed>"  \
+    "Use 'set print shadowed off' to hide shadowed variables." \
     ] "info locals second level"
 
 gdb_breakpoint $srcfile:$bp_line4
@@ -80,8 +82,21 @@ gdb_test "info locals" [multi_line \
     "a = 103\t<$srcfile:$a_line, shadowed>"   \
     "val1 = 1\t<$srcfile:$val1_d1, shadowed>" \
     "val2 = 2\t<$srcfile:$val2_d1, shadowed>" \
+    "Use 'set print shadowed off' to hide shadowed variables." \
     ] "info locals at innermost level"
 
+gdb_test_no_output "set print shadowed off"
+
+gdb_test "info locals" [multi_line \
+    "a = 999"   \
+    "val1 = 6"  \
+    "val2 = 7"  \
+    "val3 = 8"  \
+    "Use 'set print shadowed on' to include shadowed variables." \
+    ] "info locals at innermost level with filtered out shadowed"
+
+gdb_test_no_output "set print shadowed on"
+
 gdb_breakpoint $srcfile:$bp_line5
 gdb_continue_to_breakpoint "continue to outermost level last" \
     ".*$srcfile:$bp_line5.*"
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 3e7a37338b3..4b65e438c33 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -90,6 +90,7 @@ struct value_print_options user_print_options =
   false,			/* print_array_indexes */
   false,			/* deref_ref */
   true,				/* static_field_print */
+  true,				/* print_shadowed */
   true,				/* pascal_static_field_print */
   false,			/* raw */
   false,			/* summary */
@@ -2395,6 +2396,16 @@ show_static_field_print (struct ui_file *file, int from_tty,
 	      value);
 }
 
+static void
+show_shadowed_print (struct ui_file *file, int from_tty,
+		     struct cmd_list_element *c,
+		     const char *value)
+{
+  gdb_printf (file,
+	      _("Printing of shadowed variables is %s.\n"),
+	      value);
+}
+
 
 
 /* A couple typedefs to make writing the options a bit more
@@ -2557,6 +2568,15 @@ pretty-printers for that value.")
     NULL, /* help_doc */
   },
 
+  boolean_option_def {
+    "shadowed",
+    [] (value_print_options *opt) { return &opt->print_shadowed; },
+    show_shadowed_print, /* show_cmd_cb */
+    N_("Set printing of shadowed variables."),
+    N_("Show printing of shadowed variables."),
+    NULL, /* help_doc */
+  },
+
   boolean_option_def {
     "symbol",
     [] (value_print_options *opt) { return &opt->symbol_print; },
diff --git a/gdb/valprint.h b/gdb/valprint.h
index 15d0be2ac7a..e63c3b9fb03 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -99,6 +99,9 @@ struct value_print_options
   /* If true, print static fields.  */
   bool static_field_print;
 
+  /* If true, print shadowed variables.  */
+  bool print_shadowed;
+
   /* If true, print static fields for Pascal.  FIXME: C++ has a
      flag, why not share with Pascal too?  */
   bool pascal_static_field_print;
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.