Cast __builtin_swap64 to uint64_t.
strace 4.19 causes clang to emit a questionable warning:
external/strace/nlattr.c:254:35: error: format specifies type 'unsigned long' but the argument has type 'unsigned long long' [-Werror,-Wformat]
tprintf("htobe64(%" PRIu64 ")", be64toh(num));
~~~ ^~~~~~~~~~~~
bionic/libc/include/sys/endian.h:100:20: note: expanded from macro 'be64toh'
#define be64toh(x) htobe64(x)
^~~~~~~~~~
bionic/libc/include/sys/endian.h:80:17: note: expanded from macro 'htobe64'
#define htobe64 __swap64
^
bionic/libc/include/sys/endian.h:48:18: note: expanded from macro '__swap64'
#define __swap64 __builtin_bswap64
^
What's happened here is:
* be64toh is __builtin_bswap64
* PRIu64 is "lu"
* __builtin_bswap64 is internally declared a "ULLiULLi"
All of which leads to the `unsigned long` != `unsigned long long` complaint.
It seems like __builtin_bswap64 should be ULiULi on 64-bit architectures,
where `long` rather than `long long` is the natural way to refer to 64-bit
types, as evidenced by the "lu" for PRIu64.
As long as clang behaves like this, though, we can work around it with a cast.
Also clean up the other casts in the file, be more consistent about whether
these function-like macros are defined with an argument, and remove an
incorrect comment.
Bug: http://b/65495954
Test: ran tests, built strace 4.19 without warnings
Change-Id: I8e06d4bf71e95d62f556eab8661033e04d487e0d
diff --git a/libc/include/sys/endian.h b/libc/include/sys/endian.h
index 799a6f2..9155b4c 100644
--- a/libc/include/sys/endian.h
+++ b/libc/include/sys/endian.h
@@ -45,7 +45,7 @@
#define __swap16 __builtin_bswap16
#define __swap32 __builtin_bswap32
-#define __swap64 __builtin_bswap64
+#define __swap64(x) __BIONIC_CAST(static_cast,uint64_t,__builtin_bswap64(x))
/* glibc compatibility. */
__BEGIN_DECLS
@@ -70,17 +70,17 @@
#define PDP_ENDIAN _PDP_ENDIAN
#define BYTE_ORDER _BYTE_ORDER
-#define NTOHL(x) (x) = ntohl((u_int32_t)(x))
-#define NTOHS(x) (x) = ntohs((u_int16_t)(x))
-#define HTONL(x) (x) = htonl((u_int32_t)(x))
-#define HTONS(x) (x) = htons((u_int16_t)(x))
+#define NTOHL(x) (x) = ntohl(__BIONIC_CAST(static_cast,u_int32_t,(x)))
+#define NTOHS(x) (x) = ntohs(__BIONIC_CAST(static_cast,u_int16_t,(x)))
+#define HTONL(x) (x) = htonl(__BIONIC_CAST(static_cast,u_int32_t,(x)))
+#define HTONS(x) (x) = htons(__BIONIC_CAST(static_cast,u_int16_t,(x)))
-#define htobe16 __swap16
-#define htobe32 __swap32
-#define htobe64 __swap64
-#define betoh16 __swap16
-#define betoh32 __swap32
-#define betoh64 __swap64
+#define htobe16(x) __swap16(x)
+#define htobe32(x) __swap32(x)
+#define htobe64(x) __swap64(x)
+#define betoh16(x) __swap16(x)
+#define betoh32(x) __swap32(x)
+#define betoh64(x) __swap64(x)
#define htole16(x) (x)
#define htole32(x) (x)
@@ -101,6 +101,7 @@
#define le16toh(x) htole16(x)
#define le32toh(x) htole32(x)
#define le64toh(x) htole64(x)
-#endif /* __USE_BSD */
+
+#endif
#endif