stdbool.m4 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Check for stdbool.h that conforms to C99.
  2. dnl Copyright (C) 2002-2006, 2009 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. # Prepare for substituting <stdbool.h> if it is not supported.
  7. AC_DEFUN([AM_STDBOOL_H],
  8. [
  9. AC_REQUIRE([AC_HEADER_STDBOOL])
  10. # Define two additional variables used in the Makefile substitution.
  11. if test "$ac_cv_header_stdbool_h" = yes; then
  12. STDBOOL_H=''
  13. else
  14. STDBOOL_H='stdbool.h'
  15. fi
  16. AC_SUBST([STDBOOL_H])
  17. if test "$ac_cv_type__Bool" = yes; then
  18. HAVE__BOOL=1
  19. else
  20. HAVE__BOOL=0
  21. fi
  22. AC_SUBST([HAVE__BOOL])
  23. ])
  24. # AM_STDBOOL_H will be renamed to gl_STDBOOL_H in the future.
  25. AC_DEFUN([gl_STDBOOL_H], [AM_STDBOOL_H])
  26. # This macro is only needed in autoconf <= 2.59. Newer versions of autoconf
  27. # have this macro built-in.
  28. AC_DEFUN([AC_HEADER_STDBOOL],
  29. [AC_CACHE_CHECK([for stdbool.h that conforms to C99],
  30. [ac_cv_header_stdbool_h],
  31. [AC_TRY_COMPILE(
  32. [
  33. #include <stdbool.h>
  34. #ifndef bool
  35. "error: bool is not defined"
  36. #endif
  37. #ifndef false
  38. "error: false is not defined"
  39. #endif
  40. #if false
  41. "error: false is not 0"
  42. #endif
  43. #ifndef true
  44. "error: true is not defined"
  45. #endif
  46. #if true != 1
  47. "error: true is not 1"
  48. #endif
  49. #ifndef __bool_true_false_are_defined
  50. "error: __bool_true_false_are_defined is not defined"
  51. #endif
  52. struct s { _Bool s: 1; _Bool t; } s;
  53. char a[true == 1 ? 1 : -1];
  54. char b[false == 0 ? 1 : -1];
  55. char c[__bool_true_false_are_defined == 1 ? 1 : -1];
  56. char d[(bool) 0.5 == true ? 1 : -1];
  57. bool e = &s;
  58. char f[(_Bool) 0.0 == false ? 1 : -1];
  59. char g[true];
  60. char h[sizeof (_Bool)];
  61. char i[sizeof s.t];
  62. enum { j = false, k = true, l = false * true, m = true * 256 };
  63. _Bool n[m];
  64. char o[sizeof n == m * sizeof n[0] ? 1 : -1];
  65. char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
  66. #if defined __xlc__ || defined __GNUC__
  67. /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
  68. reported by James Lemley on 2005-10-05; see
  69. http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html
  70. This test is not quite right, since xlc is allowed to
  71. reject this program, as the initializer for xlcbug is
  72. not one of the forms that C requires support for.
  73. However, doing the test right would require a run-time
  74. test, and that would make cross-compilation harder.
  75. Let us hope that IBM fixes the xlc bug, and also adds
  76. support for this kind of constant expression. In the
  77. meantime, this test will reject xlc, which is OK, since
  78. our stdbool.h substitute should suffice. We also test
  79. this with GCC, where it should work, to detect more
  80. quickly whether someone messes up the test in the
  81. future. */
  82. char digs[] = "0123456789";
  83. int xlcbug = 1 / (&(digs + 5)[-2 + (bool) 1] == &digs[4] ? 1 : -1);
  84. #endif
  85. /* Catch a bug in an HP-UX C compiler. See
  86. http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
  87. http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
  88. */
  89. _Bool q = true;
  90. _Bool *pq = &q;
  91. ],
  92. [
  93. *pq |= q;
  94. *pq |= ! q;
  95. /* Refer to every declared value, to avoid compiler optimizations. */
  96. return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l
  97. + !m + !n + !o + !p + !q + !pq);
  98. ],
  99. [ac_cv_header_stdbool_h=yes],
  100. [ac_cv_header_stdbool_h=no])])
  101. AC_CHECK_TYPES([_Bool])
  102. if test $ac_cv_header_stdbool_h = yes; then
  103. AC_DEFINE([HAVE_STDBOOL_H], [1], [Define to 1 if stdbool.h conforms to C99.])
  104. fi])