From patchwork Fri Nov 18 16:57:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 60849 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 89A603853D74 for ; Fri, 18 Nov 2022 16:58:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89A603853D74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668790694; bh=5mYZ3SEMVaCedVDqcHzd/EyR3rD23SLkfhEFu9t0LhQ=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=EHlG02jUnhow4qgCxA+7lmlAkahWcAyvj+HlgURLDlqGTN5hgs5nkOtx+U5DEjf5F h6APohMYJkd/IaKKSCCpNV64hKWjoWopeo/TrhVySZssiyEE0rQb9X07uGInJb+vI6 qz5V0W5zvqRu3s1KdNrG9RyhNdGILygRDwkDvQKk= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A796A385457F for ; Fri, 18 Nov 2022 16:57:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A796A385457F Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-338-_JytBC5kPoWgDk-g69kY_Q-1; Fri, 18 Nov 2022 11:57:42 -0500 X-MC-Unique: _JytBC5kPoWgDk-g69kY_Q-1 Received: by mail-wr1-f72.google.com with SMTP id w23-20020adf8bd7000000b002358f733307so1805853wra.17 for ; Fri, 18 Nov 2022 08:57:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5mYZ3SEMVaCedVDqcHzd/EyR3rD23SLkfhEFu9t0LhQ=; b=Oh9g/KTzm8hdxasscYAXvhtx/W5CerpgvqjOutC9MkK4K1dbLNtSRYkdci6Lm465Wf VLLtkRrzNNdBbOw/qVd22Q2nNwwIG/ZyLfCnSm52+Q0O5wFbmYfe40rY+mXqhpsRmIh+ rFjIIMe6aT5lrbwoG1Sln0pajU/olnm0IBVa324e4syN1WQHN9YY8CPrR8WCFtOX1BQJ g3iGPs0hPm3HOU9vYgQRapx44jml6kEjaWM3dVaGr4QNEEHXoKDu1QjTUA2E2yrpQ8vJ OjvciA8e2p40jkIfvxdf/3Xzq5s0KfOeWZPpEMq9tx/YEqz7IvrGTivIS38F1wGAd5M5 ssxQ== X-Gm-Message-State: ANoB5pkskenoBfygIXpEYJB6JlyuK+5/CbLoBe4i9d60bQfqwCHBBkMf F7tcTmD4yKqXiontbRPyJbgOBtpie96jlz4PHqIXMT/eaIMSGIPGa/57TMPgNarBvlEGQV9J1xB TPEvGRwOs7mfxiC1NdVcGd5KKvEZ+vxnTsm1rpwuOcZ39OG/a6jwICNVYV0gziEII853E+GArEA == X-Received: by 2002:a05:600c:4f51:b0:3b4:a6c4:70fb with SMTP id m17-20020a05600c4f5100b003b4a6c470fbmr9277819wmq.79.1668790660712; Fri, 18 Nov 2022 08:57:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf6qvHGZJG/rldsfmyvfV2glvXARQB/BWhj5kKRxFMn3NlgIfO1ADbvkyWHYY6vPWameJXwPzw== X-Received: by 2002:a05:600c:4f51:b0:3b4:a6c4:70fb with SMTP id m17-20020a05600c4f5100b003b4a6c470fbmr9277797wmq.79.1668790660383; Fri, 18 Nov 2022 08:57:40 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id n27-20020a05600c3b9b00b003cfa81e2eb4sm5868569wms.38.2022.11.18.08.57.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 08:57:39 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv3 3/3] gdb: mark disassembler function callback types as noexcept Date: Fri, 18 Nov 2022 16:57:32 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" In disasm.h we define a set of types that are used by the various disassembler classes to hold callback functions before passing the callbacks into libopcodes. Because libopcodes is C code, and on some (many?) targets, C code is compiled without exception support, it is important that GDB not try to throw an exception over libopcode code. In the previous commit all the existing callbacks were marked as noexcept, however, this doesn't protect us from a future change to GDB either adding a new callback that is not noexcept, or removing the noexcept keyword from an existing callback. In this commit I mark all the callback types as noexcept. This means that GDB's disassembler classes will no longer compile if we try to pass a callback that is not marked as noexcept. At least, that's the idea. Unfortunately, it's not that easy. Before C++17, the noexcept keyword on a function typedef would be ignored, thus: using func_type = void (*) (void) noexcept; void a_func () { throw 123; } void some_func (func_type f) { f (); } int main () { some_func (a_func); return 0; } Will compile just fine for C++11 and C++14 with GCC. Clang on the other hand complains that 'noexcept' should not appear on function types, but then does appear to correctly complain that passing a_func is a mismatch in the set of exceptions that could be thrown. Switching to C++17 and both GCC and Clang correctly point out that passing a_func is an invalid conversion relating to the noexcept keyword. Changing a_func to: void a_func () noexcept { /* Nothing. */ } And for C++17 both GCC and Clang compile this just fine. My conclusion then is that adding the noexcept keyword to the function types is pointless while GDB is not compiled as C++17, and silencing the warnings would require us to jump through a bunch of hoops. And so, in this commit, I define a macro LIBOPCODE_CALLBACK_NOEXCEPT, this macro expands to noexcept when compiling for C++17, but otherwise expands to nothing. I then add this macro to the function types. I've compiled GDB as the default C++11 and also forced the compile to C++17. When compiling as C++17 I spotted a few additional places where callbacks needed to be marked noexcept (these fixes were merged into the previous commit, but this confirmed to be that the macro is working as expected). --- gdb/disasm.h | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/gdb/disasm.h b/gdb/disasm.h index 58c6c623098..d6aec9a4705 100644 --- a/gdb/disasm.h +++ b/gdb/disasm.h @@ -26,6 +26,12 @@ struct gdbarch; struct ui_out; struct ui_file; +#if __cplusplus >= 201703L +#define LIBOPCODE_CALLBACK_NOEXCEPT noexcept +#else +#define LIBOPCODE_CALLBACK_NOEXCEPT +#endif + /* A wrapper around a disassemble_info and a gdbarch. This is the core set of data that all disassembler sub-classes will need. This class doesn't actually implement the disassembling process, that is something @@ -51,18 +57,28 @@ struct gdb_disassemble_info protected: - /* Types for the function callbacks within m_di. It would be nice if - these function types were all defined to include the noexcept - keyword, as every implementation of these must be noexcept. However, - using noexcept within a function typedef like this is a C++17 - feature, trying to do this for earlier C++ versions results in a - warning from GCC/Clang, and the noexcept isn't checked. After we - move to C++17 these should be updated to add noexcept. */ - using read_memory_ftype = decltype (disassemble_info::read_memory_func); - using memory_error_ftype = decltype (disassemble_info::memory_error_func); - using print_address_ftype = decltype (disassemble_info::print_address_func); - using fprintf_ftype = decltype (disassemble_info::fprintf_func); - using fprintf_styled_ftype = decltype (disassemble_info::fprintf_styled_func); + /* Types for the function callbacks within m_di. The actual function + signatures here are taken from include/dis-asm.h. The noexcept macro + expands to 'noexcept' for C++17 and later, otherwise, it expands to + nothing. This is because including noexcept was ignored for function + types before C++17, but both GCC and Clang warn that the noexcept + will become relevant when you switch to C++17, and this warning + causes the build to fail. */ + using read_memory_ftype + = int (*) (bfd_vma, bfd_byte *, unsigned int, struct disassemble_info *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using memory_error_ftype + = void (*) (int, bfd_vma, struct disassemble_info *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using print_address_ftype + = void (*) (bfd_vma, struct disassemble_info *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using fprintf_ftype + = int (*) (void *, const char *, ...) + LIBOPCODE_CALLBACK_NOEXCEPT; + using fprintf_styled_ftype + = int (*) (void *, enum disassembler_style, const char *, ...) + LIBOPCODE_CALLBACK_NOEXCEPT; /* Constructor, many fields in m_di are initialized from GDBARCH. The remaining arguments are function callbacks that are written into m_di.