Blame lib/verify.h

Packit 8f70b4
/* Compile-time assert-like macros.
Packit 8f70b4
Packit 8f70b4
   Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
/* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
Packit 8f70b4
Packit 8f70b4
#ifndef _GL_VERIFY_H
Packit 8f70b4
#define _GL_VERIFY_H
Packit 8f70b4
Packit 8f70b4
Packit 8f70b4
/* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11.
Packit 8f70b4
   This is supported by GCC 4.6.0 and later, in C mode, and its use
Packit 8f70b4
   here generates easier-to-read diagnostics when verify (R) fails.
Packit 8f70b4
Packit 8f70b4
   Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11.
Packit 8f70b4
   This will likely be supported by future GCC versions, in C++ mode.
Packit 8f70b4
Packit 8f70b4
   Use this only with GCC.  If we were willing to slow 'configure'
Packit 8f70b4
   down we could also use it with other compilers, but since this
Packit 8f70b4
   affects only the quality of diagnostics, why bother?  */
Packit 8f70b4
#if (4 < __GNUC__ + (6 <= __GNUC_MINOR__) \
Packit 8f70b4
     && (201112L <= __STDC_VERSION__  || !defined __STRICT_ANSI__) \
Packit 8f70b4
     && !defined __cplusplus)
Packit 8f70b4
# define _GL_HAVE__STATIC_ASSERT 1
Packit 8f70b4
#endif
Packit 8f70b4
/* The condition (99 < __GNUC__) is temporary, until we know about the
Packit 8f70b4
   first G++ release that supports static_assert.  */
Packit 8f70b4
#if (99 < __GNUC__) && defined __cplusplus
Packit 8f70b4
# define _GL_HAVE_STATIC_ASSERT 1
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
Packit 8f70b4
   system headers, defines a conflicting _Static_assert that is no
Packit 8f70b4
   better than ours; override it.  */
Packit 8f70b4
#ifndef _GL_HAVE_STATIC_ASSERT
Packit 8f70b4
# include <stddef.h>
Packit 8f70b4
# undef _Static_assert
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Each of these macros verifies that its argument R is nonzero.  To
Packit 8f70b4
   be portable, R should be an integer constant expression.  Unlike
Packit 8f70b4
   assert (R), there is no run-time overhead.
Packit 8f70b4
Packit 8f70b4
   If _Static_assert works, verify (R) uses it directly.  Similarly,
Packit 8f70b4
   _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
Packit 8f70b4
   that is an operand of sizeof.
Packit 8f70b4
Packit 8f70b4
   The code below uses several ideas for C++ compilers, and for C
Packit 8f70b4
   compilers that do not support _Static_assert:
Packit 8f70b4
Packit 8f70b4
   * The first step is ((R) ? 1 : -1).  Given an expression R, of
Packit 8f70b4
     integral or boolean or floating-point type, this yields an
Packit 8f70b4
     expression of integral type, whose value is later verified to be
Packit 8f70b4
     constant and nonnegative.
Packit 8f70b4
Packit 8f70b4
   * Next this expression W is wrapped in a type
Packit 8f70b4
     struct _gl_verify_type {
Packit 8f70b4
       unsigned int _gl_verify_error_if_negative: W;
Packit 8f70b4
     }.
Packit 8f70b4
     If W is negative, this yields a compile-time error.  No compiler can
Packit 8f70b4
     deal with a bit-field of negative size.
Packit 8f70b4
Packit 8f70b4
     One might think that an array size check would have the same
Packit 8f70b4
     effect, that is, that the type struct { unsigned int dummy[W]; }
Packit 8f70b4
     would work as well.  However, inside a function, some compilers
Packit 8f70b4
     (such as C++ compilers and GNU C) allow local parameters and
Packit 8f70b4
     variables inside array size expressions.  With these compilers,
Packit 8f70b4
     an array size check would not properly diagnose this misuse of
Packit 8f70b4
     the verify macro:
Packit 8f70b4
Packit 8f70b4
       void function (int n) { verify (n < 0); }
Packit 8f70b4
Packit 8f70b4
   * For the verify macro, the struct _gl_verify_type will need to
Packit 8f70b4
     somehow be embedded into a declaration.  To be portable, this
Packit 8f70b4
     declaration must declare an object, a constant, a function, or a
Packit 8f70b4
     typedef name.  If the declared entity uses the type directly,
Packit 8f70b4
     such as in
Packit 8f70b4
Packit 8f70b4
       struct dummy {...};
Packit 8f70b4
       typedef struct {...} dummy;
Packit 8f70b4
       extern struct {...} *dummy;
Packit 8f70b4
       extern void dummy (struct {...} *);
Packit 8f70b4
       extern struct {...} *dummy (void);
Packit 8f70b4
Packit 8f70b4
     two uses of the verify macro would yield colliding declarations
Packit 8f70b4
     if the entity names are not disambiguated.  A workaround is to
Packit 8f70b4
     attach the current line number to the entity name:
