Commit | Line | Data |
---|---|---|
9c541725 PA |
1 | /* Compile-time valid expression checker for GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2017-2020 Free Software Foundation, Inc. |
9c541725 PA |
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 | ||
268a13a5 TT |
29 | #include "gdbsupport/preprocessor.h" |
30 | #include "gdbsupport/traits.h" | |
9c541725 PA |
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 | \ | |
9c541725 | 61 | template <TYPENAMES> \ |
1945192c | 62 | using archetype = decltype (EXPR); \ |
9c541725 | 63 | \ |
1945192c PA |
64 | static_assert (gdb::is_detected_exact<EXPR_TYPE, \ |
65 | archetype, TYPES>::value == VALID, \ | |
9c541725 | 66 | ""); \ |
9c541725 PA |
67 | } /* namespace */ |
68 | ||
69 | /* A few convenience macros that support expressions involving a | |
70 | varying numbers of types. If you need more types, feel free to add | |
71 | another variant. */ | |
72 | ||
73 | #define CHECK_VALID_EXPR_1(T1, VALID, EXPR_TYPE, EXPR) \ | |
cf08fb29 PA |
74 | CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1), \ |
75 | ESC_PARENS (T1), \ | |
9c541725 PA |
76 | VALID, EXPR_TYPE, EXPR) |
77 | ||
78 | #define CHECK_VALID_EXPR_2(T1, T2, VALID, EXPR_TYPE, EXPR) \ | |
cf08fb29 PA |
79 | CHECK_VALID_EXPR_INT (ESC_PARENS(typename T1, typename T2), \ |
80 | ESC_PARENS (T1, T2), \ | |
9c541725 PA |
81 | VALID, EXPR_TYPE, EXPR) |
82 | ||
83 | #define CHECK_VALID_EXPR_3(T1, T2, T3, VALID, EXPR_TYPE, EXPR) \ | |
cf08fb29 PA |
84 | CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, typename T3), \ |
85 | ESC_PARENS (T1, T2, T3), \ | |
9c541725 PA |
86 | VALID, EXPR_TYPE, EXPR) |
87 | ||
88 | #define CHECK_VALID_EXPR_4(T1, T2, T3, T4, VALID, EXPR_TYPE, EXPR) \ | |
cf08fb29 PA |
89 | CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \ |
90 | typename T3, typename T4), \ | |
91 | ESC_PARENS (T1, T2, T3, T4), \ | |
9c541725 PA |
92 | VALID, EXPR_TYPE, EXPR) |
93 | ||
04902b09 PA |
94 | #define CHECK_VALID_EXPR_5(T1, T2, T3, T4, T5, VALID, EXPR_TYPE, EXPR) \ |
95 | CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \ | |
96 | typename T3, typename T4, \ | |
97 | typename T5), \ | |
98 | ESC_PARENS (T1, T2, T3, T4, T5), \ | |
99 | VALID, EXPR_TYPE, EXPR) | |
100 | ||
101 | #define CHECK_VALID_EXPR_6(T1, T2, T3, T4, T5, T6, \ | |
102 | VALID, EXPR_TYPE, EXPR) \ | |
103 | CHECK_VALID_EXPR_INT (ESC_PARENS (typename T1, typename T2, \ | |
104 | typename T3, typename T4, \ | |
105 | typename T5, typename T6), \ | |
106 | ESC_PARENS (T1, T2, T3, T4, T5, T6), \ | |
107 | VALID, EXPR_TYPE, EXPR) | |
108 | ||
9c541725 | 109 | #endif /* COMMON_VALID_EXPR_H */ |