2024-01-12 23:30:44 +08:00
|
|
|
/* Copyright (C) 2017-2024 Free Software Foundation, Inc.
|
Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and
cu_offset types were made structs in order to prevent incorrect mixing
of those offsets. Now that we require C++11, we can make them
integers again, while keeping the safety, by exploiting "enum class".
We can add a bit more safety, even, by defining operators that the
types _should_ support, helping making the suspicious uses stand out
more.
Getting at the underlying type is done with the new to_underlying
function added by the previous patch, which also helps better spot
where do we need to step out of the safety net. Mostly, that's around
parsing the DWARF, and when we print the offset for complaint/debug
purposes. But there are other occasional uses.
Since we have to define the sect_offset/cu_offset types in a header
anyway, I went ahead and generalized/library-fied the idea of "offset"
types, making it trivial to add more such types if we find a use. See
common/offset-type.h and the DEFINE_OFFSET_TYPE macro.
I needed a couple generaly-useful preprocessor bits (e.g., yet another
CONCAT implementation), so I started a new common/preprocessor.h file.
I included units tests covering the "offset" types API. These are
mostly compile-time tests, using SFINAE to check that expressions that
shouldn't compile (e.g., comparing unrelated offset types) really are
invalid and would fail to compile. This same idea appeared in my
pending enum-flags revamp from a few months ago (though this version
is a bit further modernized compared to what I had posted), and I plan
on reusing the "check valid expression" bits added here in that
series, so I went ahead and defined the CHECK_VALID_EXPR macro in its
own header -- common/valid-expr.h. I think that's nicer regardless.
I was borderline between calling the new types "offset" types, or
"index" types, BTW. I stuck with "offset" simply because that's what
we're already calling them, mostly.
gdb/ChangeLog:
2017-04-04 Pedro Alves <palves@redhat.com>
* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
unittests/offset-type-selftests.c.
(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
* common/offset-type.h: New file.
* common/preprocessor.h: New file.
* common/traits.h: New file.
* common/valid-expr.h: New file.
* dwarf2expr.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* dwarf2loc.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* gdbtypes.h: Include "common/offset-type.h".
(cu_offset): Now an offset type (strong typedef) instead of a
struct.
(sect_offset): Likewise.
(union call_site_parameter_u): Rename "param_offset" field to
"param_cu_off".
* unittests/offset-type-selftests.c: New file.
2017-04-05 03:03:26 +08:00
|
|
|
|
|
|
|
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_TRAITS_H
|
|
|
|
#define COMMON_TRAITS_H
|
|
|
|
|
More gdb::optional features
Currently we can't use gdb::optional<T> as function return type,
because gdb::optional's copy ctor is deleted. For example, with:
gdb::optional<int> function ()
{
gdb::optional<int> opt;
....
return opt;
we get:
src/gdb/foo.c: In function ‘gdb::optional<int> foo()’:
src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’
return opt;
^
In file included from src/gdb/foo.c:68:0:
src/gdb/common/gdb_optional.h:53:3: note: declared here
optional (const optional &other) = delete;
^
I started by fixing that, and then ran into another missing feature,
also fixed by this patch.
The next feature I'm missing most from gdb::optional<T> compared to
std::optional<T> is construction/move/assignment from a T, instead of
having to default construct an gdb::optional and then use
optional::emplace(....).
For example:
gdb::optional<std::string> function ()
{
gdb::optional<std::string> opt;
std::string str;
...
opt.emplace (std::move (str));
return opt;
vs
gdb::optional<std::string> function ()
{
std::string str;
...
return str;
The copy/move ctor/assign methods weren't initialy implemented because
std::optional supports construction from a type U if U is convertible
to T too, and has rules to decide whether the ctors are
explicit/implicit based on that, and rules for whether the ctor should
be trivial or not, etc., which leads to a much more complicated
implementation.
If we stick to supporting copy/move construction/assignment of/to an
optional<T> from exactly only optional<T> and T, then all that
conversion-related complication disappears, and we still gain
convenience in most use cases.
The patch also makes emplace return a reference to the constructor
object, per C++17 std::optional, and adds a reset method, againt
because std::optional has one and it's trivial to support it. These
two changes are a requirement of the gdb::optional unit testing patch
that will follow.
gdb/ChangeLog:
2017-04-18 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h: Include common/traits.h.
(in_place_t): New type.
(in_place): New constexpr variable.
(optional::optional): Remove member initialization of
m_instantiated.
(optional::optional(in_place_t...)): New constructor.
(optional::~optional): Use reset.
(optional::optional(const optional&)): New.
(optional::optional(const optional&&)): New.
(optional::optional(T &)): New.
(optional::optional(T &&)): New.
(operator::operator=(const optional &)): New.
(operator::operator=(optional &&)): New.
(operator::operator= (const T &))
(operator::operator= (T &&))
(operator::emplace (Args &&... args)): Return a T&. Use reset.
(operator::reset): New.
(operator::m_instantiated):: Add in-class initializer.
* common/traits.h: Include <type_traits>.
(struct And): New types.
2017-04-19 04:39:24 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and
cu_offset types were made structs in order to prevent incorrect mixing
of those offsets. Now that we require C++11, we can make them
integers again, while keeping the safety, by exploiting "enum class".
We can add a bit more safety, even, by defining operators that the
types _should_ support, helping making the suspicious uses stand out
more.
Getting at the underlying type is done with the new to_underlying
function added by the previous patch, which also helps better spot
where do we need to step out of the safety net. Mostly, that's around
parsing the DWARF, and when we print the offset for complaint/debug
purposes. But there are other occasional uses.
Since we have to define the sect_offset/cu_offset types in a header
anyway, I went ahead and generalized/library-fied the idea of "offset"
types, making it trivial to add more such types if we find a use. See
common/offset-type.h and the DEFINE_OFFSET_TYPE macro.
I needed a couple generaly-useful preprocessor bits (e.g., yet another
CONCAT implementation), so I started a new common/preprocessor.h file.
I included units tests covering the "offset" types API. These are
mostly compile-time tests, using SFINAE to check that expressions that
shouldn't compile (e.g., comparing unrelated offset types) really are
invalid and would fail to compile. This same idea appeared in my
pending enum-flags revamp from a few months ago (though this version
is a bit further modernized compared to what I had posted), and I plan
on reusing the "check valid expression" bits added here in that
series, so I went ahead and defined the CHECK_VALID_EXPR macro in its
own header -- common/valid-expr.h. I think that's nicer regardless.
I was borderline between calling the new types "offset" types, or
"index" types, BTW. I stuck with "offset" simply because that's what
we're already calling them, mostly.
gdb/ChangeLog:
2017-04-04 Pedro Alves <palves@redhat.com>
* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
unittests/offset-type-selftests.c.
(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
* common/offset-type.h: New file.
* common/preprocessor.h: New file.
* common/traits.h: New file.
* common/valid-expr.h: New file.
* dwarf2expr.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* dwarf2loc.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* gdbtypes.h: Include "common/offset-type.h".
(cu_offset): Now an offset type (strong typedef) instead of a
struct.
(sect_offset): Likewise.
(union call_site_parameter_u): Rename "param_offset" field to
"param_cu_off".
* unittests/offset-type-selftests.c: New file.
2017-04-05 03:03:26 +08:00
|
|
|
namespace gdb {
|
|
|
|
|
Rewrite valid-expr.h's internals in terms of the detection idiom (C++17/N4502)
An earlier attempt at doing this had failed (wouldn't work in GCCs
around 4.8, IIRC), but now that I try again, it works. I suspect that
my previous attempt did not use the pre C++14-safe void_t (in
traits.h).
I want to switch to this model because:
- It's the standard detection idiom that folks will learn starting
with C++17.
- In the enum_flags unit tests, I have a static_assert that triggers
a warning (resulting in build error), which GCC does not suppress
because the warning is not being triggered in the SFINAE context.
Switching to the detection idiom fixes that. Alternatively,
switching to the C++03-style expression-validity checking with a
varargs overload would allow addressing that, but I think that
would be going backwards idiomatically speaking.
- While this patch shows a net increase of lines of code, the magic
being added to traits.h can be removed in a few years when we start
requiring C++17.
gdbsupport/ChangeLog:
* traits.h (struct nonesuch, struct detector, detected_or)
(detected_or_t, is_detected, detected_t, detected_or)
(detected_or_t, is_detected_exact, is_detected_convertible): New.
* valid-expr.h (CHECK_VALID_EXPR_INT): Use gdb::is_detected_exact.
2020-09-15 04:16:57 +08:00
|
|
|
/* Implementation of the detection idiom:
|
|
|
|
|
|
|
|
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
|
|
|
|
- http://en.cppreference.com/w/cpp/experimental/is_detected
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct nonesuch
|
|
|
|
{
|
|
|
|
nonesuch () = delete;
|
|
|
|
~nonesuch () = delete;
|
|
|
|
nonesuch (const nonesuch &) = delete;
|
|
|
|
void operator= (const nonesuch &) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace detection_detail {
|
|
|
|
/* Implementation of the detection idiom (negative case). */
|
|
|
|
template<typename Default, typename AlwaysVoid,
|
|
|
|
template<typename...> class Op, typename... Args>
|
|
|
|
struct detector
|
|
|
|
{
|
|
|
|
using value_t = std::false_type;
|
|
|
|
using type = Default;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Implementation of the detection idiom (positive case). */
|
|
|
|
template<typename Default, template<typename...> class Op, typename... Args>
|
2023-10-16 03:40:03 +08:00
|
|
|
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
|
Rewrite valid-expr.h's internals in terms of the detection idiom (C++17/N4502)
An earlier attempt at doing this had failed (wouldn't work in GCCs
around 4.8, IIRC), but now that I try again, it works. I suspect that
my previous attempt did not use the pre C++14-safe void_t (in
traits.h).
I want to switch to this model because:
- It's the standard detection idiom that folks will learn starting
with C++17.
- In the enum_flags unit tests, I have a static_assert that triggers
a warning (resulting in build error), which GCC does not suppress
because the warning is not being triggered in the SFINAE context.
Switching to the detection idiom fixes that. Alternatively,
switching to the C++03-style expression-validity checking with a
varargs overload would allow addressing that, but I think that
would be going backwards idiomatically speaking.
- While this patch shows a net increase of lines of code, the magic
being added to traits.h can be removed in a few years when we start
requiring C++17.
gdbsupport/ChangeLog:
* traits.h (struct nonesuch, struct detector, detected_or)
(detected_or_t, is_detected, detected_t, detected_or)
(detected_or_t, is_detected_exact, is_detected_convertible): New.
* valid-expr.h (CHECK_VALID_EXPR_INT): Use gdb::is_detected_exact.
2020-09-15 04:16:57 +08:00
|
|
|
{
|
|
|
|
using value_t = std::true_type;
|
|
|
|
using type = Op<Args...>;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Detect whether Op<Args...> is a valid type, use Default if not. */
|
|
|
|
template<typename Default, template<typename...> class Op,
|
|
|
|
typename... Args>
|
|
|
|
using detected_or = detector<Default, void, Op, Args...>;
|
|
|
|
|
|
|
|
/* Op<Args...> if that is a valid type, otherwise Default. */
|
|
|
|
template<typename Default, template<typename...> class Op,
|
|
|
|
typename... Args>
|
|
|
|
using detected_or_t
|
|
|
|
= typename detected_or<Default, Op, Args...>::type;
|
|
|
|
|
|
|
|
} /* detection_detail */
|
|
|
|
|
|
|
|
template<template<typename...> class Op, typename... Args>
|
|
|
|
using is_detected
|
|
|
|
= typename detection_detail::detector<nonesuch, void, Op, Args...>::value_t;
|
|
|
|
|
|
|
|
template<template<typename...> class Op, typename... Args>
|
|
|
|
using detected_t
|
|
|
|
= typename detection_detail::detector<nonesuch, void, Op, Args...>::type;
|
|
|
|
|
|
|
|
template<typename Default, template<typename...> class Op, typename... Args>
|
|
|
|
using detected_or = detection_detail::detected_or<Default, Op, Args...>;
|
|
|
|
|
|
|
|
template<typename Default, template<typename...> class Op, typename... Args>
|
|
|
|
using detected_or_t = typename detected_or<Default, Op, Args...>::type;
|
|
|
|
|
|
|
|
template<typename Expected, template<typename...> class Op, typename... Args>
|
|
|
|
using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
|
|
|
|
|
|
|
|
template<typename To, template<typename...> class Op, typename... Args>
|
|
|
|
using is_detected_convertible
|
|
|
|
= std::is_convertible<detected_t<Op, Args...>, To>;
|
|
|
|
|
More gdb::optional features
Currently we can't use gdb::optional<T> as function return type,
because gdb::optional's copy ctor is deleted. For example, with:
gdb::optional<int> function ()
{
gdb::optional<int> opt;
....
return opt;
we get:
src/gdb/foo.c: In function ‘gdb::optional<int> foo()’:
src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’
return opt;
^
In file included from src/gdb/foo.c:68:0:
src/gdb/common/gdb_optional.h:53:3: note: declared here
optional (const optional &other) = delete;
^
I started by fixing that, and then ran into another missing feature,
also fixed by this patch.
The next feature I'm missing most from gdb::optional<T> compared to
std::optional<T> is construction/move/assignment from a T, instead of
having to default construct an gdb::optional and then use
optional::emplace(....).
For example:
gdb::optional<std::string> function ()
{
gdb::optional<std::string> opt;
std::string str;
...
opt.emplace (std::move (str));
return opt;
vs
gdb::optional<std::string> function ()
{
std::string str;
...
return str;
The copy/move ctor/assign methods weren't initialy implemented because
std::optional supports construction from a type U if U is convertible
to T too, and has rules to decide whether the ctors are
explicit/implicit based on that, and rules for whether the ctor should
be trivial or not, etc., which leads to a much more complicated
implementation.
If we stick to supporting copy/move construction/assignment of/to an
optional<T> from exactly only optional<T> and T, then all that
conversion-related complication disappears, and we still gain
convenience in most use cases.
The patch also makes emplace return a reference to the constructor
object, per C++17 std::optional, and adds a reset method, againt
because std::optional has one and it's trivial to support it. These
two changes are a requirement of the gdb::optional unit testing patch
that will follow.
gdb/ChangeLog:
2017-04-18 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h: Include common/traits.h.
(in_place_t): New type.
(in_place): New constexpr variable.
(optional::optional): Remove member initialization of
m_instantiated.
(optional::optional(in_place_t...)): New constructor.
(optional::~optional): Use reset.
(optional::optional(const optional&)): New.
(optional::optional(const optional&&)): New.
(optional::optional(T &)): New.
(optional::optional(T &&)): New.
(operator::operator=(const optional &)): New.
(operator::operator=(optional &&)): New.
(operator::operator= (const T &))
(operator::operator= (T &&))
(operator::emplace (Args &&... args)): Return a T&. Use reset.
(operator::reset): New.
(operator::m_instantiated):: Add in-class initializer.
* common/traits.h: Include <type_traits>.
(struct And): New types.
2017-04-19 04:39:24 +08:00
|
|
|
/* A few trait helpers, mainly stolen from libstdc++. Uppercase
|
Poison non-POD memset & non-trivially-copyable memcpy/memmove
This patch catches invalid initialization of non-POD types with
memset, at compile time.
This is what I used to catch the problems fixed by the previous
patches in the series:
$ make -k 2>&1 | grep "deleted function"
src/gdb/breakpoint.c:951:53: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = bp_location; <template-parameter-1-2> = void; size_t = long unsigned int]’
src/gdb/breakpoint.c:7325:32: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = bp_location; <template-parameter-1-2> = void; size_t = long unsigned int]’
src/gdb/btrace.c:1153:42: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = btrace_insn; <template-parameter-1-2> = void; size_t = long unsigned int]’
...
gdb/ChangeLog:
2017-04-25 Pedro Alves <palves@redhat.com>
* common/common-defs.h: Include "common/poison.h".
* common/function-view.h: (Not, Or, Requires): Move to traits.h
and adjust.
* common/poison.h: New file.
* common/traits.h: Include <type_traits>.
(Not, Or, Requires): New, moved from common/function-view.h.
2017-04-25 08:27:41 +08:00
|
|
|
because "and/or", etc. are reserved keywords. */
|
|
|
|
|
|
|
|
template<typename Predicate>
|
|
|
|
struct Not : public std::integral_constant<bool, !Predicate::value>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename...>
|
|
|
|
struct Or;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Or<> : public std::false_type
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1>
|
|
|
|
struct Or<B1> : public B1
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1, typename B2>
|
|
|
|
struct Or<B1, B2>
|
|
|
|
: public std::conditional<B1::value, B1, B2>::type
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1,typename B2,typename B3, typename... Bn>
|
|
|
|
struct Or<B1, B2, B3, Bn...>
|
|
|
|
: public std::conditional<B1::value, B1, Or<B2, B3, Bn...>>::type
|
|
|
|
{};
|
More gdb::optional features
Currently we can't use gdb::optional<T> as function return type,
because gdb::optional's copy ctor is deleted. For example, with:
gdb::optional<int> function ()
{
gdb::optional<int> opt;
....
return opt;
we get:
src/gdb/foo.c: In function ‘gdb::optional<int> foo()’:
src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’
return opt;
^
In file included from src/gdb/foo.c:68:0:
src/gdb/common/gdb_optional.h:53:3: note: declared here
optional (const optional &other) = delete;
^
I started by fixing that, and then ran into another missing feature,
also fixed by this patch.
The next feature I'm missing most from gdb::optional<T> compared to
std::optional<T> is construction/move/assignment from a T, instead of
having to default construct an gdb::optional and then use
optional::emplace(....).
For example:
gdb::optional<std::string> function ()
{
gdb::optional<std::string> opt;
std::string str;
...
opt.emplace (std::move (str));
return opt;
vs
gdb::optional<std::string> function ()
{
std::string str;
...
return str;
The copy/move ctor/assign methods weren't initialy implemented because
std::optional supports construction from a type U if U is convertible
to T too, and has rules to decide whether the ctors are
explicit/implicit based on that, and rules for whether the ctor should
be trivial or not, etc., which leads to a much more complicated
implementation.
If we stick to supporting copy/move construction/assignment of/to an
optional<T> from exactly only optional<T> and T, then all that
conversion-related complication disappears, and we still gain
convenience in most use cases.
The patch also makes emplace return a reference to the constructor
object, per C++17 std::optional, and adds a reset method, againt
because std::optional has one and it's trivial to support it. These
two changes are a requirement of the gdb::optional unit testing patch
that will follow.
gdb/ChangeLog:
2017-04-18 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h: Include common/traits.h.
(in_place_t): New type.
(in_place): New constexpr variable.
(optional::optional): Remove member initialization of
m_instantiated.
(optional::optional(in_place_t...)): New constructor.
(optional::~optional): Use reset.
(optional::optional(const optional&)): New.
(optional::optional(const optional&&)): New.
(optional::optional(T &)): New.
(optional::optional(T &&)): New.
(operator::operator=(const optional &)): New.
(operator::operator=(optional &&)): New.
(operator::operator= (const T &))
(operator::operator= (T &&))
(operator::emplace (Args &&... args)): Return a T&. Use reset.
(operator::reset): New.
(operator::m_instantiated):: Add in-class initializer.
* common/traits.h: Include <type_traits>.
(struct And): New types.
2017-04-19 04:39:24 +08:00
|
|
|
|
|
|
|
template<typename...>
|
|
|
|
struct And;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct And<> : public std::true_type
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1>
|
|
|
|
struct And<B1> : public B1
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1, typename B2>
|
|
|
|
struct And<B1, B2>
|
|
|
|
: public std::conditional<B1::value, B2, B1>::type
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename B1, typename B2, typename B3, typename... Bn>
|
|
|
|
struct And<B1, B2, B3, Bn...>
|
|
|
|
: public std::conditional<B1::value, And<B2, B3, Bn...>, B1>::type
|
|
|
|
{};
|
|
|
|
|
Poison non-POD memset & non-trivially-copyable memcpy/memmove
This patch catches invalid initialization of non-POD types with
memset, at compile time.
This is what I used to catch the problems fixed by the previous
patches in the series:
$ make -k 2>&1 | grep "deleted function"
src/gdb/breakpoint.c:951:53: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = bp_location; <template-parameter-1-2> = void; size_t = long unsigned int]’
src/gdb/breakpoint.c:7325:32: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = bp_location; <template-parameter-1-2> = void; size_t = long unsigned int]’
src/gdb/btrace.c:1153:42: error: use of deleted function ‘void* memset(T*, int, size_t) [with T = btrace_insn; <template-parameter-1-2> = void; size_t = long unsigned int]’
...
gdb/ChangeLog:
2017-04-25 Pedro Alves <palves@redhat.com>
* common/common-defs.h: Include "common/poison.h".
* common/function-view.h: (Not, Or, Requires): Move to traits.h
and adjust.
* common/poison.h: New file.
* common/traits.h: Include <type_traits>.
(Not, Or, Requires): New, moved from common/function-view.h.
2017-04-25 08:27:41 +08:00
|
|
|
/* Concepts-light-like helper to make SFINAE logic easier to read. */
|
|
|
|
template<typename Condition>
|
|
|
|
using Requires = typename std::enable_if<Condition::value, void>::type;
|
Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and
cu_offset types were made structs in order to prevent incorrect mixing
of those offsets. Now that we require C++11, we can make them
integers again, while keeping the safety, by exploiting "enum class".
We can add a bit more safety, even, by defining operators that the
types _should_ support, helping making the suspicious uses stand out
more.
Getting at the underlying type is done with the new to_underlying
function added by the previous patch, which also helps better spot
where do we need to step out of the safety net. Mostly, that's around
parsing the DWARF, and when we print the offset for complaint/debug
purposes. But there are other occasional uses.
Since we have to define the sect_offset/cu_offset types in a header
anyway, I went ahead and generalized/library-fied the idea of "offset"
types, making it trivial to add more such types if we find a use. See
common/offset-type.h and the DEFINE_OFFSET_TYPE macro.
I needed a couple generaly-useful preprocessor bits (e.g., yet another
CONCAT implementation), so I started a new common/preprocessor.h file.
I included units tests covering the "offset" types API. These are
mostly compile-time tests, using SFINAE to check that expressions that
shouldn't compile (e.g., comparing unrelated offset types) really are
invalid and would fail to compile. This same idea appeared in my
pending enum-flags revamp from a few months ago (though this version
is a bit further modernized compared to what I had posted), and I plan
on reusing the "check valid expression" bits added here in that
series, so I went ahead and defined the CHECK_VALID_EXPR macro in its
own header -- common/valid-expr.h. I think that's nicer regardless.
I was borderline between calling the new types "offset" types, or
"index" types, BTW. I stuck with "offset" simply because that's what
we're already calling them, mostly.
gdb/ChangeLog:
2017-04-04 Pedro Alves <palves@redhat.com>
* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
unittests/offset-type-selftests.c.
(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
* common/offset-type.h: New file.
* common/preprocessor.h: New file.
* common/traits.h: New file.
* common/valid-expr.h: New file.
* dwarf2expr.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* dwarf2loc.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* gdbtypes.h: Include "common/offset-type.h".
(cu_offset): Now an offset type (strong typedef) instead of a
struct.
(sect_offset): Likewise.
(union call_site_parameter_u): Rename "param_offset" field to
"param_cu_off".
* unittests/offset-type-selftests.c: New file.
2017-04-05 03:03:26 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 04:10:15 +08:00
|
|
|
template<typename T>
|
|
|
|
using RequireLongest = gdb::Requires<gdb::Or<std::is_same<T, LONGEST>,
|
|
|
|
std::is_same<T, ULONGEST>>>;
|
|
|
|
|
Make sect_offset and cu_offset strong typedefs instead of structs
A while ago, back when GDB was a C program, the sect_offset and
cu_offset types were made structs in order to prevent incorrect mixing
of those offsets. Now that we require C++11, we can make them
integers again, while keeping the safety, by exploiting "enum class".
We can add a bit more safety, even, by defining operators that the
types _should_ support, helping making the suspicious uses stand out
more.
Getting at the underlying type is done with the new to_underlying
function added by the previous patch, which also helps better spot
where do we need to step out of the safety net. Mostly, that's around
parsing the DWARF, and when we print the offset for complaint/debug
purposes. But there are other occasional uses.
Since we have to define the sect_offset/cu_offset types in a header
anyway, I went ahead and generalized/library-fied the idea of "offset"
types, making it trivial to add more such types if we find a use. See
common/offset-type.h and the DEFINE_OFFSET_TYPE macro.
I needed a couple generaly-useful preprocessor bits (e.g., yet another
CONCAT implementation), so I started a new common/preprocessor.h file.
I included units tests covering the "offset" types API. These are
mostly compile-time tests, using SFINAE to check that expressions that
shouldn't compile (e.g., comparing unrelated offset types) really are
invalid and would fail to compile. This same idea appeared in my
pending enum-flags revamp from a few months ago (though this version
is a bit further modernized compared to what I had posted), and I plan
on reusing the "check valid expression" bits added here in that
series, so I went ahead and defined the CHECK_VALID_EXPR macro in its
own header -- common/valid-expr.h. I think that's nicer regardless.
I was borderline between calling the new types "offset" types, or
"index" types, BTW. I stuck with "offset" simply because that's what
we're already calling them, mostly.
gdb/ChangeLog:
2017-04-04 Pedro Alves <palves@redhat.com>
* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
unittests/offset-type-selftests.c.
(SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o.
* common/offset-type.h: New file.
* common/preprocessor.h: New file.
* common/traits.h: New file.
* common/valid-expr.h: New file.
* dwarf2expr.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2expr.h: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* dwarf2loc.c: Include "common/underlying.h". Adjust to use
sect_offset and cu_offset strong typedefs throughout.
* dwarf2read.c: Adjust to use sect_offset and cu_offset strong
typedefs throughout.
* gdbtypes.h: Include "common/offset-type.h".
(cu_offset): Now an offset type (strong typedef) instead of a
struct.
(sect_offset): Likewise.
(union call_site_parameter_u): Rename "param_offset" field to
"param_cu_off".
* unittests/offset-type-selftests.c: New file.
2017-04-05 03:03:26 +08:00
|
|
|
#endif /* COMMON_TRAITS_H */
|