Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output)
Date: Wed, 05 Jun 2019 20:42:00 -0000	[thread overview]
Message-ID: <39321bf6-9a38-df4a-102a-abf4c04ad21d@redhat.com> (raw)
In-Reply-To: <625cd0ba-058d-d4bf-8ba3-8676f335b0f3@redhat.com>

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(-)

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?  */
-- 
2.14.5


  reply	other threads:[~2019-06-05 20:42 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-05  2:01 [PATCH] Style "pwd" output Tom Tromey
2019-06-05  8:36 ` Pedro Alves
2019-06-05 13:42   ` Tom Tromey
2019-06-05 15:21     ` Pedro Alves
2019-06-05 18:12       ` ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) Pedro Alves
2019-06-05 20:27         ` Tom Tromey
2019-06-05 20:39           ` Pedro Alves
2019-06-05 20:42             ` Pedro Alves [this message]
2019-06-05 20:49               ` Tom Tromey
2019-06-05 20:47             ` Tom Tromey
2019-06-05 21:25               ` Pedro Alves
2019-06-05 22:21                 ` Tom Tromey
2019-06-06 15:49                   ` Pedro Alves
2019-06-06 23:55                     ` Tom Tromey
2019-06-07 18:27                   ` Tom Tromey
2019-06-07 19:20                     ` Tom Tromey
2019-07-01 12:23                       ` Pedro Alves
2019-07-01 12:55                         ` Pedro Alves
2019-07-01 13:06                           ` Pedro Alves
2019-07-01 17:26                             ` Tom Tromey
2019-07-01 19:24                               ` [users/palves/format_strings] Down with .ptr() (Re: ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output)) Pedro Alves
2019-07-01 13:17                           ` ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) Pedro Alves
2019-07-01 13:20                             ` Pedro Alves
2019-07-01 17:38                             ` Tom Tromey
2019-07-01 18:49                               ` Tom Tromey
2019-07-01 18:56                                 ` Pedro Alves
2019-07-01 19:30                                   ` [users/palves/format_strings] Document the gdb-specific formatters Pedro Alves
2019-07-01 19:25                               ` [users/palves/format_strings] Introduce string_field Pedro Alves
2019-07-01 17:43                         ` ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) Tom Tromey
2019-07-01 19:29                           ` [users/palves/format_strings] Make printf_filtered support the gdb-specific formatters too Pedro Alves
2019-07-01 12:01                     ` ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) Pedro Alves
2019-07-01 12:25                       ` Tom Tromey
2019-07-01 12:37                         ` Pedro Alves
2019-07-01 17:20                           ` Tom Tromey
2019-07-01 19:27                             ` [users/palves/format_strings] %pS/%pN -> %p[/%p] Pedro Alves
2019-07-01 19:32                         ` ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) Philippe Waroquiers
2019-07-03 12:20                           ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=39321bf6-9a38-df4a-102a-abf4c04ad21d@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox