From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id F3F48386545B; Thu, 21 Jul 2022 18:20:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F3F48386545B 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] gdbsupport: add checked_static_cast X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 08106042d9f5fdff60c129bf33190639f1a98b2a X-Git-Newrev: 11da1b13b313ae46c84008ebf096ffed1701e3c1 Message-Id: <20220721182026.F3F48386545B@sourceware.org> Date: Thu, 21 Jul 2022 18:20:26 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2022 18:20:27 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D11da1b13b313= ae46c84008ebf096ffed1701e3c1 commit 11da1b13b313ae46c84008ebf096ffed1701e3c1 Author: Andrew Burgess Date: Mon Jun 27 13:29:06 2022 +0100 gdbsupport: add checked_static_cast =20 This commit was inspired by these mailing list posts: =20 https://sourceware.org/pipermail/gdb-patches/2022-June/190323.html https://sourceware.org/pipermail/gdb-patches/2022-April/188098.html =20 The idea is to add a new function gdb::checked_static_cast, which can, in some cases, be used as a drop-in replacement for static_cast. And so, if I previously wrote this: =20 BaseClass *base =3D get_base_class_pointer (); DerivedClass *derived =3D static_cast (base); =20 I can now write: =20 BaseClass *base =3D get_base_class_pointer (); DerivedClass *derived =3D gdb::checked_static_cast (b= ase); =20 The requirement is that BaseClass and DerivedClass must be polymorphic. =20 The benefit of making this change is that, when GDB is built in developer mode, a run-time check will be made to ensure that `base` really is of type DerivedClass before the cast is performed. If `base` is not of type DerivedClass then GDB will assert. =20 In a non-developer build gdb::checked_static_cast is equivalent to a static_cast, and there should be no performance difference. =20 This commit adds the support function, but does not make use of this function, a use will be added in the next commit. =20 Co-Authored-By: Pedro Alves Co-Authored-By: Tom Tromey Diff: --- gdbsupport/gdb-checked-static-cast.h | 68 ++++++++++++++++++++++++++++++++= ++++ 1 file changed, 68 insertions(+) diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-= static-cast.h new file mode 100644 index 00000000000..7f281250285 --- /dev/null +++ b/gdbsupport/gdb-checked-static-cast.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . = */ + +#ifndef COMMON_GDB_CHECKED_DYNAMIC_CAST_H +#define COMMON_GDB_CHECKED_DYNAMIC_CAST_H + +#include "gdbsupport/traits.h" + +namespace gdb +{ + +/* This function can be used in place of static_cast when casting between + pointers of polymorphic types. The benefit of using this call is that, + when compiling in developer mode, dynamic_cast will be used to validate + the cast. This use of dynamic_cast is why this function will only + work for polymorphic types. + + In non-developer (i.e. production) builds, the dynamic_cast is replaced + with a static_cast which is usually significantly faster. */ + +template +T +checked_static_cast (V *v) +{ + /* We only support casting to pointer types. */ + static_assert (std::is_pointer::value, "target must be a pointer type= "); + + /* Check for polymorphic types explicitly in case we're in release mode.= */ + static_assert (std::is_polymorphic::value, "types must be polymorphic= "); + + /* Figure out the type that T points to. */ + using T_no_P =3D typename std::remove_pointer::type; + + /* In developer mode this cast uses dynamic_cast to confirm at run-time + that the cast from V* to T is valid. However, we can catch some + mistakes at compile time, this assert prevents anything other than + downcasts, or casts to same type. */ + static_assert (std::is_base_of::value + || std::is_base_of::value, + "types must be related"); + +#ifdef DEVELOPMENT + T result =3D dynamic_cast (v); + gdb_assert (result !=3D nullptr); +#else + T result =3D static_cast (v); +#endif + + return result; +} + +} + +#endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */