From patchwork Mon Apr 8 17:24: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: 32203 Received: (qmail 60241 invoked by alias); 8 Apr 2019 17:24:34 -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 60190 invoked by uid 89); 8 Apr 2019 17:24:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=1969 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 08 Apr 2019 17:24:28 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 281E1307EAA2; Mon, 8 Apr 2019 17:24:27 +0000 (UTC) Received: from localhost.localdomain (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68B9B5D71B; Mon, 8 Apr 2019 17:24:26 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Some gdb_exception{,error,quit} tweaks Date: Mon, 8 Apr 2019 18:24:25 +0100 Message-Id: <20190408172425.31039-1-palves@redhat.com> - Explicitly include for for std::string. - Use std::make_shared to construct gdb_exception::message instead of operator new, avoiding one heap allocation (2 instead of 3). Add 'const char *fmt, va_list ap' parameters to gdb_exception{,error,quit}'s ctors, and do the std::make_shared in the gdb_exception ctor. - gdb_exception_error's contructor does not need to have an 'enum return_reason' parameter, since always are always RETURN_ERROR, by definition. - Similarly, gdb_exception_quit's contructror does not need to have 'enum return_reason'/'enum errors' parameters. - In the gdb_exception_{quit,_error} ctors that take a gdb_exception as argument, assert that they're being passed a gdb_exception object of the right 'reason'. gdb/ChangeLog: yyyy-mm-dd Pedro Alves * common/common-exceptions.c (throw_exception): Don't create named object to throw; throw directly. (throw_it): Likewise. Don't initialize gdb_exception::message here, with new; pass FMT and AP to the ctor instead. * common/common-exceptions.h: Include . (gdb_exception::gdb_exception(enum return_reason, enum errors, const char *, va_list)): New ctor. Use std::make_shared. (gdb_exception_error::gdb_exception_error(enum return_reason, enum errors)): Delete. (gdb_exception_error::gdb_exception_error(enum errors, const char *, va_list)): New. (gdb_exception_error::gdb_exception_error(const gdb_exception &)): Add assertion. (gdb_exception_quit::gdb_exception_quit(enum return_reason, enum errors)): Delete. (gdb_exception_quit::gdb_exception_quit(const char *, va_list)): New. (gdb_exception_quit::gdb_exception_quit(const gdb_exception &)): Add assertion. --- gdb/common/common-exceptions.c | 22 ++++------------------ gdb/common/common-exceptions.h | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/gdb/common/common-exceptions.c b/gdb/common/common-exceptions.c index 83f2c74bfd4..6378dc40d6d 100644 --- a/gdb/common/common-exceptions.c +++ b/gdb/common/common-exceptions.c @@ -184,15 +184,9 @@ void throw_exception (const gdb_exception &exception) { if (exception.reason == RETURN_QUIT) - { - gdb_exception_quit ex (exception); - throw ex; - } + throw gdb_exception_quit (exception); else if (exception.reason == RETURN_ERROR) - { - gdb_exception_error ex (exception); - throw ex; - } + throw gdb_exception_error (exception); else gdb_assert_not_reached ("invalid return reason"); } @@ -202,17 +196,9 @@ throw_it (enum return_reason reason, enum errors error, const char *fmt, va_list ap) { if (reason == RETURN_QUIT) - { - gdb_exception_quit ex (reason, error); - ex.message.reset (new std::string (string_vprintf (fmt, ap))); - throw ex; - } + throw gdb_exception_quit (fmt, ap); else if (reason == RETURN_ERROR) - { - gdb_exception_error ex (reason, error); - ex.message.reset (new std::string (string_vprintf (fmt, ap))); - throw ex; - } + throw gdb_exception_error (error, fmt, ap); else gdb_assert_not_reached ("invalid return reason"); } diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h index 1aedb831a62..8871df6e823 100644 --- a/gdb/common/common-exceptions.h +++ b/gdb/common/common-exceptions.h @@ -23,6 +23,7 @@ #include #include #include +#include /* Reasons for calling throw_exceptions(). NOTE: all reason values must be different from zero. enum value 0 is reserved for internal @@ -123,6 +124,14 @@ struct gdb_exception { } + gdb_exception (enum return_reason r, enum errors e, + const char *fmt, va_list ap) + : reason (r), + error (e), + message (std::make_shared (string_vprintf (fmt, ap))) + { + } + /* The copy constructor exists so that we can mark it "noexcept", which is a good practice for any sort of exception object. */ gdb_exception (const gdb_exception &other) noexcept @@ -214,27 +223,29 @@ extern int exceptions_state_mc_catch (struct gdb_exception *, int); struct gdb_exception_error : public gdb_exception { - gdb_exception_error (enum return_reason r, enum errors e) - : gdb_exception (r, e) + gdb_exception_error (enum errors e, const char *fmt, va_list ap) + : gdb_exception (RETURN_ERROR, e, fmt, ap) { } explicit gdb_exception_error (const gdb_exception &ex) noexcept : gdb_exception (ex) { + gdb_assert (ex.reason == RETURN_ERROR); } }; struct gdb_exception_quit : public gdb_exception { - gdb_exception_quit (enum return_reason r, enum errors e) - : gdb_exception (r, e) + gdb_exception_quit (const char *fmt, va_list ap) + : gdb_exception (RETURN_QUIT, GDB_NO_ERROR, fmt, ap) { } explicit gdb_exception_quit (const gdb_exception &ex) noexcept : gdb_exception (ex) { + gdb_assert (ex.reason == RETURN_QUIT); } };