verify.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Compile-time assert-like macros.
  2. Copyright (C) 2005-2006, 2009-2020 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 <https://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 (R, DIAGNOSTIC)
  17. works as per C11. This is supported by GCC 4.6.0 and later, in C
  18. mode, and by clang (also in C++ mode).
  19. Define _GL_HAVE__STATIC_ASSERT1 to 1 if _Static_assert (R) works as
  20. per C2X. This is supported by GCC 9.1 and later, and by clang in
  21. C++1z mode.
  22. Define _GL_HAVE_STATIC_ASSERT1 if static_assert (R) works as per
  23. C++17. This is supported by GCC 9.1 and later, and by clang in
  24. C++1z mode.
  25. Support compilers claiming conformance to the relevant standard,
  26. and also support GCC when not pedantic. If we were willing to slow
  27. 'configure' down we could also use it with other compilers, but
  28. since this affects only the quality of diagnostics, why bother? */
  29. #ifndef __cplusplus
  30. # if (201112L <= __STDC_VERSION__ \
  31. || (!defined __STRICT_ANSI__ \
  32. && (4 < __GNUC__ + (6 <= __GNUC_MINOR__) || 4 <= __clang_major__)))
  33. # define _GL_HAVE__STATIC_ASSERT 1
  34. # endif
  35. # if (202000L <= __STDC_VERSION__ \
  36. || (!defined __STRICT_ANSI__ && 9 <= __GNUC__))
  37. # define _GL_HAVE__STATIC_ASSERT1 1
  38. # endif
  39. #else
  40. # if 4 <= __clang_major__
  41. # define _GL_HAVE__STATIC_ASSERT 1
  42. # endif
  43. # if 4 <= __clang_major__ && 201411 <= __cpp_static_assert
  44. # define _GL_HAVE__STATIC_ASSERT1 1
  45. # endif
  46. # if 201703L <= __cplusplus \
  47. || 9 <= __GNUC__ \
  48. || (4 <= __clang_major__ && 201411 <= __cpp_static_assert)
  49. # define _GL_HAVE_STATIC_ASSERT1 1
  50. # endif
  51. #endif
  52. /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
  53. system headers, defines a conflicting _Static_assert that is no
  54. better than ours; override it. */
  55. #ifndef _GL_HAVE__STATIC_ASSERT
  56. # include <stddef.h>
  57. # undef _Static_assert
  58. #endif
  59. /* Each of these macros verifies that its argument R is nonzero. To
  60. be portable, R should be an integer constant expression. Unlike
  61. assert (R), there is no run-time overhead.
  62. If _Static_assert works, verify (R) uses it directly. Similarly,
  63. _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
  64. that is an operand of sizeof.
  65. The code below uses several ideas for C++ compilers, and for C
  66. compilers that do not support _Static_assert:
  67. * The first step is ((R) ? 1 : -1). Given an expression R, of
  68. integral or boolean or floating-point type, this yields an
  69. expression of integral type, whose value is later verified to be
  70. constant and nonnegative.
  71. * Next this expression W is wrapped in a type
  72. struct _gl_verify_type {
  73. unsigned int _gl_verify_error_if_negative: W;
  74. }.
  75. If W is negative, this yields a compile-time error. No compiler can
  76. deal with a bit-field of negative size.
  77. One might think that an array size check would have the same
  78. effect, that is, that the type struct { unsigned int dummy[W]; }
  79. would work as well. However, inside a function, some compilers
  80. (such as C++ compilers and GNU C) allow local parameters and
  81. variables inside array size expressions. With these compilers,
  82. an array size check would not properly diagnose this misuse of
  83. the verify macro:
  84. void function (int n) { verify (n < 0); }
  85. * For the verify macro, the struct _gl_verify_type will need to
  86. somehow be embedded into a declaration. To be portable, this
  87. declaration must declare an object, a constant, a function, or a
  88. typedef name. If the declared entity uses the type directly,
  89. such as in
  90. struct dummy {...};
  91. typedef struct {...} dummy;
  92. extern struct {...} *dummy;
  93. extern void dummy (struct {...} *);
  94. extern struct {...} *dummy (void);
  95. two uses of the verify macro would yield colliding declarations
  96. if the entity names are not disambiguated. A workaround is to
  97. attach the current line number to the entity name:
  98. #define _GL_CONCAT0(x, y) x##y
  99. #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  100. extern struct {...} * _GL_CONCAT (dummy, __LINE__);
  101. But this has the problem that two invocations of verify from
  102. within the same macro would collide, since the __LINE__ value
  103. would be the same for both invocations. (The GCC __COUNTER__
  104. macro solves this problem, but is not portable.)
  105. A solution is to use the sizeof operator. It yields a number,
  106. getting rid of the identity of the type. Declarations like
  107. extern int dummy [sizeof (struct {...})];
  108. extern void dummy (int [sizeof (struct {...})]);
  109. extern int (*dummy (void)) [sizeof (struct {...})];
  110. can be repeated.
  111. * Should the implementation use a named struct or an unnamed struct?
  112. Which of the following alternatives can be used?
  113. extern int dummy [sizeof (struct {...})];
  114. extern int dummy [sizeof (struct _gl_verify_type {...})];
  115. extern void dummy (int [sizeof (struct {...})]);
  116. extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
  117. extern int (*dummy (void)) [sizeof (struct {...})];
  118. extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
  119. In the second and sixth case, the struct type is exported to the
  120. outer scope; two such declarations therefore collide. GCC warns
  121. about the first, third, and fourth cases. So the only remaining
  122. possibility is the fifth case:
  123. extern int (*dummy (void)) [sizeof (struct {...})];
  124. * GCC warns about duplicate declarations of the dummy function if
  125. -Wredundant-decls is used. GCC 4.3 and later have a builtin
  126. __COUNTER__ macro that can let us generate unique identifiers for
  127. each dummy function, to suppress this warning.
  128. * This implementation exploits the fact that older versions of GCC,
  129. which do not support _Static_assert, also do not warn about the
  130. last declaration mentioned above.
  131. * GCC warns if -Wnested-externs is enabled and 'verify' is used
  132. within a function body; but inside a function, you can always
  133. arrange to use verify_expr instead.
  134. * In C++, any struct definition inside sizeof is invalid.
  135. Use a template type to work around the problem. */
  136. /* Concatenate two preprocessor tokens. */
  137. #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  138. #define _GL_CONCAT0(x, y) x##y
  139. /* _GL_COUNTER is an integer, preferably one that changes each time we
  140. use it. Use __COUNTER__ if it works, falling back on __LINE__
  141. otherwise. __LINE__ isn't perfect, but it's better than a
  142. constant. */
  143. #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
  144. # define _GL_COUNTER __COUNTER__
  145. #else
  146. # define _GL_COUNTER __LINE__
  147. #endif
  148. /* Generate a symbol with the given prefix, making it unique if
  149. possible. */
  150. #define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
  151. /* Verify requirement R at compile-time, as an integer constant expression
  152. that returns 1. If R is false, fail at compile-time, preferably
  153. with a diagnostic that includes the string-literal DIAGNOSTIC. */
  154. #define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
  155. (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
  156. #ifdef __cplusplus
  157. # if !GNULIB_defined_struct__gl_verify_type
  158. template <int w>
  159. struct _gl_verify_type {
  160. unsigned int _gl_verify_error_if_negative: w;
  161. };
  162. # define GNULIB_defined_struct__gl_verify_type 1
  163. # endif
  164. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  165. _gl_verify_type<(R) ? 1 : -1>
  166. #elif defined _GL_HAVE__STATIC_ASSERT
  167. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  168. struct { \
  169. _Static_assert (R, DIAGNOSTIC); \
  170. int _gl_dummy; \
  171. }
  172. #else
  173. # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
  174. struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
  175. #endif
  176. /* Verify requirement R at compile-time, as a declaration without a
  177. trailing ';'. If R is false, fail at compile-time.
  178. This macro requires three or more arguments but uses at most the first
  179. two, so that the _Static_assert macro optionally defined below supports
  180. both the C11 two-argument syntax and the C2X one-argument syntax.
  181. Unfortunately, unlike C11, this implementation must appear as an
  182. ordinary declaration, and cannot appear inside struct { ... }. */
  183. #if defined _GL_HAVE__STATIC_ASSERT
  184. # define _GL_VERIFY(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
  185. #else
  186. # define _GL_VERIFY(R, DIAGNOSTIC, ...) \
  187. extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
  188. [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
  189. #endif
  190. /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
  191. #ifdef _GL_STATIC_ASSERT_H
  192. # if !defined _GL_HAVE__STATIC_ASSERT1 && !defined _Static_assert
  193. # define _Static_assert(...) \
  194. _GL_VERIFY (__VA_ARGS__, "static assertion failed", -)
  195. # endif
  196. # if !defined _GL_HAVE_STATIC_ASSERT1 && !defined static_assert
  197. # define static_assert _Static_assert /* C11 requires this #define. */
  198. # endif
  199. #endif
  200. /* @assert.h omit start@ */
  201. #if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))
  202. # define _GL_HAS_BUILTIN_TRAP 1
  203. #elif defined __has_builtin
  204. # define _GL_HAS_BUILTIN_TRAP __has_builtin (__builtin_trap)
  205. #else
  206. # define _GL_HAS_BUILTIN_TRAP 0
  207. #endif
  208. #if 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
  209. # define _GL_HAS_BUILTIN_UNREACHABLE 1
  210. #elif defined __has_builtin
  211. # define _GL_HAS_BUILTIN_UNREACHABLE __has_builtin (__builtin_unreachable)
  212. #else
  213. # define _GL_HAS_BUILTIN_UNREACHABLE 0
  214. #endif
  215. /* Each of these macros verifies that its argument R is nonzero. To
  216. be portable, R should be an integer constant expression. Unlike
  217. assert (R), there is no run-time overhead.
  218. There are two macros, since no single macro can be used in all
  219. contexts in C. verify_expr (R, E) is for scalar contexts, including
  220. integer constant expression contexts. verify (R) is for declaration
  221. contexts, e.g., the top level. */
  222. /* Verify requirement R at compile-time. Return the value of the
  223. expression E. */
  224. #define verify_expr(R, E) \
  225. (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
  226. /* Verify requirement R at compile-time, as a declaration without a
  227. trailing ';'. verify (R) acts like static_assert (R) except that
  228. it is portable to C11/C++14 and earlier, it can issue better
  229. diagnostics, and its name is shorter and may be more convenient. */
  230. #ifdef __PGI
  231. /* PGI barfs if R is long. */
  232. # define verify(R) _GL_VERIFY (R, "verify (...)", -)
  233. #else
  234. # define verify(R) _GL_VERIFY (R, "verify (" #R ")", -)
  235. #endif
  236. /* Assume that R always holds. Behavior is undefined if R is false,
  237. fails to evaluate, or has side effects.
  238. 'assume (R)' is a directive from the programmer telling the
  239. compiler that R is true so the compiler needn't generate code to
  240. test R. This is why 'assume' is in verify.h: it's related to
  241. static checking (in this case, static checking done by the
  242. programmer), not dynamic checking.
  243. 'assume (R)' can affect compilation of all the code, not just code
  244. that happens to be executed after the assume (R) is "executed".
  245. For example, if the code mistakenly does 'assert (R); assume (R);'
  246. the compiler is entitled to optimize away the 'assert (R)'.
  247. Although assuming R can help a compiler generate better code or
  248. diagnostics, performance can suffer if R uses hard-to-optimize
  249. features such as function calls not inlined by the compiler.
  250. Avoid Clang's __builtin_assume, as it breaks GNU Emacs master
  251. as of 2020-08-23T21:09:49Z!eggert@cs.ucla.edu; see
  252. <https://bugs.gnu.org/43152#71>. It's not known whether this breakage
  253. is a Clang bug or an Emacs bug; play it safe for now. */
  254. #if _GL_HAS_BUILTIN_UNREACHABLE
  255. # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
  256. #elif 1200 <= _MSC_VER
  257. # define assume(R) __assume (R)
  258. #elif (defined GCC_LINT || defined lint) && _GL_HAS_BUILTIN_TRAP
  259. /* Doing it this way helps various packages when configured with
  260. --enable-gcc-warnings, which compiles with -Dlint. It's nicer
  261. when 'assume' silences warnings even with older GCCs. */
  262. # define assume(R) ((R) ? (void) 0 : __builtin_trap ())
  263. #else
  264. /* Some tools grok NOTREACHED, e.g., Oracle Studio 12.6. */
  265. # define assume(R) ((R) ? (void) 0 : /*NOTREACHED*/ (void) 0)
  266. #endif
  267. /* @assert.h omit end@ */
  268. #endif