Blame lib/verify.h

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