Debian Codegraph @

gnulib/m4/assert_h.m4

raw
1 # assert_h.m4
2 # serial 4
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.
7
8 dnl From Paul Eggert.
9
10 AC_DEFUN([gl_ASSERT_H],
11 [
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"])
17 AC_COMPILE_IFELSE(
18 [AC_LANG_PROGRAM(
19 [[#if defined __clang__ && __STDC_VERSION__ < 202311
20 #pragma clang diagnostic error "-Wc2x-extensions"
21 #pragma clang diagnostic error "-Wc++1z-extensions"
22 #endif
23 #ifdef INCLUDE_ASSERT_H
24 #include <assert.h>
25 #endif
26 static_assert (2 + 2 == 4, "arithmetic does not work");
27 static_assert (2 + 2 == 4);
28 ]],
29 [[
30 static_assert (sizeof (char) == 1, "sizeof does not work");
31 static_assert (sizeof (char) == 1);
32 ]])
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
39 ])
40
41 GL_GENERATE_ASSERT_H=false
42 AS_CASE([$gl_cv_static_assert],
43 [yes*keyword*],
44 [AC_DEFINE([HAVE_C_STATIC_ASSERT], [1],
45 [Define to 1 if the static_assert keyword works.])],
46 [no],
47 [GL_GENERATE_ASSERT_H=true
48 gl_NEXT_HEADERS([assert.h])])
49
50 dnl The "zz" puts this toward config.h's end, to avoid potential
51 dnl collisions with other definitions.
52 dnl Hardcode the known configuration results for GCC and clang, so that
53 dnl a configuration made with the C compiler works also with the C++ compiler
54 dnl and vice versa.
55 dnl The seemingly redundant parentheses are necessary for MSVC 14.
56 dnl #undef assert so that programs are not tempted to use it without
57 dnl specifically including assert.h.
58 dnl #undef __ASSERT_H__ so that on IRIX, when programs later include
59 dnl <assert.h>, this include actually defines assert.
60 dnl Break the #undef_s apart with a comment so that 'configure' does
61 dnl not comment them out.
62 AH_VERBATIM([zzstatic_assert],
63 [#if (!(defined __clang__ \
64 ? (defined __cplusplus \
65 ? __cplusplus >= 201703L \
66 : __STDC_VERSION__ >= 202000L && __clang_major__ >= 16) \
67 : (defined __GNUC__ \
68 ? (defined __cplusplus \
69 ? __cplusplus >= 201103L && __GNUG__ >= 6 \
70 : __STDC_VERSION__ >= 202000L && __GNUC__ >= 13) \
71 : defined HAVE_C_STATIC_ASSERT)) \
72 && !defined assert \
73 && (!defined __cplusplus \
74 || (__cpp_static_assert < 201411 \
75 && __GNUG__ < 6 && __clang_major__ < 6)))
76 #include <assert.h>
77 #undef/**/assert
78 #ifdef __sgi
79 #undef/**/__ASSERT_H__
80 #endif
81 /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
82 We need it also to be invocable with a single argument.
83 Haiku 2022 <assert.h> does not define static_assert at all. */
84 #if (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
85 #undef/**/static_assert
86 #define static_assert _Static_assert
87 #endif
88 #endif])
89 ])