Packit 8f70b4
Packit 8f70b4
       #define _GL_CONCAT0(x, y) x##y
Packit 8f70b4
       #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
Packit 8f70b4
       extern struct {...} * _GL_CONCAT (dummy, __LINE__);
Packit 8f70b4
Packit 8f70b4
     But this has the problem that two invocations of verify from
Packit 8f70b4
     within the same macro would collide, since the __LINE__ value
Packit 8f70b4
     would be the same for both invocations.  (The GCC __COUNTER__
Packit 8f70b4
     macro solves this problem, but is not portable.)
Packit 8f70b4
Packit 8f70b4
     A solution is to use the sizeof operator.  It yields a number,
Packit 8f70b4
     getting rid of the identity of the type.  Declarations like
Packit 8f70b4
Packit 8f70b4
       extern int dummy [sizeof (struct {...})];
Packit 8f70b4
       extern void dummy (int [sizeof (struct {...})]);
Packit 8f70b4
       extern int (*dummy (void)) [sizeof (struct {...})];
Packit 8f70b4
Packit 8f70b4
     can be repeated.
Packit 8f70b4
Packit 8f70b4
   * Should the implementation use a named struct or an unnamed struct?
Packit 8f70b4
     Which of the following alternatives can be used?
Packit 8f70b4
Packit 8f70b4
       extern int dummy [sizeof (struct {...})];
Packit 8f70b4
       extern int dummy [sizeof (struct _gl_verify_type {...})];
Packit 8f70b4
       extern void dummy (int [sizeof (struct {...})]);
Packit 8f70b4
       extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
Packit 8f70b4
       extern int (*dummy (void)) [sizeof (struct {...})];
Packit 8f70b4
       extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
Packit 8f70b4
Packit 8f70b4
     In the second and sixth case, the struct type is exported to the
Packit 8f70b4
     outer scope; two such declarations therefore collide.  GCC warns
Packit 8f70b4
     about the first, third, and fourth cases.  So the only remaining
Packit 8f70b4
     possibility is the fifth case:
Packit 8f70b4
Packit 8f70b4
       extern int (*dummy (void)) [sizeof (struct {...})];
Packit 8f70b4
Packit 8f70b4
   * GCC warns about duplicate declarations of the dummy function if
Packit 8f70b4
     -Wredundant-decls is used.  GCC 4.3 and later have a builtin
Packit 8f70b4
     __COUNTER__ macro that can let us generate unique identifiers for
Packit 8f70b4
     each dummy function, to suppress this warning.
Packit 8f70b4
Packit 8f70b4
   * This implementation exploits the fact that older versions of GCC,
Packit 8f70b4
     which do not support _Static_assert, also do not warn about the
Packit 8f70b4
     last declaration mentioned above.
Packit 8f70b4
Packit 8f70b4
   * GCC warns if -Wnested-externs is enabled and verify() is used
Packit 8f70b4
     within a function body; but inside a function, you can always
Packit 8f70b4
     arrange to use verify_expr() instead.
Packit 8f70b4
Packit 8f70b4
   * In C++, any struct definition inside sizeof is invalid.
Packit 8f70b4
     Use a template type to work around the problem.  */
Packit 8f70b4
Packit 8f70b4
/* Concatenate two preprocessor tokens.  */
Packit 8f70b4
#define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
Packit 8f70b4
#define _GL_CONCAT0(x, y) x##y
Packit 8f70b4
Packit 8f70b4
/* _GL_COUNTER is an integer, preferably one that changes each time we
Packit 8f70b4
   use it.  Use __COUNTER__ if it works, falling back on __LINE__
Packit 8f70b4
   otherwise.  __LINE__ isn't perfect, but it's better than a
Packit 8f70b4
   constant.  */
Packit 8f70b4
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
Packit 8f70b4
# define _GL_COUNTER __COUNTER__
Packit 8f70b4
#else
Packit 8f70b4
# define _GL_COUNTER __LINE__
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Generate a symbol with the given prefix, making it unique if
Packit 8f70b4
   possible.  */
Packit 8f70b4
#define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
Packit 8f70b4
Packit 8f70b4
/* Verify requirement R at compile-time, as an integer constant expression
Packit 8f70b4
   that returns 1.  If R is false, fail at compile-time, preferably
Packit 8f70b4
   with a diagnostic that includes the string-literal DIAGNOSTIC.  */
