]>
Commit | Line | Data |
---|---|---|
8690e634 JK |
1 | # Check for stdbool.h that conforms to C99. |
2 | ||
dc6c21da | 3 | dnl Copyright (C) 2002-2006, 2009-2022 Free Software Foundation, Inc. |
8690e634 JK |
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 | ||
dc6c21da | 8 | #serial 9 |
8690e634 JK |
9 | |
10 | # Prepare for substituting <stdbool.h> if it is not supported. | |
11 | ||
dc6c21da | 12 | AC_DEFUN([gl_STDBOOL_H], |
8690e634 JK |
13 | [ |
14 | AC_REQUIRE([AC_CHECK_HEADER_STDBOOL]) | |
c0c3707f | 15 | AC_REQUIRE([AC_CANONICAL_HOST]) |
8690e634 | 16 | |
c0c3707f CB |
17 | dnl On some platforms, <stdbool.h> does not exist or does not conform to C99. |
18 | dnl On Solaris 10 with CC=cc CXX=CC, <stdbool.h> exists but is not usable | |
19 | dnl in C++ mode (and no <cstdbool> exists). In this case, we use our | |
20 | dnl replacement, also in C mode (for binary compatibility between C and C++). | |
8690e634 | 21 | if test "$ac_cv_header_stdbool_h" = yes; then |
c0c3707f CB |
22 | case "$host_os" in |
23 | solaris*) | |
24 | if test -z "$GCC"; then | |
dc6c21da | 25 | GL_GENERATE_STDBOOL_H=true |
c0c3707f | 26 | else |
dc6c21da | 27 | GL_GENERATE_STDBOOL_H=false |
c0c3707f CB |
28 | fi |
29 | ;; | |
30 | *) | |
dc6c21da | 31 | GL_GENERATE_STDBOOL_H=false |
c0c3707f CB |
32 | ;; |
33 | esac | |
8690e634 | 34 | else |
dc6c21da | 35 | GL_GENERATE_STDBOOL_H=true |
8690e634 | 36 | fi |
8690e634 JK |
37 | |
38 | if test "$ac_cv_type__Bool" = yes; then | |
39 | HAVE__BOOL=1 | |
40 | else | |
41 | HAVE__BOOL=0 | |
42 | fi | |
43 | AC_SUBST([HAVE__BOOL]) | |
44 | ]) | |
45 | ||
8690e634 JK |
46 | # This version of the macro is needed in autoconf <= 2.68. |
47 | ||
48 | AC_DEFUN([AC_CHECK_HEADER_STDBOOL], | |
49 | [AC_CACHE_CHECK([for stdbool.h that conforms to C99], | |
50 | [ac_cv_header_stdbool_h], | |
51 | [AC_COMPILE_IFELSE( | |
52 | [AC_LANG_PROGRAM( | |
53 | [[ | |
54 | #include <stdbool.h> | |
49e4877c PA |
55 | |
56 | #ifdef __cplusplus | |
57 | typedef bool Bool; | |
58 | #else | |
59 | typedef _Bool Bool; | |
60 | #ifndef bool | |
61 | "error: bool is not defined" | |
62 | #endif | |
63 | #ifndef false | |
64 | "error: false is not defined" | |
65 | #endif | |
66 | #if false | |
67 | "error: false is not 0" | |
68 | #endif | |
69 | #ifndef true | |
70 | "error: true is not defined" | |
71 | #endif | |
72 | #if true != 1 | |
73 | "error: true is not 1" | |
74 | #endif | |
8690e634 | 75 | #endif |
49e4877c | 76 | |
8690e634 JK |
77 | #ifndef __bool_true_false_are_defined |
78 | "error: __bool_true_false_are_defined is not defined" | |
79 | #endif | |
80 | ||
49e4877c | 81 | struct s { Bool s: 1; Bool t; bool u: 1; bool v; } s; |
8690e634 JK |
82 | |
83 | char a[true == 1 ? 1 : -1]; | |
84 | char b[false == 0 ? 1 : -1]; | |
85 | char c[__bool_true_false_are_defined == 1 ? 1 : -1]; | |
86 | char d[(bool) 0.5 == true ? 1 : -1]; | |
87 | /* See body of main program for 'e'. */ | |
49e4877c | 88 | char f[(Bool) 0.0 == false ? 1 : -1]; |
8690e634 | 89 | char g[true]; |
49e4877c | 90 | char h[sizeof (Bool)]; |
8690e634 JK |
91 | char i[sizeof s.t]; |
92 | enum { j = false, k = true, l = false * true, m = true * 256 }; | |
93 | /* The following fails for | |
94 | HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]. */ | |
49e4877c | 95 | Bool n[m]; |
8690e634 | 96 | char o[sizeof n == m * sizeof n[0] ? 1 : -1]; |
49e4877c | 97 | char p[-1 - (Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1]; |
8690e634 | 98 | /* Catch a bug in an HP-UX C compiler. See |
c0c3707f CB |
99 | https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html |
100 | https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html | |
8690e634 | 101 | */ |
49e4877c PA |
102 | Bool q = true; |
103 | Bool *pq = &q; | |
104 | bool *qq = &q; | |
8690e634 JK |
105 | ]], |
106 | [[ | |
107 | bool e = &s; | |
49e4877c PA |
108 | *pq |= q; *pq |= ! q; |
109 | *qq |= q; *qq |= ! q; | |
8690e634 JK |
110 | /* Refer to every declared value, to avoid compiler optimizations. */ |
111 | return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l | |
49e4877c | 112 | + !m + !n + !o + !p + !q + !pq + !qq); |
8690e634 JK |
113 | ]])], |
114 | [ac_cv_header_stdbool_h=yes], | |
115 | [ac_cv_header_stdbool_h=no])]) | |
116 | AC_CHECK_TYPES([_Bool]) | |
117 | ]) |