summaryrefslogtreecommitdiff
path: root/gdb/compile/compile-c-types.c
diff options
context:
space:
mode:
authorPedro Alves <pedro@palves.net>2020-09-14 21:16:59 +0100
committerPedro Alves <pedro@palves.net>2020-09-14 22:21:07 +0100
commit04902b0995f12ba6f4bdc7c8a2d4a46afae4365d (patch)
tree77b228a70973ea34eaf54b9bfd330b6f22bd3a99 /gdb/compile/compile-c-types.c
parent1945192cb9a6184fb805d514ce4ca1bc8999b587 (diff)
downloadbinutils-gdb-04902b0995f12ba6f4bdc7c8a2d4a46afae4365d.tar.gz
Rewrite enum_flags, add unit tests, fix problems
This patch started by adding comprehensive unit tests for enum_flags. For the testing part, it adds: - tests of normal expected uses of the API. - checks that _invalid_ uses of the API would fail to compile. I.e., it validates that enum_flags really is a strong type, and that incorrect mixing of enum types would be caught at compile time. It pulls that off making use of SFINEA and C++11's decltype/constexpr. This revealed many holes in the enum_flags API. For example, the f1 assignment below currently incorrectly fails to compile: enum_flags<flags> f1 = FLAG1; enum_flags<flags> f2 = FLAG2 | f1; The unit tests also revealed that this useful use case doesn't work: enum flag { FLAG1 = 1, FLAG2 = 2 }; enum_flags<flag> src = FLAG1; enum_flags<flag> f1 = condition ? src : FLAG2; It fails to compile because enum_flags<flag> and flag are convertible to each other. Turns out that making enum_flags be implicitly convertible to the backing raw enum type was not a good idea. If we make it convertible to the underlying type instead, we fix that ternary operator use case, and, we find cases throughout the codebase that should be using the enum_flags but were using the raw backing enum instead. So it's a good change overall. Also, several operators were missing. These holes and more are plugged by this patch, by reworking how the enum_flags operators are implemented, and making use of C++11's feature of being able to delete methods/functions. There are cases in gdb/compile/ where we need to call a function in a C plugin API that expects the raw enum. To address cases like that, this adds a "raw()" method to enum_flags. This way we can keep using the safer enum_flags to construct the value, and then be explicit when we need to get at the raw enum. This makes most of the enum_flags operators constexpr. Beyond enabling more compiler optimizations and enabling the new unit tests, this has other advantages, like making it possible to use operator| with enum_flags values in switch cases, where only compile-time constants are allowed: enum_flags<flags> f = FLAG1 | FLAG2; switch (f) { case FLAG1 | FLAG2: break; } Currently that fails to compile. It also switches to a different mechanism of enabling the global operators. The current mechanism isn't namespace friendly, the new one is. It also switches to C++11-style SFINAE -- instead of wrapping the return type in a SFINAE-friently structure, we use an unnamed template parameter. I.e., this: template <typename enum_type, typename = is_enum_flags_enum_type_t<enum_type>> enum_type operator& (enum_type e1, enum_type e2) instead of: template <typename enum_type> typename enum_flags_type<enum_type>::type operator& (enum_type e1, enum_type e2) Note that the static_assert inside operator~() was converted to a couple overloads (signed vs unsigned), because static_assert is too late for SFINAE-based tests, which is important for the CHECK_VALID unit tests. Tested with gcc {4.8, 7.1, 9.3} and clang {5.0.2, 10.0.0}. gdb/ChangeLog: * Makefile.in (SELFTESTS_SRCS): Add unittests/enum-flags-selftests.c. * btrace.c (ftrace_update_caller, ftrace_fixup_calle): Use btrace_function_flags instead of enum btrace_function_flag. * compile/compile-c-types.c (convert_qualified): Use enum_flags::raw. * compile/compile-cplus-symbols.c (convert_one_symbol) (convert_symbol_bmsym): * compile/compile-cplus-types.c (compile_cplus_convert_method) (compile_cplus_convert_struct_or_union_methods) (compile_cplus_instance::convert_qualified_base): * go-exp.y (parse_string_or_char): Add cast to int. * unittests/enum-flags-selftests.c: New file. * record-btrace.c (btrace_thread_flag_to_str): Change parameter's type to btrace_thread_flags from btrace_thread_flag. (record_btrace_cancel_resume, record_btrace_step_thread): Change local's type to btrace_thread_flags from btrace_thread_flag. Add cast in DEBUG call. gdbsupport/ChangeLog: * enum-flags.h: Include "traits.h". (DEF_ENUM_FLAGS_TYPE): Declare a function instead of defining a structure. (enum_underlying_type): Update comment. (namespace enum_flags_detail): New. Move struct zero_type here. (EnumIsUnsigned, EnumIsSigned): New. (class enum_flags): Make most methods constexpr. (operator&=, operator|=, operator^=): Take an enum_flags instead of an enum_type. Make rvalue ref versions deleted. (operator enum_type()): Delete. (operator&, operator|, operator^, operator~): Delete, moved out of class. (raw()): New method. (is_enum_flags_enum_type_t): Declare. (ENUM_FLAGS_GEN_BINOP, ENUM_FLAGS_GEN_COMPOUND_ASSIGN) (ENUM_FLAGS_GEN_COMP): New. Use them to reimplement global operators. (operator~): Now constexpr and reimplemented. (operator<<, operator>>): New deleted functions. * valid-expr.h (CHECK_VALID_EXPR_5, CHECK_VALID_EXPR_6): New.
Diffstat (limited to 'gdb/compile/compile-c-types.c')
-rw-r--r--gdb/compile/compile-c-types.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/gdb/compile/compile-c-types.c b/gdb/compile/compile-c-types.c
index 6d9bb37b17c..6c9135947ea 100644
--- a/gdb/compile/compile-c-types.c
+++ b/gdb/compile/compile-c-types.c
@@ -254,7 +254,8 @@ convert_qualified (compile_c_instance *context, struct type *type)
if (TYPE_RESTRICT (type))
quals |= GCC_QUALIFIER_RESTRICT;
- return context->plugin ().build_qualified_type (unqual_converted, quals);
+ return context->plugin ().build_qualified_type (unqual_converted,
+ quals.raw ());
}
/* Convert a complex type to its gcc representation. */