From patchwork Wed Mar 13 03:02:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 31834 Received: (qmail 96702 invoked by alias); 13 Mar 2019 03:02:26 -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 96689 invoked by uid 89); 13 Mar 2019 03:02:25 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-13.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_SOFTFAIL autolearn=ham version=3.3.1 spammy=duration, mi3, forgotten, realized X-HELO: barracuda.ebox.ca Received: from barracuda.ebox.ca (HELO barracuda.ebox.ca) (96.127.255.19) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Mar 2019 03:02:23 +0000 Received: from smtp.ebox.ca (smtp.electronicbox.net [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 9GvWdsdabDEYUwmk (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 12 Mar 2019 23:02:19 -0400 (EDT) Received: from simark.lan (unknown [192.222.164.54]) by smtp.ebox.ca (Postfix) with ESMTP id 0CB0C441B21; Tue, 12 Mar 2019 23:02:19 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] Factor out mi_ui_out instantiation logic Date: Tue, 12 Mar 2019 23:02:18 -0400 Message-Id: <20190313030218.21452-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 X-IsSubscribed: yes When re-reviewing this [1] I noticed that there were two spots encoding the logic of instantiating an mi_ui_out object based on the interpreter name ("mi", "mi1", "mi2" or "mi3"): - mi_interp::init - mi_load_progress Both encode the logic to choose what the default version is when the interpreter name is "mi". I had forgotten the one in mi_load_progress. Therefore, I propose extracting that logic to a single function. I started to add a new overload of mi_out_new, then realized the current mi_out_new wasn't very useful, being just a thing wrapper around "new mi_ui_out". So I ended up with just an mi_out_new function taking the interp name as parameter. I ran the gdb.mi tests, and verified manually the behavior (including the load command). [1] https://sourceware.org/ml/gdb-patches/2019-01/msg00427.html gdb/ChangeLog: * mi/mi-out.h (mi_out_new): Change parameter to const char *. * mi/mi-out.c (mi_out_new): Change parameter to const char *, instantiate mi_ui_out based on interpreter name. * mi/mi-interp.c (mi_interp::init): Use the new mi_out_new. * mi/mi-main.c (mi_load_progress): Likewise. --- gdb/mi/mi-interp.c | 18 ++---------------- gdb/mi/mi-main.c | 12 ++---------- gdb/mi/mi-out.c | 21 +++++++++++++++++---- gdb/mi/mi-out.h | 7 ++++++- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c index 3e9f36897a86..f17e09ff04f5 100644 --- a/gdb/mi/mi-interp.c +++ b/gdb/mi/mi-interp.c @@ -114,7 +114,6 @@ void mi_interp::init (bool top_level) { mi_interp *mi = this; - int mi_version; /* Store the current output channel, so that we can create a console channel that encapsulates and prefixes all gdb_output-type bits @@ -128,21 +127,8 @@ mi_interp::init (bool top_level) mi->log = mi->err; mi->targ = new mi_console_file (mi->raw_stdout, "@", '"'); mi->event_channel = new mi_console_file (mi->raw_stdout, "=", 0); - - /* INTERP_MI selects the most recent released version. "mi2" was - released as part of GDB 6.0. */ - if (strcmp (name (), INTERP_MI) == 0) - mi_version = 2; - else if (strcmp (name (), INTERP_MI1) == 0) - mi_version = 1; - else if (strcmp (name (), INTERP_MI2) == 0) - mi_version = 2; - else if (strcmp (name (), INTERP_MI3) == 0) - mi_version = 3; - else - gdb_assert_not_reached ("unhandled MI version"); - - mi->mi_uiout = mi_out_new (mi_version); + mi->mi_uiout = mi_out_new (name ()); + gdb_assert (mi->mi_uiout != nullptr); mi->cli_uiout = cli_out_new (mi->out); if (top_level) diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 46bc928d9fe9..f4e5e48bc221 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -2173,16 +2173,8 @@ mi_load_progress (const char *section_name, which means uiout may not be correct. Fix it for the duration of this function. */ - std::unique_ptr uiout; - - if (current_interp_named_p (INTERP_MI) - || current_interp_named_p (INTERP_MI2)) - uiout.reset (mi_out_new (2)); - else if (current_interp_named_p (INTERP_MI1)) - uiout.reset (mi_out_new (1)); - else if (current_interp_named_p (INTERP_MI3)) - uiout.reset (mi_out_new (3)); - else + std::unique_ptr uiout (mi_out_new (current_interpreter ()->name ())); + if (uiout == nullptr) return; scoped_restore save_uiout diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c index 11991dca6639..4f8c9bff8e58 100644 --- a/gdb/mi/mi-out.c +++ b/gdb/mi/mi-out.c @@ -20,10 +20,14 @@ along with this program. If not, see . */ #include "defs.h" -#include "ui-out.h" #include "mi-out.h" + #include +#include "interps.h" +#include "ui-out.h" +#include "utils.h" + /* Mark beginning of a table. */ void @@ -288,12 +292,21 @@ mi_ui_out::~mi_ui_out () { } -/* Initialize private members at startup. */ +/* See mi/mi-out.h. */ mi_ui_out * -mi_out_new (int mi_version) +mi_out_new (const char *mi_version) { - return new mi_ui_out (mi_version); + if (streq (mi_version, INTERP_MI3)) + return new mi_ui_out (3); + + if (streq (mi_version, INTERP_MI2) || streq (mi_version, INTERP_MI)) + return new mi_ui_out (2); + + if (streq (mi_version, INTERP_MI1)) + return new mi_ui_out (1); + + return nullptr; } /* Helper function to return the given UIOUT as an mi_ui_out. It is an error diff --git a/gdb/mi/mi-out.h b/gdb/mi/mi-out.h index 0c74eaf80fda..82f77592da84 100644 --- a/gdb/mi/mi-out.h +++ b/gdb/mi/mi-out.h @@ -90,7 +90,12 @@ private: std::vector m_streams; }; -mi_ui_out *mi_out_new (int mi_version); +/* Create an MI ui-out object with MI version MI_VERSION, which should be equal + to one of the INTERP_MI* constants (see interps.h). + + Return nullptr if an invalid version is provided. */ +mi_ui_out *mi_out_new (const char *mi_version); + int mi_version (ui_out *uiout); void mi_out_put (ui_out *uiout, struct ui_file *stream); void mi_out_rewind (ui_out *uiout);