1353940Sdim# Check for stdbool.h that conforms to C99.
2353940Sdim
3353940Sdimdnl Copyright (C) 2002-2006 Free Software Foundation, Inc.
4353940Sdimdnl This file is free software; the Free Software Foundation
5353940Sdimdnl gives unlimited permission to copy and/or distribute it,
6353940Sdimdnl with or without modifications, as long as this notice is preserved.
7353940Sdim
8353940Sdim# Prepare for substituting <stdbool.h> if it is not supported.
9353940Sdim
10353940SdimAC_DEFUN([AM_STDBOOL_H],
11353940Sdim[
12353940Sdim  AC_REQUIRE([AC_HEADER_STDBOOL])
13353940Sdim
14353940Sdim  # Define two additional variables used in the Makefile substitution.
15353940Sdim
16353940Sdim  if test "$ac_cv_header_stdbool_h" = yes; then
17353940Sdim    STDBOOL_H=''
18353940Sdim  else
19353940Sdim    STDBOOL_H='stdbool.h'
20353940Sdim  fi
21353940Sdim  AC_SUBST([STDBOOL_H])
22353940Sdim
23353940Sdim  if test "$ac_cv_type__Bool" = yes; then
24353940Sdim    HAVE__BOOL=1
25353940Sdim  else
26353940Sdim    HAVE__BOOL=0
27353940Sdim  fi
28353940Sdim  AC_SUBST([HAVE__BOOL])
29353940Sdim])
30353940Sdim
31353940Sdim# AM_STDBOOL_H will be renamed to gl_STDBOOL_H in the future.
32353940SdimAC_DEFUN([gl_STDBOOL_H], [AM_STDBOOL_H])
33353940Sdim
34353940Sdim# This macro is only needed in autoconf <= 2.59.  Newer versions of autoconf
35353940Sdim# have this macro built-in.
36353940Sdim
37353940SdimAC_DEFUN([AC_HEADER_STDBOOL],
38353940Sdim  [AC_CACHE_CHECK([for stdbool.h that conforms to C99],
39353940Sdim     [ac_cv_header_stdbool_h],
40353940Sdim     [AC_TRY_COMPILE(
41353940Sdim	[
42353940Sdim	  #include <stdbool.h>
43353940Sdim	  #ifndef bool
44353940Sdim	   "error: bool is not defined"
45353940Sdim	  #endif
46353940Sdim	  #ifndef false
47353940Sdim	   "error: false is not defined"
48353940Sdim	  #endif
49353940Sdim	  #if false
50353940Sdim	   "error: false is not 0"
51353940Sdim	  #endif
52353940Sdim	  #ifndef true
53353940Sdim	   "error: true is not defined"
54353940Sdim	  #endif
55353940Sdim	  #if true != 1
56353940Sdim	   "error: true is not 1"
57353940Sdim	  #endif
58353940Sdim	  #ifndef __bool_true_false_are_defined
59353940Sdim	   "error: __bool_true_false_are_defined is not defined"
60353940Sdim	  #endif
61353940Sdim
62353940Sdim	  struct s { _Bool s: 1; _Bool t; } s;
63353940Sdim
64353940Sdim	  char a[true == 1 ? 1 : -1];
65353940Sdim	  char b[false == 0 ? 1 : -1];
66353940Sdim	  char c[__bool_true_false_are_defined == 1 ? 1 : -1];
67353940Sdim	  char d[(bool) 0.5 == true ? 1 : -1];
68353940Sdim	  bool e = &s;
69353940Sdim	  char f[(_Bool) 0.0 == false ? 1 : -1];
70353940Sdim	  char g[true];
71353940Sdim	  char h[sizeof (_Bool)];
72353940Sdim	  char i[sizeof s.t];
73353940Sdim	  enum { j = false, k = true, l = false * true, m = true * 256 };
74353940Sdim	  _Bool n[m];
75353940Sdim	  char o[sizeof n == m * sizeof n[0] ? 1 : -1];
76353940Sdim	  char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
77353940Sdim	  #if defined __xlc__ || defined __GNUC__
78353940Sdim	   /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
79353940Sdim	      reported by James Lemley on 2005-10-05; see
80353940Sdim	      http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html
81353940Sdim	      This test is not quite right, since xlc is allowed to
82353940Sdim	      reject this program, as the initializer for xlcbug is
83353940Sdim	      not one of the forms that C requires support for.
84353940Sdim	      However, doing the test right would require a run-time
85353940Sdim	      test, and that would make cross-compilation harder.
86353940Sdim	      Let us hope that IBM fixes the xlc bug, and also adds
87353940Sdim	      support for this kind of constant expression.  In the
88353940Sdim	      meantime, this test will reject xlc, which is OK, since
89353940Sdim	      our stdbool.h substitute should suffice.  We also test
90353940Sdim	      this with GCC, where it should work, to detect more
91353940Sdim	      quickly whether someone messes up the test in the
92353940Sdim	      future.  */
93353940Sdim	   char digs[] = "0123456789";
94353940Sdim	   int xlcbug = 1 / (&(digs + 5)[-2 + (bool) 1] == &digs[4] ? 1 : -1);
95353940Sdim	  #endif
96353940Sdim	  /* Catch a bug in an HP-UX C compiler.  See
97353940Sdim	     http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
98353940Sdim	     http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
99353940Sdim	   */
100353940Sdim	  _Bool q = true;
101353940Sdim	  _Bool *pq = &q;
102353940Sdim	],
103353940Sdim	[
104353940Sdim	  *pq |= q;
105353940Sdim	  *pq |= ! q;
106353940Sdim	  /* Refer to every declared value, to avoid compiler optimizations.  */
107353940Sdim	  return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l
108353940Sdim		  + !m + !n + !o + !p + !q + !pq);
109353940Sdim	],
110353940Sdim	[ac_cv_header_stdbool_h=yes],
111353940Sdim	[ac_cv_header_stdbool_h=no])])
112353940Sdim   AC_CHECK_TYPES([_Bool])
113353940Sdim   if test $ac_cv_header_stdbool_h = yes; then
114353940Sdim     AC_DEFINE(HAVE_STDBOOL_H, 1, [Define to 1 if stdbool.h conforms to C99.])
115353940Sdim   fi])
116353940Sdim