verify.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Compile-time assert-like macros.
  2. Copyright (C) 2005-2006, 2009-2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
  14. #ifndef _GL_VERIFY_H
  15. # define _GL_VERIFY_H
  16. /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per the
  17. C1X draft N1548 section 6.7.10. This is supported by GCC 4.6.0 and
  18. later, in C mode, and its use here generates easier-to-read diagnostics
  19. when verify (R) fails.
  20. Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per the
  21. C++0X draft N3242 section 7.(4).
  22. This will likely be supported by future GCC versions, in C++ mode.
  23. Use this only with GCC. If we were willing to slow 'configure'
  24. down we could also use it with other compilers, but since this
  25. affects only the quality of diagnostics, why bother? */
  26. # if (4 < __GNUC__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__)) && !defined __cplusplus
  27. # define _GL_HAVE__STATIC_ASSERT 1
  28. # endif
  29. /* The condition (99 < __GNUC__) is temporary, until we know about the
  30. first G++ release that supports static_assert. */
  31. # if (99 < __GNUC__) && defined __cplusplus
  32. # define _GL_HAVE_STATIC_ASSERT 1
  33. # endif
  34. /* Each of these macros verifies that its argument R is nonzero. To
  35. be portable, R should be an integer constant expression. Unlike
  36. assert (R), there is no run-time overhead.
  37. If _Static_assert works, verify (R) uses it directly. Similarly,
  38. _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
  39. that is an operand of sizeof.
  40. The code below uses several ideas for C++ compilers, and for C
  41. compilers that do not support _Static_assert:
  42. * The first step is ((R) ? 1 : -1). Given an expression R, of
  43. integral or boolean or floating-point type, this yields an
  44. expression of integral type, whose value is later verified to be
  45. constant and nonnegative.
  46. * Next this expression W is wrapped in a type
  47. struct _gl_verify_type {
  48. unsigned int _gl_verify_error_if_negative: W;
  49. }.
  50. If W is negative, this yields a compile-time error. No compiler can
  51. deal with a bit-field of negative size.
  52. One might think that an array size check would have the same
  53. effect, that is, that the type struct { unsigned int dummy[W]; }
  54. would work as well. However, inside a function, some compilers
  55. (such as C++ compilers and GNU C) allow local parameters and
  56. variables inside array size expressions. With these compilers,
  57. an array size check would not properly diagnose this misuse of
  58. the verify macro:
  59. void function (int n) { verify (n < 0); }
  60. * For the verify macro, the struct _gl_verify_type will need to
  61. somehow be embedded into a declaration. To be portable, this
  62. declaration must declare an object, a constant, a function, or a
  63. typedef name. If the declared entity uses the type directly,
  64. such as in
  65. struct dummy {...};
  66. typedef struct {...} dummy;
  67. extern struct {...} *dummy;
  68. extern void dummy (struct {...} *);
  69. extern struct {...} *dummy (void);
  70. two uses of the verify macro would yield colliding declarations
  71. if the entity names are not disambiguated. A workaround is to
  72. attach the current line number to the entity name:
  73. #define _GL_CONCAT0(x, y) x##y
  74. #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  75. extern struct {...} * _GL_CONCAT (dummy, __LINE__);
  76. But this has the problem that two invocations of verify from
  77. within the same macro would collide, since the __LINE__ value
  78. would be the same for both invocations. (The GCC __COUNTER__
  79. macro solves this problem, but is not portable.)
  80. A solution is to use the sizeof operator. It yields a number,
  81. getting rid of the identity of the type. Declarations like
  82. extern int dummy [sizeof (struct {...})];
  83. extern void dummy (int [sizeof (struct {...})]);
  84. extern int (*dummy (void)) [sizeof (struct {...})];
  85. can be repeated.
  86. * Should the implementation use a named struct or an unnamed struct?
  87. Which of the following alternatives can be used?
  88. extern int dummy [sizeof (struct {...})];
  89. extern int dummy [sizeof (struct _gl_verify_type {...})];
  90. extern void dummy (int [sizeof (struct {...})]);
  91. extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
  92. extern int (*dummy (void)) [sizeof (struct {...})];
  93. extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
  94. In the second and sixth case, the struct type is exported to the
  95. outer scope; two such declarations therefore collide. GCC warns
  96. about the first, third, and fourth cases. So the only remaining
  97. possibility is the fifth case:
  98. extern int (*dummy (void)) [sizeof (struct {...})];
  99. * GCC warns about duplicate declarations of the dummy function if
  100. -Wredundant_decls is used. GCC 4.3 and later have a builtin
  101. __COUNTER__ macro that can let us generate unique identifiers for
  102. each dummy function, to suppress this warning.
  103. * This implementation exploits the fact that older versions of GCC,
  104. which do not support _Static_assert, also do not warn about the
  105. last declaration mentioned above.
  106. * In C++, any struct definition inside sizeof is invalid.
  107. Use a template type to work around the problem. */
  108. /* Concatenate two preprocessor tokens. */
  109. # define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  110. # define _GL_CONCAT0(x, y) x##y
  111. /* _GL_COUNTER is an integer, preferably one that changes each time we
  112. use it. Use __COUNTER__ if it works, falling back on __LINE__
  113. otherwise. __LINE__ isn't perfect, but it's better than a
  114. constant. */
  115. # if defined __COUNTER__ && __COUNTER__ != __COUNTER__
  116. # define _GL_COUNTER __COUNTER__
  117. # else
  118. # define _GL_COUNTER __LINE__
  119. # endif
  120. /* Generate a symbol with the given prefix, making it unique if
  121. possible. */
  122. # define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
  123. /* Verify requirement R at compile-time, as an integer constant expression
  124. that returns 1. If R is false, fail at compile-time, preferably
  125. with a diagnostic that includes the string-literal DIAGNOSTIC. */
  126. # define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
  127. (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
  128. # ifdef __cplusplus
  129. # if !GNULIB_defined_struct__gl_verify_type
  130. template <int w>
  131. struct _gl_verify_type {
  132. unsigned int _gl_verify_error_if_negative: w;
  133. };
  134. # define GNULIB_defined_struct__gl_verify_type 1
  135. # endif
  136. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  137. _gl_verify_type<(R) ? 1 : -1>
  138. # elif defined _GL_HAVE__STATIC_ASSERT
  139. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  140. struct { \
  141. _Static_assert (R, DIAGNOSTIC); \
  142. int _gl_dummy; \
  143. }
  144. # else
  145. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  146. struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
  147. # endif
  148. /* Verify requirement R at compile-time, as a declaration without a
  149. trailing ';'. If R is false, fail at compile-time, preferably
  150. with a diagnostic that includes the string-literal DIAGNOSTIC.
  151. Unfortunately, unlike C1X, this implementation must appear as an
  152. ordinary declaration, and cannot appear inside struct { ... }. */
  153. # ifdef _GL_HAVE__STATIC_ASSERT
  154. # define _GL_VERIFY _Static_assert
  155. # else
  156. # define _GL_VERIFY(R, DIAGNOSTIC) \
  157. extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
  158. [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
  159. # endif
  160. /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
  161. # ifdef _GL_STATIC_ASSERT_H
  162. # if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
  163. # define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
  164. # endif
  165. # if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
  166. # define static_assert _Static_assert /* Draft C1X requires this #define. */
  167. # endif
  168. # endif
  169. /* @assert.h omit start@ */
  170. /* Each of these macros verifies that its argument R is nonzero. To
  171. be portable, R should be an integer constant expression. Unlike
  172. assert (R), there is no run-time overhead.
  173. There are two macros, since no single macro can be used in all
  174. contexts in C. verify_true (R) is for scalar contexts, including
  175. integer constant expression contexts. verify (R) is for declaration
  176. contexts, e.g., the top level. */
  177. /* Verify requirement R at compile-time, as an integer constant expression.
  178. Return 1. This is equivalent to verify_expr (R, 1).
  179. verify_true is obsolescent; please use verify_expr instead. */
  180. # define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
  181. /* Verify requirement R at compile-time. Return the value of the
  182. expression E. */
  183. # define verify_expr(R, E) \
  184. (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
  185. /* Verify requirement R at compile-time, as a declaration without a
  186. trailing ';'. */
  187. # define verify(R) _GL_VERIFY (R, "verify (" #R ")")
  188. /* @assert.h omit end@ */
  189. #endif