GDBserver

valid-expr.h
Go to the documentation of this file.
1 /* Compile-time valid expression checker for GDB, the GNU debugger.
2
3 Copyright (C) 2017-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Helper macros used to build compile-time unit tests that make sure
21 that invalid expressions that should not compile would not compile,
22 and that expressions that should compile do compile, and have the
23 right type. This is mainly used to verify that some utility's API
24 is really as safe as intended. */
25
26 #ifndef COMMON_VALID_EXPR_H
27 #define COMMON_VALID_EXPR_H
28
29 #include "common/preprocessor.h"
30 #include "common/traits.h"
31
32 /* Macro that uses SFINAE magic to detect whether the EXPR expression
33 is either valid or ill-formed, at compile time, without actually
34 producing compile-time errors. I.e., check that bad uses of the
35 types (e.g., involving mismatching types) would be caught at
36 compile time. If the expression is valid, also check whether the
37 expression has the right type.
38
39 EXPR must be defined in terms of some of the template parameters,
40 so that template substitution failure discards the overload instead
41 of causing a real compile error. TYPES is thus the list of types
42 involved in the expression, and TYPENAMES is the same list, but
43 with each element prefixed by "typename". These are passed as
44 template parameter types to the templates within the macro.
45
46 VALID is a boolean that indicates whether the expression is
47 supposed to be valid or invalid.
48
49 EXPR_TYPE is the expected type of EXPR. Only meaningful iff VALID
50 is true. If VALID is false, then you must pass "void" as expected
51 type.
52
53 Each invocation of the macro is wrapped in its own namespace to
54 avoid ODR violations. The generated namespace only includes the
55 line number, so client code should wrap sets of calls in a
56 test-specific namespace too, to fully guarantee uniqueness between
57 the multiple clients in the codebase. */
58 #define CHECK_VALID_EXPR_INT(TYPENAMES, TYPES, VALID, EXPR_TYPE, EXPR) \
59 namespace CONCAT (check_valid_expr, __LINE__) { \
60 \
61 template<typename, typename, typename = void> \
62 struct is_valid_expression \
63 : std::false_type {}; \
64 \
65 template <TYPENAMES> \
66 struct is_valid_expression<TYPES, gdb::void_t<decltype (EXPR)>> \
67 : std::true_type {}; \
68 \
69 static_assert (is_valid_expression<TYPES>::value == VALID, \
70 ""); \
71 \
72 template<TYPENAMES, typename = void> \
73 struct is_same_type \
74 : std::is_same<EXPR_TYPE, void> {}; \
75 \
76 template <TYPENAMES> \
77 struct is_same_type<TYPES, gdb::void_t<decltype (EXPR)>> \
78 : std::is_same<EXPR_TYPE, decltype (EXPR)> {}; \
79 \
80 static_assert (is_same_type<TYPES>::value, ""); \
81 } /* namespace */
82
83 /* A few convenience macros that support expressions involving a
84 varying numbers of types. If you need more types, feel free to add
85 another variant. */
86
87 #define CHECK_VALID_EXPR_1(T1, VALID, EXPR_TYPE, EXPR) \
88 CHECK_VALID_EXPR_INT (ESC (typename T1), \
89 ESC (T1), \
90 VALID, EXPR_TYPE, EXPR)
91
92 #define CHECK_VALID_EXPR_2(T1, T2, VALID, EXPR_TYPE, EXPR) \
93 CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2), \
94 ESC (T1, T2), \
95 VALID, EXPR_TYPE, EXPR)
96
97 #define CHECK_VALID_EXPR_3(T1, T2, T3, VALID, EXPR_TYPE, EXPR) \
98 CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2, typename T3), \
99 ESC (T1, T2, T3), \
100 VALID, EXPR_TYPE, EXPR)
101
102 #define CHECK_VALID_EXPR_4(T1, T2, T3, T4, VALID, EXPR_TYPE, EXPR) \
103 CHECK_VALID_EXPR_INT (ESC (typename T1, typename T2, \
104 typename T3, typename T4), \
105 ESC (T1, T2, T3, T4), \
106 VALID, EXPR_TYPE, EXPR)
107
108 #endif /* COMMON_VALID_EXPR_H */
traits.h
preprocessor.h