git-merge-changelog: Improve last commit.
[gnulib.git] / m4 / assert_h.m4
blobb90d0f193904da1f25b0cf2ea36ad060e5ccf1b0
1 # assert_h.m4
2 # serial 1
3 dnl Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl From Paul Eggert.
10 AC_DEFUN([gl_ASSERT_H],
12   AC_CACHE_CHECK([for static_assert], [gl_cv_static_assert],
13     [gl_saved_CFLAGS=$CFLAGS
14      for gl_working in "yes, a keyword" "yes, an <assert.h> macro"; do
15       AS_CASE([$gl_working],
16         [*assert.h*], [CFLAGS="$gl_saved_CFLAGS -DINCLUDE_ASSERT_H"])
18       AC_COMPILE_IFELSE(
19        [AC_LANG_PROGRAM(
20           [[#if defined __clang__ && __STDC_VERSION__ < 202311
21              #pragma clang diagnostic error "-Wc2x-extensions"
22              #pragma clang diagnostic error "-Wc++1z-extensions"
23             #endif
24             #ifdef INCLUDE_ASSERT_H
25              #include <assert.h>
26             #endif
27             static_assert (2 + 2 == 4, "arithmetic does not work");
28             static_assert (2 + 2 == 4);
29           ]],
30           [[
31             static_assert (sizeof (char) == 1, "sizeof does not work");
32             static_assert (sizeof (char) == 1);
33           ]])],
34        [gl_cv_static_assert=$gl_working],
35        [gl_cv_static_assert=no])
36       CFLAGS=$gl_saved_CFLAGS
37       test "$gl_cv_static_assert" != no && break
38      done])
40   GL_GENERATE_ASSERT_H=false
41   AS_CASE([$gl_cv_static_assert],
42     [yes*keyword*],
43       [AC_DEFINE([HAVE_C_STATIC_ASSERT], [1],
44          [Define to 1 if the static_assert keyword works.])],
45     [no],
46       [GL_GENERATE_ASSERT_H=true
47        gl_NEXT_HEADERS([assert.h])])
49   dnl The "zz" puts this toward config.h's end, to avoid potential
50   dnl collisions with other definitions.
51   dnl #undef assert so that programs are not tempted to use it without
52   dnl specifically including assert.h.
53   dnl #undef __ASSERT_H__ so that on IRIX, when programs later include
54   dnl <assert.h>, this include actually defines assert.
55   dnl Break the #undef_s apart with a comment so that 'configure' does
56   dnl not comment them out.
57   AH_VERBATIM([zzstatic_assert],
58 [#if (!defined HAVE_C_STATIC_ASSERT && !defined assert \
59      && (!defined __cplusplus \
60          || (__cpp_static_assert < 201411 \
61              && __GNUG__ < 6 && __clang_major__ < 6)))
62  #include <assert.h>
63  #undef/**/assert
64  #ifdef __sgi
65   #undef/**/__ASSERT_H__
66  #endif
67  /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
68     We need it also to be invocable with a single argument.  */
69  #if defined __sun && (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
70   #undef/**/static_assert
71   #define static_assert _Static_assert
72  #endif
73 #endif])