-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc] add LLVM_LIBC_CAST
macro.
#127319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] add LLVM_LIBC_CAST
macro.
#127319
Conversation
@llvm/pr-subscribers-libc Author: None (c8ef) Changesrelated: #127238 This patch adds a macro called Full diff: https://github.com/llvm/llvm-project/pull/127319.diff 1 Files Affected:
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index a0fa506c01ab8..83682e0a440d8 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,6 +47,9 @@
#define __NOEXCEPT throw()
#endif
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
+
#else // not __cplusplus
#undef __BEGIN_C_DECLS
@@ -85,6 +88,9 @@
#undef _Returns_twice
#define _Returns_twice __attribute__((returns_twice))
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) ((type)(value))
+
#endif // __cplusplus
#endif // LLVM_LIBC_COMMON_H
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but please do use it now from endian.h
libc/include/__llvm-libc-common.h
Outdated
@@ -47,6 +47,9 @@ | |||
#define __NOEXCEPT throw() | |||
#endif | |||
|
|||
#undef LLVM_LIBC_CAST | |||
#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This merits a comment about its intended uses.
No macro should be defined in this file unless it's in the implementation namespace.
Sticking to the __
prefix seems like the best convention, with the exception of the C11 keywords that are in the _[A-Z]
part of the implementation namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
#define htole16(x) ((uint16_t)(x)) | ||
#define htole32(x) ((uint32_t)(x)) | ||
#define htole64(x) ((uint64_t)(x)) | ||
#define htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just make the reinterpret cast implicit? Unsure if we'd ever need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bionic implementation omits it, while the glibc, musl, and freebsd implementations enforce it. Unsure which is more suitable.
The CI error seems unrelated. |
#define htole16(x) ((uint16_t)(x)) | ||
#define htole32(x) ((uint32_t)(x)) | ||
#define htole64(x) ((uint64_t)(x)) | ||
#define htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be a reinterpret_cast
. Since these types should be compatible I think this should be a static_cast
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thank you for your review! I will merge this PR now and address any further comments in future patches. |
related: #127238
This patch adds a macro called
LLVM_LIBC_CAST
, similar to__BIONIC_CAST
, for type conversion inendian.h
.