[3611] | 1 | /* Compile-time assert-like macros.
|
---|
| 2 |
|
---|
| 3 | Copyright (C) 2005-2006, 2009-2022 Free Software Foundation, Inc.
|
---|
| 4 |
|
---|
| 5 | This file is free software: you can redistribute it and/or modify
|
---|
| 6 | it under the terms of the GNU Lesser General Public License as
|
---|
| 7 | published by the Free Software Foundation; either version 2.1 of the
|
---|
| 8 | License, or (at your option) any later version.
|
---|
| 9 |
|
---|
| 10 | This file is distributed in the hope that it will be useful,
|
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | GNU Lesser General Public License for more details.
|
---|
| 14 |
|
---|
| 15 | You should have received a copy of the GNU Lesser General Public License
|
---|
| 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
---|
| 17 |
|
---|
| 18 | /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
|
---|
| 19 |
|
---|
| 20 | #ifndef _GL_VERIFY_H
|
---|
| 21 | #define _GL_VERIFY_H
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert (R, DIAGNOSTIC)
|
---|
| 25 | works as per C11. This is supported by GCC 4.6.0+ and by clang 4+.
|
---|
| 26 |
|
---|
| 27 | Define _GL_HAVE__STATIC_ASSERT1 to 1 if _Static_assert (R) works as
|
---|
| 28 | per C23. This is supported by GCC 9.1+.
|
---|
| 29 |
|
---|
| 30 | Support compilers claiming conformance to the relevant standard,
|
---|
| 31 | and also support GCC when not pedantic. If we were willing to slow
|
---|
| 32 | 'configure' down we could also use it with other compilers, but
|
---|
| 33 | since this affects only the quality of diagnostics, why bother? */
|
---|
| 34 | #ifndef __cplusplus
|
---|
| 35 | # if (201112 <= __STDC_VERSION__ \
|
---|
| 36 | || (!defined __STRICT_ANSI__ \
|
---|
| 37 | && (4 < __GNUC__ + (6 <= __GNUC_MINOR__) || 5 <= __clang_major__)))
|
---|
| 38 | # define _GL_HAVE__STATIC_ASSERT 1
|
---|
| 39 | # endif
|
---|
| 40 | # if (202000 <= __STDC_VERSION__ \
|
---|
| 41 | || (!defined __STRICT_ANSI__ && 9 <= __GNUC__))
|
---|
| 42 | # define _GL_HAVE__STATIC_ASSERT1 1
|
---|
| 43 | # endif
|
---|
| 44 | #endif
|
---|
| 45 |
|
---|
| 46 | /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
|
---|
| 47 | system headers, defines a conflicting _Static_assert that is no
|
---|
| 48 | better than ours; override it. */
|
---|
| 49 | #ifndef _GL_HAVE__STATIC_ASSERT
|
---|
| 50 | # include <stddef.h>
|
---|
| 51 | # undef _Static_assert
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | /* Each of these macros verifies that its argument R is nonzero. To
|
---|
| 55 | be portable, R should be an integer constant expression. Unlike
|
---|
| 56 | assert (R), there is no run-time overhead.
|
---|
| 57 |
|
---|
| 58 | If _Static_assert works, verify (R) uses it directly. Similarly,
|
---|
| 59 | _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
|
---|
| 60 | that is an operand of sizeof.
|
---|
| 61 |
|
---|
| 62 | The code below uses several ideas for C++ compilers, and for C
|
---|
| 63 | compilers that do not support _Static_assert:
|
---|
| 64 |
|
---|
| 65 | * The first step is ((R) ? 1 : -1). Given an expression R, of
|
---|
| 66 | integral or boolean or floating-point type, this yields an
|
---|
| 67 | expression of integral type, whose value is later verified to be
|
---|
| 68 | constant and nonnegative.
|
---|
| 69 |
|
---|
| 70 | * Next this expression W is wrapped in a type
|
---|
| 71 | struct _gl_verify_type {
|
---|
| 72 | unsigned int _gl_verify_error_if_negative: W;
|
---|
| 73 | }.
|
---|
| 74 | If W is negative, this yields a compile-time error. No compiler can
|
---|
| 75 | deal with a bit-field of negative size.
|
---|
| 76 |
|
---|
| 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 |
|
---|
| 85 | void function (int n) { verify (n < 0); }
|
---|
| 86 |
|
---|
| 87 | * For the verify macro, the struct _gl_verify_type will need to
|
---|
| 88 | somehow be embedded into a declaration. To be portable, this
|
---|
| 89 | declaration must declare an object, a constant, a function, or a
|
---|
| 90 | typedef name. If the declared entity uses the type directly,
|
---|
| 91 | such as in
|
---|
| 92 |
|
---|
| 93 | struct dummy {...};
|
---|
| 94 | typedef struct {...} dummy;
|
---|
| 95 | extern struct {...} *dummy;
|
---|
| 96 | extern void dummy (struct {...} *);
|
---|
| 97 | extern struct {...} *dummy (void);
|
---|
| 98 |
|
---|
| 99 | two uses of the verify macro would yield colliding declarations
|
---|
| 100 | if the entity names are not disambiguated. A workaround is to
|
---|
| 101 | attach the current line number to the entity name:
|
---|
| 102 |
|
---|
| 103 | #define _GL_CONCAT0(x, y) x##y
|
---|
| 104 | #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
|
---|
| 105 | extern struct {...} * _GL_CONCAT (dummy, __LINE__);
|
---|
| 106 |
|
---|
| 107 | But this has the problem that two invocations of verify from
|
---|
| 108 | within the same macro would collide, since the __LINE__ value
|
---|
| 109 | would be the same for both invocations. (The GCC __COUNTER__
|
---|
| 110 | macro solves this problem, but is not portable.)
|
---|
| 111 |
|
---|
| 112 | A solution is to use the sizeof operator. It yields a number,
|
---|
| 113 | getting rid of the identity of the type. Declarations like
|
---|
| 114 |
|
---|
| 115 | extern int dummy [sizeof (struct {...})];
|
---|
| 116 | extern void dummy (int [sizeof (struct {...})]);
|
---|
| 117 | extern int (*dummy (void)) [sizeof (struct {...})];
|
---|
| 118 |
|
---|
| 119 | can be repeated.
|
---|
| 120 |
|
---|
| 121 | * Should the implementation use a named struct or an unnamed struct?
|
---|
| 122 | Which of the following alternatives can be used?
|
---|
| 123 |
|
---|
| 124 | extern int dummy [sizeof (struct {...})];
|
---|
| 125 | extern int dummy [sizeof (struct _gl_verify_type {...})];
|
---|
| 126 | extern void dummy (int [sizeof (struct {...})]);
|
---|
| 127 | extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
|
---|
| 128 | extern int (*dummy (void)) [sizeof (struct {...})];
|
---|
| 129 | extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
|
---|
| 130 |
|
---|
| 131 | In the second and sixth case, the struct type is exported to the
|
---|
| 132 | outer scope; two such declarations therefore collide. GCC warns
|
---|
| 133 | about the first, third, and fourth cases. So the only remaining
|
---|
| 134 | possibility is the fifth case:
|
---|
| 135 |
|
---|
| 136 | extern int (*dummy (void)) [sizeof (struct {...})];
|
---|
| 137 |
|
---|
| 138 | * GCC warns about duplicate declarations of the dummy function if
|
---|
| 139 | -Wredundant-decls is used. GCC 4.3 and later have a builtin
|
---|
| 140 | __COUNTER__ macro that can let us generate unique identifiers for
|
---|
| 141 | each dummy function, to suppress this warning.
|
---|
| 142 |
|
---|
| 143 | * This implementation exploits the fact that older versions of GCC,
|
---|
| 144 | which do not support _Static_assert, also do not warn about the
|
---|
| 145 | last declaration mentioned above.
|
---|
| 146 |
|
---|
| 147 | * GCC warns if -Wnested-externs is enabled and 'verify' is used
|
---|
| 148 | within a function body; but inside a function, you can always
|
---|
| 149 | arrange to use verify_expr instead.
|
---|
| 150 |
|
---|
| 151 | * In C++, any struct definition inside sizeof is invalid.
|
---|
| 152 | Use a template type to work around the problem. */
|
---|
| 153 |
|
---|
| 154 | /* Concatenate two preprocessor tokens. */
|
---|
| 155 | #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
|
---|
| 156 | #define _GL_CONCAT0(x, y) x##y
|
---|
| 157 |
|
---|
| 158 | /* _GL_COUNTER is an integer, preferably one that changes each time we
|
---|
| 159 | use it. Use __COUNTER__ if it works, falling back on __LINE__
|
---|
| 160 | otherwise. __LINE__ isn't perfect, but it's better than a
|
---|
| 161 | constant. */
|
---|
| 162 | #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
|
---|
| 163 | # define _GL_COUNTER __COUNTER__
|
---|
| 164 | #else
|
---|
| 165 | # define _GL_COUNTER __LINE__
|
---|
| 166 | #endif
|
---|
| 167 |
|
---|
| 168 | /* Generate a symbol with the given prefix, making it unique if
|
---|
| 169 | possible. */
|
---|
| 170 | #define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
|
---|
| 171 |
|
---|
| 172 | /* Verify requirement R at compile-time, as an integer constant expression
|
---|
| 173 | that returns 1. If R is false, fail at compile-time, preferably
|
---|
| 174 | with a diagnostic that includes the string-literal DIAGNOSTIC. */
|
---|
| 175 |
|
---|
| 176 | #define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
|
---|
| 177 | (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
|
---|
| 178 |
|
---|
| 179 | #ifdef __cplusplus
|
---|
| 180 | # if !GNULIB_defined_struct__gl_verify_type
|
---|
| 181 | template <int w>
|
---|
| 182 | struct _gl_verify_type {
|
---|
| 183 | unsigned int _gl_verify_error_if_negative: w;
|
---|
| 184 | };
|
---|
| 185 | # define GNULIB_defined_struct__gl_verify_type 1
|
---|
| 186 | # endif
|
---|
| 187 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
|
---|
| 188 | _gl_verify_type<(R) ? 1 : -1>
|
---|
| 189 | #elif defined _GL_HAVE__STATIC_ASSERT
|
---|
| 190 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
|
---|
| 191 | struct { \
|
---|
| 192 | _Static_assert (R, DIAGNOSTIC); \
|
---|
| 193 | int _gl_dummy; \
|
---|
| 194 | }
|
---|
| 195 | #else
|
---|
| 196 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
|
---|
| 197 | struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
|
---|
| 198 | #endif
|
---|
| 199 |
|
---|
| 200 | /* Verify requirement R at compile-time, as a declaration without a
|
---|
| 201 | trailing ';'. If R is false, fail at compile-time.
|
---|
| 202 |
|
---|
| 203 | This macro requires three or more arguments but uses at most the first
|
---|
| 204 | two, so that the _Static_assert macro optionally defined below supports
|
---|
| 205 | both the C11 two-argument syntax and the C23 one-argument syntax.
|
---|
| 206 |
|
---|
| 207 | Unfortunately, unlike C11, this implementation must appear as an
|
---|
| 208 | ordinary declaration, and cannot appear inside struct { ... }. */
|
---|
| 209 |
|
---|
| 210 | #if 202311 <= __STDC_VERSION__ || 200410 <= __cpp_static_assert
|
---|
| 211 | # define _GL_VERIFY(R, DIAGNOSTIC, ...) static_assert (R, DIAGNOSTIC)
|
---|
| 212 | #elif defined _GL_HAVE__STATIC_ASSERT
|
---|
| 213 | # define _GL_VERIFY(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
|
---|
| 214 | #else
|
---|
| 215 | # define _GL_VERIFY(R, DIAGNOSTIC, ...) \
|
---|
| 216 | extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
|
---|
| 217 | [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
|
---|
| 218 | # if 4 < __GNUC__ + (6 <= __GNUC_MINOR__)
|
---|
| 219 | # pragma GCC diagnostic ignored "-Wnested-externs"
|
---|
| 220 | # endif
|
---|
| 221 | #endif
|
---|
| 222 |
|
---|
| 223 | /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
|
---|
| 224 | #ifdef _GL_STATIC_ASSERT_H
|
---|
| 225 | # if !defined _GL_HAVE__STATIC_ASSERT1 && !defined _Static_assert
|
---|
| 226 | # define _Static_assert(R, ...) \
|
---|
| 227 | _GL_VERIFY ((R), "static assertion failed", -)
|
---|
| 228 | # endif
|
---|
| 229 | # if (!defined static_assert \
|
---|
| 230 | && __STDC_VERSION__ < 202311 \
|
---|
| 231 | && (!defined __cplusplus \
|
---|
| 232 | || (__cpp_static_assert < 201411 \
|
---|
| 233 | && __GNUG__ < 6 && __clang_major__ < 6)))
|
---|
| 234 | # if defined __cplusplus && _MSC_VER >= 1900 && !defined __clang__
|
---|
| 235 | /* MSVC 14 in C++ mode supports the two-arguments static_assert but not
|
---|
| 236 | the one-argument static_assert, and it does not support _Static_assert.
|
---|
| 237 | We have to play preprocessor tricks to distinguish the two cases.
|
---|
| 238 | Since the MSVC preprocessor is not ISO C compliant (cf.
|
---|
| 239 | <https://stackoverflow.com/questions/5134523/>), the solution is specific
|
---|
| 240 | to MSVC. */
|
---|
| 241 | # define _GL_EXPAND(x) x
|
---|
| 242 | # define _GL_SA1(a1) static_assert ((a1), "static assertion failed")
|
---|
| 243 | # define _GL_SA2 static_assert
|
---|
| 244 | # define _GL_SA3 static_assert
|
---|
| 245 | # define _GL_SA_PICK(x1,x2,x3,x4,...) x4
|
---|
| 246 | # define static_assert(...) _GL_EXPAND(_GL_SA_PICK(__VA_ARGS__,_GL_SA3,_GL_SA2,_GL_SA1)) (__VA_ARGS__)
|
---|
| 247 | # else
|
---|
| 248 | # define static_assert _Static_assert /* C11 requires this #define. */
|
---|
| 249 | # endif
|
---|
| 250 | # endif
|
---|
| 251 | #endif
|
---|
| 252 |
|
---|
| 253 | /* @assert.h omit start@ */
|
---|
| 254 |
|
---|
| 255 | #if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))
|
---|
| 256 | # define _GL_HAS_BUILTIN_TRAP 1
|
---|
| 257 | #elif defined __has_builtin
|
---|
| 258 | # define _GL_HAS_BUILTIN_TRAP __has_builtin (__builtin_trap)
|
---|
| 259 | #else
|
---|
| 260 | # define _GL_HAS_BUILTIN_TRAP 0
|
---|
| 261 | #endif
|
---|
| 262 |
|
---|
| 263 | #if 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
|
---|
| 264 | # define _GL_HAS_BUILTIN_UNREACHABLE 1
|
---|
| 265 | #elif defined __has_builtin
|
---|
| 266 | # define _GL_HAS_BUILTIN_UNREACHABLE __has_builtin (__builtin_unreachable)
|
---|
| 267 | #else
|
---|
| 268 | # define _GL_HAS_BUILTIN_UNREACHABLE 0
|
---|
| 269 | #endif
|
---|
| 270 |
|
---|
| 271 | /* Each of these macros verifies that its argument R is nonzero. To
|
---|
| 272 | be portable, R should be an integer constant expression. Unlike
|
---|
| 273 | assert (R), there is no run-time overhead.
|
---|
| 274 |
|
---|
| 275 | There are two macros, since no single macro can be used in all
|
---|
| 276 | contexts in C. verify_expr (R, E) is for scalar contexts, including
|
---|
| 277 | integer constant expression contexts. verify (R) is for declaration
|
---|
| 278 | contexts, e.g., the top level. */
|
---|
| 279 |
|
---|
| 280 | /* Verify requirement R at compile-time. Return the value of the
|
---|
| 281 | expression E. */
|
---|
| 282 |
|
---|
| 283 | #define verify_expr(R, E) \
|
---|
| 284 | (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
|
---|
| 285 |
|
---|
| 286 | /* Verify requirement R at compile-time, as a declaration without a
|
---|
| 287 | trailing ';'. verify (R) acts like static_assert (R) except that
|
---|
| 288 | it is portable to C11/C++14 and earlier, it can issue better
|
---|
| 289 | diagnostics, and its name is shorter and may be more convenient. */
|
---|
| 290 |
|
---|
| 291 | #ifdef __PGI
|
---|
| 292 | /* PGI barfs if R is long. */
|
---|
| 293 | # define verify(R) _GL_VERIFY (R, "verify (...)", -)
|
---|
| 294 | #else
|
---|
| 295 | # define verify(R) _GL_VERIFY (R, "verify (" #R ")", -)
|
---|
| 296 | #endif
|
---|
| 297 |
|
---|
| 298 | /* Assume that R always holds. Behavior is undefined if R is false,
|
---|
| 299 | fails to evaluate, or has side effects.
|
---|
| 300 |
|
---|
| 301 | 'assume (R)' is a directive from the programmer telling the
|
---|
| 302 | compiler that R is true so the compiler needn't generate code to
|
---|
| 303 | test R. This is why 'assume' is in verify.h: it's related to
|
---|
| 304 | static checking (in this case, static checking done by the
|
---|
| 305 | programmer), not dynamic checking.
|
---|
| 306 |
|
---|
| 307 | 'assume (R)' can affect compilation of all the code, not just code
|
---|
| 308 | that happens to be executed after the assume (R) is "executed".
|
---|
| 309 | For example, if the code mistakenly does 'assert (R); assume (R);'
|
---|
| 310 | the compiler is entitled to optimize away the 'assert (R)'.
|
---|
| 311 |
|
---|
| 312 | Although assuming R can help a compiler generate better code or
|
---|
| 313 | diagnostics, performance can suffer if R uses hard-to-optimize
|
---|
| 314 | features such as function calls not inlined by the compiler.
|
---|
| 315 |
|
---|
| 316 | Avoid Clang's __builtin_assume, as it breaks GNU Emacs master
|
---|
| 317 | as of 2020-08-23T21:09:49Z!eggert@cs.ucla.edu; see
|
---|
| 318 | <https://bugs.gnu.org/43152#71>. It's not known whether this breakage
|
---|
| 319 | is a Clang bug or an Emacs bug; play it safe for now. */
|
---|
| 320 |
|
---|
| 321 | #if _GL_HAS_BUILTIN_UNREACHABLE
|
---|
| 322 | # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
|
---|
| 323 | #elif 1200 <= _MSC_VER
|
---|
| 324 | # define assume(R) __assume (R)
|
---|
| 325 | #elif 202311 <= __STDC_VERSION__
|
---|
| 326 | # include <stddef.h>
|
---|
| 327 | # define assume(R) ((R) ? (void) 0 : unreachable ())
|
---|
| 328 | #elif (defined GCC_LINT || defined lint) && _GL_HAS_BUILTIN_TRAP
|
---|
| 329 | /* Doing it this way helps various packages when configured with
|
---|
| 330 | --enable-gcc-warnings, which compiles with -Dlint. It's nicer
|
---|
| 331 | if 'assume' silences warnings with GCC 3.4 through GCC 4.4.7 (2012). */
|
---|
| 332 | # define assume(R) ((R) ? (void) 0 : __builtin_trap ())
|
---|
| 333 | #else
|
---|
| 334 | /* Some older tools grok NOTREACHED, e.g., Oracle Studio 12.6 (2017). */
|
---|
| 335 | # define assume(R) ((R) ? (void) 0 : /*NOTREACHED*/ (void) 0)
|
---|
| 336 | #endif
|
---|
| 337 |
|
---|
| 338 | /* @assert.h omit end@ */
|
---|
| 339 |
|
---|
| 340 | #endif
|
---|