gdb: switch "set language" to getter/setter
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 17 Apr 2023 16:36:51 +0000 (12:36 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Fri, 21 Apr 2023 18:09:42 +0000 (14:09 -0400)
The `language` global variable is mostly a scratch variable used for the
setting.  The source of truth is really current_language and
language_mode (auto vs manual), which are set by the
set_language_command callback.

Switch the setting to use the add_setshow_enum_cmd overload that takes a
value getter and setter.

Change-Id: Ief5b2f93fd7337eed7ec96023639ae3dfe62250b
Reviewed-By: Tom Tromey <tom@tromey.com>
gdb/language.c

index 1ab356597d74b602e36f203ac4973dea05d9a40f..42bce92c647c5b4799dadea24b7e621f2527c213 100644 (file)
@@ -93,7 +93,6 @@ const struct language_defn *language_defn::languages[nr_languages];
 
 /* The current values of the "set language/range/case-sensitive" enum
    commands.  */
-static const char *language;
 static const char *range;
 static const char *case_sensitive;
 
@@ -135,10 +134,10 @@ show_language_command (struct ui_file *file, int from_tty,
     }
 }
 
-/* Set command.  Change the current working language.  */
+/* Set callback for the "set/show language" setting.  */
+
 static void
-set_language_command (const char *ignore,
-                     int from_tty, struct cmd_list_element *c)
+set_language (const char *language)
 {
   enum language flang = language_unknown;
 
@@ -192,6 +191,17 @@ set_language_command (const char *ignore,
                  language);
 }
 
+/* Get callback for the "set/show language" setting.  */
+
+static const char *
+get_language ()
+{
+  if (language_mode == language_mode_auto)
+    return "auto";
+
+  return current_language->name ();
+}
+
 /* Show command.  Display a warning if the range setting does
    not match the current language.  */
 static void
@@ -372,7 +382,7 @@ language_info ()
     return;
 
   expected_language = current_language;
-  gdb_printf (_("Current language:  %s\n"), language);
+  gdb_printf (_("Current language:  %s\n"), get_language ());
   show_language_command (gdb_stdout, 1, NULL, NULL);
 }
 \f
@@ -465,8 +475,7 @@ add_set_language_command ()
   /* Display "auto", "local" and "unknown" first, and then the rest,
      alpha sorted.  */
   const char **language_names_p = language_names;
-  language = language_def (language_auto)->name ();
-  *language_names_p++ = language;
+  *language_names_p++ = language_def (language_auto)->name ();;
   *language_names_p++ = "local";
   *language_names_p++ = language_def (language_unknown)->name ();
   const char **sort_begin = language_names_p;
@@ -509,10 +518,11 @@ add_set_language_command ()
 
   add_setshow_enum_cmd ("language", class_support,
                        language_names,
-                       &language,
                        doc.c_str (),
                        _("Show the current source language."),
-                       NULL, set_language_command,
+                       NULL,
+                       set_language,
+                       get_language,
                        show_language_command,
                        &setlist, &showlist);
 }