[binutils-gdb] Apply styling to error messages

Tom Tromey via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=23ac625889c291ff3ca7e536ceb0e7f140d695a9

commit 23ac625889c291ff3ca7e536ceb0e7f140d695a9
Author: Tom Tromey <[email protected]>
Date:   Sun Jan 11 14:28:12 2026 -0700

    Apply styling to error messages
    
    This patch changes gdb to apply styling to error messages.  The
    approach taken is that styling is always applied when forming an
    exception's string value; and then if styling is not desired, the
    styling is stripped before printing.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32289

Diff:
---
 gdb/exceptions.c               |  6 ++++++
 gdb/ui-file.c                  |  1 +
 gdb/utils.c                    |  4 +++-
 gdbsupport/common-exceptions.h | 12 ++++++++++++
 4 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/gdb/exceptions.c b/gdb/exceptions.c
index 1e6a4f89111..7eb210f02c6 100644
--- a/gdb/exceptions.c
+++ b/gdb/exceptions.c
@@ -66,6 +66,12 @@ print_flush (void)
 static void
 print_exception (struct ui_file *file, const struct gdb_exception &e)
 {
+  /* Exceptions are always created using styling.  If styling is not
+     desired, then it has to be removed here.  */
+  no_terminal_escape_file<wrapped_file<ui_file *>> strip_escapes (file);
+  if (!file->can_emit_style_escape ())
+    file = &strip_escapes;
+
   /* KLUDGE: cagney/2005-01-13: Write the string out one line at a time
      as that way the MI's behavior is preserved.  */
   const char *start;
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index 8830dcfdf8f..006bdc923e4 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -504,3 +504,4 @@ tab_expansion_file::write (const char *buf, long length_buf)
    because these classes aren't instantiated in very many ways.  */
 template class escape_buffering_file<stdio_file>;
 template class no_terminal_escape_file<stdio_file>;
+template class no_terminal_escape_file<wrapped_file<ui_file *>>;
diff --git a/gdb/utils.c b/gdb/utils.c
index bbb6bcb4162..6908256de4d 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -189,7 +189,9 @@ vwarning (const char *string, va_list args)
 void
 verror (const char *string, va_list args)
 {
-  throw_verror (GENERIC_ERROR, string, args);
+  string_file text (true);
+  text.vprintf (string, args);
+  throw gdb_exception_error (GENERIC_ERROR, text.release ());
 }
 
 /* Emit a message and abort.  */
diff --git a/gdbsupport/common-exceptions.h b/gdbsupport/common-exceptions.h
index 040419e30df..5a50ff44c31 100644
--- a/gdbsupport/common-exceptions.h
+++ b/gdbsupport/common-exceptions.h
@@ -130,6 +130,13 @@ struct gdb_exception
   {
   }
 
+  gdb_exception (enum return_reason r, enum errors e, std::string &&str)
+    : reason (r),
+      error (e),
+      message (std::make_shared<std::string> (std::move (str)))
+  {
+  }
+
   gdb_exception (enum return_reason r, enum errors e,
 		 const char *fmt, va_list ap)
     ATTRIBUTE_PRINTF (4, 0)
@@ -277,6 +284,11 @@ struct gdb_exception_error : public gdb_exception
   {
   }
 
+  gdb_exception_error (enum errors e, std::string &&str)
+    : gdb_exception (RETURN_ERROR, e, std::move (str))
+  {
+  }
+
   explicit gdb_exception_error (gdb_exception &&ex) noexcept
     : gdb_exception (std::move (ex))
   {
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.