verify.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Compile-time assert-like macros.
  2. Copyright (C) 2005, 2006 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 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.
  60. A solution is to use the sizeof operator. It yields a number,
  61. getting rid of the identity of the type. Declarations like
  62. extern int dummy [sizeof (struct {...})];
  63. extern void dummy (int [sizeof (struct {...})]);
  64. extern int (*dummy (void)) [sizeof (struct {...})];
  65. can be repeated.
  66. * Should the implementation use a named struct or an unnamed struct?
  67. Which of the following alternatives can be used?
  68. extern int dummy [sizeof (struct {...})];
  69. extern int dummy [sizeof (struct verify_type__ {...})];
  70. extern void dummy (int [sizeof (struct {...})]);
  71. extern void dummy (int [sizeof (struct verify_type__ {...})]);
  72. extern int (*dummy (void)) [sizeof (struct {...})];
  73. extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
  74. In the second and sixth case, the struct type is exported to the
  75. outer scope; two such declarations therefore collide. GCC warns
  76. about the first, third, and fourth cases. So the only remaining
  77. possibility is the fifth case:
  78. extern int (*dummy (void)) [sizeof (struct {...})];
  79. * This implementation exploits the fact that GCC does not warn about
  80. the last declaration mentioned above. If a future version of GCC
  81. introduces a warning for this, the problem could be worked around
  82. by using code specialized to GCC, e.g.,:
  83. #if 4 <= __GNUC__
  84. # define verify(R) \
  85. extern int (* verify_function__ (void)) \
  86. [__builtin_constant_p (R) && (R) ? 1 : -1]
  87. #endif
  88. * In C++, any struct definition inside sizeof is invalid.
  89. Use a template type to work around the problem. */
  90. /* Verify requirement R at compile-time, as an integer constant expression.
  91. Return 1. */
  92. # ifdef __cplusplus
  93. template <int w>
  94. struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
  95. # define verify_true(R) \
  96. (!!sizeof (verify_type__<(R) ? 1 : -1>))
  97. # else
  98. # define verify_true(R) \
  99. (!!sizeof \
  100. (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
  101. # endif
  102. /* Verify requirement R at compile-time, as a declaration without a
  103. trailing ';'. */
  104. # define verify(R) extern int (* verify_function__ (void)) [verify_true (R)]
  105. #endif