test-stdbool.c | test-stdbool.c | |||
---|---|---|---|---|
/* Test of <stdbool.h> substitute. | /* Test of <stdbool.h> substitute. | |||
Copyright (C) 2002-2007, 2009-2010 Free Software Foundation, Inc. | Copyright (C) 2002-2007, 2009-2014 Free Software Foundation, Inc. | |||
This program is free software: you can redistribute it and/or modify | This program is free software: you can redistribute it and/or modify | |||
it under the terms of the GNU General Public License as published by | it under the terms of the GNU General Public License as published by | |||
the Free Software Foundation; either version 3 of the License, or | the Free Software Foundation; either version 3 of the License, or | |||
(at your option) any later version. | (at your option) any later version. | |||
This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
GNU General Public License for more details. | GNU General Public License for more details. | |||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | |||
along with this program. If not, see <http://www.gnu.org/licenses/>. * / | along with this program. If not, see <http://www.gnu.org/licenses/>. * / | |||
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |||
/* We want this test to succeed even when using gcc's -Werror; but to | ||||
do that requires a pragma that didn't exist before 4.3.0. */ | ||||
#ifndef __GNUC__ | ||||
# define ADDRESS_CHECK_OKAY | ||||
#elif __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) | ||||
/* No way to silence -Waddress. */ | ||||
#else | ||||
# pragma GCC diagnostic ignored "-Waddress" | ||||
# define ADDRESS_CHECK_OKAY | ||||
#endif | ||||
#include <config.h> | #include <config.h> | |||
#include <stdbool.h> | #include <stdbool.h> | |||
#ifndef bool | #ifndef bool | |||
"error: bool is not defined" | "error: bool is not defined" | |||
#endif | #endif | |||
#ifndef false | #ifndef false | |||
"error: false is not defined" | "error: false is not defined" | |||
#endif | #endif | |||
skipping to change at line 42 | skipping to change at line 53 | |||
#ifndef true | #ifndef true | |||
"error: true is not defined" | "error: true is not defined" | |||
#endif | #endif | |||
#if true != 1 | #if true != 1 | |||
"error: true is not 1" | "error: true is not 1" | |||
#endif | #endif | |||
#ifndef __bool_true_false_are_defined | #ifndef __bool_true_false_are_defined | |||
"error: __bool_true_false_are_defined is not defined" | "error: __bool_true_false_are_defined is not defined" | |||
#endif | #endif | |||
#if 0 /* Cannot be guaranteed with gnulib's <stdbool.h>. */ | /* Several tests cannot be guaranteed with gnulib's <stdbool.h>, at | |||
least, not for all compilers and compiler options. */ | ||||
#if HAVE_STDBOOL_H || 3 <= __GNUC__ | ||||
struct s { _Bool s: 1; _Bool t; } s; | struct s { _Bool s: 1; _Bool t; } s; | |||
#endif | #endif | |||
char a[true == 1 ? 1 : -1]; | char a[true == 1 ? 1 : -1]; | |||
char b[false == 0 ? 1 : -1]; | char b[false == 0 ? 1 : -1]; | |||
char c[__bool_true_false_are_defined == 1 ? 1 : -1]; | char c[__bool_true_false_are_defined == 1 ? 1 : -1]; | |||
#if 0 /* Cannot be guaranteed with gnulib's <stdbool.h>, at least, | #if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */ | |||
not for all compilers. */ | ||||
char d[(bool) 0.5 == true ? 1 : -1]; | char d[(bool) 0.5 == true ? 1 : -1]; | |||
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */ | ||||
/* C99 may plausibly be interpreted as not requiring support for a cast fro | ||||
m | ||||
a variable's address to bool in a static initializer. So treat it like | ||||
a | ||||
GCC extension. */ | ||||
# ifdef __GNUC__ | ||||
bool e = &s; | bool e = &s; | |||
# endif | ||||
# endif | ||||
char f[(_Bool) 0.0 == false ? 1 : -1]; | char f[(_Bool) 0.0 == false ? 1 : -1]; | |||
#endif | #endif | |||
char g[true]; | char g[true]; | |||
char h[sizeof (_Bool)]; | char h[sizeof (_Bool)]; | |||
#if 0 /* See above. */ | #if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */ | |||
char i[sizeof s.t]; | char i[sizeof s.t]; | |||
#endif | #endif | |||
enum { j = false, k = true, l = false * true, m = true * 256 }; | enum { j = false, k = true, l = false * true, m = true * 256 }; | |||
_Bool n[m]; | _Bool n[m]; | |||
char o[sizeof n == m * sizeof n[0] ? 1 : -1]; | char o[sizeof n == m * sizeof n[0] ? 1 : -1]; | |||
char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1]; | char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1]; | |||
#if 0 /* Cannot be guaranteed with gnulib's <stdbool.h>. */ | ||||
#if defined __xlc__ || defined __GNUC__ | ||||
/* Catch a bug in IBM AIX xlc compiler version 6.0.0.0 | ||||
reported by James Lemley on 2005-10-05; see | ||||
http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html | ||||
This test is not quite right, since xlc is allowed to | ||||
reject this program, as the initializer for xlcbug is | ||||
not one of the forms that C requires support for. | ||||
However, doing the test right would require a run-time | ||||
test, and that would make cross-compilation harder. | ||||
Let us hope that IBM fixes the xlc bug, and also adds | ||||
support for this kind of constant expression. In the | ||||
meantime, this test will reject xlc, which is OK, since | ||||
our stdbool.h substitute should suffice. We also test | ||||
this with GCC, where it should work, to detect more | ||||
quickly whether someone messes up the test in the | ||||
future. */ | ||||
char digs[] = "0123456789"; | ||||
int xlcbug = 1 / (&(digs + 5)[-2 + (bool) 1] == &digs[4] ? 1 : -1); | ||||
#endif | ||||
#endif | ||||
/* Catch a bug in an HP-UX C compiler. See | /* Catch a bug in an HP-UX C compiler. See | |||
http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html | http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html | |||
http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html | http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html | |||
*/ | */ | |||
_Bool q = true; | _Bool q = true; | |||
_Bool *pq = &q; | _Bool *pq = &q; | |||
int | int | |||
main () | main () | |||
{ | { | |||
return 0; | int error = 0; | |||
#if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */ | ||||
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */ | ||||
/* A cast from a variable's address to bool is valid in expressions. */ | ||||
{ | ||||
bool e1 = &s; | ||||
if (!e1) | ||||
error = 1; | ||||
} | ||||
# endif | ||||
#endif | ||||
/* Catch a bug in IBM AIX xlc compiler version 6.0.0.0 | ||||
reported by James Lemley on 2005-10-05; see | ||||
http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html | ||||
This is a runtime test, since a corresponding compile-time | ||||
test would rely on initializer extensions. */ | ||||
{ | ||||
char digs[] = "0123456789"; | ||||
if (&(digs + 5)[-2 + (bool) 1] != &digs[4]) | ||||
error = 1; | ||||
} | ||||
return error; | ||||
} | } | |||
End of changes. 9 change blocks. | ||||
27 lines changed or deleted | 51 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |