[binutils-gdb] Rewrite Modula-2 character printer
Tom Tromey
tromey@sourceware.org
Sat Jan 31 19:27:38 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a51fd5b8c25f2a82bbbba62ad72b6894cb07fca7
commit a51fd5b8c25f2a82bbbba62ad72b6894cb07fca7
Author: Tom Tromey <tom@tromey.com>
Date: Tue Dec 23 12:46:38 2025 -0700
Rewrite Modula-2 character printer
This adds a Modula-2-specific subclass of wchar_printer and arranges
to use it.
Diff:
---
gdb/m2-lang.c | 144 ++++++++++++----------------------------------------------
gdb/m2-lang.h | 5 --
2 files changed, 28 insertions(+), 121 deletions(-)
diff --git a/gdb/m2-lang.c b/gdb/m2-lang.c
index 8b4c53e8730..de8edacbdaf 100644
--- a/gdb/m2-lang.c
+++ b/gdb/m2-lang.c
@@ -29,6 +29,7 @@
#include "valprint.h"
#include "gdbarch.h"
#include "m2-exp.h"
+#include "char-print.h"
/* A helper function for UNOP_HIGH. */
@@ -139,15 +140,37 @@ m2_language::language_arch_info (struct gdbarch *gdbarch,
lai->set_bool_type (builtin->builtin_bool, "BOOLEAN");
}
+class m2_wchar_printer : public wchar_printer
+{
+public:
+
+ using wchar_printer::wchar_printer;
+
+protected:
+
+ bool printable (gdb_wchar_t w) const override
+ {
+ /* Historically the Modula-2 code in gdb handled \e as well. */
+ return (w == LCST ('\033')
+ || wchar_printer::printable (w));
+ }
+
+ void print_char (gdb_wchar_t w) override
+ {
+ if (w == LCST ('\033'))
+ m_file.write (LCST ("\\e"));
+ else
+ wchar_printer::print_char (w);
+ }
+};
+
/* See language.h. */
void
m2_language::printchar (int c, struct type *type,
struct ui_file *stream) const
{
- gdb_puts ("'", stream);
- emitchar (c, type, stream, '\'');
- gdb_puts ("'", stream);
+ m2_wchar_printer (type, '\'').print (c, stream);
}
/* See language.h. */
@@ -158,119 +181,8 @@ m2_language::printstr (struct ui_file *stream, struct type *elttype,
const char *encoding, int force_ellipses,
const struct value_print_options *options) const
{
- unsigned int i;
- unsigned int things_printed = 0;
- int in_quotes = 0;
- int need_comma = 0;
-
- if (length == 0)
- {
- gdb_puts ("\"\"");
- return;
- }
-
- unsigned int print_max_chars = get_print_max_chars (options);
- for (i = 0; i < length && things_printed < print_max_chars; ++i)
- {
- /* Position of the character we are examining
- to see whether it is repeated. */
- unsigned int rep1;
- /* Number of repetitions we have detected so far. */
- unsigned int reps;
-
- QUIT;
-
- if (need_comma)
- {
- gdb_puts (", ", stream);
- need_comma = 0;
- }
-
- rep1 = i + 1;
- reps = 1;
- while (rep1 < length && string[rep1] == string[i])
- {
- ++rep1;
- ++reps;
- }
-
- if (reps > options->repeat_count_threshold)
- {
- if (in_quotes)
- {
- gdb_puts ("\", ", stream);
- in_quotes = 0;
- }
- printchar (string[i], elttype, stream);
- gdb_printf (stream, " <repeats %u times>", reps);
- i = rep1 - 1;
- things_printed += options->repeat_count_threshold;
- need_comma = 1;
- }
- else
- {
- if (!in_quotes)
- {
- gdb_puts ("\"", stream);
- in_quotes = 1;
- }
- emitchar (string[i], elttype, stream, '"');
- ++things_printed;
- }
- }
-
- /* Terminate the quotes if necessary. */
- if (in_quotes)
- gdb_puts ("\"", stream);
-
- if (force_ellipses || i < length)
- gdb_puts ("...", stream);
-}
-
-/* See language.h. */
-
-void
-m2_language::emitchar (int ch, struct type *chtype,
- struct ui_file *stream, int quoter) const
-{
- ch &= 0xFF; /* Avoid sign bit follies. */
-
- if (PRINT_LITERAL_FORM (ch))
- {
- if (ch == '\\' || ch == quoter)
- gdb_puts ("\\", stream);
- gdb_printf (stream, "%c", ch);
- }
- else
- {
- switch (ch)
- {
- case '\n':
- gdb_puts ("\\n", stream);
- break;
- case '\b':
- gdb_puts ("\\b", stream);
- break;
- case '\t':
- gdb_puts ("\\t", stream);
- break;
- case '\f':
- gdb_puts ("\\f", stream);
- break;
- case '\r':
- gdb_puts ("\\r", stream);
- break;
- case '\033':
- gdb_puts ("\\e", stream);
- break;
- case '\007':
- gdb_puts ("\\a", stream);
- break;
- default:
- gdb_printf (stream, "\\%.3o", (unsigned int) ch);
- break;
- }
- }
+ m2_wchar_printer printer (elttype, '"', encoding);
+ printer.print (stream, string, length, force_ellipses, 0, options);
}
/* Called during architecture gdbarch initialisation to create language
diff --git a/gdb/m2-lang.h b/gdb/m2-lang.h
index f7c8e0e8d5b..3492b4cd4fe 100644
--- a/gdb/m2-lang.h
+++ b/gdb/m2-lang.h
@@ -92,11 +92,6 @@ public:
/* See language.h. */
- void emitchar (int ch, struct type *chtype,
- struct ui_file *stream, int quoter) const;
-
- /* See language.h. */
-
void printchar (int ch, struct type *chtype,
struct ui_file *stream) const override;
More information about the Gdb-cvs
mailing list