From patchwork Wed Jun 5 20:42:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 33032 Received: (qmail 37644 invoked by alias); 5 Jun 2019 20:42:30 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 37579 invoked by uid 89); 5 Jun 2019 20:42:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=*str X-HELO: mail-wr1-f68.google.com Received: from mail-wr1-f68.google.com (HELO mail-wr1-f68.google.com) (209.85.221.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 05 Jun 2019 20:42:28 +0000 Received: by mail-wr1-f68.google.com with SMTP id x17so111875wrl.9 for ; Wed, 05 Jun 2019 13:42:28 -0700 (PDT) Return-Path: Received: from ?IPv6:2001:8a0:f913:f700:4eeb:42ff:feef:f164? ([2001:8a0:f913:f700:4eeb:42ff:feef:f164]) by smtp.gmail.com with ESMTPSA id k66sm12201731wmb.34.2019.06.05.13.42.25 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:42:25 -0700 (PDT) Subject: Re: ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) To: Tom Tromey References: <20190605020116.1550-1-tom@tromey.com> <1ee4bd6b-4cdf-f3a9-74af-0843bf123a8b@redhat.com> <87lfygi1x0.fsf@tromey.com> <32872d6a-15d6-9718-59ae-957694e114c9@redhat.com> <87imtjhj6b.fsf@tromey.com> <625cd0ba-058d-d4bf-8ba3-8676f335b0f3@redhat.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: <39321bf6-9a38-df4a-102a-abf4c04ad21d@redhat.com> Date: Wed, 5 Jun 2019 21:42:25 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 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 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? */