verify.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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 VERIFY_H
  15. # define VERIFY_H 1
  16. /* Define 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 HAVE_STATIC_ASSERT to 1 if static_assert works as per the
  21. C1X draft N1548 section 7.2 or the C++0X draft N3242 section 7.(4).
  22. This will likely be supported by future GCC versions, in C++ mode.
  23. For now, use this only with GCC. Eventually whether _Static_assert
  24. and static_assert works should be determined by 'configure'. */
  25. # if (4 < __GNUC__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__)) && !defined __cplusplus
  26. # define HAVE__STATIC_ASSERT 1
  27. # endif
  28. /* The condition (99 < __GNUC__) is temporary, until we know about the
  29. first G++ release that supports static_assert. */
  30. # if (99 < __GNUC__) && defined __cplusplus
  31. # define HAVE_STATIC_ASSERT 1
  32. # endif
  33. /* Each of these macros verifies that its argument R is nonzero. To
  34. be portable, R should be an integer constant expression. Unlike
  35. assert (R), there is no run-time overhead.
  36. There are two macros, since no single macro can be used in all
  37. contexts in C. verify_true (R) is for scalar contexts, including
  38. integer constant expression contexts. verify (R) is for declaration
  39. contexts, e.g., the top level.
  40. Symbols ending in "__" are private to this header.
  41. If _Static_assert works, verify (R) uses it directly. Similarly,
  42. verify_true (R) works by packaging a _Static_assert inside a struct
  43. that is an operand of sizeof.
  44. The code below uses several ideas for C++ compilers, and for C
  45. compilers that do not support _Static_assert:
  46. * The first step is ((R) ? 1 : -1). Given an expression R, of
  47. integral or boolean or floating-point type, this yields an
  48. expression of integral type, whose value is later verified to be
  49. constant and nonnegative.
  50. * Next this expression W is wrapped in a type
  51. struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }.
  52. If W is negative, this yields a compile-time error. No compiler can
  53. deal with a bit-field of negative size.
  54. One might think that an array size check would have the same
  55. effect, that is, that the type struct { unsigned int dummy[W]; }
  56. would work as well. However, inside a function, some compilers
  57. (such as C++ compilers and GNU C) allow local parameters and
  58. variables inside array size expressions. With these compilers,
  59. an array size check would not properly diagnose this misuse of
  60. the verify macro:
  61. void function (int n) { verify (n < 0); }
  62. * For the verify macro, the struct verify_type__ will need to
  63. somehow be embedded into a declaration. To be portable, this
  64. declaration must declare an object, a constant, a function, or a
  65. typedef name. If the declared entity uses the type directly,
  66. such as in
  67. struct dummy {...};
  68. typedef struct {...} dummy;
  69. extern struct {...} *dummy;
  70. extern void dummy (struct {...} *);
  71. extern struct {...} *dummy (void);
  72. two uses of the verify macro would yield colliding declarations
  73. if the entity names are not disambiguated. A workaround is to
  74. attach the current line number to the entity name:
  75. #define _GL_CONCAT0(x, y) x##y
  76. #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  77. extern struct {...} * _GL_CONCAT (dummy, __LINE__);
  78. But this has the problem that two invocations of verify from
  79. within the same macro would collide, since the __LINE__ value
  80. would be the same for both invocations. (The GCC __COUNTER__
  81. macro solves this problem, but is not portable.)
  82. A solution is to use the sizeof operator. It yields a number,
  83. getting rid of the identity of the type. Declarations like
  84. extern int dummy [sizeof (struct {...})];
  85. extern void dummy (int [sizeof (struct {...})]);
  86. extern int (*dummy (void)) [sizeof (struct {...})];
  87. can be repeated.
  88. * Should the implementation use a named struct or an unnamed struct?
  89. Which of the following alternatives can be used?
  90. extern int dummy [sizeof (struct {...})];
  91. extern int dummy [sizeof (struct verify_type__ {...})];
  92. extern void dummy (int [sizeof (struct {...})]);
  93. extern void dummy (int [sizeof (struct verify_type__ {...})]);
  94. extern int (*dummy (void)) [sizeof (struct {...})];
  95. extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
  96. In the second and sixth case, the struct type is exported to the
  97. outer scope; two such declarations therefore collide. GCC warns
  98. about the first, third, and fourth cases. So the only remaining
  99. possibility is the fifth case:
  100. extern int (*dummy (void)) [sizeof (struct {...})];
  101. * GCC warns about duplicate declarations of the dummy function if
  102. -Wredundant_decls is used. GCC 4.3 and later have a builtin
  103. __COUNTER__ macro that can let us generate unique identifiers for
  104. each dummy function, to suppress this warning.
  105. * This implementation exploits the fact that older versions of GCC,
  106. which do not support _Static_assert, also do not warn about the
  107. last declaration mentioned above.
  108. * In C++, any struct definition inside sizeof is invalid.
  109. Use a template type to work around the problem. */
  110. /* Concatenate two preprocessor tokens. */
  111. # define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  112. # define _GL_CONCAT0(x, y) x##y
  113. /* _GL_COUNTER is an integer, preferably one that changes each time we
  114. use it. Use __COUNTER__ if it works, falling back on __LINE__
  115. otherwise. __LINE__ isn't perfect, but it's better than a
  116. constant. */
  117. # if defined __COUNTER__ && __COUNTER__ != __COUNTER__
  118. # define _GL_COUNTER __COUNTER__
  119. # else
  120. # define _GL_COUNTER __LINE__
  121. # endif
  122. /* Generate a symbol with the given prefix, making it unique if
  123. possible. */
  124. # define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
  125. /* Verify requirement R at compile-time, as an integer constant expression.
  126. Return 1. */
  127. # ifdef __cplusplus
  128. template <int w>
  129. struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
  130. # define verify_true(R) \
  131. (!!sizeof (verify_type__<(R) ? 1 : -1>))
  132. # elif HAVE__STATIC_ASSERT
  133. # define verify_true(R) \
  134. (!!sizeof \
  135. (struct { \
  136. _Static_assert (R, "verify_true (" #R ")"); \
  137. int verify_dummy__; \
  138. }))
  139. # elif HAVE_STATIC_ASSERT
  140. # define verify_true(R) \
  141. (!!sizeof \
  142. (struct { \
  143. static_assert (R, "verify_true (" #R ")"); \
  144. int verify_dummy__; \
  145. }))
  146. # else
  147. # define verify_true(R) \
  148. (!!sizeof \
  149. (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
  150. # endif
  151. /* Verify requirement R at compile-time, as a declaration without a
  152. trailing ';'. */
  153. # if HAVE__STATIC_ASSERT
  154. # define verify(R) _Static_assert (R, "verify (" #R ")")
  155. # elif HAVE_STATIC_ASSERT
  156. # define verify(R) static_assert (R, "verify (" #R ")")
  157. # else
  158. # define verify(R) \
  159. extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)]
  160. # endif
  161. #endif