| 1 |
/* Test bool. |
| 2 |
Copyright (C) 2002-2007, 2009-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2007. */ |
| 18 |
|
| 19 |
/* Define ADDRESS_CHECK_OKAY if it is OK to assign an address to a 'bool' |
| 20 |
and this does not generate a warning (because we want this test to succeed |
| 21 |
even when using gcc's -Werror). */ |
| 22 |
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) \ |
| 23 |
|| (__clang_major__ >= 4) |
| 24 |
/* We can silence the warning. */ |
| 25 |
# pragma GCC diagnostic ignored "-Waddress" |
| 26 |
# define ADDRESS_CHECK_OKAY |
| 27 |
#elif defined __GNUC__ || defined __clang__ |
| 28 |
/* There may be a warning. */ |
| 29 |
#else |
| 30 |
/* Ignore warnings from other compilers. */ |
| 31 |
# define ADDRESS_CHECK_OKAY |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#ifdef TEST_STDBOOL_H |
| 37 |
# include <stdbool.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#if false |
| 41 |
"error: false is not 0" |
| 42 |
#endif |
| 43 |
#if true != 1 |
| 44 |
"error: true is not 1" |
| 45 |
#endif |
| 46 |
|
| 47 |
/* Several tests cannot be guaranteed with gnulib's <stdbool.h>, at |
| 48 |
least, not for all compilers and compiler options. */ |
| 49 |
#if ((HAVE_C_BOOL || defined __cplusplus \ |
| 50 |
|| HAVE_STDBOOL_H || 3 <= __GNUC__ || 4 <= __clang_major__) \ |
| 51 |
&& !(defined _MSC_VER || defined __SUNPRO_C)) |
| 52 |
# define WORKING_BOOL 1 |
| 53 |
#else |
| 54 |
# define WORKING_BOOL 0 |
| 55 |
#endif |
| 56 |
|
| 57 |
#if WORKING_BOOL |
| 58 |
struct s { bool s: 1; bool t; } s; |
| 59 |
#endif |
| 60 |
|
| 61 |
char a[true == 1 ? 1 : -1]; |
| 62 |
char b[false == 0 ? 1 : -1]; |
| 63 |
#if WORKING_BOOL |
| 64 |
char d[(bool) 0.5 == true ? 1 : -1]; |
| 65 |
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */ |
| 66 |
/* C99 may plausibly be interpreted as not requiring support for a cast from |
| 67 |
a variable's address to bool in a static initializer. So treat it like a |
| 68 |
GCC extension. */ |
| 69 |
# if defined __GNUC__ || defined __clang__ |
| 70 |
bool e = &s; |
| 71 |
# endif |
| 72 |
# endif |
| 73 |
char f[(bool) 0.0 == false ? 1 : -1]; |
| 74 |
#endif |
| 75 |
char g[true]; |
| 76 |
char h[sizeof (bool)]; |
| 77 |
#if WORKING_BOOL |
| 78 |
char i[sizeof s.t]; |
| 79 |
#endif |
| 80 |
enum { j = false, k = true, l = false * true, m = true * 256 }; |
| 81 |
bool n[m]; |
| 82 |
char o[sizeof n == m * sizeof n[0] ? 1 : -1]; |
| 83 |
char p[-1 - (bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1]; |
| 84 |
/* Catch a bug in an HP-UX C compiler. See |
| 85 |
https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html |
| 86 |
https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html |
| 87 |
*/ |
| 88 |
bool q = true; |
| 89 |
bool *pq = &q; |
| 90 |
|
| 91 |
int |
| 92 |
main () |
| 93 |
{ |
| 94 |
int error = 0; |
| 95 |
|
| 96 |
#if WORKING_BOOL |
| 97 |
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */ |
| 98 |
/* A cast from a variable's address to bool is valid in expressions. */ |
| 99 |
{ |
| 100 |
bool e1 = &s; |
| 101 |
if (!e1) |
| 102 |
error = 1; |
| 103 |
} |
| 104 |
# endif |
| 105 |
#endif |
| 106 |
|
| 107 |
/* Catch a bug in IBM AIX xlc compiler version 6.0.0.0 |
| 108 |
reported by James Lemley on 2005-10-05; see |
| 109 |
https://lists.gnu.org/r/bug-coreutils/2005-10/msg00086.html |
| 110 |
This is a runtime test, since a corresponding compile-time |
| 111 |
test would rely on initializer extensions. */ |
| 112 |
{ |
| 113 |
char digs[] = "0123456789"; |
| 114 |
if (&(digs + 5)[-2 + (bool) 1] != &digs[4]) |
| 115 |
error = 1; |
| 116 |
} |
| 117 |
|
| 118 |
return error; |
| 119 |
} |