bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* limits-h: Fix BOOL_MAX value
@ 2024-02-12 13:09 Bruno Haible
  2024-02-12 14:48 ` Collin Funk
  0 siblings, 1 reply; 3+ messages in thread
From: Bruno Haible @ 2024-02-12 13:09 UTC (permalink / raw)
  To: bug-gnulib

With clang 17 and CC="clang -std=gnu2x", I see this compilation error:

../../gltests/test-limits-h.c:118:16: error: static assertion failed due to requirement '1 == (((1U << (8 - 1)) - 1) * 2) + 1'
  118 | static_assert (BOOL_MAX == (((1U << (BOOL_WIDTH - 1)) - 1) * 2) + 1);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/limits.h:186:20: note: expanded from macro 'BOOL_MAX'
  186 | #  define BOOL_MAX 1
      |                    ^
1 error generated.
make[3]: *** [Makefile:3853: test-limits-h.o] Error 1

The cause are the values of BOOL_WIDTH and BOOL_MAX. This program
================================================================================
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
int main () { printf ("BOOL_WIDTH = %d, BOOL_MAX = %d\n", BOOL_WIDTH, BOOL_MAX); }
================================================================================
prints
  BOOL_WIDTH = 1, BOOL_MAX = 1
with CC="gcc -std=gnu2x", but
  BOOL_WIDTH = 8, BOOL_MAX = 1
with CC="clang -std=gnu2x" (for all clang version 14 ... 17).

Apparently I had misinterpreted the meaning of BOOL_WIDTH. (Cf.
ISO C 23 § E.(3)).

This patch fixes the compilation error.


2024-02-12  Bruno Haible  <bruno@clisp.org>

	limits-h: Fix BOOL_MAX value.
	* lib/limits.in.h (BOOL_MAX): Define to 1, not to 2^BOOL_WIDTH-1.
	* tests/test-limits-h.c (main): Change expected value of BOOL_MAX.

diff --git a/lib/limits.in.h b/lib/limits.in.h
index 236fc58e52..c65eb4c1cf 100644
--- a/lib/limits.in.h
+++ b/lib/limits.in.h
@@ -130,7 +130,7 @@
 #  define BOOL_WIDTH 1
 #  define BOOL_MAX 1
 # elif ! defined BOOL_MAX
-#  define BOOL_MAX ((((1U << (BOOL_WIDTH - 1)) - 1) << 1) + 1)
+#  define BOOL_MAX 1
 # endif
 #endif
 
diff --git a/tests/test-limits-h.c b/tests/test-limits-h.c
index 817e239c6e..6f574c88a0 100644
--- a/tests/test-limits-h.c
+++ b/tests/test-limits-h.c
@@ -115,7 +115,7 @@ verify_width (ULLONG_WIDTH, 0, ULLONG_MAX);
 /* Macros specified by C23.  */
 
 int bool_attrs[] = { BOOL_MAX, BOOL_WIDTH };
-static_assert (BOOL_MAX == (((1U << (BOOL_WIDTH - 1)) - 1) * 2) + 1);
+static_assert (BOOL_MAX == 1);
 
 static_assert (0 < MB_LEN_MAX);
 





^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: limits-h: Fix BOOL_MAX value
  2024-02-12 13:09 limits-h: Fix BOOL_MAX value Bruno Haible
@ 2024-02-12 14:48 ` Collin Funk
  2024-02-12 16:25   ` Bruno Haible
  0 siblings, 1 reply; 3+ messages in thread
From: Collin Funk @ 2024-02-12 14:48 UTC (permalink / raw)
  To: bug-gnulib


On 2/12/24 5:09 AM, Bruno Haible wrote:
>   BOOL_WIDTH = 1, BOOL_MAX = 1
> with CC="gcc -std=gnu2x", but
>   BOOL_WIDTH = 8, BOOL_MAX = 1
> with CC="clang -std=gnu2x" (for all clang version 14 ... 17).

Same results with GCC 14 and Clang 19.

> Apparently I had misinterpreted the meaning of BOOL_WIDTH. (Cf.
> ISO C 23 § E.(3)).

Today I learned. I was under the impression that BOOL_WIDTH would be
the same as the underlying type (8-bit char for example). Am I
misreading or would this be a Clang bug? Both ISO C 23 § 5.2.4.2.1 and
Annex E seem to say BOOL_WIDTH must be exactly 1. That would also make
the previous definition correct. :)



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: limits-h: Fix BOOL_MAX value
  2024-02-12 14:48 ` Collin Funk
@ 2024-02-12 16:25   ` Bruno Haible
  0 siblings, 0 replies; 3+ messages in thread
From: Bruno Haible @ 2024-02-12 16:25 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Collin Funk

Collin Funk wrote:
> Both ISO C 23 § 5.2.4.2.1 and
> Annex E seem to say BOOL_WIDTH must be exactly 1.

I have no idea what the footnote "This value is exact." in § 5.2.4.2.1 means.
And Annex E is informational, not normative.

Therefore, I think, the grounds for claiming a clang bug are weak.

Bruno





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-12 16:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-12 13:09 limits-h: Fix BOOL_MAX value Bruno Haible
2024-02-12 14:48 ` Collin Funk
2024-02-12 16:25   ` Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).