verify.h 9.8 KB

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