Blame gettext-runtime/gnulib-lib/verify.h

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