[v2,04/11] gdb: use compile_symbol_search_regex in stack.c

Message ID 20261001020952.698419-5-simon.marchi@polymtl.ca
State New
Headers
Series None |

Commit Message

Simon Marchi Oct. 1, 2026, 2:09 a.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

prepare_reg in stack.c does the same thing as compile_symbol_search_regex
in symtab.c, but with an awkward output parameter.  Expose
compile_symbol_search_regex in symtab.h and use it instead of prepare_reg.

Since compiled_regex is not movable, the optionals in
print_variable_and_value_data must be initialized directly from the
return value of compile_symbol_search_regex.  Add a constructor to
print_variable_and_value_data to do so.  Make the constructor initialize
the other fields as well, instead of assigning them after construction.

Change-Id: I69939bba3488a888ef17b4d5ec71d5a9efe83dda
---
 gdb/stack.c  | 51 ++++++++++++++++++---------------------------------
 gdb/symtab.c |  6 ++----
 gdb/symtab.h |  8 ++++++++
 3 files changed, 28 insertions(+), 37 deletions(-)
  

Comments

Andrew Burgess Oct. 1, 2026, 1:43 p.m. UTC | #1
simon.marchi@polymtl.ca writes:

> From: Simon Marchi <simon.marchi@polymtl.ca>
>
> prepare_reg in stack.c does the same thing as compile_symbol_search_regex
> in symtab.c, but with an awkward output parameter.  Expose
> compile_symbol_search_regex in symtab.h and use it instead of prepare_reg.
>
> Since compiled_regex is not movable, the optionals in
> print_variable_and_value_data must be initialized directly from the
> return value of compile_symbol_search_regex.  Add a constructor to
> print_variable_and_value_data to do so.  Make the constructor initialize
> the other fields as well, instead of assigning them after construction.

LGTM.

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew
  
Tom Tromey Oct. 1, 2026, 3:36 p.m. UTC | #2
>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> Since compiled_regex is not movable

I have a patch to do that if it would be useful.

Tom
  
Simon Marchi Oct. 1, 2026, 3:52 p.m. UTC | #3
On 10/1/26 11:36 AM, Tom Tromey wrote:
>>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> Since compiled_regex is not movable
> 
> I have a patch to do that if it would be useful.
> 
> Tom

I think the resulting code with the constructor makes sense anyway, so I
would say it's not strictly needed for this series.

Thanks,

Simon
  

Patch

diff --git a/gdb/stack.c b/gdb/stack.c
index 79a67865affe..c6466e2cb799 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -2289,13 +2289,24 @@  iterate_over_block_local_vars_printing
 
 struct print_variable_and_value_data
 {
+  print_variable_and_value_data (const char *regexp, const char *t_regexp,
+				 frame_id frame_id, int num_tabs,
+				 ui_file *stream, bool print_shadowed)
+    : name_regex (compile_symbol_search_regex (regexp)),
+      type_regex (compile_symbol_search_regex (t_regexp)),
+      frame_id (frame_id),
+      num_tabs (num_tabs),
+      stream (stream),
+      print_shadowed (print_shadowed)
+  {}
+
   std::optional<compiled_regex> name_regex;
   std::optional<compiled_regex> type_regex;
   struct frame_id frame_id;
   int num_tabs;
   struct ui_file *stream;
-  int values_printed;
-  bool print_shadowed = true;
+  int values_printed = 0;
+  bool print_shadowed;
   bool printed_shadowed_variables = false;
   bool omitted_shadowed_variables = false;
 
@@ -2344,22 +2355,6 @@  print_variable_and_value_data::operator() (const char *print_name,
     printed_shadowed_variables = true;
 }
 
-/* Prepares the regular expression REG from REGEXP.
-   If REGEXP is NULL, it results in an empty regular expression.  */
-
-static void
-prepare_reg (const char *regexp, std::optional<compiled_regex> *reg)
-{
-  if (regexp != NULL)
-    {
-      int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
-				? REG_ICASE : 0);
-      reg->emplace (regexp, cflags, _("Invalid regexp"));
-    }
-  else
-    reg->reset ();
-}
-
 /* Print all variables from the innermost up to the function block of FRAME.
    Print them with values to STREAM indented by NUM_TABS.
    PRINT_SHADOWED controls whether shadowed variables are printed.
@@ -2379,7 +2374,6 @@  print_frame_local_vars (const frame_info_ptr &frame,
 			const char *t_regexp, int num_tabs,
 			struct ui_file *stream)
 {
-  struct print_variable_and_value_data cb_data;
   const struct block *block;
   std::optional<CORE_ADDR> pc;
 
@@ -2399,13 +2393,9 @@  print_frame_local_vars (const frame_info_ptr &frame,
       return;
     }
 
-  prepare_reg (regexp, &cb_data.name_regex);
-  prepare_reg (t_regexp, &cb_data.type_regex);
-  cb_data.frame_id = get_frame_id (frame);
-  cb_data.num_tabs = 4 * num_tabs;
-  cb_data.stream = stream;
-  cb_data.values_printed = 0;
-  cb_data.print_shadowed = print_shadowed;
+  print_variable_and_value_data cb_data (regexp, t_regexp,
+					 get_frame_id (frame), 4 * num_tabs,
+					 stream, print_shadowed);
 
   /* Temporarily change the selected frame to the given FRAME.
      This allows routines that rely on the selected frame instead
@@ -2572,7 +2562,6 @@  print_frame_arg_vars (const frame_info_ptr &frame,
 		      const char *regexp, const char *t_regexp,
 		      struct ui_file *stream)
 {
-  struct print_variable_and_value_data cb_data;
   struct symbol *func;
   std::optional<CORE_ADDR> pc;
 
@@ -2592,12 +2581,8 @@  print_frame_arg_vars (const frame_info_ptr &frame,
       return;
     }
 
-  prepare_reg (regexp, &cb_data.name_regex);
-  prepare_reg (t_regexp, &cb_data.type_regex);
-  cb_data.frame_id = get_frame_id (frame);
-  cb_data.num_tabs = 0;
-  cb_data.stream = stream;
-  cb_data.values_printed = 0;
+  print_variable_and_value_data cb_data (regexp, t_regexp,
+					 get_frame_id (frame), 0, stream, true);
 
   iterate_over_block_arg_vars (func->value_block (),
     [&] (const char *print_name, symbol *sym)
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 49e8719f4ec0..2c63f9456bc3 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4938,11 +4938,9 @@  global_symbol_searcher::add_matching_msymbols
   return true;
 }
 
-/* Compile REGEXP, a symbol name or symbol type regexp.
-
-   If REGEXP is nullptr, return an empty optional.  */
+/* See symtab.h.  */
 
-static std::optional<compiled_regex>
+std::optional<compiled_regex>
 compile_symbol_search_regex (const char *regexp)
 {
   if (regexp == nullptr)
diff --git a/gdb/symtab.h b/gdb/symtab.h
index e60b32230239..de8cf40974e0 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -2604,6 +2604,14 @@  enum class symbol_search_kind
   MODULE,
 };
 
+/* Compile REGEXP, a regexp used to match symbol names or types.  The regexp
+   is case-insensitive if case sensitivity is off.
+
+   If REGEXP is nullptr, return an empty optional.  */
+
+extern std::optional<compiled_regex> compile_symbol_search_regex
+  (const char *regexp);
+
 /* In order to search for global symbols of a particular kind matching
    particular regular expressions, create an instance of this structure and
    call the SEARCH member function.  */