binutils-gdb/gdbsupport/gdb-checked-static-cast.h

84 lines
2.9 KiB
C
Raw Normal View History

/* Copyright (C) 2022-2024 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 <http://www.gnu.org/licenses/>. */
#ifndef COMMON_GDB_CHECKED_STATIC_CAST_H
#define COMMON_GDB_CHECKED_STATIC_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<typename T, typename V>
T
checked_static_cast (V *v)
{
/* We only support casting to pointer types. */
static_assert (std::is_pointer<T>::value, "target must be a pointer type");
/* Check for polymorphic types explicitly in case we're in release mode. */
static_assert (std::is_polymorphic<V>::value, "types must be polymorphic");
/* Figure out the type that T points to. */
using T_no_P = typename std::remove_pointer<T>::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<V, T_no_P>::value
|| std::is_base_of<T_no_P, V>::value,
"types must be related");
#ifdef DEVELOPMENT
gdb: use static_cast in gdb::checked_static_cast This commit: commit 6fe4779ac4b1874c995345e3eabd89cb1a05fbdf Date: Sat Feb 24 11:00:20 2024 +0100 [gdb/build] Fix static cast of virtual base addressed an issue where GDB would not compile in production mode due to a use of gdb::checked_static_cast. The problem was that we were asking GDB to cast from a virtual base class to a sub-class, this works fine when using dynamic_cast, but does not work with static_cast. The gdb::checked_static_cast actually uses dynamic_cast under the hood in development mode in order to ensure that the cast is valid, while in a production build we use static_cast as this is more efficient. What this meant however, was that when gdb::checked_static_cast was used to cast from a virtual base class, the dynamic_cast of a non-production build worked fine, while the production build's static_cast caused a build failure. However, the gdb::checked_static_cast function already contains some static_assert calls that are intended to catch any issues with invalid type casting, the goal of these asserts was to prevent issues like this: the build only failing in production mode. Clearly the current asserts are not enough. I don't think there is a std::is_virtual_base type trait check, so what I propose instead is that in non-production mode we also make use of static_cast. This will ensure that any errors that crop up in production mode should also be revealed in non-production mode, and should catch issues like this in the future. There should be no user visible changes after this commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31399 Co-Authored-By: Simon Marchi <simon.marchi@polymtl.ca>
2024-03-07 01:28:48 +08:00
gdb_assert (v == nullptr || dynamic_cast<T> (v) != nullptr);
#endif
gdb: use static_cast in gdb::checked_static_cast This commit: commit 6fe4779ac4b1874c995345e3eabd89cb1a05fbdf Date: Sat Feb 24 11:00:20 2024 +0100 [gdb/build] Fix static cast of virtual base addressed an issue where GDB would not compile in production mode due to a use of gdb::checked_static_cast. The problem was that we were asking GDB to cast from a virtual base class to a sub-class, this works fine when using dynamic_cast, but does not work with static_cast. The gdb::checked_static_cast actually uses dynamic_cast under the hood in development mode in order to ensure that the cast is valid, while in a production build we use static_cast as this is more efficient. What this meant however, was that when gdb::checked_static_cast was used to cast from a virtual base class, the dynamic_cast of a non-production build worked fine, while the production build's static_cast caused a build failure. However, the gdb::checked_static_cast function already contains some static_assert calls that are intended to catch any issues with invalid type casting, the goal of these asserts was to prevent issues like this: the build only failing in production mode. Clearly the current asserts are not enough. I don't think there is a std::is_virtual_base type trait check, so what I propose instead is that in non-production mode we also make use of static_cast. This will ensure that any errors that crop up in production mode should also be revealed in non-production mode, and should catch issues like this in the future. There should be no user visible changes after this commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31399 Co-Authored-By: Simon Marchi <simon.marchi@polymtl.ca>
2024-03-07 01:28:48 +08:00
/* If a base class of V is virtual then the dynamic_cast above will
succeed, but this static_cast will fail. */
return static_cast<T> (v);
}
/* Same as the above, but to cast from a reference type to another. */
template<typename T, typename V, typename = gdb::Requires<std::is_reference<T>>>
T
checked_static_cast (V &v)
{
static_assert (std::is_reference<T>::value, "target must be a reference type");
using T_no_R = typename std::remove_reference<T>::type;
using T_P = typename std::add_pointer<T_no_R>::type;
using V_no_R = typename std::remove_reference<V>::type;
return *checked_static_cast<T_P, V_no_R> (&v);
}
}
#endif /* COMMON_GDB_CHECKED_STATIC_CAST_H */