From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id E95AB3855167; Mon, 28 Nov 2022 19:24:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E95AB3855167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669663471; bh=71yKEq+9OU63LiDeVTHV5j+vg2fucdq/PesRlL6EK50=; h=From:To:Subject:Date:From; b=Aw16P7BtAKXXf9uVA84AS0QJeHFPy5GkNtAyuXMWGEf2ofzoWz+2BjF7mysVZojja AzgroowsYmtumM0Rm8E+vmJmMJdtQbcXNMk9v9hDVDi9i3ETRNCNSqlaIuGiGTych6 7qwBfcY96pN93i40qQB3aKGo+qGyjqmec1RSaZSs= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: mark disassembler function callback types as noexcept X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 8eb7d135e32ad6b7cdcfeffe486b195058206cdb X-Git-Newrev: 5975a5caceb238be7eddb3b279d51145a1628af8 Message-Id: <20221128192431.E95AB3855167@sourceware.org> Date: Mon, 28 Nov 2022 19:24:31 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D5975a5caceb2= 38be7eddb3b279d51145a1628af8 commit 5975a5caceb238be7eddb3b279d51145a1628af8 Author: Andrew Burgess Date: Thu Oct 27 15:20:10 2022 +0100 gdb: mark disassembler function callback types as noexcept =20 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. =20 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. =20 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. =20 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. =20 At least, that's the idea. Unfortunately, it's not that easy. =20 Before C++17, the noexcept keyword on a function typedef would be ignored, thus: =20 using func_type =3D void (*) (void) noexcept; =20 void a_func () { throw 123; } =20 void some_func (func_type f) { f (); } =20 int main () { some_func (a_func); return 0; } =20 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. =20 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: =20 void a_func () noexcept { /* Nothing. */ } =20 And for C++17 both GCC and Clang compile this just fine. =20 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. =20 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. =20 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). Diff: --- 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; =20 +#if __cplusplus >=3D 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 =20 protected: =20 - /* 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 =3D decltype (disassemble_info::read_memory_func= ); - using memory_error_ftype =3D decltype (disassemble_info::memory_error_fu= nc); - using print_address_ftype =3D decltype (disassemble_info::print_address_= func); - using fprintf_ftype =3D decltype (disassemble_info::fprintf_func); - using fprintf_styled_ftype =3D decltype (disassemble_info::fprintf_style= d_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 + =3D int (*) (bfd_vma, bfd_byte *, unsigned int, struct disassemble_inf= o *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using memory_error_ftype + =3D void (*) (int, bfd_vma, struct disassemble_info *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using print_address_ftype + =3D void (*) (bfd_vma, struct disassemble_info *) + LIBOPCODE_CALLBACK_NOEXCEPT; + using fprintf_ftype + =3D int (*) (void *, const char *, ...) + LIBOPCODE_CALLBACK_NOEXCEPT; + using fprintf_styled_ftype + =3D int (*) (void *, enum disassembler_style, const char *, ...) + LIBOPCODE_CALLBACK_NOEXCEPT; =20 /* Constructor, many fields in m_di are initialized from GDBARCH. The remaining arguments are function callbacks that are written into m_di.