aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport/valid-expr.h
blob: 6d2b481c6e13f3f6bab0a872338a4bf39ea84aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Compile-time valid expression checker for GDB, the GNU debugger.

   Copyright (C) 2017-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/>.  */

/* 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, typename = decltype (EXPR)>			\
  struct archetype							\
  {									\
  };									\
									\
  static_assert (gdb::is_detected_exact<archetype<TYPES, 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 */