verify.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Compile-time assert-like macros.
  2. Copyright (C) 2005-2006, 2009-2010 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. /* Each of these macros verifies that its argument R is nonzero. To
  17. be portable, R should be an integer constant expression. Unlike
  18. assert (R), there is no run-time overhead.
  19. There are two macros, since no single macro can be used in all
  20. contexts in C. verify_true (R) is for scalar contexts, including
  21. integer constant expression contexts. verify (R) is for declaration
  22. contexts, e.g., the top level.
  23. Symbols ending in "__" are private to this header.
  24. The code below uses several ideas.
  25. * The first step is ((R) ? 1 : -1). Given an expression R, of
  26. integral or boolean or floating-point type, this yields an
  27. expression of integral type, whose value is later verified to be
  28. constant and nonnegative.
  29. * Next this expression W is wrapped in a type
  30. struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }.
  31. If W is negative, this yields a compile-time error. No compiler can
  32. deal with a bit-field of negative size.
  33. One might think that an array size check would have the same
  34. effect, that is, that the type struct { unsigned int dummy[W]; }
  35. would work as well. However, inside a function, some compilers
  36. (such as C++ compilers and GNU C) allow local parameters and
  37. variables inside array size expressions. With these compilers,
  38. an array size check would not properly diagnose this misuse of
  39. the verify macro:
  40. void function (int n) { verify (n < 0); }
  41. * For the verify macro, the struct verify_type__ will need to
  42. somehow be embedded into a declaration. To be portable, this
  43. declaration must declare an object, a constant, a function, or a
  44. typedef name. If the declared entity uses the type directly,
  45. such as in
  46. struct dummy {...};
  47. typedef struct {...} dummy;
  48. extern struct {...} *dummy;
  49. extern void dummy (struct {...} *);
  50. extern struct {...} *dummy (void);
  51. two uses of the verify macro would yield colliding declarations
  52. if the entity names are not disambiguated. A workaround is to
  53. attach the current line number to the entity name:
  54. #define _GL_CONCAT0(x, y) x##y
  55. #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  56. extern struct {...} * _GL_CONCAT (dummy, __LINE__);
  57. But this has the problem that two invocations of verify from
  58. within the same macro would collide, since the __LINE__ value
  59. would be the same for both invocations. (The GCC __COUNTER__
  60. macro solves this problem, but is not portable.)
  61. A solution is to use the sizeof operator. It yields a number,
  62. getting rid of the identity of the type. Declarations like
  63. extern int dummy [sizeof (struct {...})];
  64. extern void dummy (int [sizeof (struct {...})]);
  65. extern int (*dummy (void)) [sizeof (struct {...})];
  66. can be repeated.
  67. * Should the implementation use a named struct or an unnamed struct?
  68. Which of the following alternatives can be used?
  69. extern int dummy [sizeof (struct {...})];
  70. extern int dummy [sizeof (struct verify_type__ {...})];
  71. extern void dummy (int [sizeof (struct {...})]);
  72. extern void dummy (int [sizeof (struct verify_type__ {...})]);
  73. extern int (*dummy (void)) [sizeof (struct {...})];
  74. extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
  75. In the second and sixth case, the struct type is exported to the
  76. outer scope; two such declarations therefore collide. GCC warns
  77. about the first, third, and fourth cases. So the only remaining
  78. possibility is the fifth case:
  79. extern int (*dummy (void)) [sizeof (struct {...})];
  80. * GCC warns about duplicate declarations of the dummy function if
  81. -Wredundant_decls is used. GCC 4.3 and later have a builtin
  82. __COUNTER__ macro that can let us generate unique identifiers for
  83. each dummy function, to suppress this warning.
  84. * This implementation exploits the fact that GCC does not warn about
  85. the last declaration mentioned above. If a future version of GCC
  86. introduces a warning for this, the problem could be worked around
  87. by using code specialized to GCC, just as __COUNTER__ is already
  88. being used if available.
  89. #if 4 <= __GNUC__
  90. # define verify(R) [another version to keep GCC happy]
  91. #endif
  92. * In C++, any struct definition inside sizeof is invalid.
  93. Use a template type to work around the problem. */
  94. /* Concatenate two preprocessor tokens. */
  95. # define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
  96. # define _GL_CONCAT0(x, y) x##y
  97. /* _GL_COUNTER is an integer, preferably one that changes each time we
  98. use it. Use __COUNTER__ if it works, falling back on __LINE__
  99. otherwise. __LINE__ isn't perfect, but it's better than a
  100. constant. */
  101. # if defined __COUNTER__ && __COUNTER__ != __COUNTER__
  102. # define _GL_COUNTER __COUNTER__
  103. # else
  104. # define _GL_COUNTER __LINE__
  105. # endif
  106. /* Generate a symbol with the given prefix, making it unique if
  107. possible. */
  108. # define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
  109. /* Verify requirement R at compile-time, as an integer constant expression.
  110. Return 1. */
  111. # ifdef __cplusplus
  112. template <int w>
  113. struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
  114. # define verify_true(R) \
  115. (!!sizeof (verify_type__<(R) ? 1 : -1>))
  116. # else
  117. # define verify_true(R) \
  118. (!!sizeof \
  119. (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
  120. # endif
  121. /* Verify requirement R at compile-time, as a declaration without a
  122. trailing ';'. */
  123. # define verify(R) \
  124. extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)]
  125. #endif