Debian Codegraph @

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 dnl This file is offered as-is, without any warranty.
8
9 dnl From Paul Eggert.
10
11 AC_DEFUN([gl_ASSERT_H],
12 [
13 AC_CACHE_CHECK([for static_assert], [gl_cv_static_assert],
14 [gl_saved_CFLAGS=$CFLAGS
15 for gl_working in "yes, a keyword" "yes, an <assert.h> macro"; do
16 AS_CASE([$gl_working],
17 [*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 ],
35 [gl_cv_static_assert=$gl_working],
36 [gl_cv_static_assert=no])
37 CFLAGS=$gl_saved_CFLAGS
38 test "$gl_cv_static_assert" != no && break
39 done
40 ])
41
42 GL_GENERATE_ASSERT_H=false
43 AS_CASE([$gl_cv_static_assert],
44 [yes*keyword*],
45 [AC_DEFINE([HAVE_C_STATIC_ASSERT], [1],
46 [Define to 1 if the static_assert keyword works.])],
47 [no],
48 [GL_GENERATE_ASSERT_H=true
49 gl_NEXT_HEADERS([assert.h])])
50
51 dnl The "zz" puts this toward config.h's end, to avoid potential
52 dnl collisions with other definitions.
53 dnl Hardcode the known configuration results for GCC and clang, so that
54 dnl a configuration made with the C compiler works also with the C++ compiler
55 dnl and vice versa.
56 dnl The seemingly redundant parentheses are necessary for MSVC 14.
57 dnl #undef assert so that programs are not tempted to use it without
58 dnl specifically including assert.h.
59 dnl #undef __ASSERT_H__ so that on IRIX, when programs later include
60 dnl <assert.h>, this include actually defines assert.
61 dnl Break the #undef_s apart with a comment so that 'configure' does
62 dnl not comment them out.
63 AH_VERBATIM([zzstatic_assert],
64 [#if (!(defined __clang__ \
65 ? (defined __cplusplus \
66 ? __cplusplus >= 201703L \
67 : __STDC_VERSION__ >= 202000L && __clang_major__ >= 16) \
68 : (defined __GNUC__ \
69 ? (defined __cplusplus \
70 ? __cplusplus >= 201103L && __GNUG__ >= 6 \
71 : __STDC_VERSION__ >= 202000L && __GNUC__ >= 13) \
72 : defined HAVE_C_STATIC_ASSERT)) \
73 && !defined assert \
74 && (!defined __cplusplus \
75 || (__cpp_static_assert < 201411 \
76 && __GNUG__ < 6 && __clang_major__ < 6)))
77 #include <assert.h>
78 #undef/**/assert
79 #ifdef __sgi
80 #undef/**/__ASSERT_H__
81 #endif
82 /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
83 We need it also to be invocable with a single argument.
84 Haiku 2022 <assert.h> does not define static_assert at all. */
85 #if (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
86 #undef/**/static_assert
87 #define static_assert _Static_assert
88 #endif
89 #endif])
90 ])