Blame gl/verify.h

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