assert_h.m4 2.5 KB

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