Blame gl/verify.h

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