diff options
author | Pedro Alves <palves@redhat.com> | 2017-04-04 20:03:26 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-04-04 20:03:26 +0100 |
commit | 9c5417255690af00751c7d506172459afe856894 (patch) | |
tree | 8d6da311b5442e8add81838cb178eb1aba8ad13d /gdb/unittests | |
parent | ecfb656c37b982479d8eb07f240b434772d98fd6 (diff) | |
download | gdb-9c5417255690af00751c7d506172459afe856894.zip gdb-9c5417255690af00751c7d506172459afe856894.tar.gz gdb-9c5417255690af00751c7d506172459afe856894.tar.bz2 |
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.
Diffstat (limited to 'gdb/unittests')
-rw-r--r-- | gdb/unittests/offset-type-selftests.c | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/gdb/unittests/offset-type-selftests.c b/gdb/unittests/offset-type-selftests.c new file mode 100644 index 0000000..a11e908 --- /dev/null +++ b/gdb/unittests/offset-type-selftests.c @@ -0,0 +1,178 @@ +/* Self tests for offset types for GDB, the GNU debugger. + + Copyright (C) 2017 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/>. */ + +#include "defs.h" +#include "selftest.h" +#include "common/offset-type.h" +#include "common/underlying.h" +#include "common/valid-expr.h" + +namespace selftests { +namespace offset_type { + +DEFINE_OFFSET_TYPE (off_A, unsigned int); +DEFINE_OFFSET_TYPE (off_B, unsigned int); + +/* First, compile-time tests that: + + - make sure that incorrect operations with mismatching types are + caught at compile time. + + - make sure that the same operations but involving the right types + do compile and that they return the correct type. +*/ + +#define CHECK_VALID(VALID, EXPR_TYPE, EXPR) \ + CHECK_VALID_EXPR_2 (off_A, off_B, VALID, EXPR_TYPE, EXPR) + +off_A lval_a {}; +off_B lval_b {}; + +using undrl = std::underlying_type<off_A>::type; + +/* Offset +/- underlying. */ + +CHECK_VALID (true, off_A, off_A {} + undrl {}); +CHECK_VALID (true, off_A, off_A {} - undrl {}); +CHECK_VALID (true, off_A, undrl {} + off_A {}); +CHECK_VALID (true, off_A, undrl {} - off_A {}); + +/* Add offset types. Both same and different. */ + +CHECK_VALID (false, void, off_A {} + off_A {}); +CHECK_VALID (false, void, off_A {} + off_B {}); + +/* Subtract offset types. Both same and different. */ + +CHECK_VALID (false, void, off_B {} - off_A {}); +CHECK_VALID (true, undrl, off_A {} - off_A {}); + +/* Add/assign offset types. Both same and different. */ + +CHECK_VALID (false, void, lval_a += off_A {}); +CHECK_VALID (false, void, lval_a += off_B {}); +CHECK_VALID (false, void, lval_a -= off_A {}); +CHECK_VALID (false, void, lval_a -= off_B {}); + +/* operator OP+= (offset, underlying), lvalue ref on the lhs. */ + +CHECK_VALID (true, off_A&, lval_a += undrl {}); +CHECK_VALID (true, off_A&, lval_a -= undrl {}); + +/* operator OP+= (offset, underlying), rvalue ref on the lhs. */ + +CHECK_VALID (false, void, off_A {} += undrl {}); +CHECK_VALID (false, void, off_A {} -= undrl {}); + +/* Rel ops, with same type. */ + +CHECK_VALID (true, bool, off_A {} < off_A {}); +CHECK_VALID (true, bool, off_A {} > off_A {}); +CHECK_VALID (true, bool, off_A {} <= off_A {}); +CHECK_VALID (true, bool, off_A {} >= off_A {}); + +/* Rel ops, with unrelated offset types. */ + +CHECK_VALID (false, void, off_A {} < off_B {}); +CHECK_VALID (false, void, off_A {} > off_B {}); +CHECK_VALID (false, void, off_A {} <= off_B {}); +CHECK_VALID (false, void, off_A {} >= off_B {}); + +/* Rel ops, with unrelated types. */ + +CHECK_VALID (false, void, off_A {} < undrl {}); +CHECK_VALID (false, void, off_A {} > undrl {}); +CHECK_VALID (false, void, off_A {} <= undrl {}); +CHECK_VALID (false, void, off_A {} >= undrl {}); + +static void +run_tests () +{ + /* Test op+ and op-. */ + { + constexpr off_A a {}; + static_assert (to_underlying (a) == 0, ""); + + { + constexpr off_A res1 = a + 2; + static_assert (to_underlying (res1) == 2, ""); + + constexpr off_A res2 = res1 - 1; + static_assert (to_underlying (res2) == 1, ""); + } + + { + constexpr off_A res1 = 2 + a; + static_assert (to_underlying (res1) == 2, ""); + + constexpr off_A res2 = 3 - res1; + static_assert (to_underlying (res2) == 1, ""); + } + } + + /* Test op+= and op-=. */ + { + off_A o {}; + + o += 10; + SELF_CHECK (to_underlying (o) == 10); + o -= 5; + SELF_CHECK (to_underlying (o) == 5); + } + + /* Test op-. */ + { + constexpr off_A o1 = (off_A) 10; + constexpr off_A o2 = (off_A) 20; + + constexpr unsigned int delta = o2 - o1; + + static_assert (delta == 10, ""); + } + + /* Test <, <=, >, >=. */ + { + constexpr off_A o1 = (off_A) 10; + constexpr off_A o2 = (off_A) 20; + + static_assert (o1 < o2, ""); + static_assert (!(o2 < o1), ""); + + static_assert (o2 > o1, ""); + static_assert (!(o1 > o2), ""); + + static_assert (o1 <= o2, ""); + static_assert (!(o2 <= o1), ""); + + static_assert (o2 >= o1, ""); + static_assert (!(o1 >= o2), ""); + + static_assert (o1 <= o1, ""); + static_assert (o1 >= o1, ""); + } +} + +} /* namespace offset_type */ +} /* namespace selftests */ + +void +_initialize_offset_type_selftests () +{ + register_self_test (selftests::offset_type::run_tests); +} |