]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Apply styling to error messages
authorTom Tromey <tom@tromey.com>
Sun, 11 Jan 2026 21:28:12 +0000 (14:28 -0700)
committerTom Tromey <tom@tromey.com>
Mon, 23 Mar 2026 18:51:57 +0000 (12:51 -0600)
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

gdb/exceptions.c
gdb/ui-file.c
gdb/utils.c
gdbsupport/common-exceptions.h

index 1e6a4f891111a9d72b08b34c58e79c9f39d091e4..7eb210f02c6e55b2022f3b107dfbbd775ecc9965 100644 (file)
@@ -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;
index 8830dcfdf8fab6e47a73aea994a8cb4e1a103f7d..006bdc923e4c037458d2d121912d94a8d0f53cb5 100644 (file)
@@ -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 *>>;
index bbb6bcb416240addde767244e03eb215bf4483e1..6908256de4d4a4d9b863940d8cba374d827d30f1 100644 (file)
@@ -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.  */
index 040419e30df9759d0ccd2bb4482e40275f48a351..5a50ff44c31bf3ebb4037f96657ba274f5e2ecf1 100644 (file)
@@ -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))
   {