valid-expr.h 4.4 KB

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