Files
binutils-gdb/gdbsupport/valid-expr.h
Pedro Alves 04902b0995 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.
2020-09-14 22:21:07 +01:00

110 lines
4.4 KiB
C++

/* Compile-time valid expression checker for GDB, the GNU debugger.
Copyright (C) 2017-2020 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/>. */
/* Helper macros used to build compile-time unit tests that make sure
that invalid expressions that should not compile would not compile,
and that expressions that should compile do compile, and have the
right type. This is mainly used to verify that some utility's API
is really as safe as intended. */
#ifndef COMMON_VALID_EXPR_H
#define COMMON_VALID_EXPR_H
#include "gdbsupport/preprocessor.h"
#include "gdbsupport/traits.h"
/* Macro that uses SFINAE magic to detect whether the EXPR expression
is either valid or ill-formed, at compile time, without actually
producing compile-time errors. I.e., check that bad uses of the
types (e.g., involving mismatching types) would be caught at
compile time. If the expression is valid, also check whether the
expression has the right type.
EXPR must be defined in terms of some of the template parameters,
so that template substitution failure discards the overload instead
of causing a real compile error. TYPES is thus the list of types
involved in the expression, and TYPENAMES is the same list, but
with each element prefixed by "typename". These are passed as
template parameter types to the templates within the macro.
VALID is a boolean that indicates whether the expression is
supposed to be valid or invalid.
EXPR_TYPE is the expected type of EXPR. Only meaningful iff VALID
is true. If VALID is false, then you must pass "void" as expected
type.
Each invocation of the macro is wrapped in its own namespace to
avoid ODR violations. The generated namespace only includes the
line number, so client code should wrap sets of calls in a
test-specific namespace too, to fully guarantee uniqueness between
the multiple clients in the codebase. */
#define CHECK_VALID_EXPR_INT(TYPENAMES, TYPES, VALID, EXPR_TYPE, EXPR) \
namespace CONCAT (check_valid_expr, __LINE__) { \
\
template <TYPENAMES> \
using archetype = decltype (EXPR); \
\
static_assert (gdb::is_detected_exact<EXPR_TYPE, \
archetype, TYPES>::value == VALID, \
""); \
} /* namespace */
/* A few convenience macros that support expressions involving a
varying numbers of types. If you need more types, feel free to add
another variant. */
#define CHECK_VALID_EXPR_1(T1, VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1), \
ESC_PARENS (T1), \
VALID, EXPR_TYPE, EXPR)
#define CHECK_VALID_EXPR_2(T1, T2, VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS(typename T1, typename T2), \
ESC_PARENS (T1, T2), \
VALID, EXPR_TYPE, EXPR)
#define CHECK_VALID_EXPR_3(T1, T2, T3, VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, typename T3), \
ESC_PARENS (T1, T2, T3), \
VALID, EXPR_TYPE, EXPR)
#define CHECK_VALID_EXPR_4(T1, T2, T3, T4, VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \
typename T3, typename T4), \
ESC_PARENS (T1, T2, T3, T4), \
VALID, EXPR_TYPE, EXPR)
#define CHECK_VALID_EXPR_5(T1, T2, T3, T4, T5, VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \
typename T3, typename T4, \
typename T5), \
ESC_PARENS (T1, T2, T3, T4, T5), \
VALID, EXPR_TYPE, EXPR)
#define CHECK_VALID_EXPR_6(T1, T2, T3, T4, T5, T6, \
VALID, EXPR_TYPE, EXPR) \
CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \
typename T3, typename T4, \
typename T5, typename T6), \
ESC_PARENS (T1, T2, T3, T4, T5, T6), \
VALID, EXPR_TYPE, EXPR)
#endif /* COMMON_VALID_EXPR_H */