Blame lib/verify.h

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