c: Add C2X BOOL_MAX and BOOL_WIDTH to limits.h

Message ID alpine.DEB.2.21.2007022142340.21468@digraph.polyomino.org.uk
State New
Headers show
Series
  • c: Add C2X BOOL_MAX and BOOL_WIDTH to limits.h
Related show

Commit Message

Joseph Myers July 2, 2020, 9:43 p.m.
C2X adds BOOL_MAX and BOOL_WIDTH macros to <limits.h>.  As GCC only
supports values 0 and 1 for _Bool (regardless of the number of bits in
the representation, other bits are padding bits and if any of them are
nonzero, the representation is a trap representation), the values of
those macros can just be hardcoded directly in <limits.h> rather than
needing corresponding predefined macros.

Bootstrapped with no regressions on x86_64-pc-linux-gnu.  OK to
commit?

gcc/
2020-07-02  Joseph Myers  <joseph@codesourcery.com>

	* glimits.h [__STDC_VERSION__ > 201710L] (BOOL_MAX, BOOL_WIDTH):
	New macros.

gcc/testsuite/
2020-07-02  Joseph Myers  <joseph@codesourcery.com>

	* gcc.dg/c11-bool-limits-1.c, gcc.dg/c2x-bool-limits-1.c: New
	tests.


-- 
Joseph S. Myers
joseph@codesourcery.com

Comments

Joseph Myers July 9, 2020, 3:40 p.m. | #1
Ping for this limits.h patch 
<https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549363.html>.

-- 
Joseph S. Myers
joseph@codesourcery.com
Peter Bergner via Gcc-patches July 10, 2020, 9:07 p.m. | #2
On Thu, 2020-07-09 at 15:40 +0000, Joseph Myers wrote:
> Ping for this limits.h patch 

> <https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549363.html>;.

OK.

jeff

Patch

diff --git a/gcc/glimits.h b/gcc/glimits.h
index a37f496ef1b..50927510677 100644
--- a/gcc/glimits.h
+++ b/gcc/glimits.h
@@ -150,4 +150,12 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 # define ULLONG_WIDTH __LONG_LONG_WIDTH__
 #endif
 
+#if defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L
+/* C2X width and limit of _Bool.  */
+# undef BOOL_MAX
+# define BOOL_MAX 1
+# undef BOOL_WIDTH
+# define BOOL_WIDTH 1
+#endif
+
 #endif /* _LIMITS_H___ */
diff --git a/gcc/testsuite/gcc.dg/c11-bool-limits-1.c b/gcc/testsuite/gcc.dg/c11-bool-limits-1.c
new file mode 100644
index 00000000000..9ca29be4d72
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-bool-limits-1.c
@@ -0,0 +1,13 @@ 
+/* Test limits for _Bool not in <limits.h> in C11.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11" } */
+
+#include <limits.h>
+
+#ifdef BOOL_MAX
+# error "unexpected BOOL_MAX"
+#endif
+
+#ifdef BOOL_WIDTH
+# error "unexpected BOOL_WIDTH"
+#endif
diff --git a/gcc/testsuite/gcc.dg/c2x-bool-limits-1.c b/gcc/testsuite/gcc.dg/c2x-bool-limits-1.c
new file mode 100644
index 00000000000..d32b4ef59ed
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-bool-limits-1.c
@@ -0,0 +1,19 @@ 
+/* Test limits for _Bool in <limits.h> in C2x.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x" } */
+
+#include <limits.h>
+
+#ifndef BOOL_MAX
+# error "missing BOOL_MAX"
+#endif
+
+#ifndef BOOL_WIDTH
+# error "missing BOOL_WIDTH"
+#endif
+
+/* In principle _Bool can support values wider than 1 bit, stored via
+   type punning, but this is not supported by GCC.  */
+
+_Static_assert (BOOL_MAX == 1, "bad BOOL_MAX");
+_Static_assert (BOOL_WIDTH == 1, "bad BOOL_WIDTH");