xref: /netbsd-src/external/lgpl3/mpfr/dist/src/mpfr-sassert.h (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* MPFR internal header related to Static Assertions
2 
3 Copyright 2012-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #ifndef __MPFR_STATIC_ASSERT_H__
24 #define __MPFR_STATIC_ASSERT_H__
25 
26 #include "mpfr-cvers.h"  /* for __MPFR_GNUC */
27 
28 /* How to use:
29    ===========
30    MPFR_STAT_STATIC_ASSERT:
31    + to check a condition at compile time within the statement section.
32 
33    Note: In case one would need a static assertion in an expression,
34    a sizeof(...) instead of a typedef could be used. For instance,
35    see https://stackoverflow.com/questions/9229601/what-is-in-c-code
36    (about BUILD_BUG_ON_ZERO in the Linux kernel).
37 */
38 
39 #ifdef MPFR_USE_STATIC_ASSERT
40 
41 /* C11 version */
42 # if defined (__STDC_VERSION__)
43 #  if (__STDC_VERSION__ >= 201112L)
44 #   define MPFR_STAT_STATIC_ASSERT(c) _Static_assert((c), #c )
45 #  endif
46 # endif
47 
48 /* Default version which should be compatible with nearly all compilers */
49 # if !defined(MPFR_STAT_STATIC_ASSERT)
50 #  if __MPFR_GNUC(4,8)
51 /* Get rid of annoying warnings "typedef '...' locally defined but not used"
52    (new in GCC 4.8). Thanks to Jonathan Wakely for this solution:
53    https://gcc.gnu.org/legacy-ml/gcc-help/2013-07/msg00142.html */
54 #   define MPFR_TYPEDEF_UNUSED __attribute__ ((unused))
55 #  else
56 #   define MPFR_TYPEDEF_UNUSED
57 #  endif
58 #  define MPFR_ASSERT_CAT(a,b) MPFR_ASSERT_CAT_(a,b)
59 #  define MPFR_ASSERT_CAT_(a,b) a ## b
60 #  define MPFR_STAT_STATIC_ASSERT(c) do {                                    \
61  typedef enum { MPFR_ASSERT_CAT(MPFR_STATIC_ASSERT_CONST_,__LINE__) = !(c) } \
62  MPFR_ASSERT_CAT(MPFR_ASSERT_,__LINE__)[!!(c) ? 1 : -1]                      \
63    MPFR_TYPEDEF_UNUSED; } while (0)
64 # endif
65 
66 #else
67 
68 /* No support: default to classic assertions */
69 # define MPFR_STAT_STATIC_ASSERT(c) MPFR_ASSERTN(c)
70 
71 #endif
72 
73 #endif
74