ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output)

Message ID 39321bf6-9a38-df4a-102a-abf4c04ad21d@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves June 5, 2019, 8:42 p.m. UTC
  On 6/5/19 9:39 PM, Pedro Alves wrote:

> The format strings do get a bit ... wild:
> 
>    "%pS%s%pN  %pS%s%pN\n"
> 
> With your suggestion, it'd look like:
> 
>    "%p[%s%p]  %p[%s%p]\n"
> 
> I'm thinking that %pS/%pN %p[/%pN] are more appropriate when
> the styled string is constant, like:
> 
>    "this %p[text here%p] should be styled\n"
> 
> For the seemingly common case of printing a string variable
> with a style, I'm thinking that a specific formatter would
> be better.  I'll post a follow up patch for that.

Here it is.  I used %ps.

I pushed these two to the branch, in case you want to play with
them.  (I'm not likely to play with this any more today.)

From 52bf8432db926e2ce2ff8bdbececd242e93b4ca7 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Wed, 5 Jun 2019 21:15:24 +0100
Subject: [PATCH 2/2] Introduce %ps / styled_string

---
 gdb/common/format.c |  1 +
 gdb/symtab.c        | 24 +++++++++++-------------
 gdb/ui-out.c        |  6 ++++++
 gdb/ui-out.h        | 20 ++++++++++++++++++++
 4 files changed, 38 insertions(+), 13 deletions(-)
  

Comments

Tom Tromey June 5, 2019, 8:49 p.m. UTC | #1
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Here it is.  I used %ps.

Looks good to me.

Tom
  

Patch

diff --git a/gdb/common/format.c b/gdb/common/format.c
index d33eab2b2b0..177f79afee3 100644
--- a/gdb/common/format.c
+++ b/gdb/common/format.c
@@ -256,6 +256,7 @@  format_pieces::format_pieces (const char **arg, bool gdb_extensions)
 	      {
 		switch (f[1])
 		  {
+		  case 's':
 		  case 'S':
 		  case 'F':
 		  case 'N':
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 5696d6fa890..7dbf24eb7c5 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4603,10 +4603,9 @@  print_symbol_info (enum search_domain kind,
 
       if (filename_cmp (last, s_filename) != 0)
 	{
-	  current_uiout->message ("\nFile %pS%s%pN:\n",
-				  ptr (ui_out_style_kind::FILE),
-				  s_filename,
-				  nullptr);
+	  current_uiout->message
+	    (_("\nFile %ps:\n"),
+	     styled_string (ui_out_style_kind::FILE, s_filename).ptr ());
 	}
 
       if (SYMBOL_LINE (sym) != 0)
@@ -4653,15 +4652,14 @@  print_msymbol_info (struct bound_minimal_symbol msymbol)
     tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol),
 			     16);
 
-  current_uiout->message (_("%pS%s%pN  %pS%s%pN\n"),
-			  ptr (ui_out_style_kind::ADDRESS),
-			  tmp,
-			  nullptr,
-			  (msymbol.minsym->text_p ()
-			   ? ptr (ui_out_style_kind::FUNCTION)
-			   : ptr (ui_out_style_kind::DEFAULT)),
-			   MSYMBOL_PRINT_NAME (msymbol.minsym),
-			  nullptr);
+  ui_out_style_kind sym_style = (msymbol.minsym->text_p ()
+				 ? ui_out_style_kind::FUNCTION
+				 : ui_out_style_kind::DEFAULT);
+
+  current_uiout->message
+    (_("%ps  %ps\n"),
+     styled_string (ui_out_style_kind::ADDRESS, tmp).ptr (),
+     styled_string (sym_style, MSYMBOL_PRINT_NAME (msymbol.minsym)).ptr ());
 }
 
 /* This is the guts of the commands "info functions", "info types", and
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 6e1c87ab841..4169fdcb935 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -612,6 +612,12 @@  ui_out::message (const char *format, ...)
 		field_int (field->name (), field->val ());
 	      }
 	      break;
+	    case 's':
+	      {
+		styled_string *ss = va_arg (args, styled_string *);
+		call_do_message (ss->style (), "%s", ss->str ());
+	      }
+	      break;
 	    case 'S':
 	      style = *va_arg (args, ui_out_style_kind *);
 	      break;
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 18af856313d..059d1e376aa 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -103,6 +103,26 @@  private:
   int m_val;
 };
 
+struct styled_string
+{
+  styled_string (ui_out_style_kind style, const char *str)
+    : m_style (style),
+      m_str (str)
+  {
+  }
+
+  /* We need this because we can't pass a reference via
+     va_args.  */
+  const styled_string *ptr () const { return this; }
+
+  ui_out_style_kind style () const {return m_style; }
+  const char *str () const {return m_str; }
+
+private:
+  ui_out_style_kind m_style;
+  const char *m_str;
+};
+
 /* Wrap a ui_out_style_kind in a pointer to a temporary.  */
 
 /* XXX: Make a template?  */