From: Joseph Myers Date: Thu, 29 Oct 2020 15:05:33 +0000 (+0000) Subject: stdbool.h: Update true and false expansions for C2x X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=40749db75caff2fed7b7053e0d1868feaa21bcb4;p=gcc.git stdbool.h: Update true and false expansions for C2x C2x has changed the expansions of the true and false macros in 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 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 * ginclude/stdbool.h [__STDC_VERSION__ > 201710L] (true, false): Define with type _Bool. gcc/testsuite/ 2020-10-29 Joseph Myers * gcc.dg/c11-bool-1.c, gcc.dg/c2x-bool-1.c, gcc.dg/c99-bool-4.c: New tests. --- diff --git a/gcc/ginclude/stdbool.h b/gcc/ginclude/stdbool.h index 1b56498d96f..23554223d67 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 00000000000..0412624a706 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-bool-1.c @@ -0,0 +1,50 @@ +/* Test macro expansions in in C11. */ +/* { dg-do run } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +#include + +#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 00000000000..b64da1f7b43 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-bool-1.c @@ -0,0 +1,50 @@ +/* Test macro expansions in in C2x. */ +/* { dg-do run } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +#include + +#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 00000000000..5cae18ad0ce --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-bool-4.c @@ -0,0 +1,46 @@ +/* Test macro expansions in in C99. */ +/* { dg-do run } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +#include + +#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); +}