[v3,8/8] Rewrite Fortran character printer

Message ID 20251228-string-printing-2-v3-8-ec5cc9dd0c71@tromey.com
State New
Headers
Series Refactor character printing |

Commit Message

Tom Tromey Dec. 28, 2025, 8:13 p.m. UTC
  This adds a Fortran-specific subclass of wchar_printer and arranges to
use it.
---
 gdb/f-lang.c | 27 ---------------------------
 gdb/f-lang.h | 19 +++++--------------
 2 files changed, 5 insertions(+), 41 deletions(-)
  

Patch

diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index 2720373e4f49f56ab5bf0e3a5fac003683c5e646..021a403c60eb99a4612c6429858ca2b39041bb9d 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -73,33 +73,6 @@  static value *fortran_prepare_argument (struct expression *exp,
 					int arg_num, bool is_internal_call_p,
 					struct type *func_type, enum noside noside);
 
-/* Return the encoding that should be used for the character type
-   TYPE.  */
-
-const char *
-f_language::get_encoding (struct type *type)
-{
-  const char *encoding;
-
-  switch (type->length ())
-    {
-    case 1:
-      encoding = target_charset (type->arch ());
-      break;
-    case 4:
-      if (type_byte_order (type) == BFD_ENDIAN_BIG)
-	encoding = "UTF-32BE";
-      else
-	encoding = "UTF-32LE";
-      break;
-
-    default:
-      error (_("unrecognized character type"));
-    }
-
-  return encoding;
-}
-
 /* See language.h.  */
 
 struct value *
diff --git a/gdb/f-lang.h b/gdb/f-lang.h
index e49c58426430d20455dd806ae731c71b5ad7cbc7..22c1066ac411045f5b202133b34be638671003c7 100644
--- a/gdb/f-lang.h
+++ b/gdb/f-lang.h
@@ -25,6 +25,7 @@ 
 
 #include "language.h"
 #include "valprint.h"
+#include "char-print.h"
 
 struct type_print_options;
 struct parser_state;
@@ -161,8 +162,8 @@  class f_language : public language_defn
   void printchar (int ch, struct type *chtype,
 		  struct ui_file *stream) const override
   {
-    const char *encoding = get_encoding (chtype);
-    generic_emit_char (ch, chtype, stream, '\'', encoding);
+    wchar_printer printer (chtype, '\'');
+    printer.print (ch, stream);
   }
 
   /* See language.h.  */
@@ -172,16 +173,11 @@  class f_language : public language_defn
 		 const char *encoding, int force_ellipses,
 		 const struct value_print_options *options) const override
   {
-    const char *type_encoding = get_encoding (elttype);
-
     if (elttype->length () == 4)
       gdb_puts ("4_", stream);
 
-    if (!encoding || !*encoding)
-      encoding = type_encoding;
-
-    generic_printstr (stream, elttype, string, length, encoding,
-		      force_ellipses, '\'', 0, options);
+    wchar_printer printer (elttype, '\'', encoding);
+    printer.print (stream, string, length, force_ellipses, 0, options);
   }
 
   /* See language.h.  */
@@ -237,11 +233,6 @@  class f_language : public language_defn
 	(const lookup_name_info &lookup_name) const override;
 
 private:
-  /* Return the encoding that should be used for the character type
-     TYPE.  */
-
-  static const char *get_encoding (struct type *type);
-
   /* Print any asterisks or open-parentheses needed before the variable
      name (to describe its type).