Blame lib/verify.h

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