diff options
author | Joseph Myers <joseph@codesourcery.com> | 2020-10-29 15:05:33 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2020-10-29 15:06:26 +0000 |
commit | 40749db75caff2fed7b7053e0d1868feaa21bcb4 (patch) | |
tree | d52db1f98393d6f74dc2edf5198e2f8a5e0b02f8 /gcc | |
parent | 8c84486bba104399b7e544cb1ba343646d39ea0a (diff) | |
download | gcc-40749db75caff2fed7b7053e0d1868feaa21bcb4.zip gcc-40749db75caff2fed7b7053e0d1868feaa21bcb4.tar.gz gcc-40749db75caff2fed7b7053e0d1868feaa21bcb4.tar.bz2 |
stdbool.h: Update true and false expansions for C2x
C2x has changed the expansions of the true and false macros in
<stdbool.h> so that they have type _Bool (including in #if conditions,
i.e. an unsigned type in that context). Use the new expansions in
GCC's <stdbool.h> for C2x.
See bug 82272 for related discussion (but this patch does *not*
implement the warning discussed there).
Note that it's possible there may be a further change to make bool,
true and false keywords (there was support in principle for that at
the April WG14 meeting). But currently these expansions of type _Bool
are what C2x requires and there isn't actually a paper before WG14 at
present that would introduce the new keywords.
Bootstrapped with no regressions on x86_64-pc-linux-gnu.
gcc/
2020-10-29 Joseph Myers <joseph@codesourcery.com>
* ginclude/stdbool.h [__STDC_VERSION__ > 201710L] (true, false):
Define with type _Bool.
gcc/testsuite/
2020-10-29 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/c11-bool-1.c, gcc.dg/c2x-bool-1.c, gcc.dg/c99-bool-4.c:
New tests.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ginclude/stdbool.h | 5 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c11-bool-1.c | 50 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c2x-bool-1.c | 50 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c99-bool-4.c | 46 |
4 files changed, 151 insertions, 0 deletions
diff --git a/gcc/ginclude/stdbool.h b/gcc/ginclude/stdbool.h index 1b56498..2355422 100644 --- a/gcc/ginclude/stdbool.h +++ b/gcc/ginclude/stdbool.h @@ -31,8 +31,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #ifndef __cplusplus #define bool _Bool +#if defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L +#define true ((_Bool)+1u) +#define false ((_Bool)+0u) +#else #define true 1 #define false 0 +#endif #else /* __cplusplus */ diff --git a/gcc/testsuite/gcc.dg/c11-bool-1.c b/gcc/testsuite/gcc.dg/c11-bool-1.c new file mode 100644 index 0000000..0412624 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-bool-1.c @@ -0,0 +1,50 @@ +/* Test macro expansions in <stdbool.h> in C11. */ +/* { dg-do run } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +#include <stdbool.h> + +#define str(x) xstr(x) +#define xstr(x) #x + +extern void abort (void); +extern void exit (int); +extern int strcmp (const char *, const char *); + +#if false - 1 >= 0 +#error "false unsigned in #if" +#endif + +#if false != 0 +#error "false not 0 in #if" +#endif + +#if true - 2 >= 0 +#error "true unsigned in #if" +#endif + +#if true != 1 +#error "true not 1 in #if" +#endif + +int +main (void) +{ + if (strcmp (str (bool), "_Bool") != 0) + abort (); + if (_Generic (true, int : 1) != 1) + abort (); + if (true != 1) + abort (); + if (strcmp (str (true), "1") != 0) + abort (); + if (_Generic (false, int : 1) != 1) + abort (); + if (false != 0) + abort (); + if (strcmp (str (false), "0") != 0) + abort (); + if (strcmp (str (__bool_true_false_are_defined), "1") != 0) + abort (); + exit (0); +} diff --git a/gcc/testsuite/gcc.dg/c2x-bool-1.c b/gcc/testsuite/gcc.dg/c2x-bool-1.c new file mode 100644 index 0000000..b64da1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-bool-1.c @@ -0,0 +1,50 @@ +/* Test macro expansions in <stdbool.h> in C2x. */ +/* { dg-do run } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +#include <stdbool.h> + +#define str(x) xstr(x) +#define xstr(x) #x + +extern void abort (void); +extern void exit (int); +extern int strcmp (const char *, const char *); + +#if false - 1 < 0 +#error "false signed in #if" +#endif + +#if false != 0 +#error "false not 0 in #if" +#endif + +#if true - 2 < 0 +#error "true signed in #if" +#endif + +#if true != 1 +#error "true not 1 in #if" +#endif + +int +main (void) +{ + if (strcmp (str (bool), "_Bool") != 0) + abort (); + if (_Generic (true, _Bool : 1) != 1) + abort (); + if (true != 1) + abort (); + if (strcmp (str (true), "((_Bool)+1u)") != 0) + abort (); + if (_Generic (false, _Bool : 1) != 1) + abort (); + if (false != 0) + abort (); + if (strcmp (str (false), "((_Bool)+0u)") != 0) + abort (); + if (strcmp (str (__bool_true_false_are_defined), "1") != 0) + abort (); + exit (0); +} diff --git a/gcc/testsuite/gcc.dg/c99-bool-4.c b/gcc/testsuite/gcc.dg/c99-bool-4.c new file mode 100644 index 0000000..5cae18a --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-bool-4.c @@ -0,0 +1,46 @@ +/* Test macro expansions in <stdbool.h> in C99. */ +/* { dg-do run } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +#include <stdbool.h> + +#define str(x) xstr(x) +#define xstr(x) #x + +extern void abort (void); +extern void exit (int); +extern int strcmp (const char *, const char *); + +#if false - 1 >= 0 +#error "false unsigned in #if" +#endif + +#if false != 0 +#error "false not 0 in #if" +#endif + +#if true - 2 >= 0 +#error "true unsigned in #if" +#endif + +#if true != 1 +#error "true not 1 in #if" +#endif + +int +main (void) +{ + if (strcmp (str (bool), "_Bool") != 0) + abort (); + if (true != 1) + abort (); + if (strcmp (str (true), "1") != 0) + abort (); + if (false != 0) + abort (); + if (strcmp (str (false), "0") != 0) + abort (); + if (strcmp (str (__bool_true_false_are_defined), "1") != 0) + abort (); + exit (0); +} |