mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] endian.h: use parentheses to silence warnings
@ 2025-06-24  9:02 Jacob Abrams
  2025-06-24 15:54 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Jacob Abrams @ 2025-06-24  9:02 UTC (permalink / raw)
  To: musl

It would be nice to silence some warnings that arise from endian.h, for example:

musl/include/endian.h:29:25: warning: '&' within '|' [-Wbitwise-op-parentheses]
   29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
      |                        ~ ~~~~~~^~~~~~~
musl/include/endian.h:29:25: note: place parentheses around the '&'
expression to silence this warning
   29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
      |                                ^
      |                          (            )
musl/include/endian.h:29:41: warning: '&' within '|' [-Wbitwise-op-parentheses]
   29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
      |                                        ~ ~~~~~~^~~~~~~~~
musl/include/endian.h:29:41: note: place parentheses around the '&'
expression to silence this warning
   29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
      |                                                ^
      |                                          (              )
musl/include/endian.h:34:23: warning: operator '<<' has lower
precedence than '+'; '+' will be evaluated first
[-Wshift-op-parentheses]
   34 |         return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
      |                ~~~~~~~~~~~~~~^~~~~~~
musl/include/endian.h:34:23: note: place parentheses around the '+'
expression to silence this warning
   34 |         return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
      |                              ^
      |                (                  )

Note: I'm not subscribed to the mailing-list, so CCs are appreciated
---
 include/endian.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/endian.h b/include/endian.h
index 172c4320..2819c926 100644
--- a/include/endian.h
+++ b/include/endian.h
@@ -18,17 +18,17 @@

 static __inline uint16_t __bswap16(uint16_t __x)
 {
-    return __x<<8 | __x>>8;
+    return (__x<<8) | (__x>>8);
 }

 static __inline uint32_t __bswap32(uint32_t __x)
 {
-    return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
+    return (__x>>24) | ((__x>>8)&0xff00) | ((__x<<8)&0xff0000) | (__x<<24);
 }

 static __inline uint64_t __bswap64(uint64_t __x)
 {
-    return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
+    return ((__bswap32(__x)+0ULL)<<32) | __bswap32(__x>>32);
 }

 #if __BYTE_ORDER == __LITTLE_ENDIAN
--
2.50.0


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

* Re: [PATCH] endian.h: use parentheses to silence warnings
  2025-06-24  9:02 [PATCH] endian.h: use parentheses to silence warnings Jacob Abrams
@ 2025-06-24 15:54 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2025-06-24 15:54 UTC (permalink / raw)
  To: Jacob Abrams; +Cc: musl

On Tue, Jun 24, 2025 at 05:02:05PM +0800, Jacob Abrams wrote:
> It would be nice to silence some warnings that arise from endian.h, for example:
> 
> musl/include/endian.h:29:25: warning: '&' within '|' [-Wbitwise-op-parentheses]
>    29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
>       |                        ~ ~~~~~~^~~~~~~
> musl/include/endian.h:29:25: note: place parentheses around the '&'
> expression to silence this warning
>    29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
>       |                                ^
>       |                          (            )
> musl/include/endian.h:29:41: warning: '&' within '|' [-Wbitwise-op-parentheses]
>    29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
>       |                                        ~ ~~~~~~^~~~~~~~~
> musl/include/endian.h:29:41: note: place parentheses around the '&'
> expression to silence this warning
>    29 |         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
>       |                                                ^
>       |                                          (              )
> musl/include/endian.h:34:23: warning: operator '<<' has lower
> precedence than '+'; '+' will be evaluated first
> [-Wshift-op-parentheses]
>    34 |         return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
>       |                ~~~~~~~~~~~~~~^~~~~~~
> musl/include/endian.h:34:23: note: place parentheses around the '+'
> expression to silence this warning
>    34 |         return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
>       |                              ^
>       |                (                  )
> 
> Note: I'm not subscribed to the mailing-list, so CCs are appreciated
> ---
>  include/endian.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/endian.h b/include/endian.h
> index 172c4320..2819c926 100644
> --- a/include/endian.h
> +++ b/include/endian.h
> @@ -18,17 +18,17 @@
> 
>  static __inline uint16_t __bswap16(uint16_t __x)
>  {
> -    return __x<<8 | __x>>8;
> +    return (__x<<8) | (__x>>8);
>  }
> 
>  static __inline uint32_t __bswap32(uint32_t __x)
>  {
> -    return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
> +    return (__x>>24) | ((__x>>8)&0xff00) | ((__x<<8)&0xff0000) | (__x<<24);
>  }
> 
>  static __inline uint64_t __bswap64(uint64_t __x)
>  {
> -    return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
> +    return ((__bswap32(__x)+0ULL)<<32) | __bswap32(__x>>32);
>  }
> 
>  #if __BYTE_ORDER == __LITTLE_ENDIAN
> --
> 2.50.0

While there has been some discussion lately of trying to silence
warnings from headers where GCC and clang are generating them even
after properly processing them as "system headers", often because of
how they treat argument to macro vs macro contents, the above seem to
be generated purely from misuse of the headers, passing the location
they're in with -I to a non-musl-targeting toolchain rather than using
a properly configured toolchain where the libc headers are in the
"-isystem" path.

Rich


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

end of thread, other threads:[~2025-06-24 15:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-24  9:02 [PATCH] endian.h: use parentheses to silence warnings Jacob Abrams
2025-06-24 15:54 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).