Packit 8f70b4
Packit 8f70b4
#define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
Packit 8f70b4
   (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
Packit 8f70b4
Packit 8f70b4
#ifdef __cplusplus
Packit 8f70b4
# if !GNULIB_defined_struct__gl_verify_type
Packit 8f70b4
template <int w>
Packit 8f70b4
  struct _gl_verify_type {
Packit 8f70b4
    unsigned int _gl_verify_error_if_negative: w;
Packit 8f70b4
  };
Packit 8f70b4
#  define GNULIB_defined_struct__gl_verify_type 1
Packit 8f70b4
# endif
Packit 8f70b4
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
Packit 8f70b4
    _gl_verify_type<(R) ? 1 : -1>
Packit 8f70b4
#elif defined _GL_HAVE__STATIC_ASSERT
Packit 8f70b4
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
Packit 8f70b4
    struct {                                   \
Packit 8f70b4
      _Static_assert (R, DIAGNOSTIC);          \
Packit 8f70b4
      int _gl_dummy;                          \
Packit 8f70b4
    }
Packit 8f70b4
#else
Packit 8f70b4
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
Packit 8f70b4
    struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Verify requirement R at compile-time, as a declaration without a
Packit 8f70b4
   trailing ';'.  If R is false, fail at compile-time, preferably
Packit 8f70b4
   with a diagnostic that includes the string-literal DIAGNOSTIC.
Packit 8f70b4
Packit 8f70b4
   Unfortunately, unlike C11, this implementation must appear as an
Packit 8f70b4
   ordinary declaration, and cannot appear inside struct { ... }.  */
Packit 8f70b4
Packit 8f70b4
#ifdef _GL_HAVE__STATIC_ASSERT
Packit 8f70b4
# define _GL_VERIFY _Static_assert
Packit 8f70b4
#else
Packit 8f70b4
# define _GL_VERIFY(R, DIAGNOSTIC)				       \
Packit 8f70b4
    extern int (*_GL_GENSYM (_gl_verify_function) (void))	       \
Packit 8f70b4
      [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h.  */
Packit 8f70b4
#ifdef _GL_STATIC_ASSERT_H
Packit 8f70b4
# if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
Packit 8f70b4
#  define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
Packit 8f70b4
# endif
Packit 8f70b4
# if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
Packit 8f70b4
#  define static_assert _Static_assert /* C11 requires this #define.  */
Packit 8f70b4
# endif
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* @assert.h omit start@  */
Packit 8f70b4
Packit 8f70b4
/* Each of these macros verifies that its argument R is nonzero.  To
Packit 8f70b4
   be portable, R should be an integer constant expression.  Unlike
Packit 8f70b4
   assert (R), there is no run-time overhead.
Packit 8f70b4
Packit 8f70b4
   There are two macros, since no single macro can be used in all
Packit 8f70b4
   contexts in C.  verify_true (R) is for scalar contexts, including
Packit 8f70b4
   integer constant expression contexts.  verify (R) is for declaration
Packit 8f70b4
   contexts, e.g., the top level.  */
Packit 8f70b4
Packit 8f70b4
/* Verify requirement R at compile-time, as an integer constant expression.
Packit 8f70b4
   Return 1.  This is equivalent to verify_expr (R, 1).
Packit 8f70b4
Packit 8f70b4
   verify_true is obsolescent; please use verify_expr instead.  */
Packit 8f70b4
Packit 8f70b4
#define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
Packit 8f70b4
Packit 8f70b4
/* Verify requirement R at compile-time.  Return the value of the
Packit 8f70b4
   expression E.  */
Packit 8f70b4
Packit 8f70b4
#define verify_expr(R, E) \
Packit 8f70b4
   (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
Packit 8f70b4
Packit 8f70b4
/* Verify requirement R at compile-time, as a declaration without a
Packit 8f70b4
   trailing ';'.  */
Packit 8f70b4
Packit 8f70b4
#ifdef __GNUC__
Packit 8f70b4
# define verify(R) _GL_VERIFY (R, "verify (" #R ")")
Packit 8f70b4
#else
Packit 8f70b4
/* PGI barfs if R is long.  Play it safe.  */
Packit 8f70b4
# define verify(R) _GL_VERIFY (R, "verify (...)")
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#ifndef __has_builtin
Packit 8f70b4
# define __has_builtin(x) 0
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Assume that R always holds.  This lets the compiler optimize
Packit 8f70b4
   accordingly.  R should not have side-effects; it may or may not be
Packit 8f70b4
   evaluated.  Behavior is undefined if R is false.  */
Packit 8f70b4
Packit 8f70b4
#if (__has_builtin (__builtin_unreachable) \
Packit 8f70b4
     || 4 < __GNUC__ + (5 <= __GNUC_MINOR__))
Packit 8f70b4
# define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
Packit 8f70b4
#elif 1200 <= _MSC_VER
Packit 8f70b4
# define assume(R) __assume (R)
Packit 8f70b4
#elif ((defined GCC_LINT || defined lint) \
Packit 8f70b4
       && (__has_builtin (__builtin_trap) \
Packit 8f70b4
           || 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))))
Packit 8f70b4
  /* Doing it this way helps various packages when configured with
Packit 8f70b4
     --enable-gcc-warnings, which compiles with -Dlint.  It's nicer
Packit 8f70b4
     when 'assume' silences warnings even with older GCCs.  */
Packit 8f70b4
# define assume(R) ((R) ? (void) 0 : __builtin_trap ())
Packit 8f70b4
#else
Packit 8f70b4
# define assume(R) ((void) (0 && (R)))
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* @assert.h omit end@  */
Packit 8f70b4
Packit 8f70b4
#endif